blob: 3abb76409c8735d2ac206de51c55306230e7b334 [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
cuilin20187b2a8c32019-03-26 19:52:28 -070028 "github.com/gogo/protobuf/proto"
29 "github.com/golang/protobuf/ptypes"
30 com "github.com/opencord/voltha-go/adapters/common"
31 "github.com/opencord/voltha-go/common/log"
Girish Gowdru0c588b22019-04-23 23:24:56 -040032 rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
manikkaraj kbf256be2019-03-25 00:13:48 +053033 "github.com/opencord/voltha-protos/go/common"
34 ic "github.com/opencord/voltha-protos/go/inter_container"
35 of "github.com/opencord/voltha-protos/go/openflow_13"
36 oop "github.com/opencord/voltha-protos/go/openolt"
manikkaraj kbf256be2019-03-25 00:13:48 +053037 "github.com/opencord/voltha-protos/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070038 "google.golang.org/grpc"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053039)
40
41//DeviceHandler will interact with the OLT device.
42type DeviceHandler struct {
cuilin20187b2a8c32019-03-26 19:52:28 -070043 deviceId string
44 deviceType string
45 device *voltha.Device
46 coreProxy *com.CoreProxy
manikkaraj kbf256be2019-03-25 00:13:48 +053047 AdapterProxy *com.AdapterProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070048 openOLT *OpenOLT
49 nniPort *voltha.Port
50 ponPort *voltha.Port
51 exitChannel chan int
52 lockDevice sync.RWMutex
manikkaraj kbf256be2019-03-25 00:13:48 +053053 Client oop.OpenoltClient
cuilin20187b2a8c32019-03-26 19:52:28 -070054 transitionMap *TransitionMap
55 clientCon *grpc.ClientConn
manikkaraj kbf256be2019-03-25 00:13:48 +053056 flowMgr *OpenOltFlowMgr
57 resourceMgr *rsrcMgr.OpenOltResourceMgr
Phaneendra Manda4c62c802019-03-06 21:37:49 +053058}
59
60//NewDeviceHandler creates a new device handler
cuilin20187b2a8c32019-03-26 19:52:28 -070061func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
62 var dh DeviceHandler
63 dh.coreProxy = cp
Girish Gowdru0c588b22019-04-23 23:24:56 -040064 dh.AdapterProxy = ap
cuilin20187b2a8c32019-03-26 19:52:28 -070065 cloned := (proto.Clone(device)).(*voltha.Device)
66 dh.deviceId = cloned.Id
67 dh.deviceType = cloned.Type
68 dh.device = cloned
69 dh.openOLT = adapter
70 dh.exitChannel = make(chan int, 1)
71 dh.lockDevice = sync.RWMutex{}
Phaneendra Manda4c62c802019-03-06 21:37:49 +053072
cuilin20187b2a8c32019-03-26 19:52:28 -070073 //TODO initialize the support classes.
74 return &dh
Phaneendra Manda4c62c802019-03-06 21:37:49 +053075}
76
77// start save the device to the data model
78func (dh *DeviceHandler) start(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -070079 dh.lockDevice.Lock()
80 defer dh.lockDevice.Unlock()
81 log.Debugw("starting-device-agent", log.Fields{"device": dh.device})
82 // Add the initial device to the local model
83 log.Debug("device-agent-started")
Phaneendra Manda4c62c802019-03-06 21:37:49 +053084}
85
86// stop stops the device dh. Not much to do for now
87func (dh *DeviceHandler) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -070088 dh.lockDevice.Lock()
89 defer dh.lockDevice.Unlock()
90 log.Debug("stopping-device-agent")
91 dh.exitChannel <- 1
92 log.Debug("device-agent-stopped")
Phaneendra Manda4c62c802019-03-06 21:37:49 +053093}
94
95func macAddressToUint32Array(mac string) []uint32 {
cuilin20187b2a8c32019-03-26 19:52:28 -070096 slist := strings.Split(mac, ":")
97 result := make([]uint32, len(slist))
98 var err error
99 var tmp int64
100 for index, val := range slist {
101 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
102 return []uint32{1, 2, 3, 4, 5, 6}
103 }
104 result[index] = uint32(tmp)
105 }
106 return result
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530107}
108
manikkaraj kbf256be2019-03-25 00:13:48 +0530109func GetportLabel(portNum uint32, portType voltha.Port_PortType) string {
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530110
Girish Gowdru0c588b22019-04-23 23:24:56 -0400111 if portType == voltha.Port_ETHERNET_NNI {
112 return fmt.Sprintf("nni-%d", portNum)
113 } else if portType == voltha.Port_PON_OLT {
114 return fmt.Sprintf("pon-%d", portNum)
cuilin20187b2a8c32019-03-26 19:52:28 -0700115 } else if portType == voltha.Port_ETHERNET_UNI {
116 log.Errorw("local UNI management not supported", log.Fields{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400117 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700118 }
119 return ""
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530120}
121
122func (dh *DeviceHandler) addPort(intfId uint32, portType voltha.Port_PortType, state string) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700123 var operStatus common.OperStatus_OperStatus
124 if state == "up" {
125 operStatus = voltha.OperStatus_ACTIVE
126 } else {
127 operStatus = voltha.OperStatus_DISCOVERED
128 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129 // portNum := IntfIdToPortNo(intfId,portType)
cuilin20187b2a8c32019-03-26 19:52:28 -0700130 portNum := intfId
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131 label := GetportLabel(portNum, portType)
132 if len(label) == 0 {
133 log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType})
134 return
135 }
136 // Now create Port
137 port := &voltha.Port{
cuilin20187b2a8c32019-03-26 19:52:28 -0700138 PortNo: portNum,
139 Label: label,
140 Type: portType,
141 OperStatus: operStatus,
142 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400143 log.Debugw("Sending port update to core", log.Fields{"port": port})
cuilin20187b2a8c32019-03-26 19:52:28 -0700144 // Synchronous call to update device - this method is run in its own go routine
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145 if err := dh.coreProxy.PortCreated(nil, dh.device.Id, port); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700146 log.Errorw("error-creating-nni-port", log.Fields{"deviceId": dh.device.Id, "error": err})
147 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530148}
149
150// readIndications to read the indications from the OLT device
151func (dh *DeviceHandler) readIndications() {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400152 indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700153 if err != nil {
154 log.Errorw("Failed to read indications", log.Fields{"err": err})
155 return
156 }
157 if indications == nil {
158 log.Errorw("Indications is nil", log.Fields{})
159 return
160 }
161 for {
162 indication, err := indications.Recv()
163 if err == io.EOF {
164 break
165 }
166 if err != nil {
167 log.Infow("Failed to read from indications", log.Fields{"err": err})
168 continue
169 }
170 switch indication.Data.(type) {
171 case *oop.Indication_OltInd:
172 oltInd := indication.GetOltInd()
173 if oltInd.OperState == "up" {
174 dh.transitionMap.Handle(DeviceUpInd)
175 } else if oltInd.OperState == "down" {
176 dh.transitionMap.Handle(DeviceDownInd)
177 }
178 case *oop.Indication_IntfInd:
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530179
cuilin20187b2a8c32019-03-26 19:52:28 -0700180 intfInd := indication.GetIntfInd()
181 go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
182 log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd})
183 case *oop.Indication_IntfOperInd:
184 intfOperInd := indication.GetIntfOperInd()
185 if intfOperInd.GetType() == "nni" {
186 go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
187 } else if intfOperInd.GetType() == "pon" {
188 // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
189 // Handle pon port update
190 }
191 log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
192 case *oop.Indication_OnuDiscInd:
193 onuDiscInd := indication.GetOnuDiscInd()
194 log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400195 //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
196 //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId())
197 // TODO Get onu ID from the resource manager
cuilin20187b2a8c32019-03-26 19:52:28 -0700198 var onuId uint32 = 1
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199 /*if err != nil{
200 log.Errorw("onu-id-unavailable",log.Fields{"intfId":onuDiscInd.GetIntfId()})
201 return
202 }*/
manikkaraj kbf256be2019-03-25 00:13:48 +0530203
cuilin20187b2a8c32019-03-26 19:52:28 -0700204 sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400205 //FIXME: Duplicate child devices being create in go routine
206 dh.onuDiscIndication(onuDiscInd, onuId, sn)
cuilin20187b2a8c32019-03-26 19:52:28 -0700207 case *oop.Indication_OnuInd:
208 onuInd := indication.GetOnuInd()
209 log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
210 go dh.onuIndication(onuInd)
211 case *oop.Indication_OmciInd:
212 omciInd := indication.GetOmciInd()
213 log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd})
214 if err := dh.omciIndication(omciInd); err != nil {
215 log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd})
216 }
217 case *oop.Indication_PktInd:
218 pktInd := indication.GetPktInd()
219 log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
220 case *oop.Indication_PortStats:
221 portStats := indication.GetPortStats()
222 log.Infow("Received port stats indication", log.Fields{"PortStats": portStats})
223 case *oop.Indication_FlowStats:
224 flowStats := indication.GetFlowStats()
225 log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats})
226 case *oop.Indication_AlarmInd:
227 alarmInd := indication.GetAlarmInd()
228 log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd})
229 }
230 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530231}
232
233// doStateUp handle the olt up indication and update to voltha core
234func (dh *DeviceHandler) doStateUp() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235 // Synchronous call to update device state - this method is run in its own go routine
cuilin20187b2a8c32019-03-26 19:52:28 -0700236 if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237 voltha.OperStatus_ACTIVE); err != nil {
238 log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceId": dh.device.Id, "error": err})
239 return err
240 }
241 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530242}
243
244// doStateDown handle the olt down indication
245func (dh *DeviceHandler) doStateDown() error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700246 //TODO Handle oper state down
247 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530248}
249
250// doStateInit dial the grpc before going to init state
251func (dh *DeviceHandler) doStateInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400252 var err error
253 dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure())
254 if err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700255 log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceId, "HostAndPort": dh.device.GetHostAndPort(), "err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400256 return err
257 }
258 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530259}
260
261// postInit create olt client instance to invoke RPC on the olt device
262func (dh *DeviceHandler) postInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400263 dh.Client = oop.NewOpenoltClient(dh.clientCon)
264 dh.transitionMap.Handle(GrpcConnected)
265 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530266}
267
268// doStateConnected get the device info and update to voltha core
269func (dh *DeviceHandler) doStateConnected() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400270 log.Debug("OLT device has been connected")
271 deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700272 if err != nil {
273 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
274 return err
275 }
276 if deviceInfo == nil {
277 log.Errorw("Device info is nil", log.Fields{})
278 return errors.New("Failed to get device info from OLT")
279 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530280
cuilin20187b2a8c32019-03-26 19:52:28 -0700281 dh.device.Root = true
282 dh.device.Vendor = deviceInfo.Vendor
283 dh.device.Model = deviceInfo.Model
284 dh.device.ConnectStatus = voltha.ConnectStatus_REACHABLE
285 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
286 dh.device.HardwareVersion = deviceInfo.HardwareVersion
287 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
288 // TODO : Check whether this MAC address is learnt from SDPON or need to send from device
289 dh.device.MacAddress = "0a:0b:0c:0d:0e:0f"
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530290
cuilin20187b2a8c32019-03-26 19:52:28 -0700291 // Synchronous call to update device - this method is run in its own go routine
292 if err := dh.coreProxy.DeviceUpdate(nil, dh.device); err != nil {
293 log.Errorw("error-updating-device", log.Fields{"deviceId": dh.device.Id, "error": err})
294 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400295 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
296 // Instantiate resource manager
Abhilash S.L8ee90712019-04-29 16:24:22 +0530297 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceId, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400298 log.Error("Error while instantiating resource manager")
299 return errors.New("Instantiating resource manager failed")
300 }
301 // Instantiate flow manager
302 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
303 log.Error("Error while instantiating flow manager")
304 return errors.New("Instantiating flow manager failed")
305 }
306 /* TODO: Instantiate Alarm , stats , BW managers */
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530307
cuilin20187b2a8c32019-03-26 19:52:28 -0700308 // Start reading indications
309 go dh.readIndications()
310 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530311}
312
313// AdoptDevice adopts the OLT device
314func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400315 dh.transitionMap = NewTransitionMap(dh)
cuilin20187b2a8c32019-03-26 19:52:28 -0700316 log.Infow("AdoptDevice", log.Fields{"deviceId": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400317 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530318}
319
320// GetOfpDeviceInfo Get the Ofp device information
321func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700322 return &ic.SwitchCapability{
323 Desc: &of.OfpDesc{
324 HwDesc: "open_pon",
325 SwDesc: "open_pon",
326 SerialNum: dh.device.SerialNumber,
327 },
328 SwitchFeatures: &of.OfpSwitchFeatures{
329 NBuffers: 256,
330 NTables: 2,
331 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
332 of.OfpCapabilities_OFPC_TABLE_STATS |
333 of.OfpCapabilities_OFPC_PORT_STATS |
334 of.OfpCapabilities_OFPC_GROUP_STATS),
335 },
336 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530337}
338
339// GetOfpPortInfo Get Ofp port information
340func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700341 cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
342 return &ic.PortCapability{
343 Port: &voltha.LogicalPort{
344 OfpPort: &of.OfpPort{
345 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
346 Config: 0,
347 State: uint32(of.OfpPortState_OFPPS_LIVE),
348 Curr: cap,
349 Advertised: cap,
350 Peer: cap,
351 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
352 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
353 },
354 DeviceId: dh.device.Id,
355 DevicePortNo: uint32(portNo),
356 },
357 }, nil
358}
359
360func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
361 log.Debugw("omci indication", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
362
Girish Gowdru0c588b22019-04-23 23:24:56 -0400363 // ponPort := IntfIdToPortNo(omciInd.GetIntfId(),voltha.Port_PON_OLT)
cuilin20187b2a8c32019-03-26 19:52:28 -0700364 kwargs := make(map[string]interface{})
365 kwargs["onu_id"] = omciInd.OnuId
366 kwargs["parent_port_no"] = omciInd.GetIntfId()
367
368 if onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); err != nil {
369 log.Errorw("onu not found", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
370 return err
371 } else {
372 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
manikkaraj kbf256be2019-03-25 00:13:48 +0530373 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
cuilin20187b2a8c32019-03-26 19:52:28 -0700374 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, onuDevice.Type,
375 onuDevice.Id, onuDevice.ProxyAddress.DeviceId, ""); sendErr != nil {
376 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": onuDevice.Type, "onuId": onuDevice.Id, "proxyDeviceId": onuDevice.ProxyAddress.DeviceId})
377 return sendErr
378 }
379 return nil
380 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530381}
382
383// Process_inter_adapter_message process inter adater message
384func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700385 // TODO
386 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
387 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
388 msgId := msg.Header.Id
389 fromTopic := msg.Header.FromTopic
390 toTopic := msg.Header.ToTopic
391 toDeviceId := msg.Header.ToDeviceId
392 proxyDeviceId := msg.Header.ProxyDeviceId
393
394 log.Debugw("omci request message header", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
395
396 msgBody := msg.GetBody()
397
398 omciMsg := &ic.InterAdapterOmciMessage{}
399 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
400 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
401 return err
402 }
403
404 if onuDevice, err := dh.coreProxy.GetDevice(nil, dh.device.Id, toDeviceId); err != nil {
405 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceId, "error": err})
406 return err
407 } else {
408 dh.sendProxiedMessage(onuDevice, omciMsg)
409 }
410
411 } else {
412 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
413 }
414 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530415}
416
cuilin20187b2a8c32019-03-26 19:52:28 -0700417func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
418 if onuDevice.ConnectStatus != voltha.ConnectStatus_REACHABLE {
419 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"serialNumber": onuDevice.SerialNumber, "intfId": onuDevice.ProxyAddress.GetChannelId(), "onuId": onuDevice.ProxyAddress.GetOnuId()})
420 return
421 }
422
423 omciMessage := &oop.OmciMsg{IntfId: onuDevice.ProxyAddress.GetChannelId(), OnuId: onuDevice.ProxyAddress.GetOnuId(), Pkt: omciMsg.Message}
424
manikkaraj kbf256be2019-03-25 00:13:48 +0530425 dh.Client.OmciMsgOut(context.Background(), omciMessage)
cuilin20187b2a8c32019-03-26 19:52:28 -0700426 log.Debugw("omci-message-sent", log.Fields{"serialNumber": onuDevice.SerialNumber, "intfId": onuDevice.ProxyAddress.GetChannelId(), "omciMsg": string(omciMsg.Message)})
427}
428
429func (dh *DeviceHandler) activateONU(intfId uint32, onuId int64, serialNum *oop.SerialNumber, serialNumber string) {
430 log.Debugw("activate-onu", log.Fields{"intfId": intfId, "onuId": onuId, "serialNum": serialNum, "serialNumber": serialNumber})
431 // TODO: need resource manager
432 var pir uint32 = 1000000
433 Onu := oop.Onu{IntfId: intfId, OnuId: uint32(onuId), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530434 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
cuilin20187b2a8c32019-03-26 19:52:28 -0700435 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu})
436 } else {
437 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
438 }
439}
440
441func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, onuId uint32, sn string) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400442 //channelId := MkUniPortNum(onuDiscInd.GetIntfId(), onuId, uint32(0))
443 //parentPortNo := IntfIdToPortNo(onuDiscInd.GetIntfId(),voltha.Port_PON_OLT)
444 channelId := onuDiscInd.GetIntfId()
445 parentPortNo := onuDiscInd.GetIntfId()
manikkaraj kbf256be2019-03-25 00:13:48 +0530446 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 {
cuilin20187b2a8c32019-03-26 19:52:28 -0700447 log.Errorw("Create onu error", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn, "error": err})
448 return err
449 }
450
451 kwargs := make(map[string]interface{})
452 kwargs["onu_id"] = onuId
453 kwargs["parent_port_no"] = onuDiscInd.GetIntfId()
454
455 for i := 0; i < 10; i++ {
456 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
457 dh.activateONU(onuDiscInd.IntfId, int64(onuId), onuDiscInd.SerialNumber, sn)
458 return nil
459 } else {
460 time.Sleep(1 * time.Second)
461 log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1)
462 }
463 }
464 log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn})
465 return errors.New("Failed to activate onu")
466}
467
468func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
469 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
470
471 kwargs := make(map[string]interface{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400472 // ponPort := IntfIdToPortNo(onuInd.GetIntfId(),voltha.Port_PON_OLT)
manikkaraj kbf256be2019-03-25 00:13:48 +0530473
cuilin20187b2a8c32019-03-26 19:52:28 -0700474 if serialNumber != "" {
475 kwargs["serial_number"] = serialNumber
476 } else {
477 kwargs["onu_id"] = onuInd.OnuId
478 kwargs["parent_port_no"] = onuInd.GetIntfId()
479 }
480 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
481 //if intfIdFromPortNo(onuDevice.ParentPortNo) != onuInd.GetIntfId() {
482 if onuDevice.ParentPortNo != onuInd.GetIntfId() {
483 //log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": intfIdFromPortNo(onuDevice.ParentPortNo), "currentIntfId": onuInd.GetIntfId()})
484 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": onuInd.GetIntfId()})
485 }
486
487 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
488 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})
489 }
490
491 // adminState
492 if onuInd.AdminState == "down" {
493 if onuInd.OperState != "down" {
494 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
495 // Forcing the oper state change code to execute
496 onuInd.OperState = "down"
497 }
498 // Port and logical port update is taken care of by oper state block
499 } else if onuInd.AdminState == "up" {
500 log.Debugln("received-onu-admin-state up")
501 } else {
502 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
503 }
504 log.Debugln("admin-state-dealt-with")
505
506 // operState
507 if onuInd.OperState == "down" {
508 if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE {
509 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, onuDevice.OperStatus)
510 log.Debugln("onu-oper-state-is-down")
511 }
512 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
513 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, common.OperStatus_DISCOVERED)
514 }
515 log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd})
516
517 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
manikkaraj kbf256be2019-03-25 00:13:48 +0530518 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700519 } else if onuInd.OperState == "up" {
520 if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE {
521 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus)
522
523 }
524 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
525 log.Warnw("ignore onu indication", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState})
526 return
527 }
manikkaraj kbf256be2019-03-25 00:13:48 +0530528 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700529 } else {
530 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
531 }
532 } else {
533 log.Errorw("onu not found", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId})
534 return
535 }
536
537}
538
539func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
540 if serialNum != nil {
541 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
542 } else {
543 return ""
544 }
545}
546
547func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
548 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
549 fmt.Sprintf("%x", (uint32(vendorSpecific[0]&0x0f))) +
550 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
551 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
552 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
553 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
554 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
555 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
556 return tmp
557}
558
559// flows
560func (dh *DeviceHandler) Update_flows_bulk() error {
561 return errors.New("UnImplemented")
562}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400563func (dh *DeviceHandler) GetChildDevice(parentPort uint32, onuId uint32) *voltha.Device {
564 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuId": onuId})
565 kwargs := make(map[string]interface{})
566 kwargs["onu_id"] = onuId
567 kwargs["parent_port_no"] = parentPort
568 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
569 if err != nil {
570 log.Errorw("onu not found", log.Fields{"intfId": parentPort, "onuId": onuId})
571 return nil
572 }
573 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
574 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530575}
576
577func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400578 log.Debugw("In UpdateFlowsIncrementally", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups})
579 if flows != nil {
580 for _, flow := range flows.ToAdd.Items {
581 dh.flowMgr.AddFlow(flow)
582 }
583 }
584 if groups != nil {
585 for _, flow := range flows.ToRemove.Items {
586 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
587 // dh.flowMgr.RemoveFlow(flow)
588 }
589 }
590 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530591}