blob: bfd52f94493160020861c6cae6b7ea5e412daa43 [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
Girish Gowdra50e56422021-06-01 16:46:04 -070030 //"github.com/opencord/voltha-lib-go/v5/pkg/kafka"
31 "github.com/opencord/voltha-lib-go/v5/pkg/log"
dbainbri4d3a0dc2020-12-02 00:33:42 +000032 vc "github.com/opencord/voltha-protos/v4/go/common"
33 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
34 "github.com/opencord/voltha-protos/v4/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
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000039// UniPPTP Interface type - re-use values from G.988 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
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000045)
46
Himani Chawla6d2ae152020-09-02 13:11:20 +053047//onuUniPort structure holds information about the ONU attached Uni Ports
48type onuUniPort struct {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000049 enabled bool
50 name string
51 portNo uint32
Himani Chawla6d2ae152020-09-02 13:11:20 +053052 portType uniPortType
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000053 ofpPortNo string
Himani Chawla26e555c2020-08-31 12:30:20 +053054 uniID uint8
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000055 macBpNo uint8
Himani Chawla26e555c2020-08-31 12:30:20 +053056 entityID uint16
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000057 adminState vc.AdminState_Types
58 operState vc.OperStatus_Types
59 pPort *voltha.Port
60}
61
Himani Chawla6d2ae152020-09-02 13:11:20 +053062//newOnuUniPort returns a new instance of a OnuUniPort
dbainbri4d3a0dc2020-12-02 00:33:42 +000063func newOnuUniPort(ctx context.Context, aUniID uint8, aPortNo uint32, aInstNo uint16,
Himani Chawla6d2ae152020-09-02 13:11:20 +053064 aPortType uniPortType) *onuUniPort {
dbainbri4d3a0dc2020-12-02 00:33:42 +000065 logger.Infow(ctx, "init-onuUniPort", log.Fields{"uniID": aUniID,
Himani Chawla26e555c2020-08-31 12:30:20 +053066 "portNo": aPortNo, "InstNo": aInstNo, "type": aPortType})
Himani Chawla6d2ae152020-09-02 13:11:20 +053067 var onuUniPort onuUniPort
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000068 onuUniPort.enabled = false
Himani Chawla26e555c2020-08-31 12:30:20 +053069 onuUniPort.name = "uni-" + strconv.FormatUint(uint64(aPortNo), 10)
70 onuUniPort.portNo = aPortNo
71 onuUniPort.portType = aPortType
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000072 // so far it seems as here ofpPortNo/Name ist the same as the original port name ...??
73 onuUniPort.ofpPortNo = onuUniPort.name
Himani Chawla26e555c2020-08-31 12:30:20 +053074 onuUniPort.uniID = aUniID
75 onuUniPort.macBpNo = aUniID + 1 //ensure >0 instanceNo
76 onuUniPort.entityID = aInstNo
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000077 onuUniPort.adminState = vc.AdminState_ENABLED //enabled per create
78 onuUniPort.operState = vc.OperStatus_UNKNOWN
79 onuUniPort.pPort = nil // to be set on create
80 return &onuUniPort
81}
82
Himani Chawla6d2ae152020-09-02 13:11:20 +053083//createVolthaPort creates the Voltha port based on ONU UNI Port and informs the core about it
dbainbri4d3a0dc2020-12-02 00:33:42 +000084func (oo *onuUniPort) createVolthaPort(ctx context.Context, apDeviceHandler *deviceHandler) error {
85 logger.Debugw(ctx, "creating-voltha-uni-port", log.Fields{
divyadesai4d299552020-08-18 07:13:49 +000086 "device-id": apDeviceHandler.device.Id, "portNo": oo.portNo})
mpagenkoaf801632020-07-03 10:00:42 +000087 //200630: per [VOL-3202] OF port info is now to be delivered within UniPort create
88 // not doing so crashes rw_core processing (at least still in 200630 version)
89 name := apDeviceHandler.device.SerialNumber + "-" + strconv.FormatUint(uint64(oo.macBpNo), 10)
90 var macOctets [6]uint8
91 macOctets[5] = 0x08
92 //ponPortNumber was copied from device.ParentPortNo
93 macOctets[4] = uint8(apDeviceHandler.ponPortNumber >> 8)
94 macOctets[3] = uint8(apDeviceHandler.ponPortNumber)
95 macOctets[2] = uint8(oo.portNo >> 16)
96 macOctets[1] = uint8(oo.portNo >> 8)
97 macOctets[0] = uint8(oo.portNo)
98 hwAddr := genMacFromOctets(macOctets)
99 ofHwAddr := macAddressToUint32Array(hwAddr)
100 capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
101 ofUniPortState := of.OfpPortState_OFPPS_LINK_DOWN
102 /* as the VOLTHA port create is only called directly after Uni Port create
103 the OfPortOperState is always Down
104 Note: this way the OfPortOperState won't ever change (directly in adapter)
105 maybe that was already always the case, but looks a bit weird - to be kept in mind ...
106 if pUniPort.operState == vc.OperStatus_ACTIVE {
107 ofUniPortState = of.OfpPortState_OFPPS_LIVE
108 }
109 */
dbainbri4d3a0dc2020-12-02 00:33:42 +0000110 logger.Debugw(ctx, "ofPort values", log.Fields{
mpagenkoaf801632020-07-03 10:00:42 +0000111 "forUniPortName": oo.name, "forMacBase": hwAddr,
112 "name": name, "hwAddr": ofHwAddr, "OperState": ofUniPortState})
113
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000114 pUniPort := &voltha.Port{
115 PortNo: oo.portNo,
116 Label: oo.name,
117 Type: voltha.Port_ETHERNET_UNI,
118 AdminState: oo.adminState,
119 OperStatus: oo.operState,
120 // obviously empty peer setting
mpagenkoaf801632020-07-03 10:00:42 +0000121 OfpPort: &of.OfpPort{
122 Name: name,
123 HwAddr: ofHwAddr,
124 Config: 0,
125 State: uint32(ofUniPortState),
126 Curr: capacity,
127 Advertised: capacity,
128 Peer: capacity,
Andrea Campanella550c3402021-01-21 14:59:40 +0100129 CurrSpeed: 1000,
130 MaxSpeed: 1000,
mpagenkoaf801632020-07-03 10:00:42 +0000131 },
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000132 }
Girish Gowdra50e56422021-06-01 16:46:04 -0700133 maxRetry := 3
134 retryCnt := 0
135 var err error
136 for retryCnt = 0; retryCnt < maxRetry; retryCnt++ {
137 if err = apDeviceHandler.coreProxy.PortCreated(log.WithSpanFromContext(context.TODO(), ctx),
mpagenkoaf801632020-07-03 10:00:42 +0000138 apDeviceHandler.deviceID, pUniPort); err != nil {
Girish Gowdra50e56422021-06-01 16:46:04 -0700139 logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s, retrying after a delay", err)
140 // retry after a sleep
141 time.Sleep(2 * time.Second)
142 } else {
143 // success, break from retry loop
144 break
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000145 }
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000146 }
Girish Gowdra50e56422021-06-01 16:46:04 -0700147 if retryCnt == maxRetry { // maxed out..
148 logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s", err)
149 return fmt.Errorf("device-fsm-port-create-failed-%s", err)
150 }
151 logger.Infow(ctx, "Voltha onuUniPort-added", log.Fields{
152 "device-id": apDeviceHandler.device.Id, "PortNo": oo.portNo})
153 oo.pPort = pUniPort
154 oo.operState = vc.OperStatus_DISCOVERED
155
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000156 return nil
157}
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000158
Himani Chawla6d2ae152020-09-02 13:11:20 +0530159//setOperState modifies OperState of the the UniPort
160func (oo *onuUniPort) setOperState(aNewOperState vc.OperStatus_Types) {
mpagenkoaf801632020-07-03 10:00:42 +0000161 oo.operState = aNewOperState
162}
163
164// uni port related utility functions (so far only used here)
165func genMacFromOctets(aOctets [6]uint8) string {
166 return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
167 aOctets[5], aOctets[4], aOctets[3],
168 aOctets[2], aOctets[1], aOctets[0])
169}
170
171//copied from OLT Adapter: unify centrally ?
172func macAddressToUint32Array(mac string) []uint32 {
173 slist := strings.Split(mac, ":")
174 result := make([]uint32, len(slist))
175 var err error
176 var tmp int64
177 for index, val := range slist {
178 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
179 return []uint32{1, 2, 3, 4, 5, 6}
180 }
181 result[index] = uint32(tmp)
182 }
183 return result
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000184}