blob: 20eb7830f6bbb7e2fd1e7279979b700d8033799d [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{
Devmalya Paul70dd4972019-06-10 15:19:17 +0530465 MfrDesc: "VOLTHA Project",
cuilin20187b2a8c32019-03-26 19:52:28 -0700466 HwDesc: "open_pon",
467 SwDesc: "open_pon",
468 SerialNum: dh.device.SerialNumber,
469 },
470 SwitchFeatures: &of.OfpSwitchFeatures{
471 NBuffers: 256,
472 NTables: 2,
473 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
474 of.OfpCapabilities_OFPC_TABLE_STATS |
475 of.OfpCapabilities_OFPC_PORT_STATS |
476 of.OfpCapabilities_OFPC_GROUP_STATS),
477 },
478 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530479}
480
481// GetOfpPortInfo Get Ofp port information
482func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700483 cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
484 return &ic.PortCapability{
485 Port: &voltha.LogicalPort{
486 OfpPort: &of.OfpPort{
487 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
488 Config: 0,
489 State: uint32(of.OfpPortState_OFPPS_LIVE),
490 Curr: cap,
491 Advertised: cap,
492 Peer: cap,
493 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
494 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
495 },
496 DeviceId: dh.device.Id,
497 DevicePortNo: uint32(portNo),
498 },
499 }, nil
500}
501
502func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
503 log.Debugw("omci indication", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700504 var deviceType string
505 var deviceId string
506 var proxyDeviceId string
cuilin20187b2a8c32019-03-26 19:52:28 -0700507
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700508 onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId)
509 if onuInCache, ok := dh.onus[onuKey]; !ok {
510 log.Debugw("omci indication for a device not in cache.", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
511 ponPort := IntfIdToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
512 kwargs := make(map[string]interface{})
513 kwargs["onu_id"] = omciInd.OnuId
514 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700515
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700516 if onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); err != nil {
517 log.Errorw("onu not found", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
518 return err
519 } else {
520 deviceType = onuDevice.Type
521 deviceId = onuDevice.Id
522 proxyDeviceId = onuDevice.ProxyAddress.DeviceId
523 //if not exist in cache, then add to cache.
524 dh.onus[onuKey] = NewOnuDevice(deviceId, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceId)
cuilin20187b2a8c32019-03-26 19:52:28 -0700525 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700526 } else {
527 //found in cache
528 log.Debugw("omci indication for a device in cache.", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
529 deviceType = onuInCache.deviceType
530 deviceId = onuInCache.deviceId
531 proxyDeviceId = onuInCache.proxyDeviceId
cuilin20187b2a8c32019-03-26 19:52:28 -0700532 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700533
534 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
535 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
536 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType,
537 deviceId, proxyDeviceId, ""); sendErr != nil {
538 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuId": deviceId, "proxyDeviceId": proxyDeviceId})
539 return sendErr
540 }
541 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530542}
543
544// Process_inter_adapter_message process inter adater message
545func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700546 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
547 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
548 msgId := msg.Header.Id
549 fromTopic := msg.Header.FromTopic
550 toTopic := msg.Header.ToTopic
551 toDeviceId := msg.Header.ToDeviceId
552 proxyDeviceId := msg.Header.ProxyDeviceId
553
554 log.Debugw("omci request message header", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
555
556 msgBody := msg.GetBody()
557
558 omciMsg := &ic.InterAdapterOmciMessage{}
559 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
560 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
561 return err
562 }
563
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700564 if omciMsg.GetProxyAddress() == nil {
565 if onuDevice, err := dh.coreProxy.GetDevice(nil, dh.device.Id, toDeviceId); err != nil {
566 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceId, "error": err})
567 return err
568 } else {
569 log.Debugw("device retrieved from core", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
570 dh.sendProxiedMessage(onuDevice, omciMsg)
571 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700572 } else {
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700573 log.Debugw("Proxy Address found in omci message", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
574 dh.sendProxiedMessage(nil, omciMsg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700575 }
576
577 } else {
578 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
579 }
580 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530581}
582
cuilin20187b2a8c32019-03-26 19:52:28 -0700583func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700584 var intfId uint32
585 var onuId uint32
586 var status common.ConnectStatus_ConnectStatus
587 if onuDevice != nil {
588 intfId = onuDevice.ProxyAddress.GetChannelId()
589 onuId = onuDevice.ProxyAddress.GetOnuId()
590 status = onuDevice.ConnectStatus
591 } else {
592 intfId = omciMsg.GetProxyAddress().GetChannelId()
593 onuId = omciMsg.GetProxyAddress().GetOnuId()
594 status = omciMsg.GetConnectStatus()
595 }
596 if status != voltha.ConnectStatus_REACHABLE {
597 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfId": intfId, "onuId": onuId})
cuilin20187b2a8c32019-03-26 19:52:28 -0700598 return
599 }
600
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700601 omciMessage := &oop.OmciMsg{IntfId: intfId, OnuId: onuId, Pkt: omciMsg.Message}
cuilin20187b2a8c32019-03-26 19:52:28 -0700602
manikkaraj kbf256be2019-03-25 00:13:48 +0530603 dh.Client.OmciMsgOut(context.Background(), omciMessage)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700604 log.Debugw("omci-message-sent", log.Fields{"intfId": intfId, "onuId": onuId, "omciMsg": string(omciMsg.GetMessage())})
cuilin20187b2a8c32019-03-26 19:52:28 -0700605}
606
607func (dh *DeviceHandler) activateONU(intfId uint32, onuId int64, serialNum *oop.SerialNumber, serialNumber string) {
608 log.Debugw("activate-onu", log.Fields{"intfId": intfId, "onuId": onuId, "serialNum": serialNum, "serialNumber": serialNumber})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400609 dh.flowMgr.UpdateOnuInfo(intfId, uint32(onuId), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700610 // TODO: need resource manager
611 var pir uint32 = 1000000
612 Onu := oop.Onu{IntfId: intfId, OnuId: uint32(onuId), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530613 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400614 st, _ := status.FromError(err)
615 if st.Code() == codes.AlreadyExists {
616 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
617 } else {
618 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
619 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700620 } else {
621 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
622 }
623}
624
625func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, onuId uint32, sn string) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400626 channelId := onuDiscInd.GetIntfId()
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400627 parentPortNo := IntfIdToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400628 if _, ok := dh.discOnus[sn]; ok {
629 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
630 return nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700631 }
632
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400633 dh.lockDevice.Lock()
634 dh.discOnus[sn] = true
635 dh.lockDevice.Unlock()
636 // evict the onu serial number from local cache
637 defer func() {
638 delete(dh.discOnus, sn)
639 }()
640
cuilin20187b2a8c32019-03-26 19:52:28 -0700641 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400642 if sn != "" {
643 kwargs["serial_number"] = sn
644 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700645 kwargs["onu_id"] = onuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400646 kwargs["parent_port_no"] = parentPortNo
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400647 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
648 if onuDevice == nil {
649 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 {
650 log.Errorw("Create onu error", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn, "error": err})
651 return err
652 }
653 }
654 onuDevice, err = dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
655 if err != nil {
656 log.Errorw("failed to get ONU device information", log.Fields{"err": err})
657 return err
658 }
659 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
660 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700661
662 for i := 0; i < 10; i++ {
663 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
664 dh.activateONU(onuDiscInd.IntfId, int64(onuId), onuDiscInd.SerialNumber, sn)
665 return nil
666 } else {
667 time.Sleep(1 * time.Second)
668 log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1)
669 }
670 }
671 log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn})
672 return errors.New("Failed to activate onu")
673}
674
675func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
676 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
677
678 kwargs := make(map[string]interface{})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400679 ponPort := IntfIdToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
manikkaraj kbf256be2019-03-25 00:13:48 +0530680
cuilin20187b2a8c32019-03-26 19:52:28 -0700681 if serialNumber != "" {
682 kwargs["serial_number"] = serialNumber
683 } else {
684 kwargs["onu_id"] = onuInd.OnuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400685 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700686 }
687 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400688 if onuDevice.ParentPortNo != ponPort {
cuilin20187b2a8c32019-03-26 19:52:28 -0700689 //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 -0400690 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700691 }
692
693 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
694 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})
695 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700696 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId())
697 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
cuilin20187b2a8c32019-03-26 19:52:28 -0700698
699 // adminState
700 if onuInd.AdminState == "down" {
701 if onuInd.OperState != "down" {
702 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
703 // Forcing the oper state change code to execute
704 onuInd.OperState = "down"
705 }
706 // Port and logical port update is taken care of by oper state block
707 } else if onuInd.AdminState == "up" {
708 log.Debugln("received-onu-admin-state up")
709 } else {
710 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
711 }
712 log.Debugln("admin-state-dealt-with")
713
714 // operState
715 if onuInd.OperState == "down" {
716 if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE {
717 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, onuDevice.OperStatus)
718 log.Debugln("onu-oper-state-is-down")
719 }
720 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
721 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, common.OperStatus_DISCOVERED)
722 }
723 log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd})
724
725 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
manikkaraj kbf256be2019-03-25 00:13:48 +0530726 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700727 } else if onuInd.OperState == "up" {
728 if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE {
729 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus)
730
731 }
732 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
733 log.Warnw("ignore onu indication", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState})
734 return
735 }
manikkaraj kbf256be2019-03-25 00:13:48 +0530736 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700737 } else {
738 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
739 }
740 } else {
741 log.Errorw("onu not found", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId})
742 return
743 }
744
745}
746
747func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
748 if serialNum != nil {
749 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
750 } else {
751 return ""
752 }
753}
754
755func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
756 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
757 fmt.Sprintf("%x", (uint32(vendorSpecific[0]&0x0f))) +
758 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
759 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
760 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
761 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
762 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
763 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
764 return tmp
765}
766
767// flows
768func (dh *DeviceHandler) Update_flows_bulk() error {
769 return errors.New("UnImplemented")
770}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400771func (dh *DeviceHandler) GetChildDevice(parentPort uint32, onuId uint32) *voltha.Device {
772 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuId": onuId})
773 kwargs := make(map[string]interface{})
774 kwargs["onu_id"] = onuId
775 kwargs["parent_port_no"] = parentPort
776 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
777 if err != nil {
778 log.Errorw("onu not found", log.Fields{"intfId": parentPort, "onuId": onuId})
779 return nil
780 }
781 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
782 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530783}
784
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400785func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
786 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
787 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPort, packetPayload); err != nil {
788 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
789 return
790 }
791 log.Debug("Sent packet-in to core successfully")
792}
793
manikkaraj kbf256be2019-03-25 00:13:48 +0530794func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400795 log.Debugw("In UpdateFlowsIncrementally", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups})
796 if flows != nil {
797 for _, flow := range flows.ToAdd.Items {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400798 log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400799 dh.flowMgr.AddFlow(flow)
800 }
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400801 for _, flow := range flows.ToRemove.Items {
802 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
803 dh.flowMgr.RemoveFlow(flow)
804 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400805 }
806 if groups != nil {
807 for _, flow := range flows.ToRemove.Items {
808 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
809 // dh.flowMgr.RemoveFlow(flow)
810 }
811 }
812 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530813}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400814
815func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
816 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
817 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
818 return err
819 }
820 dh.lockDevice.Lock()
821 dh.adminState = "down"
822 dh.lockDevice.Unlock()
823 log.Debug("olt-disabled")
824
825 cloned := proto.Clone(device).(*voltha.Device)
826 // Update the all ports state on that device to disable
827 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
828 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
829 return err
830 }
831
832 //Update the device oper state
833 cloned.OperStatus = voltha.OperStatus_UNKNOWN
834 dh.device = cloned
835
836 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
837 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
838 return err
839 }
840 log.Debugw("DisableDevice-end", log.Fields{"deviceId": device.Id})
841 return nil
842}
843
844func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
845 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
846 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
847 return err
848 }
849
850 dh.lockDevice.Lock()
851 dh.adminState = "up"
852 dh.lockDevice.Unlock()
853 log.Debug("olt-reenabled")
854
855 cloned := proto.Clone(device).(*voltha.Device)
856 // Update the all ports state on that device to enable
857 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
858 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
859 return err
860 }
861
862 //Update the device oper status as ACTIVE
863 cloned.OperStatus = voltha.OperStatus_ACTIVE
864 dh.device = cloned
865
866 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
867 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
868 return err
869 }
870 log.Debugw("ReEnableDevice-end", log.Fields{"deviceId": device.Id})
871
872 return nil
873}
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400874
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400875func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
876 if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
877 log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
878 return err
879 }
880
881 log.Debugw("rebooted-device-successfully", log.Fields{"deviceId": device.Id})
882
883 return nil
884}
885
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400886func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
887 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
888 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
889 if err != nil {
890 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
891 return
892 }
893 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
894 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
895 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
896 return
897 }
898 log.Debug("Success sending packet-in to core!")
899}
900
901func (dh *DeviceHandler) PacketOut(egress_port_no int, packet *of.OfpPacketOut) error {
902 log.Debugw("PacketOut", log.Fields{"deviceId": dh.deviceId, "egress_port_no": egress_port_no, "pkt-length": len(packet.Data)})
903 var etherFrame ethernet.Frame
904 err := (&etherFrame).UnmarshalBinary(packet.Data)
905 if err != nil {
906 log.Errorw("Failed to unmarshal into ethernet frame", log.Fields{"err": err, "pkt-length": len(packet.Data)})
907 return err
908 }
909 log.Debugw("Ethernet Frame", log.Fields{"Frame": etherFrame})
910 egressPortType := IntfIdToPortTypeName(uint32(egress_port_no))
911 if egressPortType == voltha.Port_ETHERNET_UNI {
912 if etherFrame.VLAN != nil { // If double tag, remove the outer tag
913 nextEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
914 if nextEthType == 0x8100 {
915 etherFrame.VLAN = nil
916 packet.Data, err = etherFrame.MarshalBinary()
917 if err != nil {
918 log.Fatalf("failed to marshal frame: %v", err)
919 return err
920 }
921 if err := (&etherFrame).UnmarshalBinary(packet.Data); err != nil {
922 log.Fatalf("failed to unmarshal frame: %v", err)
923 return err
924 }
925 log.Debug("Double tagged packet , removed outer vlan", log.Fields{"New frame": etherFrame})
926 }
927 }
928 intfId := IntfIdFromUniPortNum(uint32(egress_port_no))
929 onuId := OnuIdFromPortNum(uint32(egress_port_no))
930 uniId := UniIdFromPortNum(uint32(egress_port_no))
931 /*gemPortId, err := dh.flowMgr.GetPacketOutGemPortId(intfId, onuId, uint32(egress_port_no))
932 if err != nil{
933 log.Errorw("Error while getting gemport to packet-out",log.Fields{"error": err})
934 return err
935 }*/
936 onuPkt := oop.OnuPacket{IntfId: intfId, OnuId: onuId, PortNo: uint32(egress_port_no), Pkt: packet.Data}
937 log.Debug("sending-packet-to-ONU", log.Fields{"egress_port_no": egress_port_no, "IntfId": intfId, "onuId": onuId,
938 "uniId": uniId, "packet": packet.Data})
939 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
940 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
941 return err
942 }
943 } else if egressPortType == voltha.Port_ETHERNET_NNI {
944 uplinkPkt := oop.UplinkPacket{IntfId: IntfIdFromNniPortNum(uint32(egress_port_no)), Pkt: packet.Data}
945 log.Debug("sending-packet-to-uplink", log.Fields{"uplink_pkt": uplinkPkt})
946 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
947 log.Errorw("Error while sending packet-out to uplink", log.Fields{"error": err})
948 return err
949 }
950 } else {
951 log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egress_port_no, "egressPortType": egressPortType})
952 }
953 return nil
954}
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700955
956func (dh *DeviceHandler) formOnuKey(intfId uint32, onuId uint32) string {
957 return ("" + strconv.Itoa(int(intfId)) + "." + strconv.Itoa(int(onuId)))
958
959}