blob: f4b255f409416144cc8931abf4953f01cb2dd23c [file] [log] [blame]
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +00001/*
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
18package adaptercoreonu
19
20import (
21 "context"
mpagenkoaf801632020-07-03 10:00:42 +000022 "fmt"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000023 "strconv"
mpagenkoaf801632020-07-03 10:00:42 +000024 "strings"
Girish Gowdra50e56422021-06-01 16:46:04 -070025 "time"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000026
27 //"sync"
28 //"time"
29
khenaidoo7d3c5582021-08-11 18:09:44 -040030 //"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 Hildebrandt0f9b88d2020-04-20 13:33:25 +000035)
36
Himani Chawla6d2ae152020-09-02 13:11:20 +053037type uniPortType uint8
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000038
ozgecanetsia124d9732021-09-16 14:31:57 +030039// UniPPTP Interface type - re-use values from G.988 (Chapter 9.3.4)TP type definition (directly used in OMCI!)
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000040const (
Himani Chawla6d2ae152020-09-02 13:11:20 +053041 // uniPPTP relates to PPTP
42 uniPPTP uniPortType = 1 // relates to PPTP
43 // uniVEIP relates to VEIP
44 uniVEIP uniPortType = 11 // relates to VEIP
ozgecanetsia124d9732021-09-16 14:31:57 +030045 // uniPPTPPots relates to PPTP POTS
46 uniPPTPPots uniPortType = 4 // relates to IP host config data (for Voice Services)
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000047)
48
Himani Chawla6d2ae152020-09-02 13:11:20 +053049//onuUniPort structure holds information about the ONU attached Uni Ports
50type onuUniPort struct {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000051 enabled bool
52 name string
53 portNo uint32
Himani Chawla6d2ae152020-09-02 13:11:20 +053054 portType uniPortType
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000055 ofpPortNo string
Himani Chawla26e555c2020-08-31 12:30:20 +053056 uniID uint8
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000057 macBpNo uint8
Himani Chawla26e555c2020-08-31 12:30:20 +053058 entityID uint16
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000059 adminState vc.AdminState_Types
60 operState vc.OperStatus_Types
61 pPort *voltha.Port
62}
63
Himani Chawla6d2ae152020-09-02 13:11:20 +053064//newOnuUniPort returns a new instance of a OnuUniPort
dbainbri4d3a0dc2020-12-02 00:33:42 +000065func newOnuUniPort(ctx context.Context, aUniID uint8, aPortNo uint32, aInstNo uint16,
Himani Chawla6d2ae152020-09-02 13:11:20 +053066 aPortType uniPortType) *onuUniPort {
dbainbri4d3a0dc2020-12-02 00:33:42 +000067 logger.Infow(ctx, "init-onuUniPort", log.Fields{"uniID": aUniID,
Himani Chawla26e555c2020-08-31 12:30:20 +053068 "portNo": aPortNo, "InstNo": aInstNo, "type": aPortType})
Himani Chawla6d2ae152020-09-02 13:11:20 +053069 var onuUniPort onuUniPort
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000070 onuUniPort.enabled = false
Himani Chawla26e555c2020-08-31 12:30:20 +053071 onuUniPort.name = "uni-" + strconv.FormatUint(uint64(aPortNo), 10)
72 onuUniPort.portNo = aPortNo
73 onuUniPort.portType = aPortType
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000074 // so far it seems as here ofpPortNo/Name ist the same as the original port name ...??
75 onuUniPort.ofpPortNo = onuUniPort.name
Himani Chawla26e555c2020-08-31 12:30:20 +053076 onuUniPort.uniID = aUniID
77 onuUniPort.macBpNo = aUniID + 1 //ensure >0 instanceNo
78 onuUniPort.entityID = aInstNo
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000079 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 Chawla6d2ae152020-09-02 13:11:20 +053085//createVolthaPort creates the Voltha port based on ONU UNI Port and informs the core about it
dbainbri4d3a0dc2020-12-02 00:33:42 +000086func (oo *onuUniPort) createVolthaPort(ctx context.Context, apDeviceHandler *deviceHandler) error {
87 logger.Debugw(ctx, "creating-voltha-uni-port", log.Fields{
divyadesai4d299552020-08-18 07:13:49 +000088 "device-id": apDeviceHandler.device.Id, "portNo": oo.portNo})
mpagenkoaf801632020-07-03 10:00:42 +000089 //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 */
dbainbri4d3a0dc2020-12-02 00:33:42 +0000112 logger.Debugw(ctx, "ofPort values", log.Fields{
mpagenkoaf801632020-07-03 10:00:42 +0000113 "forUniPortName": oo.name, "forMacBase": hwAddr,
114 "name": name, "hwAddr": ofHwAddr, "OperState": ofUniPortState})
115
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000116 pUniPort := &voltha.Port{
khenaidoo7d3c5582021-08-11 18:09:44 -0400117 DeviceId: apDeviceHandler.deviceID,
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000118 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
mpagenkoaf801632020-07-03 10:00:42 +0000124 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 Campanella550c3402021-01-21 14:59:40 +0100132 CurrSpeed: 1000,
133 MaxSpeed: 1000,
mpagenkoaf801632020-07-03 10:00:42 +0000134 },
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000135 }
Girish Gowdra50e56422021-06-01 16:46:04 -0700136 maxRetry := 3
137 retryCnt := 0
138 var err error
139 for retryCnt = 0; retryCnt < maxRetry; retryCnt++ {
khenaidoo7d3c5582021-08-11 18:09:44 -0400140 if err = apDeviceHandler.createPortInCore(ctx, pUniPort); err != nil {
Girish Gowdra50e56422021-06-01 16:46:04 -0700141 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 Hildebrandt0f9b88d2020-04-20 13:33:25 +0000147 }
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000148 }
Girish Gowdra50e56422021-06-01 16:46:04 -0700149 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 Hildebrandt0f9b88d2020-04-20 13:33:25 +0000158 return nil
159}
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000160
Himani Chawla6d2ae152020-09-02 13:11:20 +0530161//setOperState modifies OperState of the the UniPort
162func (oo *onuUniPort) setOperState(aNewOperState vc.OperStatus_Types) {
mpagenkoaf801632020-07-03 10:00:42 +0000163 oo.operState = aNewOperState
164}
165
166// uni port related utility functions (so far only used here)
167func 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 ?
174func 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 Hildebrandt24d51952020-05-04 14:03:42 +0000186}