blob: 2bbfe922fd85a6fe3a80a0f171e9f0053eb69f6a [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
54 nniPort *voltha.Port
55 ponPort *voltha.Port
56 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
65}
66
67type OnuDevice struct {
68 deviceId string
69 deviceType string
70 serialNumber string
71 onuId uint32
72 intfId uint32
73 proxyDeviceId string
74}
75
76//NewOnuDevice creates a new Onu Device
77func NewOnuDevice(devId string, deviceTp string, serialNum string, onuId uint32, intfId uint32, proxyDevId string) *OnuDevice {
78 var device OnuDevice
79 device.deviceId = devId
80 device.deviceType = deviceTp
81 device.serialNumber = serialNum
82 device.onuId = onuId
83 device.intfId = intfId
84 device.proxyDeviceId = proxyDevId
85 return &device
Phaneendra Manda4c62c802019-03-06 21:37:49 +053086}
87
88//NewDeviceHandler creates a new device handler
cuilin20187b2a8c32019-03-26 19:52:28 -070089func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
90 var dh DeviceHandler
91 dh.coreProxy = cp
Girish Gowdru0c588b22019-04-23 23:24:56 -040092 dh.AdapterProxy = ap
cuilin20187b2a8c32019-03-26 19:52:28 -070093 cloned := (proto.Clone(device)).(*voltha.Device)
94 dh.deviceId = cloned.Id
95 dh.deviceType = cloned.Type
Girish Gowdru5ba46c92019-04-25 05:00:05 -040096 dh.adminState = "up"
cuilin20187b2a8c32019-03-26 19:52:28 -070097 dh.device = cloned
98 dh.openOLT = adapter
99 dh.exitChannel = make(chan int, 1)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400100 dh.discOnus = make(map[string]bool)
cuilin20187b2a8c32019-03-26 19:52:28 -0700101 dh.lockDevice = sync.RWMutex{}
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700102 dh.onus = make(map[string]*OnuDevice)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530103
cuilin20187b2a8c32019-03-26 19:52:28 -0700104 //TODO initialize the support classes.
105 return &dh
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530106}
107
108// start save the device to the data model
109func (dh *DeviceHandler) start(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700110 dh.lockDevice.Lock()
111 defer dh.lockDevice.Unlock()
112 log.Debugw("starting-device-agent", log.Fields{"device": dh.device})
113 // Add the initial device to the local model
114 log.Debug("device-agent-started")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530115}
116
117// stop stops the device dh. Not much to do for now
118func (dh *DeviceHandler) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700119 dh.lockDevice.Lock()
120 defer dh.lockDevice.Unlock()
121 log.Debug("stopping-device-agent")
122 dh.exitChannel <- 1
123 log.Debug("device-agent-stopped")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530124}
125
126func macAddressToUint32Array(mac string) []uint32 {
cuilin20187b2a8c32019-03-26 19:52:28 -0700127 slist := strings.Split(mac, ":")
128 result := make([]uint32, len(slist))
129 var err error
130 var tmp int64
131 for index, val := range slist {
132 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
133 return []uint32{1, 2, 3, 4, 5, 6}
134 }
135 result[index] = uint32(tmp)
136 }
137 return result
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530138}
139
manikkaraj kbf256be2019-03-25 00:13:48 +0530140func GetportLabel(portNum uint32, portType voltha.Port_PortType) string {
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530141
Girish Gowdru0c588b22019-04-23 23:24:56 -0400142 if portType == voltha.Port_ETHERNET_NNI {
143 return fmt.Sprintf("nni-%d", portNum)
144 } else if portType == voltha.Port_PON_OLT {
145 return fmt.Sprintf("pon-%d", portNum)
cuilin20187b2a8c32019-03-26 19:52:28 -0700146 } else if portType == voltha.Port_ETHERNET_UNI {
147 log.Errorw("local UNI management not supported", log.Fields{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700149 }
150 return ""
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530151}
152
153func (dh *DeviceHandler) addPort(intfId uint32, portType voltha.Port_PortType, state string) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700154 var operStatus common.OperStatus_OperStatus
155 if state == "up" {
156 operStatus = voltha.OperStatus_ACTIVE
157 } else {
158 operStatus = voltha.OperStatus_DISCOVERED
159 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400160 portNum := IntfIdToPortNo(intfId, portType)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400161 label := GetportLabel(portNum, portType)
162 if len(label) == 0 {
163 log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType})
164 return
165 }
166 // Now create Port
167 port := &voltha.Port{
cuilin20187b2a8c32019-03-26 19:52:28 -0700168 PortNo: portNum,
169 Label: label,
170 Type: portType,
171 OperStatus: operStatus,
172 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400173 log.Debugw("Sending port update to core", log.Fields{"port": port})
cuilin20187b2a8c32019-03-26 19:52:28 -0700174 // Synchronous call to update device - this method is run in its own go routine
Girish Gowdru0c588b22019-04-23 23:24:56 -0400175 if err := dh.coreProxy.PortCreated(nil, dh.device.Id, port); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700176 log.Errorw("error-creating-nni-port", log.Fields{"deviceId": dh.device.Id, "error": err})
177 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530178}
179
180// readIndications to read the indications from the OLT device
181func (dh *DeviceHandler) readIndications() {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400182 indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700183 if err != nil {
184 log.Errorw("Failed to read indications", log.Fields{"err": err})
185 return
186 }
187 if indications == nil {
188 log.Errorw("Indications is nil", log.Fields{})
189 return
190 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400191 /* get device state */
192 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
193 if err != nil || device == nil {
194 /*TODO: needs to handle error scenarios */
195 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
196
197 }
198 // When the device is in DISABLED and Adapter container restarts, we need to
199 // rebuild the locally maintained admin state.
200 if device.AdminState == voltha.AdminState_DISABLED {
201 dh.lockDevice.Lock()
202 dh.adminState = "down"
203 dh.lockDevice.Unlock()
204 }
205
cuilin20187b2a8c32019-03-26 19:52:28 -0700206 for {
207 indication, err := indications.Recv()
208 if err == io.EOF {
209 break
210 }
211 if err != nil {
212 log.Infow("Failed to read from indications", log.Fields{"err": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400213 dh.transitionMap.Handle(DeviceDownInd)
214 dh.transitionMap.Handle(DeviceInit)
215 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700216 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400217 // When OLT is admin down, allow only NNI operation status change indications.
218 if dh.adminState == "down" {
219 _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd)
220 if isIntfOperInd {
221 intfOperInd := indication.GetIntfOperInd()
222 if intfOperInd.GetType() == "nni" {
223 log.Infow("olt is admin down, allow nni ind", log.Fields{})
224 }
225 } else {
226 log.Infow("olt is admin down, ignore indication", log.Fields{})
227 continue
228 }
229 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700230 switch indication.Data.(type) {
231 case *oop.Indication_OltInd:
232 oltInd := indication.GetOltInd()
233 if oltInd.OperState == "up" {
234 dh.transitionMap.Handle(DeviceUpInd)
235 } else if oltInd.OperState == "down" {
236 dh.transitionMap.Handle(DeviceDownInd)
237 }
238 case *oop.Indication_IntfInd:
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530239
cuilin20187b2a8c32019-03-26 19:52:28 -0700240 intfInd := indication.GetIntfInd()
241 go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
242 log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd})
243 case *oop.Indication_IntfOperInd:
244 intfOperInd := indication.GetIntfOperInd()
245 if intfOperInd.GetType() == "nni" {
246 go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
247 } else if intfOperInd.GetType() == "pon" {
248 // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
249 // Handle pon port update
250 }
251 log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
252 case *oop.Indication_OnuDiscInd:
253 onuDiscInd := indication.GetOnuDiscInd()
254 log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400255 //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
256 //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
257 // TODO Get onu ID from the resource manager
cuilin20187b2a8c32019-03-26 19:52:28 -0700258 var onuId uint32 = 1
Girish Gowdru0c588b22019-04-23 23:24:56 -0400259 /*if err != nil{
260 log.Errorw("onu-id-unavailable",log.Fields{"intfId":onuDiscInd.GetIntfId()})
261 return
262 }*/
manikkaraj kbf256be2019-03-25 00:13:48 +0530263
cuilin20187b2a8c32019-03-26 19:52:28 -0700264 sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400265 go dh.onuDiscIndication(onuDiscInd, onuId, sn)
cuilin20187b2a8c32019-03-26 19:52:28 -0700266 case *oop.Indication_OnuInd:
267 onuInd := indication.GetOnuInd()
268 log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
269 go dh.onuIndication(onuInd)
270 case *oop.Indication_OmciInd:
271 omciInd := indication.GetOmciInd()
272 log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd})
273 if err := dh.omciIndication(omciInd); err != nil {
274 log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd})
275 }
276 case *oop.Indication_PktInd:
277 pktInd := indication.GetPktInd()
278 log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400279 go dh.handlePacketIndication(pktInd)
cuilin20187b2a8c32019-03-26 19:52:28 -0700280 case *oop.Indication_PortStats:
281 portStats := indication.GetPortStats()
282 log.Infow("Received port stats indication", log.Fields{"PortStats": portStats})
283 case *oop.Indication_FlowStats:
284 flowStats := indication.GetFlowStats()
285 log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats})
286 case *oop.Indication_AlarmInd:
287 alarmInd := indication.GetAlarmInd()
288 log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd})
289 }
290 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530291}
292
293// doStateUp handle the olt up indication and update to voltha core
294func (dh *DeviceHandler) doStateUp() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400295 // Synchronous call to update device state - this method is run in its own go routine
cuilin20187b2a8c32019-03-26 19:52:28 -0700296 if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400297 voltha.OperStatus_ACTIVE); err != nil {
298 log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceId": dh.device.Id, "error": err})
299 return err
300 }
301 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530302}
303
304// doStateDown handle the olt down indication
305func (dh *DeviceHandler) doStateDown() error {
Girish Gowdrud4245152019-05-10 00:47:31 -0400306 log.Debug("do-state-down-start")
307
308 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
309 if err != nil || device == nil {
310 /*TODO: needs to handle error scenarios */
311 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
312 }
313
314 cloned := proto.Clone(device).(*voltha.Device)
315 // Update the all ports state on that device to disable
316 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
317 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
318 return err
319 }
320
321 //Update the device oper state and connection status
322 cloned.OperStatus = voltha.OperStatus_UNKNOWN
323 cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
324 dh.device = cloned
325
326 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
327 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
328 return err
329 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400330
331 //get the child device for the parent device
332 onuDevices, err := dh.coreProxy.GetChildDevices(nil, dh.device.Id)
333 if err != nil {
334 log.Errorw("failed to get child devices information", log.Fields{"deviceId": dh.device.Id, "error": err})
335 return err
336 }
337 for _, onuDevice := range onuDevices.Items {
338
339 // Update onu state as down in onu adapter
340 onuInd := oop.OnuIndication{}
341 onuInd.OperState = "down"
342 dh.AdapterProxy.SendInterAdapterMessage(nil, &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
343
344 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400345 log.Debugw("do-state-down-end", log.Fields{"deviceId": device.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700346 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530347}
348
349// doStateInit dial the grpc before going to init state
350func (dh *DeviceHandler) doStateInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400351 var err error
Girish Gowdrud4245152019-05-10 00:47:31 -0400352 dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock())
Girish Gowdru0c588b22019-04-23 23:24:56 -0400353 if err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700354 log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceId, "HostAndPort": dh.device.GetHostAndPort(), "err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400355 return err
356 }
357 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530358}
359
360// postInit create olt client instance to invoke RPC on the olt device
361func (dh *DeviceHandler) postInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 dh.Client = oop.NewOpenoltClient(dh.clientCon)
363 dh.transitionMap.Handle(GrpcConnected)
364 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530365}
366
367// doStateConnected get the device info and update to voltha core
368func (dh *DeviceHandler) doStateConnected() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400369 log.Debug("OLT device has been connected")
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400370
371 // Case where OLT is disabled and then rebooted.
372 if dh.adminState == "down" {
373 log.Debugln("do-state-connected--device-admin-state-down")
374 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
375 if err != nil || device == nil {
376 /*TODO: needs to handle error scenarios */
377 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
378 }
379
380 cloned := proto.Clone(device).(*voltha.Device)
381 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
382 cloned.OperStatus = voltha.OperStatus_UNKNOWN
383 dh.device = cloned
384 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
385 log.Errorw("error-updating-device-state", log.Fields{"deviceId": dh.device.Id, "error": err})
386 }
387
388 // Since the device was disabled before the OLT was rebooted, enfore the OLT to be Disabled after re-connection.
389 _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
390 if err != nil {
391 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
392 }
393
394 // Start reading indications
395 go dh.readIndications()
396 return nil
397 }
398
Girish Gowdru0c588b22019-04-23 23:24:56 -0400399 deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700400 if err != nil {
401 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
402 return err
403 }
404 if deviceInfo == nil {
405 log.Errorw("Device info is nil", log.Fields{})
406 return errors.New("Failed to get device info from OLT")
407 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400408 log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo})
cuilin20187b2a8c32019-03-26 19:52:28 -0700409 dh.device.Root = true
410 dh.device.Vendor = deviceInfo.Vendor
411 dh.device.Model = deviceInfo.Model
cuilin20187b2a8c32019-03-26 19:52:28 -0700412 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
413 dh.device.HardwareVersion = deviceInfo.HardwareVersion
414 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400415 // FIXME: Remove Hardcodings
cuilin20187b2a8c32019-03-26 19:52:28 -0700416 dh.device.MacAddress = "0a:0b:0c:0d:0e:0f"
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530417
cuilin20187b2a8c32019-03-26 19:52:28 -0700418 // Synchronous call to update device - this method is run in its own go routine
419 if err := dh.coreProxy.DeviceUpdate(nil, dh.device); err != nil {
420 log.Errorw("error-updating-device", log.Fields{"deviceId": dh.device.Id, "error": err})
421 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400422
423 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
424 if err != nil || device == nil {
425 /*TODO: needs to handle error scenarios */
426 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
427 }
428 cloned := proto.Clone(device).(*voltha.Device)
429 // Update the all ports (if available) on that device to ACTIVE.
430 // The ports do not normally exist, unless the device is coming back from a reboot
431 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
432 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
433 return err
434 }
435
Girish Gowdru0c588b22019-04-23 23:24:56 -0400436 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
437 // Instantiate resource manager
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400438 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceId, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400439 log.Error("Error while instantiating resource manager")
440 return errors.New("Instantiating resource manager failed")
441 }
442 // Instantiate flow manager
443 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
444 log.Error("Error while instantiating flow manager")
445 return errors.New("Instantiating flow manager failed")
446 }
447 /* TODO: Instantiate Alarm , stats , BW managers */
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530448
cuilin20187b2a8c32019-03-26 19:52:28 -0700449 // Start reading indications
450 go dh.readIndications()
451 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530452}
453
454// AdoptDevice adopts the OLT device
455func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400456 dh.transitionMap = NewTransitionMap(dh)
cuilin20187b2a8c32019-03-26 19:52:28 -0700457 log.Infow("AdoptDevice", log.Fields{"deviceId": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400458 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530459}
460
461// GetOfpDeviceInfo Get the Ofp device information
462func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700463 return &ic.SwitchCapability{
464 Desc: &of.OfpDesc{
465 HwDesc: "open_pon",
466 SwDesc: "open_pon",
467 SerialNum: dh.device.SerialNumber,
468 },
469 SwitchFeatures: &of.OfpSwitchFeatures{
470 NBuffers: 256,
471 NTables: 2,
472 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
473 of.OfpCapabilities_OFPC_TABLE_STATS |
474 of.OfpCapabilities_OFPC_PORT_STATS |
475 of.OfpCapabilities_OFPC_GROUP_STATS),
476 },
477 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530478}
479
480// GetOfpPortInfo Get Ofp port information
481func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700482 cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
483 return &ic.PortCapability{
484 Port: &voltha.LogicalPort{
485 OfpPort: &of.OfpPort{
486 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
487 Config: 0,
488 State: uint32(of.OfpPortState_OFPPS_LIVE),
489 Curr: cap,
490 Advertised: cap,
491 Peer: cap,
492 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
493 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
494 },
495 DeviceId: dh.device.Id,
496 DevicePortNo: uint32(portNo),
497 },
498 }, nil
499}
500
501func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
502 log.Debugw("omci indication", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700503 var deviceType string
504 var deviceId string
505 var proxyDeviceId string
cuilin20187b2a8c32019-03-26 19:52:28 -0700506
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700507 onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId)
508 if onuInCache, ok := dh.onus[onuKey]; !ok {
509 log.Debugw("omci indication for a device not in cache.", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
510 ponPort := IntfIdToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
511 kwargs := make(map[string]interface{})
512 kwargs["onu_id"] = omciInd.OnuId
513 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700514
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700515 if onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); err != nil {
516 log.Errorw("onu not found", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
517 return err
518 } else {
519 deviceType = onuDevice.Type
520 deviceId = onuDevice.Id
521 proxyDeviceId = onuDevice.ProxyAddress.DeviceId
522 //if not exist in cache, then add to cache.
523 dh.onus[onuKey] = NewOnuDevice(deviceId, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceId)
cuilin20187b2a8c32019-03-26 19:52:28 -0700524 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700525 } else {
526 //found in cache
527 log.Debugw("omci indication for a device in cache.", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
528 deviceType = onuInCache.deviceType
529 deviceId = onuInCache.deviceId
530 proxyDeviceId = onuInCache.proxyDeviceId
cuilin20187b2a8c32019-03-26 19:52:28 -0700531 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700532
533 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
534 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
535 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType,
536 deviceId, proxyDeviceId, ""); sendErr != nil {
537 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuId": deviceId, "proxyDeviceId": proxyDeviceId})
538 return sendErr
539 }
540 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530541}
542
543// Process_inter_adapter_message process inter adater message
544func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700545 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
546 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
547 msgId := msg.Header.Id
548 fromTopic := msg.Header.FromTopic
549 toTopic := msg.Header.ToTopic
550 toDeviceId := msg.Header.ToDeviceId
551 proxyDeviceId := msg.Header.ProxyDeviceId
552
553 log.Debugw("omci request message header", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
554
555 msgBody := msg.GetBody()
556
557 omciMsg := &ic.InterAdapterOmciMessage{}
558 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
559 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
560 return err
561 }
562
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700563 if omciMsg.GetProxyAddress() == nil {
564 if onuDevice, err := dh.coreProxy.GetDevice(nil, dh.device.Id, toDeviceId); err != nil {
565 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceId, "error": err})
566 return err
567 } else {
568 log.Debugw("device retrieved from core", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
569 dh.sendProxiedMessage(onuDevice, omciMsg)
570 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700571 } else {
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700572 log.Debugw("Proxy Address found in omci message", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
573 dh.sendProxiedMessage(nil, omciMsg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700574 }
575
576 } else {
577 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
578 }
579 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530580}
581
cuilin20187b2a8c32019-03-26 19:52:28 -0700582func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700583 var intfId uint32
584 var onuId uint32
585 var status common.ConnectStatus_ConnectStatus
586 if onuDevice != nil {
587 intfId = onuDevice.ProxyAddress.GetChannelId()
588 onuId = onuDevice.ProxyAddress.GetOnuId()
589 status = onuDevice.ConnectStatus
590 } else {
591 intfId = omciMsg.GetProxyAddress().GetChannelId()
592 onuId = omciMsg.GetProxyAddress().GetOnuId()
593 status = omciMsg.GetConnectStatus()
594 }
595 if status != voltha.ConnectStatus_REACHABLE {
596 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfId": intfId, "onuId": onuId})
cuilin20187b2a8c32019-03-26 19:52:28 -0700597 return
598 }
599
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700600 omciMessage := &oop.OmciMsg{IntfId: intfId, OnuId: onuId, Pkt: omciMsg.Message}
cuilin20187b2a8c32019-03-26 19:52:28 -0700601
manikkaraj kbf256be2019-03-25 00:13:48 +0530602 dh.Client.OmciMsgOut(context.Background(), omciMessage)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700603 log.Debugw("omci-message-sent", log.Fields{"intfId": intfId, "onuId": onuId, "omciMsg": string(omciMsg.GetMessage())})
cuilin20187b2a8c32019-03-26 19:52:28 -0700604}
605
606func (dh *DeviceHandler) activateONU(intfId uint32, onuId int64, serialNum *oop.SerialNumber, serialNumber string) {
607 log.Debugw("activate-onu", log.Fields{"intfId": intfId, "onuId": onuId, "serialNum": serialNum, "serialNumber": serialNumber})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400608 dh.flowMgr.UpdateOnuInfo(intfId, uint32(onuId), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700609 // TODO: need resource manager
610 var pir uint32 = 1000000
611 Onu := oop.Onu{IntfId: intfId, OnuId: uint32(onuId), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530612 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400613 st, _ := status.FromError(err)
614 if st.Code() == codes.AlreadyExists {
615 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
616 } else {
617 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
618 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700619 } else {
620 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
621 }
622}
623
624func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, onuId uint32, sn string) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400625 channelId := onuDiscInd.GetIntfId()
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400626 parentPortNo := IntfIdToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400627 if _, ok := dh.discOnus[sn]; ok {
628 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
629 return nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700630 }
631
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400632 dh.lockDevice.Lock()
633 dh.discOnus[sn] = true
634 dh.lockDevice.Unlock()
635 // evict the onu serial number from local cache
636 defer func() {
637 delete(dh.discOnus, sn)
638 }()
639
cuilin20187b2a8c32019-03-26 19:52:28 -0700640 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400641 if sn != "" {
642 kwargs["serial_number"] = sn
643 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700644 kwargs["onu_id"] = onuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400645 kwargs["parent_port_no"] = parentPortNo
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400646 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
647 if onuDevice == nil {
648 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 {
649 log.Errorw("Create onu error", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn, "error": err})
650 return err
651 }
652 }
653 onuDevice, err = dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
654 if err != nil {
655 log.Errorw("failed to get ONU device information", log.Fields{"err": err})
656 return err
657 }
658 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
659 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700660
661 for i := 0; i < 10; i++ {
662 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
663 dh.activateONU(onuDiscInd.IntfId, int64(onuId), onuDiscInd.SerialNumber, sn)
664 return nil
665 } else {
666 time.Sleep(1 * time.Second)
667 log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1)
668 }
669 }
670 log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn})
671 return errors.New("Failed to activate onu")
672}
673
674func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
675 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
676
677 kwargs := make(map[string]interface{})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400678 ponPort := IntfIdToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
manikkaraj kbf256be2019-03-25 00:13:48 +0530679
cuilin20187b2a8c32019-03-26 19:52:28 -0700680 if serialNumber != "" {
681 kwargs["serial_number"] = serialNumber
682 } else {
683 kwargs["onu_id"] = onuInd.OnuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400684 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700685 }
686 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400687 if onuDevice.ParentPortNo != ponPort {
cuilin20187b2a8c32019-03-26 19:52:28 -0700688 //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 -0400689 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700690 }
691
692 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
693 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})
694 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700695 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId())
696 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
cuilin20187b2a8c32019-03-26 19:52:28 -0700697
698 // adminState
699 if onuInd.AdminState == "down" {
700 if onuInd.OperState != "down" {
701 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
702 // Forcing the oper state change code to execute
703 onuInd.OperState = "down"
704 }
705 // Port and logical port update is taken care of by oper state block
706 } else if onuInd.AdminState == "up" {
707 log.Debugln("received-onu-admin-state up")
708 } else {
709 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
710 }
711 log.Debugln("admin-state-dealt-with")
712
713 // operState
714 if onuInd.OperState == "down" {
715 if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE {
716 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, onuDevice.OperStatus)
717 log.Debugln("onu-oper-state-is-down")
718 }
719 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
720 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, common.OperStatus_DISCOVERED)
721 }
722 log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd})
723
724 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
manikkaraj kbf256be2019-03-25 00:13:48 +0530725 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700726 } else if onuInd.OperState == "up" {
727 if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE {
728 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus)
729
730 }
731 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
732 log.Warnw("ignore onu indication", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState})
733 return
734 }
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 {
737 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
738 }
739 } else {
740 log.Errorw("onu not found", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId})
741 return
742 }
743
744}
745
746func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
747 if serialNum != nil {
748 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
749 } else {
750 return ""
751 }
752}
753
754func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
755 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
756 fmt.Sprintf("%x", (uint32(vendorSpecific[0]&0x0f))) +
757 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
758 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
759 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
760 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
761 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
762 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
763 return tmp
764}
765
766// flows
767func (dh *DeviceHandler) Update_flows_bulk() error {
768 return errors.New("UnImplemented")
769}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400770func (dh *DeviceHandler) GetChildDevice(parentPort uint32, onuId uint32) *voltha.Device {
771 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuId": onuId})
772 kwargs := make(map[string]interface{})
773 kwargs["onu_id"] = onuId
774 kwargs["parent_port_no"] = parentPort
775 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
776 if err != nil {
777 log.Errorw("onu not found", log.Fields{"intfId": parentPort, "onuId": onuId})
778 return nil
779 }
780 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
781 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530782}
783
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400784func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
785 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
786 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPort, packetPayload); err != nil {
787 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
788 return
789 }
790 log.Debug("Sent packet-in to core successfully")
791}
792
manikkaraj kbf256be2019-03-25 00:13:48 +0530793func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400794 log.Debugw("In UpdateFlowsIncrementally", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups})
795 if flows != nil {
796 for _, flow := range flows.ToAdd.Items {
797 dh.flowMgr.AddFlow(flow)
798 }
799 }
800 if groups != nil {
801 for _, flow := range flows.ToRemove.Items {
802 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
803 // dh.flowMgr.RemoveFlow(flow)
804 }
805 }
806 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530807}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400808
809func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
810 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
811 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
812 return err
813 }
814 dh.lockDevice.Lock()
815 dh.adminState = "down"
816 dh.lockDevice.Unlock()
817 log.Debug("olt-disabled")
818
819 cloned := proto.Clone(device).(*voltha.Device)
820 // Update the all ports state on that device to disable
821 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
822 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
823 return err
824 }
825
826 //Update the device oper state
827 cloned.OperStatus = voltha.OperStatus_UNKNOWN
828 dh.device = cloned
829
830 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
831 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
832 return err
833 }
834 log.Debugw("DisableDevice-end", log.Fields{"deviceId": device.Id})
835 return nil
836}
837
838func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
839 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
840 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
841 return err
842 }
843
844 dh.lockDevice.Lock()
845 dh.adminState = "up"
846 dh.lockDevice.Unlock()
847 log.Debug("olt-reenabled")
848
849 cloned := proto.Clone(device).(*voltha.Device)
850 // Update the all ports state on that device to enable
851 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
852 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
853 return err
854 }
855
856 //Update the device oper status as ACTIVE
857 cloned.OperStatus = voltha.OperStatus_ACTIVE
858 dh.device = cloned
859
860 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
861 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
862 return err
863 }
864 log.Debugw("ReEnableDevice-end", log.Fields{"deviceId": device.Id})
865
866 return nil
867}
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400868
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400869func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
870 if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
871 log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
872 return err
873 }
874
875 log.Debugw("rebooted-device-successfully", log.Fields{"deviceId": device.Id})
876
877 return nil
878}
879
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400880func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
881 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
882 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
883 if err != nil {
884 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
885 return
886 }
887 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
888 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
889 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
890 return
891 }
892 log.Debug("Success sending packet-in to core!")
893}
894
895func (dh *DeviceHandler) PacketOut(egress_port_no int, packet *of.OfpPacketOut) error {
896 log.Debugw("PacketOut", log.Fields{"deviceId": dh.deviceId, "egress_port_no": egress_port_no, "pkt-length": len(packet.Data)})
897 var etherFrame ethernet.Frame
898 err := (&etherFrame).UnmarshalBinary(packet.Data)
899 if err != nil {
900 log.Errorw("Failed to unmarshal into ethernet frame", log.Fields{"err": err, "pkt-length": len(packet.Data)})
901 return err
902 }
903 log.Debugw("Ethernet Frame", log.Fields{"Frame": etherFrame})
904 egressPortType := IntfIdToPortTypeName(uint32(egress_port_no))
905 if egressPortType == voltha.Port_ETHERNET_UNI {
906 if etherFrame.VLAN != nil { // If double tag, remove the outer tag
907 nextEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
908 if nextEthType == 0x8100 {
909 etherFrame.VLAN = nil
910 packet.Data, err = etherFrame.MarshalBinary()
911 if err != nil {
912 log.Fatalf("failed to marshal frame: %v", err)
913 return err
914 }
915 if err := (&etherFrame).UnmarshalBinary(packet.Data); err != nil {
916 log.Fatalf("failed to unmarshal frame: %v", err)
917 return err
918 }
919 log.Debug("Double tagged packet , removed outer vlan", log.Fields{"New frame": etherFrame})
920 }
921 }
922 intfId := IntfIdFromUniPortNum(uint32(egress_port_no))
923 onuId := OnuIdFromPortNum(uint32(egress_port_no))
924 uniId := UniIdFromPortNum(uint32(egress_port_no))
925 /*gemPortId, err := dh.flowMgr.GetPacketOutGemPortId(intfId, onuId, uint32(egress_port_no))
926 if err != nil{
927 log.Errorw("Error while getting gemport to packet-out",log.Fields{"error": err})
928 return err
929 }*/
930 onuPkt := oop.OnuPacket{IntfId: intfId, OnuId: onuId, PortNo: uint32(egress_port_no), Pkt: packet.Data}
931 log.Debug("sending-packet-to-ONU", log.Fields{"egress_port_no": egress_port_no, "IntfId": intfId, "onuId": onuId,
932 "uniId": uniId, "packet": packet.Data})
933 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
934 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
935 return err
936 }
937 } else if egressPortType == voltha.Port_ETHERNET_NNI {
938 uplinkPkt := oop.UplinkPacket{IntfId: IntfIdFromNniPortNum(uint32(egress_port_no)), Pkt: packet.Data}
939 log.Debug("sending-packet-to-uplink", log.Fields{"uplink_pkt": uplinkPkt})
940 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
941 log.Errorw("Error while sending packet-out to uplink", log.Fields{"error": err})
942 return err
943 }
944 } else {
945 log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egress_port_no, "egressPortType": egressPortType})
946 }
947 return nil
948}
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700949
950func (dh *DeviceHandler) formOnuKey(intfId uint32, onuId uint32) string {
951 return ("" + strconv.Itoa(int(intfId)) + "." + strconv.Itoa(int(onuId)))
952
953}