blob: f397d213a5a5eb98d19372471af8dad82e292084 [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")
347 deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700348 if err != nil {
349 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
350 return err
351 }
352 if deviceInfo == nil {
353 log.Errorw("Device info is nil", log.Fields{})
354 return errors.New("Failed to get device info from OLT")
355 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400356 log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo})
cuilin20187b2a8c32019-03-26 19:52:28 -0700357 dh.device.Root = true
358 dh.device.Vendor = deviceInfo.Vendor
359 dh.device.Model = deviceInfo.Model
360 dh.device.ConnectStatus = voltha.ConnectStatus_REACHABLE
361 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
362 dh.device.HardwareVersion = deviceInfo.HardwareVersion
363 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400364 // FIXME: Remove Hardcodings
cuilin20187b2a8c32019-03-26 19:52:28 -0700365 dh.device.MacAddress = "0a:0b:0c:0d:0e:0f"
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530366
cuilin20187b2a8c32019-03-26 19:52:28 -0700367 // Synchronous call to update device - this method is run in its own go routine
368 if err := dh.coreProxy.DeviceUpdate(nil, dh.device); err != nil {
369 log.Errorw("error-updating-device", log.Fields{"deviceId": dh.device.Id, "error": err})
370 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400371
372 device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
373 if err != nil || device == nil {
374 /*TODO: needs to handle error scenarios */
375 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
376 }
377 cloned := proto.Clone(device).(*voltha.Device)
378 // Update the all ports (if available) on that device to ACTIVE.
379 // The ports do not normally exist, unless the device is coming back from a reboot
380 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
381 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
382 return err
383 }
384
Girish Gowdru0c588b22019-04-23 23:24:56 -0400385 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
386 // Instantiate resource manager
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400387 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceId, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400388 log.Error("Error while instantiating resource manager")
389 return errors.New("Instantiating resource manager failed")
390 }
391 // Instantiate flow manager
392 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
393 log.Error("Error while instantiating flow manager")
394 return errors.New("Instantiating flow manager failed")
395 }
396 /* TODO: Instantiate Alarm , stats , BW managers */
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530397
cuilin20187b2a8c32019-03-26 19:52:28 -0700398 // Start reading indications
399 go dh.readIndications()
400 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530401}
402
403// AdoptDevice adopts the OLT device
404func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400405 dh.transitionMap = NewTransitionMap(dh)
cuilin20187b2a8c32019-03-26 19:52:28 -0700406 log.Infow("AdoptDevice", log.Fields{"deviceId": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400407 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530408}
409
410// GetOfpDeviceInfo Get the Ofp device information
411func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700412 return &ic.SwitchCapability{
413 Desc: &of.OfpDesc{
414 HwDesc: "open_pon",
415 SwDesc: "open_pon",
416 SerialNum: dh.device.SerialNumber,
417 },
418 SwitchFeatures: &of.OfpSwitchFeatures{
419 NBuffers: 256,
420 NTables: 2,
421 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
422 of.OfpCapabilities_OFPC_TABLE_STATS |
423 of.OfpCapabilities_OFPC_PORT_STATS |
424 of.OfpCapabilities_OFPC_GROUP_STATS),
425 },
426 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530427}
428
429// GetOfpPortInfo Get Ofp port information
430func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700431 cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
432 return &ic.PortCapability{
433 Port: &voltha.LogicalPort{
434 OfpPort: &of.OfpPort{
435 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
436 Config: 0,
437 State: uint32(of.OfpPortState_OFPPS_LIVE),
438 Curr: cap,
439 Advertised: cap,
440 Peer: cap,
441 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
442 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
443 },
444 DeviceId: dh.device.Id,
445 DevicePortNo: uint32(portNo),
446 },
447 }, nil
448}
449
450func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
451 log.Debugw("omci indication", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
452
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400453 ponPort := IntfIdToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
cuilin20187b2a8c32019-03-26 19:52:28 -0700454 kwargs := make(map[string]interface{})
455 kwargs["onu_id"] = omciInd.OnuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400456 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700457
458 if onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); err != nil {
459 log.Errorw("onu not found", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId})
460 return err
461 } else {
462 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
manikkaraj kbf256be2019-03-25 00:13:48 +0530463 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
cuilin20187b2a8c32019-03-26 19:52:28 -0700464 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, onuDevice.Type,
465 onuDevice.Id, onuDevice.ProxyAddress.DeviceId, ""); sendErr != nil {
466 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": onuDevice.Type, "onuId": onuDevice.Id, "proxyDeviceId": onuDevice.ProxyAddress.DeviceId})
467 return sendErr
468 }
469 return nil
470 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530471}
472
473// Process_inter_adapter_message process inter adater message
474func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700475 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
476 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
477 msgId := msg.Header.Id
478 fromTopic := msg.Header.FromTopic
479 toTopic := msg.Header.ToTopic
480 toDeviceId := msg.Header.ToDeviceId
481 proxyDeviceId := msg.Header.ProxyDeviceId
482
483 log.Debugw("omci request message header", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId})
484
485 msgBody := msg.GetBody()
486
487 omciMsg := &ic.InterAdapterOmciMessage{}
488 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
489 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
490 return err
491 }
492
493 if onuDevice, err := dh.coreProxy.GetDevice(nil, dh.device.Id, toDeviceId); err != nil {
494 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceId, "error": err})
495 return err
496 } else {
497 dh.sendProxiedMessage(onuDevice, omciMsg)
498 }
499
500 } else {
501 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
502 }
503 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530504}
505
cuilin20187b2a8c32019-03-26 19:52:28 -0700506func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
507 if onuDevice.ConnectStatus != voltha.ConnectStatus_REACHABLE {
508 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"serialNumber": onuDevice.SerialNumber, "intfId": onuDevice.ProxyAddress.GetChannelId(), "onuId": onuDevice.ProxyAddress.GetOnuId()})
509 return
510 }
511
512 omciMessage := &oop.OmciMsg{IntfId: onuDevice.ProxyAddress.GetChannelId(), OnuId: onuDevice.ProxyAddress.GetOnuId(), Pkt: omciMsg.Message}
513
manikkaraj kbf256be2019-03-25 00:13:48 +0530514 dh.Client.OmciMsgOut(context.Background(), omciMessage)
cuilin20187b2a8c32019-03-26 19:52:28 -0700515 log.Debugw("omci-message-sent", log.Fields{"serialNumber": onuDevice.SerialNumber, "intfId": onuDevice.ProxyAddress.GetChannelId(), "omciMsg": string(omciMsg.Message)})
516}
517
518func (dh *DeviceHandler) activateONU(intfId uint32, onuId int64, serialNum *oop.SerialNumber, serialNumber string) {
519 log.Debugw("activate-onu", log.Fields{"intfId": intfId, "onuId": onuId, "serialNum": serialNum, "serialNumber": serialNumber})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400520 dh.flowMgr.UpdateOnuInfo(intfId, uint32(onuId), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700521 // TODO: need resource manager
522 var pir uint32 = 1000000
523 Onu := oop.Onu{IntfId: intfId, OnuId: uint32(onuId), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530524 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400525 st, _ := status.FromError(err)
526 if st.Code() == codes.AlreadyExists {
527 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
528 } else {
529 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
530 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700531 } else {
532 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
533 }
534}
535
536func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, onuId uint32, sn string) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400537 channelId := onuDiscInd.GetIntfId()
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400538 parentPortNo := IntfIdToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400539 if _, ok := dh.discOnus[sn]; ok {
540 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
541 return nil
cuilin20187b2a8c32019-03-26 19:52:28 -0700542 }
543
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400544 dh.lockDevice.Lock()
545 dh.discOnus[sn] = true
546 dh.lockDevice.Unlock()
547 // evict the onu serial number from local cache
548 defer func() {
549 delete(dh.discOnus, sn)
550 }()
551
cuilin20187b2a8c32019-03-26 19:52:28 -0700552 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400553 if sn != "" {
554 kwargs["serial_number"] = sn
555 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700556 kwargs["onu_id"] = onuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400557 kwargs["parent_port_no"] = parentPortNo
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400558 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
559 if onuDevice == nil {
560 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 {
561 log.Errorw("Create onu error", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn, "error": err})
562 return err
563 }
564 }
565 onuDevice, err = dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
566 if err != nil {
567 log.Errorw("failed to get ONU device information", log.Fields{"err": err})
568 return err
569 }
570 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
571 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700572
573 for i := 0; i < 10; i++ {
574 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
575 dh.activateONU(onuDiscInd.IntfId, int64(onuId), onuDiscInd.SerialNumber, sn)
576 return nil
577 } else {
578 time.Sleep(1 * time.Second)
579 log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1)
580 }
581 }
582 log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn})
583 return errors.New("Failed to activate onu")
584}
585
586func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
587 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
588
589 kwargs := make(map[string]interface{})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400590 ponPort := IntfIdToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
manikkaraj kbf256be2019-03-25 00:13:48 +0530591
cuilin20187b2a8c32019-03-26 19:52:28 -0700592 if serialNumber != "" {
593 kwargs["serial_number"] = serialNumber
594 } else {
595 kwargs["onu_id"] = onuInd.OnuId
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400596 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700597 }
598 if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400599 if onuDevice.ParentPortNo != ponPort {
cuilin20187b2a8c32019-03-26 19:52:28 -0700600 //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 -0400601 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700602 }
603
604 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
605 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})
606 }
607
608 // adminState
609 if onuInd.AdminState == "down" {
610 if onuInd.OperState != "down" {
611 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
612 // Forcing the oper state change code to execute
613 onuInd.OperState = "down"
614 }
615 // Port and logical port update is taken care of by oper state block
616 } else if onuInd.AdminState == "up" {
617 log.Debugln("received-onu-admin-state up")
618 } else {
619 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
620 }
621 log.Debugln("admin-state-dealt-with")
622
623 // operState
624 if onuInd.OperState == "down" {
625 if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE {
626 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, onuDevice.OperStatus)
627 log.Debugln("onu-oper-state-is-down")
628 }
629 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
630 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, common.OperStatus_DISCOVERED)
631 }
632 log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd})
633
634 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
manikkaraj kbf256be2019-03-25 00:13:48 +0530635 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700636 } else if onuInd.OperState == "up" {
637 if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE {
638 dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus)
639
640 }
641 if onuDevice.OperStatus != common.OperStatus_DISCOVERED {
642 log.Warnw("ignore onu indication", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState})
643 return
644 }
manikkaraj kbf256be2019-03-25 00:13:48 +0530645 dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
cuilin20187b2a8c32019-03-26 19:52:28 -0700646 } else {
647 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
648 }
649 } else {
650 log.Errorw("onu not found", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId})
651 return
652 }
653
654}
655
656func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
657 if serialNum != nil {
658 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
659 } else {
660 return ""
661 }
662}
663
664func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
665 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
666 fmt.Sprintf("%x", (uint32(vendorSpecific[0]&0x0f))) +
667 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
668 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
669 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
670 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
671 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
672 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
673 return tmp
674}
675
676// flows
677func (dh *DeviceHandler) Update_flows_bulk() error {
678 return errors.New("UnImplemented")
679}
Girish Gowdru0c588b22019-04-23 23:24:56 -0400680func (dh *DeviceHandler) GetChildDevice(parentPort uint32, onuId uint32) *voltha.Device {
681 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuId": onuId})
682 kwargs := make(map[string]interface{})
683 kwargs["onu_id"] = onuId
684 kwargs["parent_port_no"] = parentPort
685 onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs)
686 if err != nil {
687 log.Errorw("onu not found", log.Fields{"intfId": parentPort, "onuId": onuId})
688 return nil
689 }
690 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
691 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530692}
693
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400694func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
695 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
696 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPort, packetPayload); err != nil {
697 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
698 return
699 }
700 log.Debug("Sent packet-in to core successfully")
701}
702
manikkaraj kbf256be2019-03-25 00:13:48 +0530703func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400704 log.Debugw("In UpdateFlowsIncrementally", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups})
705 if flows != nil {
706 for _, flow := range flows.ToAdd.Items {
707 dh.flowMgr.AddFlow(flow)
708 }
709 }
710 if groups != nil {
711 for _, flow := range flows.ToRemove.Items {
712 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
713 // dh.flowMgr.RemoveFlow(flow)
714 }
715 }
716 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530717}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400718
719func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
720 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
721 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
722 return err
723 }
724 dh.lockDevice.Lock()
725 dh.adminState = "down"
726 dh.lockDevice.Unlock()
727 log.Debug("olt-disabled")
728
729 cloned := proto.Clone(device).(*voltha.Device)
730 // Update the all ports state on that device to disable
731 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
732 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
733 return err
734 }
735
736 //Update the device oper state
737 cloned.OperStatus = voltha.OperStatus_UNKNOWN
738 dh.device = cloned
739
740 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
741 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
742 return err
743 }
744 log.Debugw("DisableDevice-end", log.Fields{"deviceId": device.Id})
745 return nil
746}
747
748func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
749 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
750 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
751 return err
752 }
753
754 dh.lockDevice.Lock()
755 dh.adminState = "up"
756 dh.lockDevice.Unlock()
757 log.Debug("olt-reenabled")
758
759 cloned := proto.Clone(device).(*voltha.Device)
760 // Update the all ports state on that device to enable
761 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
762 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
763 return err
764 }
765
766 //Update the device oper status as ACTIVE
767 cloned.OperStatus = voltha.OperStatus_ACTIVE
768 dh.device = cloned
769
770 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
771 log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err})
772 return err
773 }
774 log.Debugw("ReEnableDevice-end", log.Fields{"deviceId": device.Id})
775
776 return nil
777}
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400778
779func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
780 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
781 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
782 if err != nil {
783 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
784 return
785 }
786 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
787 if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
788 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
789 return
790 }
791 log.Debug("Success sending packet-in to core!")
792}
793
794func (dh *DeviceHandler) PacketOut(egress_port_no int, packet *of.OfpPacketOut) error {
795 log.Debugw("PacketOut", log.Fields{"deviceId": dh.deviceId, "egress_port_no": egress_port_no, "pkt-length": len(packet.Data)})
796 var etherFrame ethernet.Frame
797 err := (&etherFrame).UnmarshalBinary(packet.Data)
798 if err != nil {
799 log.Errorw("Failed to unmarshal into ethernet frame", log.Fields{"err": err, "pkt-length": len(packet.Data)})
800 return err
801 }
802 log.Debugw("Ethernet Frame", log.Fields{"Frame": etherFrame})
803 egressPortType := IntfIdToPortTypeName(uint32(egress_port_no))
804 if egressPortType == voltha.Port_ETHERNET_UNI {
805 if etherFrame.VLAN != nil { // If double tag, remove the outer tag
806 nextEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
807 if nextEthType == 0x8100 {
808 etherFrame.VLAN = nil
809 packet.Data, err = etherFrame.MarshalBinary()
810 if err != nil {
811 log.Fatalf("failed to marshal frame: %v", err)
812 return err
813 }
814 if err := (&etherFrame).UnmarshalBinary(packet.Data); err != nil {
815 log.Fatalf("failed to unmarshal frame: %v", err)
816 return err
817 }
818 log.Debug("Double tagged packet , removed outer vlan", log.Fields{"New frame": etherFrame})
819 }
820 }
821 intfId := IntfIdFromUniPortNum(uint32(egress_port_no))
822 onuId := OnuIdFromPortNum(uint32(egress_port_no))
823 uniId := UniIdFromPortNum(uint32(egress_port_no))
824 /*gemPortId, err := dh.flowMgr.GetPacketOutGemPortId(intfId, onuId, uint32(egress_port_no))
825 if err != nil{
826 log.Errorw("Error while getting gemport to packet-out",log.Fields{"error": err})
827 return err
828 }*/
829 onuPkt := oop.OnuPacket{IntfId: intfId, OnuId: onuId, PortNo: uint32(egress_port_no), Pkt: packet.Data}
830 log.Debug("sending-packet-to-ONU", log.Fields{"egress_port_no": egress_port_no, "IntfId": intfId, "onuId": onuId,
831 "uniId": uniId, "packet": packet.Data})
832 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
833 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
834 return err
835 }
836 } else if egressPortType == voltha.Port_ETHERNET_NNI {
837 uplinkPkt := oop.UplinkPacket{IntfId: IntfIdFromNniPortNum(uint32(egress_port_no)), Pkt: packet.Data}
838 log.Debug("sending-packet-to-uplink", log.Fields{"uplink_pkt": uplinkPkt})
839 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
840 log.Errorw("Error while sending packet-out to uplink", log.Fields{"error": err})
841 return err
842 }
843 } else {
844 log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egress_port_no, "egressPortType": egressPortType})
845 }
846 return nil
847}