blob: 44d9eeec6719237ddf986bf05f2dbf7e196c92e6 [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
Phaneendra Manda4c62c802019-03-06 21:37:49 +053064}
65
66//NewDeviceHandler creates a new device handler
cuilin20187b2a8c32019-03-26 19:52:28 -070067func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
68 var dh DeviceHandler
69 dh.coreProxy = cp
Girish Gowdru0c588b22019-04-23 23:24:56 -040070 dh.AdapterProxy = ap
cuilin20187b2a8c32019-03-26 19:52:28 -070071 cloned := (proto.Clone(device)).(*voltha.Device)
72 dh.deviceId = cloned.Id
73 dh.deviceType = cloned.Type
Girish Gowdru5ba46c92019-04-25 05:00:05 -040074 dh.adminState = "up"
cuilin20187b2a8c32019-03-26 19:52:28 -070075 dh.device = cloned
76 dh.openOLT = adapter
77 dh.exitChannel = make(chan int, 1)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040078 dh.discOnus = make(map[string]bool)
cuilin20187b2a8c32019-03-26 19:52:28 -070079 dh.lockDevice = sync.RWMutex{}
Phaneendra Manda4c62c802019-03-06 21:37:49 +053080
cuilin20187b2a8c32019-03-26 19:52:28 -070081 //TODO initialize the support classes.
82 return &dh
Phaneendra Manda4c62c802019-03-06 21:37:49 +053083}
84
85// start save the device to the data model
86func (dh *DeviceHandler) start(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -070087 dh.lockDevice.Lock()
88 defer dh.lockDevice.Unlock()
89 log.Debugw("starting-device-agent", log.Fields{"device": dh.device})
90 // Add the initial device to the local model
91 log.Debug("device-agent-started")
Phaneendra Manda4c62c802019-03-06 21:37:49 +053092}
93
94// stop stops the device dh. Not much to do for now
95func (dh *DeviceHandler) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -070096 dh.lockDevice.Lock()
97 defer dh.lockDevice.Unlock()
98 log.Debug("stopping-device-agent")
99 dh.exitChannel <- 1
100 log.Debug("device-agent-stopped")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530101}
102
103func macAddressToUint32Array(mac string) []uint32 {
cuilin20187b2a8c32019-03-26 19:52:28 -0700104 slist := strings.Split(mac, ":")
105 result := make([]uint32, len(slist))
106 var err error
107 var tmp int64
108 for index, val := range slist {
109 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
110 return []uint32{1, 2, 3, 4, 5, 6}
111 }
112 result[index] = uint32(tmp)
113 }
114 return result
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530115}
116
manikkaraj kbf256be2019-03-25 00:13:48 +0530117func GetportLabel(portNum uint32, portType voltha.Port_PortType) string {
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530118
Girish Gowdru0c588b22019-04-23 23:24:56 -0400119 if portType == voltha.Port_ETHERNET_NNI {
120 return fmt.Sprintf("nni-%d", portNum)
121 } else if portType == voltha.Port_PON_OLT {
122 return fmt.Sprintf("pon-%d", portNum)
cuilin20187b2a8c32019-03-26 19:52:28 -0700123 } else if portType == voltha.Port_ETHERNET_UNI {
124 log.Errorw("local UNI management not supported", log.Fields{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400125 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700126 }
127 return ""
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530128}
129
130func (dh *DeviceHandler) addPort(intfId uint32, portType voltha.Port_PortType, state string) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700131 var operStatus common.OperStatus_OperStatus
132 if state == "up" {
133 operStatus = voltha.OperStatus_ACTIVE
134 } else {
135 operStatus = voltha.OperStatus_DISCOVERED
136 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400137 portNum := IntfIdToPortNo(intfId, portType)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400138 label := GetportLabel(portNum, portType)
139 if len(label) == 0 {
140 log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType})
141 return
142 }
143 // Now create Port
144 port := &voltha.Port{
cuilin20187b2a8c32019-03-26 19:52:28 -0700145 PortNo: portNum,
146 Label: label,
147 Type: portType,
148 OperStatus: operStatus,
149 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150 log.Debugw("Sending port update to core", log.Fields{"port": port})
cuilin20187b2a8c32019-03-26 19:52:28 -0700151 // Synchronous call to update device - this method is run in its own go routine
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152 if err := dh.coreProxy.PortCreated(nil, dh.device.Id, port); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700153 log.Errorw("error-creating-nni-port", log.Fields{"deviceId": dh.device.Id, "error": err})
154 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530155}
156
157// readIndications to read the indications from the OLT device
158func (dh *DeviceHandler) readIndications() {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400159 indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700160 if err != nil {
161 log.Errorw("Failed to read indications", log.Fields{"err": err})
162 return
163 }
164 if indications == nil {
165 log.Errorw("Indications is nil", log.Fields{})
166 return
167 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400168 /* get device state */
169 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
170 if err != nil || device == nil {
171 /*TODO: needs to handle error scenarios */
172 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
173
174 }
175 // When the device is in DISABLED and Adapter container restarts, we need to
176 // rebuild the locally maintained admin state.
177 if device.AdminState == voltha.AdminState_DISABLED {
178 dh.lockDevice.Lock()
179 dh.adminState = "down"
180 dh.lockDevice.Unlock()
181 }
182
cuilin20187b2a8c32019-03-26 19:52:28 -0700183 for {
184 indication, err := indications.Recv()
185 if err == io.EOF {
186 break
187 }
188 if err != nil {
189 log.Infow("Failed to read from indications", log.Fields{"err": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400190 dh.transitionMap.Handle(DeviceDownInd)
191 dh.transitionMap.Handle(DeviceInit)
192 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700193 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400194 // When OLT is admin down, allow only NNI operation status change indications.
195 if dh.adminState == "down" {
196 _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd)
197 if isIntfOperInd {
198 intfOperInd := indication.GetIntfOperInd()
199 if intfOperInd.GetType() == "nni" {
200 log.Infow("olt is admin down, allow nni ind", log.Fields{})
201 }
202 } else {
203 log.Infow("olt is admin down, ignore indication", log.Fields{})
204 continue
205 }
206 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700207 switch indication.Data.(type) {
208 case *oop.Indication_OltInd:
209 oltInd := indication.GetOltInd()
210 if oltInd.OperState == "up" {
211 dh.transitionMap.Handle(DeviceUpInd)
212 } else if oltInd.OperState == "down" {
213 dh.transitionMap.Handle(DeviceDownInd)
214 }
215 case *oop.Indication_IntfInd:
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530216
cuilin20187b2a8c32019-03-26 19:52:28 -0700217 intfInd := indication.GetIntfInd()
218 go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
219 log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd})
220 case *oop.Indication_IntfOperInd:
221 intfOperInd := indication.GetIntfOperInd()
222 if intfOperInd.GetType() == "nni" {
223 go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
224 } else if intfOperInd.GetType() == "pon" {
225 // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
226 // Handle pon port update
227 }
228 log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
229 case *oop.Indication_OnuDiscInd:
230 onuDiscInd := indication.GetOnuDiscInd()
231 log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232 //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
233 //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
234 // TODO Get onu ID from the resource manager
cuilin20187b2a8c32019-03-26 19:52:28 -0700235 var onuId uint32 = 1
Girish Gowdru0c588b22019-04-23 23:24:56 -0400236 /*if err != nil{
237 log.Errorw("onu-id-unavailable",log.Fields{"intfId":onuDiscInd.GetIntfId()})
238 return
239 }*/
manikkaraj kbf256be2019-03-25 00:13:48 +0530240
cuilin20187b2a8c32019-03-26 19:52:28 -0700241 sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400242 go dh.onuDiscIndication(onuDiscInd, onuId, sn)
cuilin20187b2a8c32019-03-26 19:52:28 -0700243 case *oop.Indication_OnuInd:
244 onuInd := indication.GetOnuInd()
245 log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
246 go dh.onuIndication(onuInd)
247 case *oop.Indication_OmciInd:
248 omciInd := indication.GetOmciInd()
249 log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd})
250 if err := dh.omciIndication(omciInd); err != nil {
251 log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd})
252 }
253 case *oop.Indication_PktInd:
254 pktInd := indication.GetPktInd()
255 log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400256 go dh.handlePacketIndication(pktInd)
cuilin20187b2a8c32019-03-26 19:52:28 -0700257 case *oop.Indication_PortStats:
258 portStats := indication.GetPortStats()
259 log.Infow("Received port stats indication", log.Fields{"PortStats": portStats})
260 case *oop.Indication_FlowStats:
261 flowStats := indication.GetFlowStats()
262 log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats})
263 case *oop.Indication_AlarmInd:
264 alarmInd := indication.GetAlarmInd()
265 log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd})
266 }
267 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530268}
269
270// doStateUp handle the olt up indication and update to voltha core
271func (dh *DeviceHandler) doStateUp() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400272 // Synchronous call to update device state - this method is run in its own go routine
cuilin20187b2a8c32019-03-26 19:52:28 -0700273 if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400274 voltha.OperStatus_ACTIVE); err != nil {
275 log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceId": dh.device.Id, "error": err})
276 return err
277 }
278 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530279}
280
281// doStateDown handle the olt down indication
282func (dh *DeviceHandler) doStateDown() error {
Girish Gowdrud4245152019-05-10 00:47:31 -0400283 log.Debug("do-state-down-start")
284
285 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
286 if err != nil || device == nil {
287 /*TODO: needs to handle error scenarios */
288 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
289 }
290
291 cloned := proto.Clone(device).(*voltha.Device)
292 // Update the all ports state on that device to disable
293 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
294 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
295 return err
296 }
297
298 //Update the device oper state and connection status
299 cloned.OperStatus = voltha.OperStatus_UNKNOWN
300 cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
301 dh.device = cloned
302
303 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
304 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
305 return err
306 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400307
308 //get the child device for the parent device
309 onuDevices, err := dh.coreProxy.GetChildDevices(nil, dh.device.Id)
310 if err != nil {
311 log.Errorw("failed to get child devices information", log.Fields{"deviceId": dh.device.Id, "error": err})
312 return err
313 }
314 for _, onuDevice := range onuDevices.Items {
315
316 // Update onu state as down in onu adapter
317 onuInd := oop.OnuIndication{}
318 onuInd.OperState = "down"
319 dh.AdapterProxy.SendInterAdapterMessage(nil, &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
320
321 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400322 log.Debugw("do-state-down-end", log.Fields{"deviceId": device.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700323 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530324}
325
326// doStateInit dial the grpc before going to init state
327func (dh *DeviceHandler) doStateInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400328 var err error
Girish Gowdrud4245152019-05-10 00:47:31 -0400329 dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock())
Girish Gowdru0c588b22019-04-23 23:24:56 -0400330 if err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700331 log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceId, "HostAndPort": dh.device.GetHostAndPort(), "err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400332 return err
333 }
334 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530335}
336
337// postInit create olt client instance to invoke RPC on the olt device
338func (dh *DeviceHandler) postInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400339 dh.Client = oop.NewOpenoltClient(dh.clientCon)
340 dh.transitionMap.Handle(GrpcConnected)
341 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530342}
343
344// doStateConnected get the device info and update to voltha core
345func (dh *DeviceHandler) doStateConnected() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400346 log.Debug("OLT device has been connected")
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400347
348 // Case where OLT is disabled and then rebooted.
349 if dh.adminState == "down" {
350 log.Debugln("do-state-connected--device-admin-state-down")
351 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
352 if err != nil || device == nil {
353 /*TODO: needs to handle error scenarios */
354 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
355 }
356
357 cloned := proto.Clone(device).(*voltha.Device)
358 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
359 cloned.OperStatus = voltha.OperStatus_UNKNOWN
360 dh.device = cloned
361 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
362 log.Errorw("error-updating-device-state", log.Fields{"deviceId": dh.device.Id, "error": err})
363 }
364
365 // Since the device was disabled before the OLT was rebooted, enfore the OLT to be Disabled after re-connection.
366 _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
367 if err != nil {
368 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
369 }
370
371 // Start reading indications
372 go dh.readIndications()
373 return nil
374 }
375
Girish Gowdru0c588b22019-04-23 23:24:56 -0400376 deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700377 if err != nil {
378 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
379 return err
380 }
381 if deviceInfo == nil {
382 log.Errorw("Device info is nil", log.Fields{})
383 return errors.New("Failed to get device info from OLT")
384 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400385 log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo})
cuilin20187b2a8c32019-03-26 19:52:28 -0700386 dh.device.Root = true
387 dh.device.Vendor = deviceInfo.Vendor
388 dh.device.Model = deviceInfo.Model
cuilin20187b2a8c32019-03-26 19:52:28 -0700389 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
390 dh.device.HardwareVersion = deviceInfo.HardwareVersion
391 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400392 // FIXME: Remove Hardcodings
cuilin20187b2a8c32019-03-26 19:52:28 -0700393 dh.device.MacAddress = "0a:0b:0c:0d:0e:0f"
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530394
cuilin20187b2a8c32019-03-26 19:52:28 -0700395 // Synchronous call to update device - this method is run in its own go routine
396 if err := dh.coreProxy.DeviceUpdate(nil, dh.device); err != nil {
397 log.Errorw("error-updating-device", log.Fields{"deviceId": dh.device.Id, "error": err})
398 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400399
400 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
401 if err != nil || device == nil {
402 /*TODO: needs to handle error scenarios */
403 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
404 }
405 cloned := proto.Clone(device).(*voltha.Device)
406 // Update the all ports (if available) on that device to ACTIVE.
407 // The ports do not normally exist, unless the device is coming back from a reboot
408 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
409 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
410 return err
411 }
412
Girish Gowdru0c588b22019-04-23 23:24:56 -0400413 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
414 // Instantiate resource manager
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400415 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceId, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400416 log.Error("Error while instantiating resource manager")
417 return errors.New("Instantiating resource manager failed")
418 }
419 // Instantiate flow manager
420 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
421 log.Error("Error while instantiating flow manager")
422 return errors.New("Instantiating flow manager failed")
423 }
424 /* TODO: Instantiate Alarm , stats , BW managers */
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530425
cuilin20187b2a8c32019-03-26 19:52:28 -0700426 // Start reading indications
427 go dh.readIndications()
428 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530429}
430
431// AdoptDevice adopts the OLT device
432func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400433 dh.transitionMap = NewTransitionMap(dh)
cuilin20187b2a8c32019-03-26 19:52:28 -0700434 log.Infow("AdoptDevice", log.Fields{"deviceId": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400435 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530436}
437
438// GetOfpDeviceInfo Get the Ofp device information
439func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700440 return &ic.SwitchCapability{
441 Desc: &of.OfpDesc{
442 HwDesc: "open_pon",
443 SwDesc: "open_pon",
444 SerialNum: dh.device.SerialNumber,
445 },
446 SwitchFeatures: &of.OfpSwitchFeatures{
447 NBuffers: 256,
448 NTables: 2,
449 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
450 of.OfpCapabilities_OFPC_TABLE_STATS |
451 of.OfpCapabilities_OFPC_PORT_STATS |
452 of.OfpCapabilities_OFPC_GROUP_STATS),
453 },
454 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530455}
456
457// GetOfpPortInfo Get Ofp port information
458func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700459 cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
460 return &ic.PortCapability{
461 Port: &voltha.LogicalPort{
462 OfpPort: &of.OfpPort{
463 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
464 Config: 0,
465 State: uint32(of.OfpPortState_OFPPS_LIVE),
466 Curr: cap,
467 Advertised: cap,
468 Peer: cap,
469 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
470 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
471 },
472 DeviceId: dh.device.Id,
473 DevicePortNo: uint32(portNo),
474 },
475 }, nil
476}
477
478func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
479 log.Debugw("omci indication", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
480
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400481 ponPort := IntfIdToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
cuilin20187b2a8c32019-03-26 19:52:28 -0700482 kwargs := make(map[string]interface{})
483 kwargs["onu_id"] = omciInd.OnuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400484 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700485
486 if onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); err != nil {
487 log.Errorw("onu not found", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
488 return err
489 } else {
490 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
manikkaraj kbf256be2019-03-25 00:13:48 +0530491 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
cuilin20187b2a8c32019-03-26 19:52:28 -0700492 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, onuDevice.Type,
493 onuDevice.Id, onuDevice.ProxyAddress.DeviceId, ""); sendErr != nil {
494 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": onuDevice.Type, "onuId": onuDevice.Id, "proxyDeviceId": onuDevice.ProxyAddress.DeviceId})
495 return sendErr
496 }
497 return nil
498 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530499}
500
501// Process_inter_adapter_message process inter adater message
502func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700503 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
504 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
505 msgId := msg.Header.Id
506 fromTopic := msg.Header.FromTopic
507 toTopic := msg.Header.ToTopic
508 toDeviceId := msg.Header.ToDeviceId
509 proxyDeviceId := msg.Header.ProxyDeviceId
510
511 log.Debugw("omci request message header", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
512
513 msgBody := msg.GetBody()
514
515 omciMsg := &ic.InterAdapterOmciMessage{}
516 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
517 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
518 return err
519 }
520
521 if onuDevice, err := dh.coreProxy.GetDevice(nil, dh.device.Id, toDeviceId); err != nil {
522 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceId, "error": err})
523 return err
524 } else {
525 dh.sendProxiedMessage(onuDevice, omciMsg)
526 }
527
528 } else {
529 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
530 }
531 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530532}
533
cuilin20187b2a8c32019-03-26 19:52:28 -0700534func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
535 if onuDevice.ConnectStatus != voltha.ConnectStatus_REACHABLE {
536 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"serialNumber": onuDevice.SerialNumber, "intfId": onuDevice.ProxyAddress.GetChannelId(), "onuId": onuDevice.ProxyAddress.GetOnuId()})
537 return
538 }
539
540 omciMessage := &oop.OmciMsg{IntfId: onuDevice.ProxyAddress.GetChannelId(), OnuId: onuDevice.ProxyAddress.GetOnuId(), Pkt: omciMsg.Message}
541
manikkaraj kbf256be2019-03-25 00:13:48 +0530542 dh.Client.OmciMsgOut(context.Background(), omciMessage)
cuilin20187b2a8c32019-03-26 19:52:28 -0700543 log.Debugw("omci-message-sent", log.Fields{"serialNumber": onuDevice.SerialNumber, "intfId": onuDevice.ProxyAddress.GetChannelId(), "omciMsg": string(omciMsg.Message)})
544}
545
546func (dh *DeviceHandler) activateONU(intfId uint32, onuId int64, serialNum *oop.SerialNumber, serialNumber string) {
547 log.Debugw("activate-onu", log.Fields{"intfId": intfId, "onuId": onuId, "serialNum": serialNum, "serialNumber": serialNumber})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400548 dh.flowMgr.UpdateOnuInfo(intfId, uint32(onuId), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700549 // TODO: need resource manager
550 var pir uint32 = 1000000
551 Onu := oop.Onu{IntfId: intfId, OnuId: uint32(onuId), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530552 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400553 st, _ := status.FromError(err)
554 if st.Code() == codes.AlreadyExists {
555 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
556 } else {
557 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
558 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700559 } else {
560 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
561 }
562}
563
564func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, onuId uint32, sn string) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400565 channelId := onuDiscInd.GetIntfId()
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400566 parentPortNo := IntfIdToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400567 if _, ok := dh.discOnus[sn]; ok {
568 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
569 return nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700570 }
571
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400572 dh.lockDevice.Lock()
573 dh.discOnus[sn] = true
574 dh.lockDevice.Unlock()
575 // evict the onu serial number from local cache
576 defer func() {
577 delete(dh.discOnus, sn)
578 }()
579
cuilin20187b2a8c32019-03-26 19:52:28 -0700580 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400581 if sn != "" {
582 kwargs["serial_number"] = sn
583 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700584 kwargs["onu_id"] = onuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400585 kwargs["parent_port_no"] = parentPortNo
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400586 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
587 if onuDevice == nil {
588 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 {
589 log.Errorw("Create onu error", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn, "error": err})
590 return err
591 }
592 }
593 onuDevice, err = dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
594 if err != nil {
595 log.Errorw("failed to get ONU device information", log.Fields{"err": err})
596 return err
597 }
598 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
599 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700600
601 for i := 0; i < 10; i++ {
602 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
603 dh.activateONU(onuDiscInd.IntfId, int64(onuId), onuDiscInd.SerialNumber, sn)
604 return nil
605 } else {
606 time.Sleep(1 * time.Second)
607 log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1)
608 }
609 }
610 log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn})
611 return errors.New("Failed to activate onu")
612}
613
614func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
615 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
616
617 kwargs := make(map[string]interface{})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400618 ponPort := IntfIdToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
manikkaraj kbf256be2019-03-25 00:13:48 +0530619
cuilin20187b2a8c32019-03-26 19:52:28 -0700620 if serialNumber != "" {
621 kwargs["serial_number"] = serialNumber
622 } else {
623 kwargs["onu_id"] = onuInd.OnuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400624 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700625 }
626 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400627 if onuDevice.ParentPortNo != ponPort {
cuilin20187b2a8c32019-03-26 19:52:28 -0700628 //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 -0400629 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700630 }
631
632 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
633 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})
634 }
635
636 // adminState
637 if onuInd.AdminState == "down" {
638 if onuInd.OperState != "down" {
639 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
640 // Forcing the oper state change code to execute
641 onuInd.OperState = "down"
642 }
643 // Port and logical port update is taken care of by oper state block
644 } else if onuInd.AdminState == "up" {
645 log.Debugln("received-onu-admin-state up")
646 } else {
647 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
648 }
649 log.Debugln("admin-state-dealt-with")
650
651 // operState
652 if onuInd.OperState == "down" {
653 if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE {
654 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, onuDevice.OperStatus)
655 log.Debugln("onu-oper-state-is-down")
656 }
657 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
658 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, common.OperStatus_DISCOVERED)
659 }
660 log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd})
661
662 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
manikkaraj kbf256be2019-03-25 00:13:48 +0530663 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700664 } else if onuInd.OperState == "up" {
665 if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE {
666 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus)
667
668 }
669 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
670 log.Warnw("ignore onu indication", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState})
671 return
672 }
manikkaraj kbf256be2019-03-25 00:13:48 +0530673 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700674 } else {
675 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
676 }
677 } else {
678 log.Errorw("onu not found", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId})
679 return
680 }
681
682}
683
684func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
685 if serialNum != nil {
686 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
687 } else {
688 return ""
689 }
690}
691
692func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
693 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
694 fmt.Sprintf("%x", (uint32(vendorSpecific[0]&0x0f))) +
695 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
696 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
697 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
698 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
699 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
700 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
701 return tmp
702}
703
704// flows
705func (dh *DeviceHandler) Update_flows_bulk() error {
706 return errors.New("UnImplemented")
707}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400708func (dh *DeviceHandler) GetChildDevice(parentPort uint32, onuId uint32) *voltha.Device {
709 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuId": onuId})
710 kwargs := make(map[string]interface{})
711 kwargs["onu_id"] = onuId
712 kwargs["parent_port_no"] = parentPort
713 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
714 if err != nil {
715 log.Errorw("onu not found", log.Fields{"intfId": parentPort, "onuId": onuId})
716 return nil
717 }
718 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
719 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530720}
721
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400722func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
723 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
724 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPort, packetPayload); err != nil {
725 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
726 return
727 }
728 log.Debug("Sent packet-in to core successfully")
729}
730
manikkaraj kbf256be2019-03-25 00:13:48 +0530731func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400732 log.Debugw("In UpdateFlowsIncrementally", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups})
733 if flows != nil {
734 for _, flow := range flows.ToAdd.Items {
735 dh.flowMgr.AddFlow(flow)
736 }
737 }
738 if groups != nil {
739 for _, flow := range flows.ToRemove.Items {
740 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
741 // dh.flowMgr.RemoveFlow(flow)
742 }
743 }
744 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530745}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400746
747func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
748 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
749 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
750 return err
751 }
752 dh.lockDevice.Lock()
753 dh.adminState = "down"
754 dh.lockDevice.Unlock()
755 log.Debug("olt-disabled")
756
757 cloned := proto.Clone(device).(*voltha.Device)
758 // Update the all ports state on that device to disable
759 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
760 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
761 return err
762 }
763
764 //Update the device oper state
765 cloned.OperStatus = voltha.OperStatus_UNKNOWN
766 dh.device = cloned
767
768 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
769 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
770 return err
771 }
772 log.Debugw("DisableDevice-end", log.Fields{"deviceId": device.Id})
773 return nil
774}
775
776func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
777 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
778 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
779 return err
780 }
781
782 dh.lockDevice.Lock()
783 dh.adminState = "up"
784 dh.lockDevice.Unlock()
785 log.Debug("olt-reenabled")
786
787 cloned := proto.Clone(device).(*voltha.Device)
788 // Update the all ports state on that device to enable
789 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
790 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
791 return err
792 }
793
794 //Update the device oper status as ACTIVE
795 cloned.OperStatus = voltha.OperStatus_ACTIVE
796 dh.device = cloned
797
798 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
799 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
800 return err
801 }
802 log.Debugw("ReEnableDevice-end", log.Fields{"deviceId": device.Id})
803
804 return nil
805}
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400806
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400807func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
808 if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
809 log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
810 return err
811 }
812
813 log.Debugw("rebooted-device-successfully", log.Fields{"deviceId": device.Id})
814
815 return nil
816}
817
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400818func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
819 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
820 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
821 if err != nil {
822 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
823 return
824 }
825 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
826 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
827 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
828 return
829 }
830 log.Debug("Success sending packet-in to core!")
831}
832
833func (dh *DeviceHandler) PacketOut(egress_port_no int, packet *of.OfpPacketOut) error {
834 log.Debugw("PacketOut", log.Fields{"deviceId": dh.deviceId, "egress_port_no": egress_port_no, "pkt-length": len(packet.Data)})
835 var etherFrame ethernet.Frame
836 err := (&etherFrame).UnmarshalBinary(packet.Data)
837 if err != nil {
838 log.Errorw("Failed to unmarshal into ethernet frame", log.Fields{"err": err, "pkt-length": len(packet.Data)})
839 return err
840 }
841 log.Debugw("Ethernet Frame", log.Fields{"Frame": etherFrame})
842 egressPortType := IntfIdToPortTypeName(uint32(egress_port_no))
843 if egressPortType == voltha.Port_ETHERNET_UNI {
844 if etherFrame.VLAN != nil { // If double tag, remove the outer tag
845 nextEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
846 if nextEthType == 0x8100 {
847 etherFrame.VLAN = nil
848 packet.Data, err = etherFrame.MarshalBinary()
849 if err != nil {
850 log.Fatalf("failed to marshal frame: %v", err)
851 return err
852 }
853 if err := (&etherFrame).UnmarshalBinary(packet.Data); err != nil {
854 log.Fatalf("failed to unmarshal frame: %v", err)
855 return err
856 }
857 log.Debug("Double tagged packet , removed outer vlan", log.Fields{"New frame": etherFrame})
858 }
859 }
860 intfId := IntfIdFromUniPortNum(uint32(egress_port_no))
861 onuId := OnuIdFromPortNum(uint32(egress_port_no))
862 uniId := UniIdFromPortNum(uint32(egress_port_no))
863 /*gemPortId, err := dh.flowMgr.GetPacketOutGemPortId(intfId, onuId, uint32(egress_port_no))
864 if err != nil{
865 log.Errorw("Error while getting gemport to packet-out",log.Fields{"error": err})
866 return err
867 }*/
868 onuPkt := oop.OnuPacket{IntfId: intfId, OnuId: onuId, PortNo: uint32(egress_port_no), Pkt: packet.Data}
869 log.Debug("sending-packet-to-ONU", log.Fields{"egress_port_no": egress_port_no, "IntfId": intfId, "onuId": onuId,
870 "uniId": uniId, "packet": packet.Data})
871 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
872 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
873 return err
874 }
875 } else if egressPortType == voltha.Port_ETHERNET_NNI {
876 uplinkPkt := oop.UplinkPacket{IntfId: IntfIdFromNniPortNum(uint32(egress_port_no)), Pkt: packet.Data}
877 log.Debug("sending-packet-to-uplink", log.Fields{"uplink_pkt": uplinkPkt})
878 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
879 log.Errorw("Error while sending packet-out to uplink", log.Fields{"error": err})
880 return err
881 }
882 } else {
883 log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egress_port_no, "egressPortType": egressPortType})
884 }
885 return nil
886}