Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //Package adaptercoreonu provides the utility for onu devices, flows and statistics |
| 18 | package adaptercoreonu |
| 19 | |
| 20 | import ( |
| 21 | "context" |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 22 | "fmt" |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 23 | "strconv" |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 24 | "strings" |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 25 | "time" |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 26 | |
| 27 | //"sync" |
| 28 | //"time" |
| 29 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame^] | 30 | //"github.com/opencord/voltha-lib-go/v7/pkg/kafka" |
| 31 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 32 | vc "github.com/opencord/voltha-protos/v5/go/common" |
| 33 | of "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 34 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 35 | ) |
| 36 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 37 | type uniPortType uint8 |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 38 | |
ozgecanetsia | 124d973 | 2021-09-16 14:31:57 +0300 | [diff] [blame] | 39 | // UniPPTP Interface type - re-use values from G.988 (Chapter 9.3.4)TP type definition (directly used in OMCI!) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 40 | const ( |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 41 | // uniPPTP relates to PPTP |
| 42 | uniPPTP uniPortType = 1 // relates to PPTP |
| 43 | // uniVEIP relates to VEIP |
| 44 | uniVEIP uniPortType = 11 // relates to VEIP |
ozgecanetsia | 124d973 | 2021-09-16 14:31:57 +0300 | [diff] [blame] | 45 | // uniPPTPPots relates to PPTP POTS |
| 46 | uniPPTPPots uniPortType = 4 // relates to IP host config data (for Voice Services) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 47 | ) |
| 48 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 49 | //onuUniPort structure holds information about the ONU attached Uni Ports |
| 50 | type onuUniPort struct { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 51 | enabled bool |
| 52 | name string |
| 53 | portNo uint32 |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 54 | portType uniPortType |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 55 | ofpPortNo string |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 56 | uniID uint8 |
Holger Hildebrandt | dd23cc2 | 2020-05-19 13:32:18 +0000 | [diff] [blame] | 57 | macBpNo uint8 |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 58 | entityID uint16 |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 59 | adminState vc.AdminState_Types |
| 60 | operState vc.OperStatus_Types |
| 61 | pPort *voltha.Port |
| 62 | } |
| 63 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 64 | //newOnuUniPort returns a new instance of a OnuUniPort |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 65 | func newOnuUniPort(ctx context.Context, aUniID uint8, aPortNo uint32, aInstNo uint16, |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 66 | aPortType uniPortType) *onuUniPort { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 67 | logger.Infow(ctx, "init-onuUniPort", log.Fields{"uniID": aUniID, |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 68 | "portNo": aPortNo, "InstNo": aInstNo, "type": aPortType}) |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 69 | var onuUniPort onuUniPort |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 70 | onuUniPort.enabled = false |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 71 | onuUniPort.name = "uni-" + strconv.FormatUint(uint64(aPortNo), 10) |
| 72 | onuUniPort.portNo = aPortNo |
| 73 | onuUniPort.portType = aPortType |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 74 | // so far it seems as here ofpPortNo/Name ist the same as the original port name ...?? |
| 75 | onuUniPort.ofpPortNo = onuUniPort.name |
Himani Chawla | 26e555c | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 76 | onuUniPort.uniID = aUniID |
| 77 | onuUniPort.macBpNo = aUniID + 1 //ensure >0 instanceNo |
| 78 | onuUniPort.entityID = aInstNo |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 79 | onuUniPort.adminState = vc.AdminState_ENABLED //enabled per create |
| 80 | onuUniPort.operState = vc.OperStatus_UNKNOWN |
| 81 | onuUniPort.pPort = nil // to be set on create |
| 82 | return &onuUniPort |
| 83 | } |
| 84 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 85 | //createVolthaPort creates the Voltha port based on ONU UNI Port and informs the core about it |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 86 | func (oo *onuUniPort) createVolthaPort(ctx context.Context, apDeviceHandler *deviceHandler) error { |
| 87 | logger.Debugw(ctx, "creating-voltha-uni-port", log.Fields{ |
divyadesai | 4d29955 | 2020-08-18 07:13:49 +0000 | [diff] [blame] | 88 | "device-id": apDeviceHandler.device.Id, "portNo": oo.portNo}) |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 89 | //200630: per [VOL-3202] OF port info is now to be delivered within UniPort create |
| 90 | // not doing so crashes rw_core processing (at least still in 200630 version) |
| 91 | name := apDeviceHandler.device.SerialNumber + "-" + strconv.FormatUint(uint64(oo.macBpNo), 10) |
| 92 | var macOctets [6]uint8 |
| 93 | macOctets[5] = 0x08 |
| 94 | //ponPortNumber was copied from device.ParentPortNo |
| 95 | macOctets[4] = uint8(apDeviceHandler.ponPortNumber >> 8) |
| 96 | macOctets[3] = uint8(apDeviceHandler.ponPortNumber) |
| 97 | macOctets[2] = uint8(oo.portNo >> 16) |
| 98 | macOctets[1] = uint8(oo.portNo >> 8) |
| 99 | macOctets[0] = uint8(oo.portNo) |
| 100 | hwAddr := genMacFromOctets(macOctets) |
| 101 | ofHwAddr := macAddressToUint32Array(hwAddr) |
| 102 | capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 103 | ofUniPortState := of.OfpPortState_OFPPS_LINK_DOWN |
| 104 | /* as the VOLTHA port create is only called directly after Uni Port create |
| 105 | the OfPortOperState is always Down |
| 106 | Note: this way the OfPortOperState won't ever change (directly in adapter) |
| 107 | maybe that was already always the case, but looks a bit weird - to be kept in mind ... |
| 108 | if pUniPort.operState == vc.OperStatus_ACTIVE { |
| 109 | ofUniPortState = of.OfpPortState_OFPPS_LIVE |
| 110 | } |
| 111 | */ |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 112 | logger.Debugw(ctx, "ofPort values", log.Fields{ |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 113 | "forUniPortName": oo.name, "forMacBase": hwAddr, |
| 114 | "name": name, "hwAddr": ofHwAddr, "OperState": ofUniPortState}) |
| 115 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 116 | pUniPort := &voltha.Port{ |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame^] | 117 | DeviceId: apDeviceHandler.deviceID, |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 118 | PortNo: oo.portNo, |
| 119 | Label: oo.name, |
| 120 | Type: voltha.Port_ETHERNET_UNI, |
| 121 | AdminState: oo.adminState, |
| 122 | OperStatus: oo.operState, |
| 123 | // obviously empty peer setting |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 124 | OfpPort: &of.OfpPort{ |
| 125 | Name: name, |
| 126 | HwAddr: ofHwAddr, |
| 127 | Config: 0, |
| 128 | State: uint32(ofUniPortState), |
| 129 | Curr: capacity, |
| 130 | Advertised: capacity, |
| 131 | Peer: capacity, |
Andrea Campanella | 550c340 | 2021-01-21 14:59:40 +0100 | [diff] [blame] | 132 | CurrSpeed: 1000, |
| 133 | MaxSpeed: 1000, |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 134 | }, |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 135 | } |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 136 | maxRetry := 3 |
| 137 | retryCnt := 0 |
| 138 | var err error |
| 139 | for retryCnt = 0; retryCnt < maxRetry; retryCnt++ { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame^] | 140 | if err = apDeviceHandler.createPortInCore(ctx, pUniPort); err != nil { |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 141 | logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s, retrying after a delay", err) |
| 142 | // retry after a sleep |
| 143 | time.Sleep(2 * time.Second) |
| 144 | } else { |
| 145 | // success, break from retry loop |
| 146 | break |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 147 | } |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 148 | } |
Girish Gowdra | 50e5642 | 2021-06-01 16:46:04 -0700 | [diff] [blame] | 149 | if retryCnt == maxRetry { // maxed out.. |
| 150 | logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s", err) |
| 151 | return fmt.Errorf("device-fsm-port-create-failed-%s", err) |
| 152 | } |
| 153 | logger.Infow(ctx, "Voltha onuUniPort-added", log.Fields{ |
| 154 | "device-id": apDeviceHandler.device.Id, "PortNo": oo.portNo}) |
| 155 | oo.pPort = pUniPort |
| 156 | oo.operState = vc.OperStatus_DISCOVERED |
| 157 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 158 | return nil |
| 159 | } |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 160 | |
Himani Chawla | 6d2ae15 | 2020-09-02 13:11:20 +0530 | [diff] [blame] | 161 | //setOperState modifies OperState of the the UniPort |
| 162 | func (oo *onuUniPort) setOperState(aNewOperState vc.OperStatus_Types) { |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 163 | oo.operState = aNewOperState |
| 164 | } |
| 165 | |
| 166 | // uni port related utility functions (so far only used here) |
| 167 | func genMacFromOctets(aOctets [6]uint8) string { |
| 168 | return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 169 | aOctets[5], aOctets[4], aOctets[3], |
| 170 | aOctets[2], aOctets[1], aOctets[0]) |
| 171 | } |
| 172 | |
| 173 | //copied from OLT Adapter: unify centrally ? |
| 174 | func macAddressToUint32Array(mac string) []uint32 { |
| 175 | slist := strings.Split(mac, ":") |
| 176 | result := make([]uint32, len(slist)) |
| 177 | var err error |
| 178 | var tmp int64 |
| 179 | for index, val := range slist { |
| 180 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 181 | return []uint32{1, 2, 3, 4, 5, 6} |
| 182 | } |
| 183 | result[index] = uint32(tmp) |
| 184 | } |
| 185 | return result |
Holger Hildebrandt | 24d5195 | 2020-05-04 14:03:42 +0000 | [diff] [blame] | 186 | } |