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