blob: 77cce60fc54abd31611fcf572a2438b6076f6d9c [file] [log] [blame]
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +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 common provides global definitions
18package 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
37//NewOnuUniPort returns a new instance of a OnuUniPort
38func 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
59func (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)
64 name := apDeviceHandler.GetDevice().SerialNumber + "-" + strconv.FormatUint(uint64(oo.MacBpNo), 10)
65 var macOctets [6]uint8
66 macOctets[5] = 0x08
67 macOctets[4] = uint8(*apDeviceHandler.GetPonPortNumber() >> 8)
68 macOctets[3] = uint8(*apDeviceHandler.GetPonPortNumber())
69 macOctets[2] = uint8(oo.PortNo >> 16)
70 macOctets[1] = uint8(oo.PortNo >> 8)
71 macOctets[0] = uint8(oo.PortNo)
72 hwAddr := genMacFromOctets(macOctets)
73 ofHwAddr := macAddressToUint32Array(hwAddr)
74 capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
75 ofUniPortState := of.OfpPortState_OFPPS_LINK_DOWN
76 /* as the VOLTHA port create is only called directly after Uni Port create
77 the OfPortOperState is always Down
78 Note: this way the OfPortOperState won't ever change (directly in adapter)
79 maybe that was already always the case, but looks a bit weird - to be kept in mind ...
80 if pUniPort.operState == vc.OperStatus_ACTIVE {
81 ofUniPortState = of.OfpPortState_OFPPS_LIVE
82 }
83 */
84 logger.Debugw(ctx, "ofPort values", log.Fields{
85 "forUniPortName": oo.Name, "forMacBase": hwAddr,
86 "name": name, "hwAddr": ofHwAddr, "OperState": ofUniPortState})
87
88 pUniPort := &voltha.Port{
89 DeviceId: apDeviceHandler.GetDevice().Id,
90 PortNo: oo.PortNo,
91 Label: oo.Name,
92 Type: voltha.Port_ETHERNET_UNI,
93 AdminState: oo.AdminState,
94 OperStatus: oo.OperState,
95 // obviously empty peer setting
96 OfpPort: &of.OfpPort{
97 Name: name,
98 HwAddr: ofHwAddr,
99 Config: 0,
100 State: uint32(ofUniPortState),
101 Curr: capacity,
102 Advertised: capacity,
103 Peer: capacity,
104 CurrSpeed: 1000,
105 MaxSpeed: 1000,
106 },
107 }
108 maxRetry := 3
109 retryCnt := 0
110 var err error
111 for retryCnt = 0; retryCnt < maxRetry; retryCnt++ {
112 if err = apDeviceHandler.CreatePortInCore(ctx, pUniPort); err != nil {
113 logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s, retrying after a delay", err)
114 // retry after a sleep
115 time.Sleep(2 * time.Second)
116 } else {
117 // success, break from retry loop
118 break
119 }
120 }
121 if retryCnt == maxRetry { // maxed out..
122 logger.Errorf(ctx, "Device FSM: PortCreated-failed-%s", err)
123 return fmt.Errorf("device-fsm-port-create-failed-%s", err)
124 }
125 logger.Infow(ctx, "Voltha OnuUniPort-added", log.Fields{
126 "device-id": apDeviceHandler.GetDevice().Id, "PortNo": oo.PortNo})
127 oo.PPort = pUniPort
128 oo.OperState = vc.OperStatus_DISCOVERED
129
130 return nil
131}
132
133//SetOperState modifies OperState of the the UniPort
134func (oo *OnuUniPort) SetOperState(aNewOperState vc.OperStatus_Types) {
135 oo.OperState = aNewOperState
136}
137
138// uni port related utility functions (so far only used here)
139func genMacFromOctets(aOctets [6]uint8) string {
140 return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
141 aOctets[5], aOctets[4], aOctets[3],
142 aOctets[2], aOctets[1], aOctets[0])
143}
144
145//copied from OLT Adapter: unify centrally ?
146func macAddressToUint32Array(mac string) []uint32 {
147 slist := strings.Split(mac, ":")
148 result := make([]uint32, len(slist))
149 var err error
150 var tmp int64
151 for index, val := range slist {
152 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
153 return []uint32{1, 2, 3, 4, 5, 6}
154 }
155 result[index] = uint32(tmp)
156 }
157 return result
158}