Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 1 | /* |
Joey Armstrong | e8c091f | 2023-01-17 16:56:26 -0500 | [diff] [blame] | 2 | * Copyright 2020-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 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 common provides global definitions |
| 18 | package common |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "fmt" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | "time" |
| 26 | |
| 27 | //"sync" |
| 28 | //"time" |
| 29 | |
| 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" |
| 35 | ) |
| 36 | |
| 37 | //NewOnuUniPort returns a new instance of a OnuUniPort |
| 38 | func NewOnuUniPort(ctx context.Context, aUniID uint8, aPortNo uint32, aInstNo uint16, |
| 39 | aPortType UniPortType) *OnuUniPort { |
| 40 | logger.Infow(ctx, "init-onuUniPort", log.Fields{"uniID": aUniID, |
| 41 | "portNo": aPortNo, "InstNo": aInstNo, "type": aPortType}) |
| 42 | var OnuUniPort OnuUniPort |
| 43 | OnuUniPort.Enabled = false |
| 44 | OnuUniPort.Name = "uni-" + strconv.FormatUint(uint64(aPortNo), 10) |
| 45 | OnuUniPort.PortNo = aPortNo |
| 46 | OnuUniPort.PortType = aPortType |
| 47 | // so far it seems as here ofpPortNo/Name ist the same as the original port name ...?? |
| 48 | OnuUniPort.OfpPortNo = OnuUniPort.Name |
| 49 | OnuUniPort.UniID = aUniID |
| 50 | OnuUniPort.MacBpNo = aUniID + 1 //ensure >0 instanceNo |
| 51 | OnuUniPort.EntityID = aInstNo |
| 52 | OnuUniPort.AdminState = vc.AdminState_ENABLED //enabled per create |
| 53 | OnuUniPort.OperState = vc.OperStatus_UNKNOWN |
| 54 | OnuUniPort.PPort = nil // to be set on create |
| 55 | return &OnuUniPort |
| 56 | } |
| 57 | |
| 58 | //CreateVolthaPort creates the Voltha port based on ONU UNI Port and informs the core about it |
| 59 | func (oo *OnuUniPort) CreateVolthaPort(ctx context.Context, apDeviceHandler IdeviceHandler) error { |
| 60 | logger.Debugw(ctx, "creating-voltha-uni-port", log.Fields{ |
| 61 | "device-id": apDeviceHandler.GetDevice().Id, "portNo": oo.PortNo}) |
| 62 | //200630: per [VOL-3202] OF port info is now to be delivered within UniPort create |
| 63 | // not doing so crashes rw_core processing (at least still in 200630 version) |
ozgecanetsia | 1b74cc7 | 2021-10-13 09:29:19 +0300 | [diff] [blame] | 64 | var name string |
| 65 | var capacity uint32 |
| 66 | // In the future, We'll add port type into if case |
| 67 | if oo.PortType == UniPPTPPots { |
| 68 | name = apDeviceHandler.GetDevice().SerialNumber + "-P-" + strconv.FormatUint(uint64(oo.MacBpNo), 10) |
| 69 | capacity = uint32(of.OfpPortFeatures_OFPPF_COPPER | of.OfpPortFeatures_OFPPF_OTHER) |
| 70 | } else { |
| 71 | name = apDeviceHandler.GetDevice().SerialNumber + "-" + strconv.FormatUint(uint64(oo.MacBpNo), 10) |
| 72 | capacity = uint32(of.OfpPortFeatures_OFPPF_COPPER | of.OfpPortFeatures_OFPPF_1GB_FD) |
| 73 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 74 | var macOctets [6]uint8 |
| 75 | macOctets[5] = 0x08 |
| 76 | macOctets[4] = uint8(*apDeviceHandler.GetPonPortNumber() >> 8) |
| 77 | macOctets[3] = uint8(*apDeviceHandler.GetPonPortNumber()) |
| 78 | macOctets[2] = uint8(oo.PortNo >> 16) |
| 79 | macOctets[1] = uint8(oo.PortNo >> 8) |
| 80 | macOctets[0] = uint8(oo.PortNo) |
| 81 | hwAddr := genMacFromOctets(macOctets) |
| 82 | ofHwAddr := macAddressToUint32Array(hwAddr) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 83 | ofUniPortState := of.OfpPortState_OFPPS_LINK_DOWN |
| 84 | /* as the VOLTHA port create is only called directly after Uni Port create |
| 85 | the OfPortOperState is always Down |
| 86 | Note: this way the OfPortOperState won't ever change (directly in adapter) |
| 87 | maybe that was already always the case, but looks a bit weird - to be kept in mind ... |
| 88 | if pUniPort.operState == vc.OperStatus_ACTIVE { |
| 89 | ofUniPortState = of.OfpPortState_OFPPS_LIVE |
| 90 | } |
| 91 | */ |
| 92 | logger.Debugw(ctx, "ofPort values", log.Fields{ |
| 93 | "forUniPortName": oo.Name, "forMacBase": hwAddr, |
ozgecanetsia | 1b74cc7 | 2021-10-13 09:29:19 +0300 | [diff] [blame] | 94 | "name": name, "hwAddr": ofHwAddr, "OperState": ofUniPortState, "capacity": capacity}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 95 | |
| 96 | pUniPort := &voltha.Port{ |
| 97 | DeviceId: apDeviceHandler.GetDevice().Id, |
| 98 | PortNo: oo.PortNo, |
| 99 | Label: oo.Name, |
| 100 | Type: voltha.Port_ETHERNET_UNI, |
| 101 | AdminState: oo.AdminState, |
| 102 | OperStatus: oo.OperState, |
| 103 | // obviously empty peer setting |
| 104 | OfpPort: &of.OfpPort{ |
| 105 | Name: name, |
| 106 | HwAddr: ofHwAddr, |
| 107 | Config: 0, |
| 108 | State: uint32(ofUniPortState), |
| 109 | Curr: capacity, |
| 110 | Advertised: capacity, |
| 111 | Peer: capacity, |
| 112 | CurrSpeed: 1000, |
| 113 | MaxSpeed: 1000, |
| 114 | }, |
| 115 | } |
| 116 | maxRetry := 3 |
| 117 | retryCnt := 0 |
| 118 | var err error |
| 119 | for retryCnt = 0; retryCnt < maxRetry; retryCnt++ { |
| 120 | if err = apDeviceHandler.CreatePortInCore(ctx, pUniPort); err != nil { |
| 121 | logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s, retrying after a delay", err) |
| 122 | // retry after a sleep |
| 123 | time.Sleep(2 * time.Second) |
| 124 | } else { |
| 125 | // success, break from retry loop |
| 126 | break |
| 127 | } |
| 128 | } |
| 129 | if retryCnt == maxRetry { // maxed out.. |
| 130 | logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s", err) |
| 131 | return fmt.Errorf("device-fsm-port-create-failed-%s", err) |
| 132 | } |
| 133 | logger.Infow(ctx, "Voltha OnuUniPort-added", log.Fields{ |
| 134 | "device-id": apDeviceHandler.GetDevice().Id, "PortNo": oo.PortNo}) |
| 135 | oo.PPort = pUniPort |
| 136 | oo.OperState = vc.OperStatus_DISCOVERED |
| 137 | |
| 138 | return nil |
| 139 | } |
| 140 | |
| 141 | //SetOperState modifies OperState of the the UniPort |
| 142 | func (oo *OnuUniPort) SetOperState(aNewOperState vc.OperStatus_Types) { |
| 143 | oo.OperState = aNewOperState |
| 144 | } |
| 145 | |
| 146 | // uni port related utility functions (so far only used here) |
| 147 | func genMacFromOctets(aOctets [6]uint8) string { |
| 148 | return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 149 | aOctets[5], aOctets[4], aOctets[3], |
| 150 | aOctets[2], aOctets[1], aOctets[0]) |
| 151 | } |
| 152 | |
| 153 | //copied from OLT Adapter: unify centrally ? |
| 154 | func macAddressToUint32Array(mac string) []uint32 { |
| 155 | slist := strings.Split(mac, ":") |
| 156 | result := make([]uint32, len(slist)) |
| 157 | var err error |
| 158 | var tmp int64 |
| 159 | for index, val := range slist { |
| 160 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 161 | return []uint32{1, 2, 3, 4, 5, 6} |
| 162 | } |
| 163 | result[index] = uint32(tmp) |
| 164 | } |
| 165 | return result |
| 166 | } |