blob: ea8f2c5df27bb706043f7bd1205eed497480140b [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"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000025
26 //"sync"
27 //"time"
28
dbainbri4d3a0dc2020-12-02 00:33:42 +000029 //"github.com/opencord/voltha-lib-go/v4/pkg/kafka"
30 "github.com/opencord/voltha-lib-go/v4/pkg/log"
31 vc "github.com/opencord/voltha-protos/v4/go/common"
32 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
33 "github.com/opencord/voltha-protos/v4/go/voltha"
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000034)
35
Himani Chawla6d2ae152020-09-02 13:11:20 +053036type uniPortType uint8
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000037
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000038// UniPPTP Interface type - re-use values from G.988 TP type definition (directly used in OMCI!)
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000039const (
Himani Chawla6d2ae152020-09-02 13:11:20 +053040 // uniPPTP relates to PPTP
41 uniPPTP uniPortType = 1 // relates to PPTP
42 // uniVEIP relates to VEIP
43 uniVEIP uniPortType = 11 // relates to VEIP
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000044)
45
Himani Chawla6d2ae152020-09-02 13:11:20 +053046//onuUniPort structure holds information about the ONU attached Uni Ports
47type onuUniPort struct {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000048 enabled bool
49 name string
50 portNo uint32
Himani Chawla6d2ae152020-09-02 13:11:20 +053051 portType uniPortType
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000052 ofpPortNo string
Himani Chawla26e555c2020-08-31 12:30:20 +053053 uniID uint8
Holger Hildebrandtdd23cc22020-05-19 13:32:18 +000054 macBpNo uint8
Himani Chawla26e555c2020-08-31 12:30:20 +053055 entityID uint16
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000056 adminState vc.AdminState_Types
57 operState vc.OperStatus_Types
58 pPort *voltha.Port
59}
60
Himani Chawla6d2ae152020-09-02 13:11:20 +053061//newOnuUniPort returns a new instance of a OnuUniPort
dbainbri4d3a0dc2020-12-02 00:33:42 +000062func newOnuUniPort(ctx context.Context, aUniID uint8, aPortNo uint32, aInstNo uint16,
Himani Chawla6d2ae152020-09-02 13:11:20 +053063 aPortType uniPortType) *onuUniPort {
dbainbri4d3a0dc2020-12-02 00:33:42 +000064 logger.Infow(ctx, "init-onuUniPort", log.Fields{"uniID": aUniID,
Himani Chawla26e555c2020-08-31 12:30:20 +053065 "portNo": aPortNo, "InstNo": aInstNo, "type": aPortType})
Himani Chawla6d2ae152020-09-02 13:11:20 +053066 var onuUniPort onuUniPort
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000067 onuUniPort.enabled = false
Himani Chawla26e555c2020-08-31 12:30:20 +053068 onuUniPort.name = "uni-" + strconv.FormatUint(uint64(aPortNo), 10)
69 onuUniPort.portNo = aPortNo
70 onuUniPort.portType = aPortType
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000071 // so far it seems as here ofpPortNo/Name ist the same as the original port name ...??
72 onuUniPort.ofpPortNo = onuUniPort.name
Himani Chawla26e555c2020-08-31 12:30:20 +053073 onuUniPort.uniID = aUniID
74 onuUniPort.macBpNo = aUniID + 1 //ensure >0 instanceNo
75 onuUniPort.entityID = aInstNo
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000076 onuUniPort.adminState = vc.AdminState_ENABLED //enabled per create
77 onuUniPort.operState = vc.OperStatus_UNKNOWN
78 onuUniPort.pPort = nil // to be set on create
79 return &onuUniPort
80}
81
Himani Chawla6d2ae152020-09-02 13:11:20 +053082//createVolthaPort creates the Voltha port based on ONU UNI Port and informs the core about it
dbainbri4d3a0dc2020-12-02 00:33:42 +000083func (oo *onuUniPort) createVolthaPort(ctx context.Context, apDeviceHandler *deviceHandler) error {
84 logger.Debugw(ctx, "creating-voltha-uni-port", log.Fields{
divyadesai4d299552020-08-18 07:13:49 +000085 "device-id": apDeviceHandler.device.Id, "portNo": oo.portNo})
mpagenkoaf801632020-07-03 10:00:42 +000086 //200630: per [VOL-3202] OF port info is now to be delivered within UniPort create
87 // not doing so crashes rw_core processing (at least still in 200630 version)
88 name := apDeviceHandler.device.SerialNumber + "-" + strconv.FormatUint(uint64(oo.macBpNo), 10)
89 var macOctets [6]uint8
90 macOctets[5] = 0x08
91 //ponPortNumber was copied from device.ParentPortNo
92 macOctets[4] = uint8(apDeviceHandler.ponPortNumber >> 8)
93 macOctets[3] = uint8(apDeviceHandler.ponPortNumber)
94 macOctets[2] = uint8(oo.portNo >> 16)
95 macOctets[1] = uint8(oo.portNo >> 8)
96 macOctets[0] = uint8(oo.portNo)
97 hwAddr := genMacFromOctets(macOctets)
98 ofHwAddr := macAddressToUint32Array(hwAddr)
99 capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
100 ofUniPortState := of.OfpPortState_OFPPS_LINK_DOWN
101 /* as the VOLTHA port create is only called directly after Uni Port create
102 the OfPortOperState is always Down
103 Note: this way the OfPortOperState won't ever change (directly in adapter)
104 maybe that was already always the case, but looks a bit weird - to be kept in mind ...
105 if pUniPort.operState == vc.OperStatus_ACTIVE {
106 ofUniPortState = of.OfpPortState_OFPPS_LIVE
107 }
108 */
dbainbri4d3a0dc2020-12-02 00:33:42 +0000109 logger.Debugw(ctx, "ofPort values", log.Fields{
mpagenkoaf801632020-07-03 10:00:42 +0000110 "forUniPortName": oo.name, "forMacBase": hwAddr,
111 "name": name, "hwAddr": ofHwAddr, "OperState": ofUniPortState})
112
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000113 pUniPort := &voltha.Port{
114 PortNo: oo.portNo,
115 Label: oo.name,
116 Type: voltha.Port_ETHERNET_UNI,
117 AdminState: oo.adminState,
118 OperStatus: oo.operState,
119 // obviously empty peer setting
mpagenkoaf801632020-07-03 10:00:42 +0000120 OfpPort: &of.OfpPort{
121 Name: name,
122 HwAddr: ofHwAddr,
123 Config: 0,
124 State: uint32(ofUniPortState),
125 Curr: capacity,
126 Advertised: capacity,
127 Peer: capacity,
128 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
129 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
130 },
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000131 }
132 if pUniPort != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000133 if err := apDeviceHandler.coreProxy.PortCreated(log.WithSpanFromContext(context.TODO(), ctx),
mpagenkoaf801632020-07-03 10:00:42 +0000134 apDeviceHandler.deviceID, pUniPort); err != nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000135 logger.Fatalf(ctx, "adding-uni-port: create-VOLTHA-Port-failed-%s", err)
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000136 return err
137 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000138 logger.Infow(ctx, "Voltha onuUniPort-added", log.Fields{
divyadesai4d299552020-08-18 07:13:49 +0000139 "device-id": apDeviceHandler.device.Id, "PortNo": oo.portNo})
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000140 oo.pPort = pUniPort
141 oo.operState = vc.OperStatus_DISCOVERED
142 } else {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000143 logger.Warnw(ctx, "could not create Voltha UniPort", log.Fields{
divyadesai4d299552020-08-18 07:13:49 +0000144 "device-id": apDeviceHandler.device.Id, "PortNo": oo.portNo})
Andrea Campanella6515c582020-10-05 11:25:00 +0200145 return fmt.Errorf("create Voltha UniPort %d failed on %s", oo.portNo, apDeviceHandler.device.Id)
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000146 }
147 return nil
148}
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000149
Himani Chawla6d2ae152020-09-02 13:11:20 +0530150//setOperState modifies OperState of the the UniPort
151func (oo *onuUniPort) setOperState(aNewOperState vc.OperStatus_Types) {
mpagenkoaf801632020-07-03 10:00:42 +0000152 oo.operState = aNewOperState
153}
154
155// uni port related utility functions (so far only used here)
156func genMacFromOctets(aOctets [6]uint8) string {
157 return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
158 aOctets[5], aOctets[4], aOctets[3],
159 aOctets[2], aOctets[1], aOctets[0])
160}
161
162//copied from OLT Adapter: unify centrally ?
163func macAddressToUint32Array(mac string) []uint32 {
164 slist := strings.Split(mac, ":")
165 result := make([]uint32, len(slist))
166 var err error
167 var tmp int64
168 for index, val := range slist {
169 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
170 return []uint32{1, 2, 3, 4, 5, 6}
171 }
172 result[index] = uint32(tmp)
173 }
174 return result
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000175}