blob: b5ff60613361c4bf0dab11f8462c8f0df23f3fa0 [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})
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
nikesh.krishnan1ffb8132023-05-23 03:44:13 +053058// CreateVolthaPort creates the Voltha port based on ONU UNI Port and informs the core about it
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000059func (oo *OnuUniPort) CreateVolthaPort(ctx context.Context, apDeviceHandler IdeviceHandler) error {
nikesh.krishnan1ffb8132023-05-23 03:44:13 +053060 logger.Infow(ctx, "creating-voltha-uni-port", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000061 "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)
ozgecanetsia1b74cc72021-10-13 09:29:19 +030064 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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000074 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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000083 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 */
nikesh.krishnan1ffb8132023-05-23 03:44:13 +053092 logger.Infow(ctx, "ofPort values", log.Fields{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000093 "forUniPortName": oo.Name, "forMacBase": hwAddr,
ozgecanetsia1b74cc72021-10-13 09:29:19 +030094 "name": name, "hwAddr": ofHwAddr, "OperState": ofUniPortState, "capacity": capacity})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000095
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
nikesh.krishnan1ffb8132023-05-23 03:44:13 +0530141// SetOperState modifies OperState of the the UniPort
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000142func (oo *OnuUniPort) SetOperState(aNewOperState vc.OperStatus_Types) {
143 oo.OperState = aNewOperState
144}
145
146// uni port related utility functions (so far only used here)
147func 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
nikesh.krishnan1ffb8132023-05-23 03:44:13 +0530153// copied from OLT Adapter: unify centrally ?
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000154func 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}