blob: 5fddcf76ab413f0d94292bad6c33777782bd5994 [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 */
16package adaptercore
17
18import (
cuilin20187b2a8c32019-03-26 19:52:28 -070019 "context"
20 "errors"
21 "fmt"
22 "io"
23 "strconv"
24 "strings"
25 "sync"
26 "time"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053027
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040028 "google.golang.org/grpc/codes"
29
cuilin20187b2a8c32019-03-26 19:52:28 -070030 "github.com/gogo/protobuf/proto"
31 "github.com/golang/protobuf/ptypes"
manikkaraj k9eb6cac2019-05-09 12:32:03 -040032 "github.com/mdlayher/ethernet"
cuilin20187b2a8c32019-03-26 19:52:28 -070033 com "github.com/opencord/voltha-go/adapters/common"
34 "github.com/opencord/voltha-go/common/log"
Girish Gowdru0c588b22019-04-23 23:24:56 -040035 rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
manikkaraj kbf256be2019-03-25 00:13:48 +053036 "github.com/opencord/voltha-protos/go/common"
37 ic "github.com/opencord/voltha-protos/go/inter_container"
38 of "github.com/opencord/voltha-protos/go/openflow_13"
39 oop "github.com/opencord/voltha-protos/go/openolt"
manikkaraj kbf256be2019-03-25 00:13:48 +053040 "github.com/opencord/voltha-protos/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070041 "google.golang.org/grpc"
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040042 "google.golang.org/grpc/status"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053043)
44
45//DeviceHandler will interact with the OLT device.
46type DeviceHandler struct {
cuilin20187b2a8c32019-03-26 19:52:28 -070047 deviceId string
48 deviceType string
Girish Gowdru5ba46c92019-04-25 05:00:05 -040049 adminState string
cuilin20187b2a8c32019-03-26 19:52:28 -070050 device *voltha.Device
51 coreProxy *com.CoreProxy
manikkaraj kbf256be2019-03-25 00:13:48 +053052 AdapterProxy *com.AdapterProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070053 openOLT *OpenOLT
cuilin20187b2a8c32019-03-26 19:52:28 -070054 exitChannel chan int
55 lockDevice sync.RWMutex
manikkaraj kbf256be2019-03-25 00:13:48 +053056 Client oop.OpenoltClient
cuilin20187b2a8c32019-03-26 19:52:28 -070057 transitionMap *TransitionMap
58 clientCon *grpc.ClientConn
manikkaraj kbf256be2019-03-25 00:13:48 +053059 flowMgr *OpenOltFlowMgr
60 resourceMgr *rsrcMgr.OpenOltResourceMgr
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040061 discOnus map[string]bool
Mahir Gunyela3f9add2019-06-06 15:13:19 -070062 onus map[string]*OnuDevice
Girish Gowdru1110ef22019-06-24 11:17:59 -040063 nniIntfId int
Mahir Gunyela3f9add2019-06-06 15:13:19 -070064}
65
66type OnuDevice struct {
67 deviceId string
68 deviceType string
69 serialNumber string
70 onuId uint32
71 intfId uint32
72 proxyDeviceId string
73}
74
75//NewOnuDevice creates a new Onu Device
76func NewOnuDevice(devId string, deviceTp string, serialNum string, onuId uint32, intfId uint32, proxyDevId string) *OnuDevice {
77 var device OnuDevice
78 device.deviceId = devId
79 device.deviceType = deviceTp
80 device.serialNumber = serialNum
81 device.onuId = onuId
82 device.intfId = intfId
83 device.proxyDeviceId = proxyDevId
84 return &device
Phaneendra Manda4c62c802019-03-06 21:37:49 +053085}
86
87//NewDeviceHandler creates a new device handler
cuilin20187b2a8c32019-03-26 19:52:28 -070088func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
89 var dh DeviceHandler
90 dh.coreProxy = cp
Girish Gowdru0c588b22019-04-23 23:24:56 -040091 dh.AdapterProxy = ap
cuilin20187b2a8c32019-03-26 19:52:28 -070092 cloned := (proto.Clone(device)).(*voltha.Device)
93 dh.deviceId = cloned.Id
94 dh.deviceType = cloned.Type
Girish Gowdru5ba46c92019-04-25 05:00:05 -040095 dh.adminState = "up"
cuilin20187b2a8c32019-03-26 19:52:28 -070096 dh.device = cloned
97 dh.openOLT = adapter
98 dh.exitChannel = make(chan int, 1)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040099 dh.discOnus = make(map[string]bool)
cuilin20187b2a8c32019-03-26 19:52:28 -0700100 dh.lockDevice = sync.RWMutex{}
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700101 dh.onus = make(map[string]*OnuDevice)
Girish Gowdru1110ef22019-06-24 11:17:59 -0400102 // The nniIntfId is initialized to -1 (invalid) and set to right value
103 // when the first IntfOperInd with status as "up" is received for
104 // any one of the available NNI port on the OLT device.
105 dh.nniIntfId = -1
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530106
cuilin20187b2a8c32019-03-26 19:52:28 -0700107 //TODO initialize the support classes.
108 return &dh
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530109}
110
111// start save the device to the data model
112func (dh *DeviceHandler) start(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700113 dh.lockDevice.Lock()
114 defer dh.lockDevice.Unlock()
115 log.Debugw("starting-device-agent", log.Fields{"device": dh.device})
116 // Add the initial device to the local model
117 log.Debug("device-agent-started")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530118}
119
120// stop stops the device dh. Not much to do for now
121func (dh *DeviceHandler) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700122 dh.lockDevice.Lock()
123 defer dh.lockDevice.Unlock()
124 log.Debug("stopping-device-agent")
125 dh.exitChannel <- 1
126 log.Debug("device-agent-stopped")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530127}
128
129func macAddressToUint32Array(mac string) []uint32 {
cuilin20187b2a8c32019-03-26 19:52:28 -0700130 slist := strings.Split(mac, ":")
131 result := make([]uint32, len(slist))
132 var err error
133 var tmp int64
134 for index, val := range slist {
135 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
136 return []uint32{1, 2, 3, 4, 5, 6}
137 }
138 result[index] = uint32(tmp)
139 }
140 return result
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530141}
142
manikkaraj kbf256be2019-03-25 00:13:48 +0530143func GetportLabel(portNum uint32, portType voltha.Port_PortType) string {
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530144
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 if portType == voltha.Port_ETHERNET_NNI {
146 return fmt.Sprintf("nni-%d", portNum)
147 } else if portType == voltha.Port_PON_OLT {
148 return fmt.Sprintf("pon-%d", portNum)
cuilin20187b2a8c32019-03-26 19:52:28 -0700149 } else if portType == voltha.Port_ETHERNET_UNI {
150 log.Errorw("local UNI management not supported", log.Fields{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400151 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700152 }
153 return ""
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530154}
155
156func (dh *DeviceHandler) addPort(intfId uint32, portType voltha.Port_PortType, state string) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700157 var operStatus common.OperStatus_OperStatus
158 if state == "up" {
159 operStatus = voltha.OperStatus_ACTIVE
160 } else {
161 operStatus = voltha.OperStatus_DISCOVERED
162 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400163 portNum := IntfIdToPortNo(intfId, portType)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400164 label := GetportLabel(portNum, portType)
165 if len(label) == 0 {
166 log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType})
167 return
168 }
169 // Now create Port
170 port := &voltha.Port{
cuilin20187b2a8c32019-03-26 19:52:28 -0700171 PortNo: portNum,
172 Label: label,
173 Type: portType,
174 OperStatus: operStatus,
175 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 log.Debugw("Sending port update to core", log.Fields{"port": port})
cuilin20187b2a8c32019-03-26 19:52:28 -0700177 // Synchronous call to update device - this method is run in its own go routine
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178 if err := dh.coreProxy.PortCreated(nil, dh.device.Id, port); err != nil {
Girish Gowdru1110ef22019-06-24 11:17:59 -0400179 log.Errorw("error-creating-port", log.Fields{"deviceId": dh.device.Id, "portType": portType, "error": err})
180 return
181 }
182 // Once we have successfully added the NNI port to the core, if the
183 // locally cached nniIntfId is set to invalid (-1), set it to the right value.
184 if portType == voltha.Port_ETHERNET_NNI && dh.nniIntfId == -1 {
185 dh.nniIntfId = int(intfId)
cuilin20187b2a8c32019-03-26 19:52:28 -0700186 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530187}
188
189// readIndications to read the indications from the OLT device
190func (dh *DeviceHandler) readIndications() {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191 indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700192 if err != nil {
193 log.Errorw("Failed to read indications", log.Fields{"err": err})
194 return
195 }
196 if indications == nil {
197 log.Errorw("Indications is nil", log.Fields{})
198 return
199 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400200 /* get device state */
201 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
202 if err != nil || device == nil {
203 /*TODO: needs to handle error scenarios */
204 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
205
206 }
207 // When the device is in DISABLED and Adapter container restarts, we need to
208 // rebuild the locally maintained admin state.
209 if device.AdminState == voltha.AdminState_DISABLED {
210 dh.lockDevice.Lock()
211 dh.adminState = "down"
212 dh.lockDevice.Unlock()
213 }
214
cuilin20187b2a8c32019-03-26 19:52:28 -0700215 for {
216 indication, err := indications.Recv()
217 if err == io.EOF {
218 break
219 }
220 if err != nil {
221 log.Infow("Failed to read from indications", log.Fields{"err": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400222 dh.transitionMap.Handle(DeviceDownInd)
223 dh.transitionMap.Handle(DeviceInit)
224 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700225 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400226 // When OLT is admin down, allow only NNI operation status change indications.
227 if dh.adminState == "down" {
228 _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd)
229 if isIntfOperInd {
230 intfOperInd := indication.GetIntfOperInd()
231 if intfOperInd.GetType() == "nni" {
232 log.Infow("olt is admin down, allow nni ind", log.Fields{})
233 }
234 } else {
235 log.Infow("olt is admin down, ignore indication", log.Fields{})
236 continue
237 }
238 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700239 switch indication.Data.(type) {
240 case *oop.Indication_OltInd:
241 oltInd := indication.GetOltInd()
242 if oltInd.OperState == "up" {
243 dh.transitionMap.Handle(DeviceUpInd)
244 } else if oltInd.OperState == "down" {
245 dh.transitionMap.Handle(DeviceDownInd)
246 }
247 case *oop.Indication_IntfInd:
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530248
cuilin20187b2a8c32019-03-26 19:52:28 -0700249 intfInd := indication.GetIntfInd()
250 go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
251 log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd})
252 case *oop.Indication_IntfOperInd:
253 intfOperInd := indication.GetIntfOperInd()
254 if intfOperInd.GetType() == "nni" {
255 go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
256 } else if intfOperInd.GetType() == "pon" {
257 // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
258 // Handle pon port update
259 }
260 log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
261 case *oop.Indication_OnuDiscInd:
262 onuDiscInd := indication.GetOnuDiscInd()
263 log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400264 //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
265 //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
266 // TODO Get onu ID from the resource manager
cuilin20187b2a8c32019-03-26 19:52:28 -0700267 var onuId uint32 = 1
Girish Gowdru0c588b22019-04-23 23:24:56 -0400268 /*if err != nil{
269 log.Errorw("onu-id-unavailable",log.Fields{"intfId":onuDiscInd.GetIntfId()})
270 return
271 }*/
manikkaraj kbf256be2019-03-25 00:13:48 +0530272
cuilin20187b2a8c32019-03-26 19:52:28 -0700273 sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400274 go dh.onuDiscIndication(onuDiscInd, onuId, sn)
cuilin20187b2a8c32019-03-26 19:52:28 -0700275 case *oop.Indication_OnuInd:
276 onuInd := indication.GetOnuInd()
277 log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
278 go dh.onuIndication(onuInd)
279 case *oop.Indication_OmciInd:
280 omciInd := indication.GetOmciInd()
281 log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd})
282 if err := dh.omciIndication(omciInd); err != nil {
283 log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd})
284 }
285 case *oop.Indication_PktInd:
286 pktInd := indication.GetPktInd()
287 log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400288 go dh.handlePacketIndication(pktInd)
cuilin20187b2a8c32019-03-26 19:52:28 -0700289 case *oop.Indication_PortStats:
290 portStats := indication.GetPortStats()
291 log.Infow("Received port stats indication", log.Fields{"PortStats": portStats})
292 case *oop.Indication_FlowStats:
293 flowStats := indication.GetFlowStats()
294 log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats})
295 case *oop.Indication_AlarmInd:
296 alarmInd := indication.GetAlarmInd()
297 log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd})
298 }
299 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530300}
301
302// doStateUp handle the olt up indication and update to voltha core
303func (dh *DeviceHandler) doStateUp() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400304 // Synchronous call to update device state - this method is run in its own go routine
cuilin20187b2a8c32019-03-26 19:52:28 -0700305 if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400306 voltha.OperStatus_ACTIVE); err != nil {
307 log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceId": dh.device.Id, "error": err})
308 return err
309 }
310 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530311}
312
313// doStateDown handle the olt down indication
314func (dh *DeviceHandler) doStateDown() error {
Girish Gowdrud4245152019-05-10 00:47:31 -0400315 log.Debug("do-state-down-start")
316
317 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
318 if err != nil || device == nil {
319 /*TODO: needs to handle error scenarios */
320 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
321 }
322
323 cloned := proto.Clone(device).(*voltha.Device)
324 // Update the all ports state on that device to disable
325 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
326 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
327 return err
328 }
329
330 //Update the device oper state and connection status
331 cloned.OperStatus = voltha.OperStatus_UNKNOWN
332 cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
333 dh.device = cloned
334
335 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
336 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
337 return err
338 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400339
340 //get the child device for the parent device
341 onuDevices, err := dh.coreProxy.GetChildDevices(nil, dh.device.Id)
342 if err != nil {
343 log.Errorw("failed to get child devices information", log.Fields{"deviceId": dh.device.Id, "error": err})
344 return err
345 }
346 for _, onuDevice := range onuDevices.Items {
347
348 // Update onu state as down in onu adapter
349 onuInd := oop.OnuIndication{}
350 onuInd.OperState = "down"
351 dh.AdapterProxy.SendInterAdapterMessage(nil, &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
352
353 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400354 log.Debugw("do-state-down-end", log.Fields{"deviceId": device.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700355 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530356}
357
358// doStateInit dial the grpc before going to init state
359func (dh *DeviceHandler) doStateInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400360 var err error
Girish Gowdrud4245152019-05-10 00:47:31 -0400361 dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock())
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 if err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700363 log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceId, "HostAndPort": dh.device.GetHostAndPort(), "err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400364 return err
365 }
366 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530367}
368
369// postInit create olt client instance to invoke RPC on the olt device
370func (dh *DeviceHandler) postInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400371 dh.Client = oop.NewOpenoltClient(dh.clientCon)
372 dh.transitionMap.Handle(GrpcConnected)
373 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530374}
375
376// doStateConnected get the device info and update to voltha core
377func (dh *DeviceHandler) doStateConnected() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400378 log.Debug("OLT device has been connected")
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400379
380 // Case where OLT is disabled and then rebooted.
381 if dh.adminState == "down" {
382 log.Debugln("do-state-connected--device-admin-state-down")
383 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
384 if err != nil || device == nil {
385 /*TODO: needs to handle error scenarios */
386 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
387 }
388
389 cloned := proto.Clone(device).(*voltha.Device)
390 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
391 cloned.OperStatus = voltha.OperStatus_UNKNOWN
392 dh.device = cloned
393 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
394 log.Errorw("error-updating-device-state", log.Fields{"deviceId": dh.device.Id, "error": err})
395 }
396
397 // Since the device was disabled before the OLT was rebooted, enfore the OLT to be Disabled after re-connection.
398 _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
399 if err != nil {
400 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
401 }
402
403 // Start reading indications
404 go dh.readIndications()
405 return nil
406 }
407
Girish Gowdru0c588b22019-04-23 23:24:56 -0400408 deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700409 if err != nil {
410 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
411 return err
412 }
413 if deviceInfo == nil {
414 log.Errorw("Device info is nil", log.Fields{})
415 return errors.New("Failed to get device info from OLT")
416 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400417 log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo})
cuilin20187b2a8c32019-03-26 19:52:28 -0700418 dh.device.Root = true
419 dh.device.Vendor = deviceInfo.Vendor
420 dh.device.Model = deviceInfo.Model
cuilin20187b2a8c32019-03-26 19:52:28 -0700421 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
422 dh.device.HardwareVersion = deviceInfo.HardwareVersion
423 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400424 // FIXME: Remove Hardcodings
cuilin20187b2a8c32019-03-26 19:52:28 -0700425 dh.device.MacAddress = "0a:0b:0c:0d:0e:0f"
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530426
cuilin20187b2a8c32019-03-26 19:52:28 -0700427 // Synchronous call to update device - this method is run in its own go routine
428 if err := dh.coreProxy.DeviceUpdate(nil, dh.device); err != nil {
429 log.Errorw("error-updating-device", log.Fields{"deviceId": dh.device.Id, "error": err})
430 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400431
432 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
433 if err != nil || device == nil {
434 /*TODO: needs to handle error scenarios */
435 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
436 }
437 cloned := proto.Clone(device).(*voltha.Device)
438 // Update the all ports (if available) on that device to ACTIVE.
439 // The ports do not normally exist, unless the device is coming back from a reboot
440 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
441 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
442 return err
443 }
444
Girish Gowdru0c588b22019-04-23 23:24:56 -0400445 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
446 // Instantiate resource manager
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400447 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceId, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400448 log.Error("Error while instantiating resource manager")
449 return errors.New("Instantiating resource manager failed")
450 }
451 // Instantiate flow manager
452 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
453 log.Error("Error while instantiating flow manager")
454 return errors.New("Instantiating flow manager failed")
455 }
456 /* TODO: Instantiate Alarm , stats , BW managers */
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530457
cuilin20187b2a8c32019-03-26 19:52:28 -0700458 // Start reading indications
459 go dh.readIndications()
460 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530461}
462
463// AdoptDevice adopts the OLT device
464func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400465 dh.transitionMap = NewTransitionMap(dh)
cuilin20187b2a8c32019-03-26 19:52:28 -0700466 log.Infow("AdoptDevice", log.Fields{"deviceId": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400467 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530468}
469
470// GetOfpDeviceInfo Get the Ofp device information
471func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700472 return &ic.SwitchCapability{
473 Desc: &of.OfpDesc{
Devmalya Paul70dd4972019-06-10 15:19:17 +0530474 MfrDesc: "VOLTHA Project",
cuilin20187b2a8c32019-03-26 19:52:28 -0700475 HwDesc: "open_pon",
476 SwDesc: "open_pon",
477 SerialNum: dh.device.SerialNumber,
478 },
479 SwitchFeatures: &of.OfpSwitchFeatures{
480 NBuffers: 256,
481 NTables: 2,
482 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
483 of.OfpCapabilities_OFPC_TABLE_STATS |
484 of.OfpCapabilities_OFPC_PORT_STATS |
485 of.OfpCapabilities_OFPC_GROUP_STATS),
486 },
487 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530488}
489
490// GetOfpPortInfo Get Ofp port information
491func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700492 cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
493 return &ic.PortCapability{
494 Port: &voltha.LogicalPort{
495 OfpPort: &of.OfpPort{
496 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
497 Config: 0,
498 State: uint32(of.OfpPortState_OFPPS_LIVE),
499 Curr: cap,
500 Advertised: cap,
501 Peer: cap,
502 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
503 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
504 },
505 DeviceId: dh.device.Id,
506 DevicePortNo: uint32(portNo),
507 },
508 }, nil
509}
510
511func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
512 log.Debugw("omci indication", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700513 var deviceType string
514 var deviceId string
515 var proxyDeviceId string
cuilin20187b2a8c32019-03-26 19:52:28 -0700516
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700517 onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId)
518 if onuInCache, ok := dh.onus[onuKey]; !ok {
519 log.Debugw("omci indication for a device not in cache.", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
520 ponPort := IntfIdToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
521 kwargs := make(map[string]interface{})
522 kwargs["onu_id"] = omciInd.OnuId
523 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700524
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700525 if onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); err != nil {
526 log.Errorw("onu not found", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
527 return err
528 } else {
529 deviceType = onuDevice.Type
530 deviceId = onuDevice.Id
531 proxyDeviceId = onuDevice.ProxyAddress.DeviceId
532 //if not exist in cache, then add to cache.
533 dh.onus[onuKey] = NewOnuDevice(deviceId, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceId)
cuilin20187b2a8c32019-03-26 19:52:28 -0700534 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700535 } else {
536 //found in cache
537 log.Debugw("omci indication for a device in cache.", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
538 deviceType = onuInCache.deviceType
539 deviceId = onuInCache.deviceId
540 proxyDeviceId = onuInCache.proxyDeviceId
cuilin20187b2a8c32019-03-26 19:52:28 -0700541 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700542
543 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
544 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
545 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType,
546 deviceId, proxyDeviceId, ""); sendErr != nil {
547 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuId": deviceId, "proxyDeviceId": proxyDeviceId})
548 return sendErr
549 }
550 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530551}
552
553// Process_inter_adapter_message process inter adater message
554func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700555 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
556 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
557 msgId := msg.Header.Id
558 fromTopic := msg.Header.FromTopic
559 toTopic := msg.Header.ToTopic
560 toDeviceId := msg.Header.ToDeviceId
561 proxyDeviceId := msg.Header.ProxyDeviceId
562
563 log.Debugw("omci request message header", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
564
565 msgBody := msg.GetBody()
566
567 omciMsg := &ic.InterAdapterOmciMessage{}
568 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
569 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
570 return err
571 }
572
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700573 if omciMsg.GetProxyAddress() == nil {
574 if onuDevice, err := dh.coreProxy.GetDevice(nil, dh.device.Id, toDeviceId); err != nil {
575 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceId, "error": err})
576 return err
577 } else {
578 log.Debugw("device retrieved from core", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
579 dh.sendProxiedMessage(onuDevice, omciMsg)
580 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700581 } else {
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700582 log.Debugw("Proxy Address found in omci message", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
583 dh.sendProxiedMessage(nil, omciMsg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700584 }
585
586 } else {
587 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
588 }
589 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530590}
591
cuilin20187b2a8c32019-03-26 19:52:28 -0700592func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700593 var intfId uint32
594 var onuId uint32
595 var status common.ConnectStatus_ConnectStatus
596 if onuDevice != nil {
597 intfId = onuDevice.ProxyAddress.GetChannelId()
598 onuId = onuDevice.ProxyAddress.GetOnuId()
599 status = onuDevice.ConnectStatus
600 } else {
601 intfId = omciMsg.GetProxyAddress().GetChannelId()
602 onuId = omciMsg.GetProxyAddress().GetOnuId()
603 status = omciMsg.GetConnectStatus()
604 }
605 if status != voltha.ConnectStatus_REACHABLE {
606 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfId": intfId, "onuId": onuId})
cuilin20187b2a8c32019-03-26 19:52:28 -0700607 return
608 }
609
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700610 omciMessage := &oop.OmciMsg{IntfId: intfId, OnuId: onuId, Pkt: omciMsg.Message}
cuilin20187b2a8c32019-03-26 19:52:28 -0700611
manikkaraj kbf256be2019-03-25 00:13:48 +0530612 dh.Client.OmciMsgOut(context.Background(), omciMessage)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700613 log.Debugw("omci-message-sent", log.Fields{"intfId": intfId, "onuId": onuId, "omciMsg": string(omciMsg.GetMessage())})
cuilin20187b2a8c32019-03-26 19:52:28 -0700614}
615
616func (dh *DeviceHandler) activateONU(intfId uint32, onuId int64, serialNum *oop.SerialNumber, serialNumber string) {
617 log.Debugw("activate-onu", log.Fields{"intfId": intfId, "onuId": onuId, "serialNum": serialNum, "serialNumber": serialNumber})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400618 dh.flowMgr.UpdateOnuInfo(intfId, uint32(onuId), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700619 // TODO: need resource manager
620 var pir uint32 = 1000000
621 Onu := oop.Onu{IntfId: intfId, OnuId: uint32(onuId), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530622 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400623 st, _ := status.FromError(err)
624 if st.Code() == codes.AlreadyExists {
625 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
626 } else {
627 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
628 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700629 } else {
630 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
631 }
632}
633
634func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, onuId uint32, sn string) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400635 channelId := onuDiscInd.GetIntfId()
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400636 parentPortNo := IntfIdToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400637 if _, ok := dh.discOnus[sn]; ok {
638 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
639 return nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700640 }
641
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400642 dh.lockDevice.Lock()
643 dh.discOnus[sn] = true
644 dh.lockDevice.Unlock()
645 // evict the onu serial number from local cache
646 defer func() {
647 delete(dh.discOnus, sn)
648 }()
649
cuilin20187b2a8c32019-03-26 19:52:28 -0700650 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400651 if sn != "" {
652 kwargs["serial_number"] = sn
653 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700654 kwargs["onu_id"] = onuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400655 kwargs["parent_port_no"] = parentPortNo
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400656 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
657 if onuDevice == nil {
658 if err := dh.coreProxy.ChildDeviceDetected(nil, dh.device.Id, int(parentPortNo), "brcm_openomci_onu", int(channelId), string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuId)); err != nil {
659 log.Errorw("Create onu error", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn, "error": err})
660 return err
661 }
662 }
663 onuDevice, err = dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
664 if err != nil {
665 log.Errorw("failed to get ONU device information", log.Fields{"err": err})
666 return err
667 }
668 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
669 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700670
671 for i := 0; i < 10; i++ {
672 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
673 dh.activateONU(onuDiscInd.IntfId, int64(onuId), onuDiscInd.SerialNumber, sn)
674 return nil
675 } else {
676 time.Sleep(1 * time.Second)
677 log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1)
678 }
679 }
680 log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn})
681 return errors.New("Failed to activate onu")
682}
683
684func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
685 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
686
687 kwargs := make(map[string]interface{})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400688 ponPort := IntfIdToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
manikkaraj kbf256be2019-03-25 00:13:48 +0530689
cuilin20187b2a8c32019-03-26 19:52:28 -0700690 if serialNumber != "" {
691 kwargs["serial_number"] = serialNumber
692 } else {
693 kwargs["onu_id"] = onuInd.OnuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400694 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700695 }
696 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400697 if onuDevice.ParentPortNo != ponPort {
cuilin20187b2a8c32019-03-26 19:52:28 -0700698 //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 -0400699 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700700 }
701
702 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
703 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})
704 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700705 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId())
706 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
cuilin20187b2a8c32019-03-26 19:52:28 -0700707
708 // adminState
709 if onuInd.AdminState == "down" {
710 if onuInd.OperState != "down" {
711 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
712 // Forcing the oper state change code to execute
713 onuInd.OperState = "down"
714 }
715 // Port and logical port update is taken care of by oper state block
716 } else if onuInd.AdminState == "up" {
717 log.Debugln("received-onu-admin-state up")
718 } else {
719 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
720 }
721 log.Debugln("admin-state-dealt-with")
722
723 // operState
724 if onuInd.OperState == "down" {
725 if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE {
726 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, onuDevice.OperStatus)
727 log.Debugln("onu-oper-state-is-down")
728 }
729 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
730 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, common.OperStatus_DISCOVERED)
731 }
732 log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd})
733
734 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
manikkaraj kbf256be2019-03-25 00:13:48 +0530735 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700736 } else if onuInd.OperState == "up" {
737 if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE {
738 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus)
739
740 }
741 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
742 log.Warnw("ignore onu indication", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState})
743 return
744 }
manikkaraj kbf256be2019-03-25 00:13:48 +0530745 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700746 } else {
747 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
748 }
749 } else {
750 log.Errorw("onu not found", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId})
751 return
752 }
753
754}
755
756func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
757 if serialNum != nil {
758 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
759 } else {
760 return ""
761 }
762}
763
764func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
765 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
766 fmt.Sprintf("%x", (uint32(vendorSpecific[0]&0x0f))) +
767 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
768 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
769 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
770 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
771 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
772 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
773 return tmp
774}
775
776// flows
777func (dh *DeviceHandler) Update_flows_bulk() error {
778 return errors.New("UnImplemented")
779}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400780func (dh *DeviceHandler) GetChildDevice(parentPort uint32, onuId uint32) *voltha.Device {
781 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuId": onuId})
782 kwargs := make(map[string]interface{})
783 kwargs["onu_id"] = onuId
784 kwargs["parent_port_no"] = parentPort
785 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
786 if err != nil {
787 log.Errorw("onu not found", log.Fields{"intfId": parentPort, "onuId": onuId})
788 return nil
789 }
790 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
791 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530792}
793
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400794func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
795 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
796 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPort, packetPayload); err != nil {
797 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
798 return
799 }
800 log.Debug("Sent packet-in to core successfully")
801}
802
manikkaraj kbf256be2019-03-25 00:13:48 +0530803func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400804 log.Debugw("In UpdateFlowsIncrementally", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups})
805 if flows != nil {
806 for _, flow := range flows.ToAdd.Items {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400807 log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400808 dh.flowMgr.AddFlow(flow)
809 }
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400810 for _, flow := range flows.ToRemove.Items {
811 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
812 dh.flowMgr.RemoveFlow(flow)
813 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400814 }
815 if groups != nil {
816 for _, flow := range flows.ToRemove.Items {
817 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
818 // dh.flowMgr.RemoveFlow(flow)
819 }
820 }
821 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530822}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400823
824func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
825 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
826 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
827 return err
828 }
829 dh.lockDevice.Lock()
830 dh.adminState = "down"
831 dh.lockDevice.Unlock()
832 log.Debug("olt-disabled")
833
834 cloned := proto.Clone(device).(*voltha.Device)
835 // Update the all ports state on that device to disable
836 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
837 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
838 return err
839 }
840
841 //Update the device oper state
842 cloned.OperStatus = voltha.OperStatus_UNKNOWN
843 dh.device = cloned
844
845 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
846 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
847 return err
848 }
849 log.Debugw("DisableDevice-end", log.Fields{"deviceId": device.Id})
850 return nil
851}
852
853func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
854 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
855 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
856 return err
857 }
858
859 dh.lockDevice.Lock()
860 dh.adminState = "up"
861 dh.lockDevice.Unlock()
862 log.Debug("olt-reenabled")
863
864 cloned := proto.Clone(device).(*voltha.Device)
865 // Update the all ports state on that device to enable
866 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
867 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
868 return err
869 }
870
871 //Update the device oper status as ACTIVE
872 cloned.OperStatus = voltha.OperStatus_ACTIVE
873 dh.device = cloned
874
875 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
876 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
877 return err
878 }
879 log.Debugw("ReEnableDevice-end", log.Fields{"deviceId": device.Id})
880
881 return nil
882}
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400883
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400884func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
885 if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
886 log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
887 return err
888 }
889
890 log.Debugw("rebooted-device-successfully", log.Fields{"deviceId": device.Id})
891
892 return nil
893}
894
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400895func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
896 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
897 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
898 if err != nil {
899 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
900 return
901 }
902 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
903 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
904 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
905 return
906 }
907 log.Debug("Success sending packet-in to core!")
908}
909
910func (dh *DeviceHandler) PacketOut(egress_port_no int, packet *of.OfpPacketOut) error {
911 log.Debugw("PacketOut", log.Fields{"deviceId": dh.deviceId, "egress_port_no": egress_port_no, "pkt-length": len(packet.Data)})
912 var etherFrame ethernet.Frame
913 err := (&etherFrame).UnmarshalBinary(packet.Data)
914 if err != nil {
915 log.Errorw("Failed to unmarshal into ethernet frame", log.Fields{"err": err, "pkt-length": len(packet.Data)})
916 return err
917 }
918 log.Debugw("Ethernet Frame", log.Fields{"Frame": etherFrame})
919 egressPortType := IntfIdToPortTypeName(uint32(egress_port_no))
920 if egressPortType == voltha.Port_ETHERNET_UNI {
921 if etherFrame.VLAN != nil { // If double tag, remove the outer tag
922 nextEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
923 if nextEthType == 0x8100 {
924 etherFrame.VLAN = nil
925 packet.Data, err = etherFrame.MarshalBinary()
926 if err != nil {
927 log.Fatalf("failed to marshal frame: %v", err)
928 return err
929 }
930 if err := (&etherFrame).UnmarshalBinary(packet.Data); err != nil {
931 log.Fatalf("failed to unmarshal frame: %v", err)
932 return err
933 }
934 log.Debug("Double tagged packet , removed outer vlan", log.Fields{"New frame": etherFrame})
935 }
936 }
937 intfId := IntfIdFromUniPortNum(uint32(egress_port_no))
938 onuId := OnuIdFromPortNum(uint32(egress_port_no))
939 uniId := UniIdFromPortNum(uint32(egress_port_no))
940 /*gemPortId, err := dh.flowMgr.GetPacketOutGemPortId(intfId, onuId, uint32(egress_port_no))
941 if err != nil{
942 log.Errorw("Error while getting gemport to packet-out",log.Fields{"error": err})
943 return err
944 }*/
945 onuPkt := oop.OnuPacket{IntfId: intfId, OnuId: onuId, PortNo: uint32(egress_port_no), Pkt: packet.Data}
946 log.Debug("sending-packet-to-ONU", log.Fields{"egress_port_no": egress_port_no, "IntfId": intfId, "onuId": onuId,
947 "uniId": uniId, "packet": packet.Data})
948 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
949 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
950 return err
951 }
952 } else if egressPortType == voltha.Port_ETHERNET_NNI {
953 uplinkPkt := oop.UplinkPacket{IntfId: IntfIdFromNniPortNum(uint32(egress_port_no)), Pkt: packet.Data}
954 log.Debug("sending-packet-to-uplink", log.Fields{"uplink_pkt": uplinkPkt})
955 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
956 log.Errorw("Error while sending packet-out to uplink", log.Fields{"error": err})
957 return err
958 }
959 } else {
960 log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egress_port_no, "egressPortType": egressPortType})
961 }
962 return nil
963}
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700964
965func (dh *DeviceHandler) formOnuKey(intfId uint32, onuId uint32) string {
966 return ("" + strconv.Itoa(int(intfId)) + "." + strconv.Itoa(int(onuId)))
967
968}