blob: eb970bb5b7cdb5c8d9e5293fc5ed78bbb7521873 [file] [log] [blame]
Phaneendra Manda4c62c802019-03-06 21:37:49 +05301/*
2 * Copyright 2018-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 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070016
17//Package adaptercore provides the utility for olt devices, flows and statistics
Phaneendra Manda4c62c802019-03-06 21:37:49 +053018package adaptercore
19
20import (
cuilin20187b2a8c32019-03-26 19:52:28 -070021 "context"
22 "errors"
23 "fmt"
24 "io"
25 "strconv"
26 "strings"
27 "sync"
28 "time"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053029
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040030 "google.golang.org/grpc/codes"
31
cuilin20187b2a8c32019-03-26 19:52:28 -070032 "github.com/gogo/protobuf/proto"
33 "github.com/golang/protobuf/ptypes"
manikkaraj k9eb6cac2019-05-09 12:32:03 -040034 "github.com/mdlayher/ethernet"
cuilin20187b2a8c32019-03-26 19:52:28 -070035 com "github.com/opencord/voltha-go/adapters/common"
36 "github.com/opencord/voltha-go/common/log"
Girish Gowdru0c588b22019-04-23 23:24:56 -040037 rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
manikkaraj kbf256be2019-03-25 00:13:48 +053038 "github.com/opencord/voltha-protos/go/common"
39 ic "github.com/opencord/voltha-protos/go/inter_container"
40 of "github.com/opencord/voltha-protos/go/openflow_13"
41 oop "github.com/opencord/voltha-protos/go/openolt"
manikkaraj kbf256be2019-03-25 00:13:48 +053042 "github.com/opencord/voltha-protos/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070043 "google.golang.org/grpc"
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040044 "google.golang.org/grpc/status"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053045)
46
47//DeviceHandler will interact with the OLT device.
48type DeviceHandler struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070049 deviceID string
cuilin20187b2a8c32019-03-26 19:52:28 -070050 deviceType string
Girish Gowdru5ba46c92019-04-25 05:00:05 -040051 adminState string
cuilin20187b2a8c32019-03-26 19:52:28 -070052 device *voltha.Device
53 coreProxy *com.CoreProxy
manikkaraj kbf256be2019-03-25 00:13:48 +053054 AdapterProxy *com.AdapterProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070055 openOLT *OpenOLT
cuilin20187b2a8c32019-03-26 19:52:28 -070056 exitChannel chan int
57 lockDevice sync.RWMutex
manikkaraj kbf256be2019-03-25 00:13:48 +053058 Client oop.OpenoltClient
cuilin20187b2a8c32019-03-26 19:52:28 -070059 transitionMap *TransitionMap
60 clientCon *grpc.ClientConn
manikkaraj kbf256be2019-03-25 00:13:48 +053061 flowMgr *OpenOltFlowMgr
62 resourceMgr *rsrcMgr.OpenOltResourceMgr
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040063 discOnus map[string]bool
Mahir Gunyela3f9add2019-06-06 15:13:19 -070064 onus map[string]*OnuDevice
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070065 nniIntfID int
Mahir Gunyela3f9add2019-06-06 15:13:19 -070066}
67
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070068//OnuDevice represents ONU related info
Mahir Gunyela3f9add2019-06-06 15:13:19 -070069type OnuDevice struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070070 deviceID string
Mahir Gunyela3f9add2019-06-06 15:13:19 -070071 deviceType string
72 serialNumber string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070073 onuID uint32
74 intfID uint32
75 proxyDeviceID string
Mahir Gunyela3f9add2019-06-06 15:13:19 -070076}
77
78//NewOnuDevice creates a new Onu Device
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070079func NewOnuDevice(devID, deviceTp, serialNum string, onuID, intfID uint32, proxyDevID string) *OnuDevice {
Mahir Gunyela3f9add2019-06-06 15:13:19 -070080 var device OnuDevice
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070081 device.deviceID = devID
Mahir Gunyela3f9add2019-06-06 15:13:19 -070082 device.deviceType = deviceTp
83 device.serialNumber = serialNum
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070084 device.onuID = onuID
85 device.intfID = intfID
86 device.proxyDeviceID = proxyDevID
Mahir Gunyela3f9add2019-06-06 15:13:19 -070087 return &device
Phaneendra Manda4c62c802019-03-06 21:37:49 +053088}
89
90//NewDeviceHandler creates a new device handler
cuilin20187b2a8c32019-03-26 19:52:28 -070091func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
92 var dh DeviceHandler
93 dh.coreProxy = cp
Girish Gowdru0c588b22019-04-23 23:24:56 -040094 dh.AdapterProxy = ap
cuilin20187b2a8c32019-03-26 19:52:28 -070095 cloned := (proto.Clone(device)).(*voltha.Device)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070096 dh.deviceID = cloned.Id
cuilin20187b2a8c32019-03-26 19:52:28 -070097 dh.deviceType = cloned.Type
Girish Gowdru5ba46c92019-04-25 05:00:05 -040098 dh.adminState = "up"
cuilin20187b2a8c32019-03-26 19:52:28 -070099 dh.device = cloned
100 dh.openOLT = adapter
101 dh.exitChannel = make(chan int, 1)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400102 dh.discOnus = make(map[string]bool)
cuilin20187b2a8c32019-03-26 19:52:28 -0700103 dh.lockDevice = sync.RWMutex{}
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700104 dh.onus = make(map[string]*OnuDevice)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700105 // The nniIntfID is initialized to -1 (invalid) and set to right value
Girish Gowdru1110ef22019-06-24 11:17:59 -0400106 // when the first IntfOperInd with status as "up" is received for
107 // any one of the available NNI port on the OLT device.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700108 dh.nniIntfID = -1
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530109
cuilin20187b2a8c32019-03-26 19:52:28 -0700110 //TODO initialize the support classes.
111 return &dh
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530112}
113
114// start save the device to the data model
115func (dh *DeviceHandler) start(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700116 dh.lockDevice.Lock()
117 defer dh.lockDevice.Unlock()
118 log.Debugw("starting-device-agent", log.Fields{"device": dh.device})
119 // Add the initial device to the local model
120 log.Debug("device-agent-started")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530121}
122
123// stop stops the device dh. Not much to do for now
124func (dh *DeviceHandler) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700125 dh.lockDevice.Lock()
126 defer dh.lockDevice.Unlock()
127 log.Debug("stopping-device-agent")
128 dh.exitChannel <- 1
129 log.Debug("device-agent-stopped")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530130}
131
132func macAddressToUint32Array(mac string) []uint32 {
cuilin20187b2a8c32019-03-26 19:52:28 -0700133 slist := strings.Split(mac, ":")
134 result := make([]uint32, len(slist))
135 var err error
136 var tmp int64
137 for index, val := range slist {
138 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
139 return []uint32{1, 2, 3, 4, 5, 6}
140 }
141 result[index] = uint32(tmp)
142 }
143 return result
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530144}
145
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700146//GetportLabel returns the label for the NNI and the PON port based on port number and port type
manikkaraj kbf256be2019-03-25 00:13:48 +0530147func GetportLabel(portNum uint32, portType voltha.Port_PortType) string {
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530148
Girish Gowdru0c588b22019-04-23 23:24:56 -0400149 if portType == voltha.Port_ETHERNET_NNI {
150 return fmt.Sprintf("nni-%d", portNum)
151 } else if portType == voltha.Port_PON_OLT {
152 return fmt.Sprintf("pon-%d", portNum)
cuilin20187b2a8c32019-03-26 19:52:28 -0700153 } else if portType == voltha.Port_ETHERNET_UNI {
154 log.Errorw("local UNI management not supported", log.Fields{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400155 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700156 }
157 return ""
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530158}
159
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700160func (dh *DeviceHandler) addPort(intfID uint32, portType voltha.Port_PortType, state string) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700161 var operStatus common.OperStatus_OperStatus
162 if state == "up" {
163 operStatus = voltha.OperStatus_ACTIVE
164 } else {
165 operStatus = voltha.OperStatus_DISCOVERED
166 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700167 portNum := IntfIDToPortNo(intfID, portType)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400168 label := GetportLabel(portNum, portType)
169 if len(label) == 0 {
170 log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType})
171 return
172 }
173 // Now create Port
174 port := &voltha.Port{
cuilin20187b2a8c32019-03-26 19:52:28 -0700175 PortNo: portNum,
176 Label: label,
177 Type: portType,
178 OperStatus: operStatus,
179 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400180 log.Debugw("Sending port update to core", log.Fields{"port": port})
cuilin20187b2a8c32019-03-26 19:52:28 -0700181 // Synchronous call to update device - this method is run in its own go routine
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700182 if err := dh.coreProxy.PortCreated(context.TODO(), dh.device.Id, port); err != nil {
183 log.Errorw("error-creating-nni-port", log.Fields{"deviceID": dh.device.Id, "portType": portType, "error": err})
Girish Gowdru1110ef22019-06-24 11:17:59 -0400184 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700185
Girish Gowdru1110ef22019-06-24 11:17:59 -0400186 // Once we have successfully added the NNI port to the core, if the
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700187 // locally cached nniIntfID is set to invalid (-1), set it to the right value.
188 if portType == voltha.Port_ETHERNET_NNI && dh.nniIntfID == -1 {
189 dh.nniIntfID = int(intfID)
cuilin20187b2a8c32019-03-26 19:52:28 -0700190 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530191}
192
193// readIndications to read the indications from the OLT device
194func (dh *DeviceHandler) readIndications() {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700196 if err != nil {
197 log.Errorw("Failed to read indications", log.Fields{"err": err})
198 return
199 }
200 if indications == nil {
201 log.Errorw("Indications is nil", log.Fields{})
202 return
203 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400204 /* get device state */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700205 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400206 if err != nil || device == nil {
207 /*TODO: needs to handle error scenarios */
208 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700209 return
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400210 }
211 // When the device is in DISABLED and Adapter container restarts, we need to
212 // rebuild the locally maintained admin state.
213 if device.AdminState == voltha.AdminState_DISABLED {
214 dh.lockDevice.Lock()
215 dh.adminState = "down"
216 dh.lockDevice.Unlock()
217 }
218
cuilin20187b2a8c32019-03-26 19:52:28 -0700219 for {
220 indication, err := indications.Recv()
221 if err == io.EOF {
222 break
223 }
224 if err != nil {
225 log.Infow("Failed to read from indications", log.Fields{"err": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400226 dh.transitionMap.Handle(DeviceDownInd)
227 dh.transitionMap.Handle(DeviceInit)
228 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700229 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400230 // When OLT is admin down, allow only NNI operation status change indications.
231 if dh.adminState == "down" {
232 _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd)
233 if isIntfOperInd {
234 intfOperInd := indication.GetIntfOperInd()
235 if intfOperInd.GetType() == "nni" {
236 log.Infow("olt is admin down, allow nni ind", log.Fields{})
237 }
238 } else {
239 log.Infow("olt is admin down, ignore indication", log.Fields{})
240 continue
241 }
242 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530243
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700244 dh.handleIndication(indication)
manikkaraj kbf256be2019-03-25 00:13:48 +0530245
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700246 }
247}
248
249func (dh *DeviceHandler) handleOltIndication(oltIndication *oop.OltIndication) {
250 if oltIndication.OperState == "up" {
251 dh.transitionMap.Handle(DeviceUpInd)
252 } else if oltIndication.OperState == "down" {
253 dh.transitionMap.Handle(DeviceDownInd)
254 }
255}
256
257func (dh *DeviceHandler) handleIndication(indication *oop.Indication) {
258 switch indication.Data.(type) {
259 case *oop.Indication_OltInd:
260 dh.handleOltIndication(indication.GetOltInd())
261 case *oop.Indication_IntfInd:
262 intfInd := indication.GetIntfInd()
263 go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
264 log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd})
265 case *oop.Indication_IntfOperInd:
266 intfOperInd := indication.GetIntfOperInd()
267 if intfOperInd.GetType() == "nni" {
268 go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
269 } else if intfOperInd.GetType() == "pon" {
270 // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
271 // Handle pon port update
cuilin20187b2a8c32019-03-26 19:52:28 -0700272 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700273 log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
274 case *oop.Indication_OnuDiscInd:
275 onuDiscInd := indication.GetOnuDiscInd()
276 log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700277 sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400278 dh.onuDiscIndication(onuDiscInd, sn)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279 case *oop.Indication_OnuInd:
280 onuInd := indication.GetOnuInd()
281 log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
282 go dh.onuIndication(onuInd)
283 case *oop.Indication_OmciInd:
284 omciInd := indication.GetOmciInd()
285 log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd})
286 if err := dh.omciIndication(omciInd); err != nil {
287 log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd})
288 }
289 case *oop.Indication_PktInd:
290 pktInd := indication.GetPktInd()
291 log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
292 go dh.handlePacketIndication(pktInd)
293 case *oop.Indication_PortStats:
294 portStats := indication.GetPortStats()
295 log.Infow("Received port stats indication", log.Fields{"PortStats": portStats})
296 case *oop.Indication_FlowStats:
297 flowStats := indication.GetFlowStats()
298 log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats})
299 case *oop.Indication_AlarmInd:
300 alarmInd := indication.GetAlarmInd()
301 log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd})
cuilin20187b2a8c32019-03-26 19:52:28 -0700302 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530303}
304
305// doStateUp handle the olt up indication and update to voltha core
306func (dh *DeviceHandler) doStateUp() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307 // Synchronous call to update device state - this method is run in its own go routine
cuilin20187b2a8c32019-03-26 19:52:28 -0700308 if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400309 voltha.OperStatus_ACTIVE); err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700310 log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceID": dh.device.Id, "error": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400311 return err
312 }
313 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530314}
315
316// doStateDown handle the olt down indication
317func (dh *DeviceHandler) doStateDown() error {
Girish Gowdrud4245152019-05-10 00:47:31 -0400318 log.Debug("do-state-down-start")
319
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700320 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdrud4245152019-05-10 00:47:31 -0400321 if err != nil || device == nil {
322 /*TODO: needs to handle error scenarios */
323 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700324 return errors.New("failed to fetch device device")
Girish Gowdrud4245152019-05-10 00:47:31 -0400325 }
326
327 cloned := proto.Clone(device).(*voltha.Device)
328 // Update the all ports state on that device to disable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700329 if er := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); er != nil {
330 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": er})
331 return er
Girish Gowdrud4245152019-05-10 00:47:31 -0400332 }
333
334 //Update the device oper state and connection status
335 cloned.OperStatus = voltha.OperStatus_UNKNOWN
336 cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
337 dh.device = cloned
338
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700339 if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
340 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": er})
341 return er
Girish Gowdrud4245152019-05-10 00:47:31 -0400342 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400343
344 //get the child device for the parent device
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700345 onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400346 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700347 log.Errorw("failed to get child devices information", log.Fields{"deviceID": dh.device.Id, "error": err})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400348 return err
349 }
350 for _, onuDevice := range onuDevices.Items {
351
352 // Update onu state as down in onu adapter
353 onuInd := oop.OnuIndication{}
354 onuInd.OperState = "down"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700355 er := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
356 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
357 if er != nil {
358 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
359 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
360 return er
361 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400362 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700363 log.Debugw("do-state-down-end", log.Fields{"deviceID": device.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700364 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530365}
366
367// doStateInit dial the grpc before going to init state
368func (dh *DeviceHandler) doStateInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400369 var err error
Girish Gowdrud4245152019-05-10 00:47:31 -0400370 dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock())
Girish Gowdru0c588b22019-04-23 23:24:56 -0400371 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700372 log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceID, "HostAndPort": dh.device.GetHostAndPort(), "err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400373 return err
374 }
375 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530376}
377
378// postInit create olt client instance to invoke RPC on the olt device
379func (dh *DeviceHandler) postInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400380 dh.Client = oop.NewOpenoltClient(dh.clientCon)
381 dh.transitionMap.Handle(GrpcConnected)
382 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530383}
384
385// doStateConnected get the device info and update to voltha core
386func (dh *DeviceHandler) doStateConnected() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400387 log.Debug("OLT device has been connected")
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400388
389 // Case where OLT is disabled and then rebooted.
390 if dh.adminState == "down" {
391 log.Debugln("do-state-connected--device-admin-state-down")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700392 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400393 if err != nil || device == nil {
394 /*TODO: needs to handle error scenarios */
395 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
396 }
397
398 cloned := proto.Clone(device).(*voltha.Device)
399 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
400 cloned.OperStatus = voltha.OperStatus_UNKNOWN
401 dh.device = cloned
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700402 if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
403 log.Errorw("error-updating-device-state", log.Fields{"deviceID": dh.device.Id, "error": er})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400404 }
405
406 // Since the device was disabled before the OLT was rebooted, enfore the OLT to be Disabled after re-connection.
407 _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
408 if err != nil {
409 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
410 }
411
412 // Start reading indications
413 go dh.readIndications()
414 return nil
415 }
416
Girish Gowdru0c588b22019-04-23 23:24:56 -0400417 deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700418 if err != nil {
419 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
420 return err
421 }
422 if deviceInfo == nil {
423 log.Errorw("Device info is nil", log.Fields{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700424 return errors.New("failed to get device info from OLT")
cuilin20187b2a8c32019-03-26 19:52:28 -0700425 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400426 log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo})
cuilin20187b2a8c32019-03-26 19:52:28 -0700427 dh.device.Root = true
428 dh.device.Vendor = deviceInfo.Vendor
429 dh.device.Model = deviceInfo.Model
cuilin20187b2a8c32019-03-26 19:52:28 -0700430 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
431 dh.device.HardwareVersion = deviceInfo.HardwareVersion
432 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400433 // FIXME: Remove Hardcodings
cuilin20187b2a8c32019-03-26 19:52:28 -0700434 dh.device.MacAddress = "0a:0b:0c:0d:0e:0f"
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530435
cuilin20187b2a8c32019-03-26 19:52:28 -0700436 // Synchronous call to update device - this method is run in its own go routine
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700437 if er := dh.coreProxy.DeviceUpdate(context.TODO(), dh.device); er != nil {
438 log.Errorw("error-updating-device", log.Fields{"deviceID": dh.device.Id, "error": er})
cuilin20187b2a8c32019-03-26 19:52:28 -0700439 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400440
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700441 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdrud4245152019-05-10 00:47:31 -0400442 if err != nil || device == nil {
443 /*TODO: needs to handle error scenarios */
444 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700445 return err
Girish Gowdrud4245152019-05-10 00:47:31 -0400446 }
447 cloned := proto.Clone(device).(*voltha.Device)
448 // Update the all ports (if available) on that device to ACTIVE.
449 // The ports do not normally exist, unless the device is coming back from a reboot
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700450 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
451 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400452 return err
453 }
454
Girish Gowdru0c588b22019-04-23 23:24:56 -0400455 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
456 // Instantiate resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700457 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceID, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400458 log.Error("Error while instantiating resource manager")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700459 return errors.New("instantiating resource manager failed")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400460 }
461 // Instantiate flow manager
462 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
463 log.Error("Error while instantiating flow manager")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700464 return errors.New("instantiating flow manager failed")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400465 }
466 /* TODO: Instantiate Alarm , stats , BW managers */
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530467
cuilin20187b2a8c32019-03-26 19:52:28 -0700468 // Start reading indications
469 go dh.readIndications()
470 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530471}
472
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700473//AdoptDevice adopts the OLT device
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530474func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400475 dh.transitionMap = NewTransitionMap(dh)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700476 log.Infow("Adopt_device", log.Fields{"deviceID": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400477 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530478}
479
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700480//GetOfpDeviceInfo Gets the Ofp information of the given device
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530481func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700482 return &ic.SwitchCapability{
483 Desc: &of.OfpDesc{
Devmalya Paul70dd4972019-06-10 15:19:17 +0530484 MfrDesc: "VOLTHA Project",
cuilin20187b2a8c32019-03-26 19:52:28 -0700485 HwDesc: "open_pon",
486 SwDesc: "open_pon",
487 SerialNum: dh.device.SerialNumber,
488 },
489 SwitchFeatures: &of.OfpSwitchFeatures{
490 NBuffers: 256,
491 NTables: 2,
492 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
493 of.OfpCapabilities_OFPC_TABLE_STATS |
494 of.OfpCapabilities_OFPC_PORT_STATS |
495 of.OfpCapabilities_OFPC_GROUP_STATS),
496 },
497 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530498}
499
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700500//GetOfpPortInfo Get Ofp port information
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530501func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700502 capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
cuilin20187b2a8c32019-03-26 19:52:28 -0700503 return &ic.PortCapability{
504 Port: &voltha.LogicalPort{
505 OfpPort: &of.OfpPort{
506 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
507 Config: 0,
508 State: uint32(of.OfpPortState_OFPPS_LIVE),
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700509 Curr: capacity,
510 Advertised: capacity,
511 Peer: capacity,
cuilin20187b2a8c32019-03-26 19:52:28 -0700512 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
513 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
514 },
515 DeviceId: dh.device.Id,
516 DevicePortNo: uint32(portNo),
517 },
518 }, nil
519}
520
521func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700522 log.Debugw("omci indication", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700523 var deviceType string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700524 var deviceID string
525 var proxyDeviceID string
cuilin20187b2a8c32019-03-26 19:52:28 -0700526
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700527 onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId)
528 if onuInCache, ok := dh.onus[onuKey]; !ok {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700529 log.Debugw("omci indication for a device not in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
530 ponPort := IntfIDToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700531 kwargs := make(map[string]interface{})
532 kwargs["onu_id"] = omciInd.OnuId
533 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700534
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700535 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
536 if err != nil {
537 log.Errorw("onu not found", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700538 return err
cuilin20187b2a8c32019-03-26 19:52:28 -0700539 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700540 deviceType = onuDevice.Type
541 deviceID = onuDevice.Id
542 proxyDeviceID = onuDevice.ProxyAddress.DeviceId
543 //if not exist in cache, then add to cache.
544 dh.onus[onuKey] = NewOnuDevice(deviceID, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceID)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700545 } else {
546 //found in cache
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700547 log.Debugw("omci indication for a device in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700548 deviceType = onuInCache.deviceType
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700549 deviceID = onuInCache.deviceID
550 proxyDeviceID = onuInCache.proxyDeviceID
cuilin20187b2a8c32019-03-26 19:52:28 -0700551 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700552
553 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
554 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
555 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700556 deviceID, proxyDeviceID, ""); sendErr != nil {
557 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuID": deviceID, "proxyDeviceID": proxyDeviceID})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700558 return sendErr
559 }
560 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530561}
562
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700563//ProcessInterAdapterMessage sends the proxied messages to the target device
564// If the proxy address is not found in the unmarshalled message, it first fetches the onu device for which the message
565// is meant, and then send the unmarshalled omci message to this onu
566func (dh *DeviceHandler) ProcessInterAdapterMessage(msg *ic.InterAdapterMessage) error {
567 log.Debugw("Process_inter_adapter_message", log.Fields{"msgID": msg.Header.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700568 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700569 msgID := msg.Header.Id
cuilin20187b2a8c32019-03-26 19:52:28 -0700570 fromTopic := msg.Header.FromTopic
571 toTopic := msg.Header.ToTopic
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700572 toDeviceID := msg.Header.ToDeviceId
573 proxyDeviceID := msg.Header.ProxyDeviceId
cuilin20187b2a8c32019-03-26 19:52:28 -0700574
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700575 log.Debugw("omci request message header", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
cuilin20187b2a8c32019-03-26 19:52:28 -0700576
577 msgBody := msg.GetBody()
578
579 omciMsg := &ic.InterAdapterOmciMessage{}
580 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
581 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
582 return err
583 }
584
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700585 if omciMsg.GetProxyAddress() == nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700586 onuDevice, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, toDeviceID)
587 if err != nil {
588 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceID, "error": err})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700589 return err
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700590 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700591 log.Debugw("device retrieved from core", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
592 dh.sendProxiedMessage(onuDevice, omciMsg)
593
cuilin20187b2a8c32019-03-26 19:52:28 -0700594 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700595 log.Debugw("Proxy Address found in omci message", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700596 dh.sendProxiedMessage(nil, omciMsg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700597 }
598
599 } else {
600 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
601 }
602 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530603}
604
cuilin20187b2a8c32019-03-26 19:52:28 -0700605func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700606 var intfID uint32
607 var onuID uint32
608 var connectStatus common.ConnectStatus_ConnectStatus
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700609 if onuDevice != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700610 intfID = onuDevice.ProxyAddress.GetChannelId()
611 onuID = onuDevice.ProxyAddress.GetOnuId()
612 connectStatus = onuDevice.ConnectStatus
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700613 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700614 intfID = omciMsg.GetProxyAddress().GetChannelId()
615 onuID = omciMsg.GetProxyAddress().GetOnuId()
616 connectStatus = omciMsg.GetConnectStatus()
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700617 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700618 if connectStatus != voltha.ConnectStatus_REACHABLE {
619 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfID": intfID, "onuID": onuID})
cuilin20187b2a8c32019-03-26 19:52:28 -0700620 return
621 }
622
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700623 omciMessage := &oop.OmciMsg{IntfId: intfID, OnuId: onuID, Pkt: omciMsg.Message}
cuilin20187b2a8c32019-03-26 19:52:28 -0700624
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700625 _, err := dh.Client.OmciMsgOut(context.Background(), omciMessage)
626 if err != nil {
627 log.Errorw("unable to send omci-msg-out", log.Fields{"IntfID": intfID, "OnuID": onuID, "Msg": omciMessage})
628 return
629 }
630 log.Debugw("omci-message-sent", log.Fields{"intfID": intfID, "onuID": onuID, "omciMsg": string(omciMsg.GetMessage())})
cuilin20187b2a8c32019-03-26 19:52:28 -0700631}
632
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700633func (dh *DeviceHandler) activateONU(intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) {
634 log.Debugw("activate-onu", log.Fields{"intfID": intfID, "onuID": onuID, "serialNum": serialNum, "serialNumber": serialNumber})
635 dh.flowMgr.UpdateOnuInfo(intfID, uint32(onuID), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700636 // TODO: need resource manager
637 var pir uint32 = 1000000
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700638 Onu := oop.Onu{IntfId: intfID, OnuId: uint32(onuID), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530639 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400640 st, _ := status.FromError(err)
641 if st.Code() == codes.AlreadyExists {
642 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
643 } else {
644 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
645 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700646 } else {
647 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
648 }
649}
650
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400651func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, sn string) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700652 channelID := onuDiscInd.GetIntfId()
653 parentPortNo := IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400654 if _, ok := dh.discOnus[sn]; ok {
655 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
656 return nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700657 }
658
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400659 dh.lockDevice.Lock()
660 dh.discOnus[sn] = true
661 dh.lockDevice.Unlock()
662 // evict the onu serial number from local cache
663 defer func() {
664 delete(dh.discOnus, sn)
665 }()
666
cuilin20187b2a8c32019-03-26 19:52:28 -0700667 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400668 if sn != "" {
669 kwargs["serial_number"] = sn
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400670 } else {
671 log.Error("invalid onu serial number")
672 return errors.New("failed to fetch onu serial number")
673 }
674
675 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
676 var onuID uint32
677 if onuDevice == nil || err != nil {
678 onuID, err = dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
679 if err != nil {
680 log.Errorw("failed to fetch onuID from resource manager", log.Fields{"err": err})
681 return err
682 }
683 if err := dh.coreProxy.ChildDeviceDetected(context.TODO(), dh.device.Id, int(parentPortNo),
684 "brcm_openomci_onu", int(channelID),
685 string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuID)); err != nil {
686 log.Errorw("Create onu error",
687 log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(),
688 "onuID": onuID, "sn": sn, "error": err})
689 return err
690 }
691
692 } else {
693 onuID = onuDevice.ProxyAddress.OnuId
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400694 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700695 kwargs["onu_id"] = onuID
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400696 kwargs["parent_port_no"] = parentPortNo
cuilin20187b2a8c32019-03-26 19:52:28 -0700697 for i := 0; i < 10; i++ {
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400698 if onuDevice, _ := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs); onuDevice != nil {
699 err := dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
700 if err != nil {
701 log.Errorw("failed to update device state", log.Fields{"DeviceID": onuDevice.Id})
702 return err
703 }
704
705 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700706 dh.activateONU(onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn)
cuilin20187b2a8c32019-03-26 19:52:28 -0700707 return nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700708 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700709 time.Sleep(1 * time.Second)
710 log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1)
cuilin20187b2a8c32019-03-26 19:52:28 -0700711 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700712 log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuID": onuID, "sn": sn})
713 return errors.New("failed to activate onu")
cuilin20187b2a8c32019-03-26 19:52:28 -0700714}
715
716func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
717 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
718
719 kwargs := make(map[string]interface{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700720 ponPort := IntfIDToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
manikkaraj kbf256be2019-03-25 00:13:48 +0530721
cuilin20187b2a8c32019-03-26 19:52:28 -0700722 if serialNumber != "" {
723 kwargs["serial_number"] = serialNumber
724 } else {
725 kwargs["onu_id"] = onuInd.OnuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400726 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700727 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700728 if onuDevice, _ := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs); onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400729 if onuDevice.ParentPortNo != ponPort {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700730 //log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": intfIDFromPortNo(onuDevice.ParentPortNo), "currentIntfId": onuInd.GetIntfId()})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400731 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700732 }
733
734 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
735 log.Warnw("ONU-id-mismatch, can happen if both voltha and the olt rebooted", log.Fields{"expected_onu_id": onuDevice.ProxyAddress.OnuId, "received_onu_id": onuInd.OnuId})
736 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700737 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId())
738 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700739 dh.updateOnuAdminState(onuInd)
740 dh.updateOnuStates(onuDevice, onuInd)
cuilin20187b2a8c32019-03-26 19:52:28 -0700741
cuilin20187b2a8c32019-03-26 19:52:28 -0700742 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700743 log.Errorw("onu not found", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId})
cuilin20187b2a8c32019-03-26 19:52:28 -0700744 return
745 }
746
747}
748
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700749func (dh *DeviceHandler) updateOnuStates(onuDevice *voltha.Device, onuInd *oop.OnuIndication) {
750 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId())
751 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
752 dh.updateOnuAdminState(onuInd)
753 // operState
754 if onuInd.OperState == "down" {
755 if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE {
756 err := dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_UNREACHABLE,
757 onuDevice.OperStatus)
758 if err != nil {
759 log.Errorw("unable to update onu state", log.Fields{"DeviceID": onuDevice.Id})
760 return
761 }
762 log.Debugln("onu-oper-state-is-down")
763 }
764 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
765 err := dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_UNREACHABLE,
766 common.OperStatus_DISCOVERED)
767 if err != nil {
768 log.Errorw("unable to update onu state", log.Fields{"DeviceID": onuDevice.Id})
769 return
770 }
771 }
772 log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd})
773
774 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
775 err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
776 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
777 if err != nil {
778 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
779 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
780 }
781 } else if onuInd.OperState == "up" {
782 if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE {
783 err := dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus)
784 if err != nil {
785 log.Errorw("unable to update onu state", log.Fields{"DeviceID": onuDevice.Id})
786 return
787 }
788 }
789 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
790 log.Warnw("ignore onu indication", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState})
791 return
792 }
793 err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
794 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
795 if err != nil {
796 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
797 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
798 return
799 }
800 } else {
801 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
802 }
803}
804
805func (dh *DeviceHandler) updateOnuAdminState(onuInd *oop.OnuIndication) {
806 if onuInd.AdminState == "down" {
807 if onuInd.OperState != "down" {
808 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
809 // Forcing the oper state change code to execute
810 onuInd.OperState = "down"
811 }
812 // Port and logical port update is taken care of by oper state block
813 } else if onuInd.AdminState == "up" {
814 log.Debugln("received-onu-admin-state up")
815 } else {
816 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
817 }
818 log.Debugln("admin-state-dealt-with")
819}
820
cuilin20187b2a8c32019-03-26 19:52:28 -0700821func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
822 if serialNum != nil {
823 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
cuilin20187b2a8c32019-03-26 19:52:28 -0700824 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700825 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700826}
827
828func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
829 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700830 fmt.Sprintf("%x", uint32(vendorSpecific[0]&0x0f)) +
cuilin20187b2a8c32019-03-26 19:52:28 -0700831 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
832 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
833 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
834 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
835 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
836 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
837 return tmp
838}
839
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700840//UpdateFlowsBulk upates the bulk flow
841func (dh *DeviceHandler) UpdateFlowsBulk() error {
842 return errors.New("unimplemented")
cuilin20187b2a8c32019-03-26 19:52:28 -0700843}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700844
845//GetChildDevice returns the child device for given parent port and onu id
846func (dh *DeviceHandler) GetChildDevice(parentPort, onuID uint32) *voltha.Device {
847 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuID": onuID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400848 kwargs := make(map[string]interface{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700849 kwargs["onu_id"] = onuID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400850 kwargs["parent_port_no"] = parentPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700851 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400852 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700853 log.Errorw("onu not found", log.Fields{"intfID": parentPort, "onuID": onuID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400854 return nil
855 }
856 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
857 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530858}
859
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700860// SendPacketInToCore sends packet-in to core
861// For this, it calls SendPacketIn of the core-proxy which uses a device specific topic to send the request.
862// The adapter handling the device creates a device specific topic
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400863func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
864 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700865 if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPort, packetPayload); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400866 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
867 return
868 }
869 log.Debug("Sent packet-in to core successfully")
870}
871
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700872//UpdateFlowsIncrementally updates the device flow
manikkaraj kbf256be2019-03-25 00:13:48 +0530873func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700874 log.Debugw("In Update_flows_incrementally", log.Fields{"deviceID": device.Id, "flows": flows, "groups": groups})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400875 if flows != nil {
876 for _, flow := range flows.ToAdd.Items {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400877 log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400878 dh.flowMgr.AddFlow(flow)
879 }
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400880 for _, flow := range flows.ToRemove.Items {
881 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
882 dh.flowMgr.RemoveFlow(flow)
883 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400884 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700885 if groups != nil && flows != nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400886 for _, flow := range flows.ToRemove.Items {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700887 log.Debug("Removing flow", log.Fields{"deviceID": device.Id, "flowToRemove": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400888 // dh.flowMgr.RemoveFlow(flow)
889 }
890 }
891 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530892}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400893
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700894//DisableDevice disables the given device
895//It marks the following for the given device:
896//Device-Handler Admin-State : down
897//Device Port-State: UNKNOWN
898//Device Oper-State: UNKNOWN
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400899func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
900 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
901 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
902 return err
903 }
904 dh.lockDevice.Lock()
905 dh.adminState = "down"
906 dh.lockDevice.Unlock()
907 log.Debug("olt-disabled")
908
909 cloned := proto.Clone(device).(*voltha.Device)
910 // Update the all ports state on that device to disable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700911 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
912 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400913 return err
914 }
915
916 //Update the device oper state
917 cloned.OperStatus = voltha.OperStatus_UNKNOWN
918 dh.device = cloned
919
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700920 if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
921 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400922 return err
923 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700924 log.Debugw("Disable_device-end", log.Fields{"deviceID": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400925 return nil
926}
927
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700928//ReenableDevice re-enables the olt device after disable
929//It marks the following for the given device:
930//Device-Handler Admin-State : up
931//Device Port-State: ACTIVE
932//Device Oper-State: ACTIVE
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400933func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
934 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
935 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
936 return err
937 }
938
939 dh.lockDevice.Lock()
940 dh.adminState = "up"
941 dh.lockDevice.Unlock()
942 log.Debug("olt-reenabled")
943
944 cloned := proto.Clone(device).(*voltha.Device)
945 // Update the all ports state on that device to enable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700946 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
947 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400948 return err
949 }
950
951 //Update the device oper status as ACTIVE
952 cloned.OperStatus = voltha.OperStatus_ACTIVE
953 dh.device = cloned
954
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700955 if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
956 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400957 return err
958 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700959 log.Debugw("ReEnableDevice-end", log.Fields{"deviceID": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400960
961 return nil
962}
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400963
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700964//RebootDevice reboots the given device
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400965func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
966 if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
967 log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
968 return err
969 }
970
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700971 log.Debugw("rebooted-device-successfully", log.Fields{"deviceID": device.Id})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400972
973 return nil
974}
975
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400976func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
977 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
978 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
979 if err != nil {
980 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
981 return
982 }
983 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700984 if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400985 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
986 return
987 }
988 log.Debug("Success sending packet-in to core!")
989}
990
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700991// PacketOut sends packet-out from VOLTHA to OLT on the egress port provided
992func (dh *DeviceHandler) PacketOut(egressPortNo int, packet *of.OfpPacketOut) error {
993 log.Debugw("PacketOut", log.Fields{"deviceID": dh.deviceID, "egress_port_no": egressPortNo, "pkt-length": len(packet.Data)})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400994 var etherFrame ethernet.Frame
995 err := (&etherFrame).UnmarshalBinary(packet.Data)
996 if err != nil {
997 log.Errorw("Failed to unmarshal into ethernet frame", log.Fields{"err": err, "pkt-length": len(packet.Data)})
998 return err
999 }
1000 log.Debugw("Ethernet Frame", log.Fields{"Frame": etherFrame})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001001 egressPortType := IntfIDToPortTypeName(uint32(egressPortNo))
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001002 if egressPortType == voltha.Port_ETHERNET_UNI {
1003 if etherFrame.VLAN != nil { // If double tag, remove the outer tag
1004 nextEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
1005 if nextEthType == 0x8100 {
1006 etherFrame.VLAN = nil
1007 packet.Data, err = etherFrame.MarshalBinary()
1008 if err != nil {
1009 log.Fatalf("failed to marshal frame: %v", err)
1010 return err
1011 }
1012 if err := (&etherFrame).UnmarshalBinary(packet.Data); err != nil {
1013 log.Fatalf("failed to unmarshal frame: %v", err)
1014 return err
1015 }
1016 log.Debug("Double tagged packet , removed outer vlan", log.Fields{"New frame": etherFrame})
1017 }
1018 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001019 intfID := IntfIDFromUniPortNum(uint32(egressPortNo))
1020 onuID := OnuIDFromPortNum(uint32(egressPortNo))
1021 uniID := UniIDFromPortNum(uint32(egressPortNo))
1022 /*gemPortId, err := dh.flowMgr.GetPacketOutGemPortId(intfID, onuID, uint32(egress_port_no))
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001023 if err != nil{
1024 log.Errorw("Error while getting gemport to packet-out",log.Fields{"error": err})
1025 return err
1026 }*/
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001027 onuPkt := oop.OnuPacket{IntfId: intfID, OnuId: onuID, PortNo: uint32(egressPortNo), Pkt: packet.Data}
1028 log.Debug("sending-packet-to-ONU", log.Fields{"egress_port_no": egressPortNo, "IntfId": intfID, "onuID": onuID,
1029 "uniID": uniID, "packet": packet.Data})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001030 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
1031 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
1032 return err
1033 }
1034 } else if egressPortType == voltha.Port_ETHERNET_NNI {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001035 uplinkPkt := oop.UplinkPacket{IntfId: IntfIDFromNniPortNum(uint32(egressPortNo)), Pkt: packet.Data}
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001036 log.Debug("sending-packet-to-uplink", log.Fields{"uplink_pkt": uplinkPkt})
1037 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
1038 log.Errorw("Error while sending packet-out to uplink", log.Fields{"error": err})
1039 return err
1040 }
1041 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001042 log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egressPortNo, "egressPortType": egressPortType})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001043 }
1044 return nil
1045}
Mahir Gunyela3f9add2019-06-06 15:13:19 -07001046
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001047func (dh *DeviceHandler) formOnuKey(intfID, onuID uint32) string {
1048 return "" + strconv.Itoa(int(intfID)) + "." + strconv.Itoa(int(onuID))
Mahir Gunyela3f9add2019-06-06 15:13:19 -07001049}