blob: e00f626dd56695697a7f4e1f2d69f5a1b04ced6d [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 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070016
17//Package adaptercore provides the utility for olt devices, flows and statistics
Phaneendra Manda4c62c802019-03-06 21:37:49 +053018package adaptercore
19
20import (
cuilin20187b2a8c32019-03-26 19:52:28 -070021 "context"
Matt Jeanneret1359c732019-08-01 21:40:02 -040022 "encoding/hex"
cuilin20187b2a8c32019-03-26 19:52:28 -070023 "errors"
24 "fmt"
25 "io"
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -040026 "net"
cuilin20187b2a8c32019-03-26 19:52:28 -070027 "strconv"
28 "strings"
29 "sync"
30 "time"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053031
Chaitrashree G Sb2b62dd2019-07-24 21:47:04 -040032 "google.golang.org/grpc/codes"
33
cuilin20187b2a8c32019-03-26 19:52:28 -070034 "github.com/gogo/protobuf/proto"
35 "github.com/golang/protobuf/ptypes"
36 com "github.com/opencord/voltha-go/adapters/common"
37 "github.com/opencord/voltha-go/common/log"
Girish Gowdru0c588b22019-04-23 23:24:56 -040038 rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
manikkaraj kbf256be2019-03-25 00:13:48 +053039 "github.com/opencord/voltha-protos/go/common"
40 ic "github.com/opencord/voltha-protos/go/inter_container"
41 of "github.com/opencord/voltha-protos/go/openflow_13"
42 oop "github.com/opencord/voltha-protos/go/openolt"
manikkaraj kbf256be2019-03-25 00:13:48 +053043 "github.com/opencord/voltha-protos/go/voltha"
cuilin20187b2a8c32019-03-26 19:52:28 -070044 "google.golang.org/grpc"
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040045 "google.golang.org/grpc/status"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053046)
47
48//DeviceHandler will interact with the OLT device.
49type DeviceHandler struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070050 deviceID string
cuilin20187b2a8c32019-03-26 19:52:28 -070051 deviceType string
Girish Gowdru5ba46c92019-04-25 05:00:05 -040052 adminState string
cuilin20187b2a8c32019-03-26 19:52:28 -070053 device *voltha.Device
54 coreProxy *com.CoreProxy
manikkaraj kbf256be2019-03-25 00:13:48 +053055 AdapterProxy *com.AdapterProxy
Devmalya Paulfb990a52019-07-09 10:01:49 -040056 EventProxy *com.EventProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070057 openOLT *OpenOLT
cuilin20187b2a8c32019-03-26 19:52:28 -070058 exitChannel chan int
59 lockDevice sync.RWMutex
manikkaraj kbf256be2019-03-25 00:13:48 +053060 Client oop.OpenoltClient
cuilin20187b2a8c32019-03-26 19:52:28 -070061 transitionMap *TransitionMap
62 clientCon *grpc.ClientConn
manikkaraj kbf256be2019-03-25 00:13:48 +053063 flowMgr *OpenOltFlowMgr
Devmalya Paulfb990a52019-07-09 10:01:49 -040064 eventMgr *OpenOltEventMgr
manikkaraj kbf256be2019-03-25 00:13:48 +053065 resourceMgr *rsrcMgr.OpenOltResourceMgr
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040066 discOnus map[string]bool
Mahir Gunyela3f9add2019-06-06 15:13:19 -070067 onus map[string]*OnuDevice
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070068 nniIntfID int
Mahir Gunyela3f9add2019-06-06 15:13:19 -070069}
70
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070071//OnuDevice represents ONU related info
Mahir Gunyela3f9add2019-06-06 15:13:19 -070072type OnuDevice struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070073 deviceID string
Mahir Gunyela3f9add2019-06-06 15:13:19 -070074 deviceType string
75 serialNumber string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070076 onuID uint32
77 intfID uint32
78 proxyDeviceID string
Mahir Gunyela3f9add2019-06-06 15:13:19 -070079}
80
81//NewOnuDevice creates a new Onu Device
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070082func NewOnuDevice(devID, deviceTp, serialNum string, onuID, intfID uint32, proxyDevID string) *OnuDevice {
Mahir Gunyela3f9add2019-06-06 15:13:19 -070083 var device OnuDevice
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070084 device.deviceID = devID
Mahir Gunyela3f9add2019-06-06 15:13:19 -070085 device.deviceType = deviceTp
86 device.serialNumber = serialNum
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070087 device.onuID = onuID
88 device.intfID = intfID
89 device.proxyDeviceID = proxyDevID
Mahir Gunyela3f9add2019-06-06 15:13:19 -070090 return &device
Phaneendra Manda4c62c802019-03-06 21:37:49 +053091}
92
93//NewDeviceHandler creates a new device handler
Devmalya Paulfb990a52019-07-09 10:01:49 -040094func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, ep *com.EventProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
cuilin20187b2a8c32019-03-26 19:52:28 -070095 var dh DeviceHandler
96 dh.coreProxy = cp
Girish Gowdru0c588b22019-04-23 23:24:56 -040097 dh.AdapterProxy = ap
Devmalya Paulfb990a52019-07-09 10:01:49 -040098 dh.EventProxy = ep
cuilin20187b2a8c32019-03-26 19:52:28 -070099 cloned := (proto.Clone(device)).(*voltha.Device)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700100 dh.deviceID = cloned.Id
cuilin20187b2a8c32019-03-26 19:52:28 -0700101 dh.deviceType = cloned.Type
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400102 dh.adminState = "up"
cuilin20187b2a8c32019-03-26 19:52:28 -0700103 dh.device = cloned
104 dh.openOLT = adapter
105 dh.exitChannel = make(chan int, 1)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400106 dh.discOnus = make(map[string]bool)
cuilin20187b2a8c32019-03-26 19:52:28 -0700107 dh.lockDevice = sync.RWMutex{}
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700108 dh.onus = make(map[string]*OnuDevice)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700109 // The nniIntfID is initialized to -1 (invalid) and set to right value
Girish Gowdru1110ef22019-06-24 11:17:59 -0400110 // when the first IntfOperInd with status as "up" is received for
111 // any one of the available NNI port on the OLT device.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700112 dh.nniIntfID = -1
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530113
cuilin20187b2a8c32019-03-26 19:52:28 -0700114 //TODO initialize the support classes.
115 return &dh
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530116}
117
118// start save the device to the data model
119func (dh *DeviceHandler) start(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700120 dh.lockDevice.Lock()
121 defer dh.lockDevice.Unlock()
122 log.Debugw("starting-device-agent", log.Fields{"device": dh.device})
123 // Add the initial device to the local model
124 log.Debug("device-agent-started")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530125}
126
127// stop stops the device dh. Not much to do for now
128func (dh *DeviceHandler) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700129 dh.lockDevice.Lock()
130 defer dh.lockDevice.Unlock()
131 log.Debug("stopping-device-agent")
132 dh.exitChannel <- 1
133 log.Debug("device-agent-stopped")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530134}
135
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400136func macifyIP(ip net.IP) string {
137 if len(ip) > 0 {
138 oct1 := strconv.FormatInt(int64(ip[12]), 16)
139 oct2 := strconv.FormatInt(int64(ip[13]), 16)
140 oct3 := strconv.FormatInt(int64(ip[14]), 16)
141 oct4 := strconv.FormatInt(int64(ip[15]), 16)
142 return fmt.Sprintf("00:00:%02v:%02v:%02v:%02v", oct1, oct2, oct3, oct4)
143 }
144 return ""
145}
146
147func generateMacFromHost(host string) (string, error) {
148 var genmac string
149 var addr net.IP
150 var ips []string
151 var err error
152
153 log.Debugw("generating-mac-from-host", log.Fields{"host": host})
154
155 if addr = net.ParseIP(host); addr == nil {
156 log.Debugw("looking-up-hostname", log.Fields{"host": host})
157
158 if ips, err = net.LookupHost(host); err == nil {
159 log.Debugw("dns-result-ips", log.Fields{"ips": ips})
160 if addr = net.ParseIP(ips[0]); addr == nil {
161 log.Errorw("unable-to-parse-ip", log.Fields{"ip": ips[0]})
162 return "", errors.New("unable-to-parse-ip")
163 }
164 genmac = macifyIP(addr)
165 log.Debugw("using-ip-as-mac", log.Fields{"host": ips[0], "mac": genmac})
166 return genmac, nil
167 }
168 log.Errorw("cannot-resolve-hostname-to-ip", log.Fields{"host": host})
169 return "", err
170 }
171
172 genmac = macifyIP(addr)
173 log.Debugw("using-ip-as-mac", log.Fields{"host": host, "mac": genmac})
174 return genmac, nil
175}
176
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530177func macAddressToUint32Array(mac string) []uint32 {
cuilin20187b2a8c32019-03-26 19:52:28 -0700178 slist := strings.Split(mac, ":")
179 result := make([]uint32, len(slist))
180 var err error
181 var tmp int64
182 for index, val := range slist {
183 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
184 return []uint32{1, 2, 3, 4, 5, 6}
185 }
186 result[index] = uint32(tmp)
187 }
188 return result
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530189}
190
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700191//GetportLabel returns the label for the NNI and the PON port based on port number and port type
manikkaraj kbf256be2019-03-25 00:13:48 +0530192func GetportLabel(portNum uint32, portType voltha.Port_PortType) string {
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530193
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194 if portType == voltha.Port_ETHERNET_NNI {
195 return fmt.Sprintf("nni-%d", portNum)
196 } else if portType == voltha.Port_PON_OLT {
197 return fmt.Sprintf("pon-%d", portNum)
cuilin20187b2a8c32019-03-26 19:52:28 -0700198 } else if portType == voltha.Port_ETHERNET_UNI {
199 log.Errorw("local UNI management not supported", log.Fields{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400200 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700201 }
202 return ""
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530203}
204
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700205func (dh *DeviceHandler) addPort(intfID uint32, portType voltha.Port_PortType, state string) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700206 var operStatus common.OperStatus_OperStatus
207 if state == "up" {
208 operStatus = voltha.OperStatus_ACTIVE
209 } else {
210 operStatus = voltha.OperStatus_DISCOVERED
211 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700212 portNum := IntfIDToPortNo(intfID, portType)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400213 label := GetportLabel(portNum, portType)
214 if len(label) == 0 {
215 log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType})
216 return
217 }
218 // Now create Port
219 port := &voltha.Port{
cuilin20187b2a8c32019-03-26 19:52:28 -0700220 PortNo: portNum,
221 Label: label,
222 Type: portType,
223 OperStatus: operStatus,
224 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400225 log.Debugw("Sending port update to core", log.Fields{"port": port})
cuilin20187b2a8c32019-03-26 19:52:28 -0700226 // Synchronous call to update device - this method is run in its own go routine
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700227 if err := dh.coreProxy.PortCreated(context.TODO(), dh.device.Id, port); err != nil {
228 log.Errorw("error-creating-nni-port", log.Fields{"deviceID": dh.device.Id, "portType": portType, "error": err})
Girish Gowdru1110ef22019-06-24 11:17:59 -0400229 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700230
Girish Gowdru1110ef22019-06-24 11:17:59 -0400231 // Once we have successfully added the NNI port to the core, if the
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700232 // locally cached nniIntfID is set to invalid (-1), set it to the right value.
233 if portType == voltha.Port_ETHERNET_NNI && dh.nniIntfID == -1 {
234 dh.nniIntfID = int(intfID)
cuilin20187b2a8c32019-03-26 19:52:28 -0700235 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530236}
237
238// readIndications to read the indications from the OLT device
239func (dh *DeviceHandler) readIndications() {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400240 indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700241 if err != nil {
242 log.Errorw("Failed to read indications", log.Fields{"err": err})
243 return
244 }
245 if indications == nil {
246 log.Errorw("Indications is nil", log.Fields{})
247 return
248 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400249 /* get device state */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700250 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400251 if err != nil || device == nil {
252 /*TODO: needs to handle error scenarios */
253 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254 return
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400255 }
256 // When the device is in DISABLED and Adapter container restarts, we need to
257 // rebuild the locally maintained admin state.
258 if device.AdminState == voltha.AdminState_DISABLED {
259 dh.lockDevice.Lock()
260 dh.adminState = "down"
261 dh.lockDevice.Unlock()
262 }
263
cuilin20187b2a8c32019-03-26 19:52:28 -0700264 for {
265 indication, err := indications.Recv()
266 if err == io.EOF {
267 break
268 }
269 if err != nil {
270 log.Infow("Failed to read from indications", log.Fields{"err": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400271 dh.transitionMap.Handle(DeviceDownInd)
272 dh.transitionMap.Handle(DeviceInit)
273 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700274 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400275 // When OLT is admin down, allow only NNI operation status change indications.
276 if dh.adminState == "down" {
277 _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd)
278 if isIntfOperInd {
279 intfOperInd := indication.GetIntfOperInd()
280 if intfOperInd.GetType() == "nni" {
281 log.Infow("olt is admin down, allow nni ind", log.Fields{})
282 }
283 } else {
284 log.Infow("olt is admin down, ignore indication", log.Fields{})
285 continue
286 }
287 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530288
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700289 dh.handleIndication(indication)
manikkaraj kbf256be2019-03-25 00:13:48 +0530290
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700291 }
292}
293
294func (dh *DeviceHandler) handleOltIndication(oltIndication *oop.OltIndication) {
Daniele Rossi051466a2019-07-26 13:39:37 +0000295 raisedTs := time.Now().UnixNano()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 if oltIndication.OperState == "up" {
297 dh.transitionMap.Handle(DeviceUpInd)
298 } else if oltIndication.OperState == "down" {
299 dh.transitionMap.Handle(DeviceDownInd)
300 }
Daniele Rossi051466a2019-07-26 13:39:37 +0000301 // Send or clear Alarm
302 dh.eventMgr.oltUpDownIndication(oltIndication, dh.deviceID, raisedTs)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700303}
304
305func (dh *DeviceHandler) handleIndication(indication *oop.Indication) {
Devmalya Paulfb990a52019-07-09 10:01:49 -0400306 raisedTs := time.Now().UnixNano()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700307 switch indication.Data.(type) {
308 case *oop.Indication_OltInd:
309 dh.handleOltIndication(indication.GetOltInd())
310 case *oop.Indication_IntfInd:
311 intfInd := indication.GetIntfInd()
312 go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
313 log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd})
314 case *oop.Indication_IntfOperInd:
315 intfOperInd := indication.GetIntfOperInd()
316 if intfOperInd.GetType() == "nni" {
317 go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
318 } else if intfOperInd.GetType() == "pon" {
319 // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
320 // Handle pon port update
cuilin20187b2a8c32019-03-26 19:52:28 -0700321 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700322 log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
323 case *oop.Indication_OnuDiscInd:
324 onuDiscInd := indication.GetOnuDiscInd()
325 log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700326 sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
Matt Jeanneret53539512019-07-20 14:47:02 -0400327 go dh.onuDiscIndication(onuDiscInd, sn)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700328 case *oop.Indication_OnuInd:
329 onuInd := indication.GetOnuInd()
330 log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
331 go dh.onuIndication(onuInd)
332 case *oop.Indication_OmciInd:
333 omciInd := indication.GetOmciInd()
334 log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd})
335 if err := dh.omciIndication(omciInd); err != nil {
336 log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd})
337 }
338 case *oop.Indication_PktInd:
339 pktInd := indication.GetPktInd()
340 log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
341 go dh.handlePacketIndication(pktInd)
342 case *oop.Indication_PortStats:
343 portStats := indication.GetPortStats()
344 log.Infow("Received port stats indication", log.Fields{"PortStats": portStats})
345 case *oop.Indication_FlowStats:
346 flowStats := indication.GetFlowStats()
347 log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats})
348 case *oop.Indication_AlarmInd:
349 alarmInd := indication.GetAlarmInd()
350 log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd})
Devmalya Paulfb990a52019-07-09 10:01:49 -0400351 dh.eventMgr.ProcessEvents(alarmInd, dh.deviceID, raisedTs)
352
cuilin20187b2a8c32019-03-26 19:52:28 -0700353 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530354}
355
356// doStateUp handle the olt up indication and update to voltha core
357func (dh *DeviceHandler) doStateUp() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400358 // Synchronous call to update device state - this method is run in its own go routine
cuilin20187b2a8c32019-03-26 19:52:28 -0700359 if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400360 voltha.OperStatus_ACTIVE); err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700361 log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceID": dh.device.Id, "error": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400362 return err
363 }
364 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530365}
366
367// doStateDown handle the olt down indication
368func (dh *DeviceHandler) doStateDown() error {
Girish Gowdrud4245152019-05-10 00:47:31 -0400369 log.Debug("do-state-down-start")
370
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700371 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdrud4245152019-05-10 00:47:31 -0400372 if err != nil || device == nil {
373 /*TODO: needs to handle error scenarios */
374 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700375 return errors.New("failed to fetch device device")
Girish Gowdrud4245152019-05-10 00:47:31 -0400376 }
377
378 cloned := proto.Clone(device).(*voltha.Device)
379 // Update the all ports state on that device to disable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700380 if er := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); er != nil {
381 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": er})
382 return er
Girish Gowdrud4245152019-05-10 00:47:31 -0400383 }
384
385 //Update the device oper state and connection status
386 cloned.OperStatus = voltha.OperStatus_UNKNOWN
387 cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
388 dh.device = cloned
389
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700390 if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
391 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": er})
392 return er
Girish Gowdrud4245152019-05-10 00:47:31 -0400393 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400394
395 //get the child device for the parent device
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700396 onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400397 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398 log.Errorw("failed to get child devices information", log.Fields{"deviceID": dh.device.Id, "error": err})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400399 return err
400 }
401 for _, onuDevice := range onuDevices.Items {
402
403 // Update onu state as down in onu adapter
404 onuInd := oop.OnuIndication{}
405 onuInd.OperState = "down"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700406 er := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
407 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
408 if er != nil {
409 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
410 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
411 return er
412 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400413 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700414 log.Debugw("do-state-down-end", log.Fields{"deviceID": device.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700415 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530416}
417
418// doStateInit dial the grpc before going to init state
419func (dh *DeviceHandler) doStateInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400420 var err error
Girish Gowdrud4245152019-05-10 00:47:31 -0400421 dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock())
Girish Gowdru0c588b22019-04-23 23:24:56 -0400422 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700423 log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceID, "HostAndPort": dh.device.GetHostAndPort(), "err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400424 return err
425 }
426 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530427}
428
429// postInit create olt client instance to invoke RPC on the olt device
430func (dh *DeviceHandler) postInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400431 dh.Client = oop.NewOpenoltClient(dh.clientCon)
432 dh.transitionMap.Handle(GrpcConnected)
433 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530434}
435
436// doStateConnected get the device info and update to voltha core
437func (dh *DeviceHandler) doStateConnected() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400438 log.Debug("OLT device has been connected")
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400439
440 // Case where OLT is disabled and then rebooted.
441 if dh.adminState == "down" {
442 log.Debugln("do-state-connected--device-admin-state-down")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700443 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400444 if err != nil || device == nil {
445 /*TODO: needs to handle error scenarios */
446 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
447 }
448
449 cloned := proto.Clone(device).(*voltha.Device)
450 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
451 cloned.OperStatus = voltha.OperStatus_UNKNOWN
452 dh.device = cloned
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700453 if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
454 log.Errorw("error-updating-device-state", log.Fields{"deviceID": dh.device.Id, "error": er})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400455 }
456
457 // Since the device was disabled before the OLT was rebooted, enfore the OLT to be Disabled after re-connection.
458 _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
459 if err != nil {
460 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
461 }
462
463 // Start reading indications
464 go dh.readIndications()
465 return nil
466 }
467
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400468 deviceInfo, err := dh.populateDeviceInfo()
cuilin20187b2a8c32019-03-26 19:52:28 -0700469 if err != nil {
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400470 log.Errorw("Unable to populate Device Info", log.Fields{"err": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700471 return err
472 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400473
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700474 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdrud4245152019-05-10 00:47:31 -0400475 if err != nil || device == nil {
476 /*TODO: needs to handle error scenarios */
477 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700478 return err
Girish Gowdrud4245152019-05-10 00:47:31 -0400479 }
480 cloned := proto.Clone(device).(*voltha.Device)
481 // Update the all ports (if available) on that device to ACTIVE.
482 // The ports do not normally exist, unless the device is coming back from a reboot
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700483 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
484 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400485 return err
486 }
487
Girish Gowdru0c588b22019-04-23 23:24:56 -0400488 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
489 // Instantiate resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700490 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceID, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400491 log.Error("Error while instantiating resource manager")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700492 return errors.New("instantiating resource manager failed")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400493 }
494 // Instantiate flow manager
495 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
496 log.Error("Error while instantiating flow manager")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700497 return errors.New("instantiating flow manager failed")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400498 }
499 /* TODO: Instantiate Alarm , stats , BW managers */
Devmalya Paulfb990a52019-07-09 10:01:49 -0400500 /* Instantiating Event Manager to handle Alarms and KPIs */
501 dh.eventMgr = NewEventMgr(dh.EventProxy)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530502
cuilin20187b2a8c32019-03-26 19:52:28 -0700503 // Start reading indications
504 go dh.readIndications()
505 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530506}
507
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400508func (dh *DeviceHandler) populateDeviceInfo() (*oop.DeviceInfo, error) {
509 var err error
510 var deviceInfo *oop.DeviceInfo
511
512 deviceInfo, err = dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
513
514 if err != nil {
515 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
516 return nil, err
517 }
518 if deviceInfo == nil {
519 log.Errorw("Device info is nil", log.Fields{})
520 return nil, errors.New("failed to get device info from OLT")
521 }
522
523 log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo})
524 dh.device.Root = true
525 dh.device.Vendor = deviceInfo.Vendor
526 dh.device.Model = deviceInfo.Model
527 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
528 dh.device.HardwareVersion = deviceInfo.HardwareVersion
529 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
530
531 if deviceInfo.DeviceId == "" {
532 log.Warnw("no-device-id-provided-using-host", log.Fields{"hostport": dh.device.GetHostAndPort()})
533 host := strings.Split(dh.device.GetHostAndPort(), ":")[0]
534 genmac, err := generateMacFromHost(host)
535 if err != nil {
536 return nil, err
537 }
538 log.Debugw("using-host-for-mac-address", log.Fields{"host": host, "mac": genmac})
539 dh.device.MacAddress = genmac
540 } else {
541 dh.device.MacAddress = deviceInfo.DeviceId
542 }
543
544 // Synchronous call to update device - this method is run in its own go routine
545 if err := dh.coreProxy.DeviceUpdate(context.TODO(), dh.device); err != nil {
546 log.Errorw("error-updating-device", log.Fields{"deviceID": dh.device.Id, "error": err})
547 return nil, err
548 }
549
550 return deviceInfo, nil
551}
552
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700553//AdoptDevice adopts the OLT device
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530554func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400555 dh.transitionMap = NewTransitionMap(dh)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700556 log.Infow("Adopt_device", log.Fields{"deviceID": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400557 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530558}
559
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700560//GetOfpDeviceInfo Gets the Ofp information of the given device
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530561func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700562 return &ic.SwitchCapability{
563 Desc: &of.OfpDesc{
Devmalya Paul70dd4972019-06-10 15:19:17 +0530564 MfrDesc: "VOLTHA Project",
cuilin20187b2a8c32019-03-26 19:52:28 -0700565 HwDesc: "open_pon",
566 SwDesc: "open_pon",
567 SerialNum: dh.device.SerialNumber,
568 },
569 SwitchFeatures: &of.OfpSwitchFeatures{
570 NBuffers: 256,
571 NTables: 2,
572 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
573 of.OfpCapabilities_OFPC_TABLE_STATS |
574 of.OfpCapabilities_OFPC_PORT_STATS |
575 of.OfpCapabilities_OFPC_GROUP_STATS),
576 },
577 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530578}
579
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700580//GetOfpPortInfo Get Ofp port information
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530581func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700582 capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
cuilin20187b2a8c32019-03-26 19:52:28 -0700583 return &ic.PortCapability{
584 Port: &voltha.LogicalPort{
585 OfpPort: &of.OfpPort{
586 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
587 Config: 0,
588 State: uint32(of.OfpPortState_OFPPS_LIVE),
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700589 Curr: capacity,
590 Advertised: capacity,
591 Peer: capacity,
cuilin20187b2a8c32019-03-26 19:52:28 -0700592 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
593 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
594 },
595 DeviceId: dh.device.Id,
596 DevicePortNo: uint32(portNo),
597 },
598 }, nil
599}
600
601func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700602 log.Debugw("omci indication", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700603 var deviceType string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700604 var deviceID string
605 var proxyDeviceID string
cuilin20187b2a8c32019-03-26 19:52:28 -0700606
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700607 onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId)
608 if onuInCache, ok := dh.onus[onuKey]; !ok {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700609 log.Debugw("omci indication for a device not in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
610 ponPort := IntfIDToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700611 kwargs := make(map[string]interface{})
612 kwargs["onu_id"] = omciInd.OnuId
613 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700614
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700615 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
616 if err != nil {
617 log.Errorw("onu not found", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700618 return err
cuilin20187b2a8c32019-03-26 19:52:28 -0700619 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700620 deviceType = onuDevice.Type
621 deviceID = onuDevice.Id
622 proxyDeviceID = onuDevice.ProxyAddress.DeviceId
623 //if not exist in cache, then add to cache.
624 dh.onus[onuKey] = NewOnuDevice(deviceID, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceID)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700625 } else {
626 //found in cache
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700627 log.Debugw("omci indication for a device in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700628 deviceType = onuInCache.deviceType
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700629 deviceID = onuInCache.deviceID
630 proxyDeviceID = onuInCache.proxyDeviceID
cuilin20187b2a8c32019-03-26 19:52:28 -0700631 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700632
633 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
634 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
635 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700636 deviceID, proxyDeviceID, ""); sendErr != nil {
637 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuID": deviceID, "proxyDeviceID": proxyDeviceID})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700638 return sendErr
639 }
640 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530641}
642
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700643//ProcessInterAdapterMessage sends the proxied messages to the target device
644// If the proxy address is not found in the unmarshalled message, it first fetches the onu device for which the message
645// is meant, and then send the unmarshalled omci message to this onu
646func (dh *DeviceHandler) ProcessInterAdapterMessage(msg *ic.InterAdapterMessage) error {
647 log.Debugw("Process_inter_adapter_message", log.Fields{"msgID": msg.Header.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700648 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700649 msgID := msg.Header.Id
cuilin20187b2a8c32019-03-26 19:52:28 -0700650 fromTopic := msg.Header.FromTopic
651 toTopic := msg.Header.ToTopic
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700652 toDeviceID := msg.Header.ToDeviceId
653 proxyDeviceID := msg.Header.ProxyDeviceId
cuilin20187b2a8c32019-03-26 19:52:28 -0700654
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700655 log.Debugw("omci request message header", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
cuilin20187b2a8c32019-03-26 19:52:28 -0700656
657 msgBody := msg.GetBody()
658
659 omciMsg := &ic.InterAdapterOmciMessage{}
660 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
661 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
662 return err
663 }
664
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700665 if omciMsg.GetProxyAddress() == nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700666 onuDevice, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, toDeviceID)
667 if err != nil {
668 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceID, "error": err})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700669 return err
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700670 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700671 log.Debugw("device retrieved from core", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
672 dh.sendProxiedMessage(onuDevice, omciMsg)
673
cuilin20187b2a8c32019-03-26 19:52:28 -0700674 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700675 log.Debugw("Proxy Address found in omci message", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700676 dh.sendProxiedMessage(nil, omciMsg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700677 }
678
679 } else {
680 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
681 }
682 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530683}
684
cuilin20187b2a8c32019-03-26 19:52:28 -0700685func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700686 var intfID uint32
687 var onuID uint32
688 var connectStatus common.ConnectStatus_ConnectStatus
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700689 if onuDevice != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700690 intfID = onuDevice.ProxyAddress.GetChannelId()
691 onuID = onuDevice.ProxyAddress.GetOnuId()
692 connectStatus = onuDevice.ConnectStatus
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700693 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700694 intfID = omciMsg.GetProxyAddress().GetChannelId()
695 onuID = omciMsg.GetProxyAddress().GetOnuId()
696 connectStatus = omciMsg.GetConnectStatus()
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700697 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700698 if connectStatus != voltha.ConnectStatus_REACHABLE {
699 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfID": intfID, "onuID": onuID})
cuilin20187b2a8c32019-03-26 19:52:28 -0700700 return
701 }
702
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700703 omciMessage := &oop.OmciMsg{IntfId: intfID, OnuId: onuID, Pkt: omciMsg.Message}
cuilin20187b2a8c32019-03-26 19:52:28 -0700704
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700705 _, err := dh.Client.OmciMsgOut(context.Background(), omciMessage)
706 if err != nil {
707 log.Errorw("unable to send omci-msg-out", log.Fields{"IntfID": intfID, "OnuID": onuID, "Msg": omciMessage})
708 return
709 }
710 log.Debugw("omci-message-sent", log.Fields{"intfID": intfID, "onuID": onuID, "omciMsg": string(omciMsg.GetMessage())})
cuilin20187b2a8c32019-03-26 19:52:28 -0700711}
712
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700713func (dh *DeviceHandler) activateONU(intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) {
714 log.Debugw("activate-onu", log.Fields{"intfID": intfID, "onuID": onuID, "serialNum": serialNum, "serialNumber": serialNumber})
715 dh.flowMgr.UpdateOnuInfo(intfID, uint32(onuID), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700716 // TODO: need resource manager
717 var pir uint32 = 1000000
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700718 Onu := oop.Onu{IntfId: intfID, OnuId: uint32(onuID), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530719 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400720 st, _ := status.FromError(err)
721 if st.Code() == codes.AlreadyExists {
722 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
723 } else {
724 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
725 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700726 } else {
727 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
728 }
729}
730
Matt Jeanneret53539512019-07-20 14:47:02 -0400731func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, sn string) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700732 channelID := onuDiscInd.GetIntfId()
733 parentPortNo := IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Matt Jeanneret53539512019-07-20 14:47:02 -0400734
735 log.Debugw("new-discovery-indication", log.Fields{"sn": sn})
736 dh.lockDevice.Lock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400737 if _, ok := dh.discOnus[sn]; ok {
Matt Jeanneret53539512019-07-20 14:47:02 -0400738 dh.lockDevice.Unlock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400739 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
Matt Jeanneret53539512019-07-20 14:47:02 -0400740 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700741 }
742
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400743 dh.discOnus[sn] = true
Matt Jeanneret53539512019-07-20 14:47:02 -0400744 log.Debugw("new-discovery-indications-list", log.Fields{"discOnus": dh.discOnus})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400745 dh.lockDevice.Unlock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400746
cuilin20187b2a8c32019-03-26 19:52:28 -0700747 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400748 if sn != "" {
749 kwargs["serial_number"] = sn
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400750 } else {
Matt Jeanneret53539512019-07-20 14:47:02 -0400751 log.Errorw("invalid onu serial number", log.Fields{"sn": sn})
752 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400753 }
754
755 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
756 var onuID uint32
757 if onuDevice == nil || err != nil {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700758 //This is the first time ONU discovered. Create an OnuID for it.
Matt Jeanneret53539512019-07-20 14:47:02 -0400759 ponintfid := onuDiscInd.GetIntfId()
760 dh.lockDevice.Lock()
761 onuID, err = dh.resourceMgr.GetONUID(ponintfid)
762 dh.lockDevice.Unlock()
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400763 if err != nil {
Matt Jeanneret53539512019-07-20 14:47:02 -0400764 log.Errorw("failed to fetch onuID from resource manager", log.Fields{"pon-intf-id": ponintfid, "err": err})
765 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400766 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700767 if onuDevice, err = dh.coreProxy.ChildDeviceDetected(context.TODO(), dh.device.Id, int(parentPortNo),
Chaitrashree G See824a22019-07-28 18:28:27 -0400768 "", int(channelID),
Mahir Gunyele77977b2019-06-27 05:36:22 -0700769 string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuID)); onuDevice == nil {
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400770 log.Errorw("Create onu error",
771 log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(),
772 "onuID": onuID, "sn": sn, "error": err})
Matt Jeanneret53539512019-07-20 14:47:02 -0400773 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400774 }
Matt Jeanneret53539512019-07-20 14:47:02 -0400775 log.Debugw("onu-child-device-added", log.Fields{"onuDevice": onuDevice})
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400776
777 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700778 //ONU already discovered before. Use the same OnuID.
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400779 onuID = onuDevice.ProxyAddress.OnuId
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400780 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700781 //Insert the ONU into cache to use in OnuIndication.
782 //TODO: Do we need to remove this from the cache on ONU change, or wait for overwritten on next discovery.
Scott Baker7eb0a932019-07-26 10:33:22 -0700783 log.Debugw("ONU discovery indication key create", log.Fields{"onuID": onuID,
784 "intfId": onuDiscInd.GetIntfId()})
Mahir Gunyele77977b2019-06-27 05:36:22 -0700785 onuKey := dh.formOnuKey(onuDiscInd.GetIntfId(), onuID)
Matt Jeanneret53539512019-07-20 14:47:02 -0400786
787 dh.lockDevice.Lock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700788 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuID, onuDiscInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
Matt Jeanneret53539512019-07-20 14:47:02 -0400789 log.Debugw("new-onu-device-discovered", log.Fields{"onu": dh.onus[onuKey]})
790 dh.lockDevice.Unlock()
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400791
Mahir Gunyele77977b2019-06-27 05:36:22 -0700792 err = dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
793 if err != nil {
Matt Jeanneret53539512019-07-20 14:47:02 -0400794 log.Errorw("failed to update device state", log.Fields{"DeviceID": onuDevice.Id, "err": err})
795 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700796 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700797 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
798 //TODO: We put this sleep here to prevent the race between state update and onuIndication
799 //In onuIndication the operStatus of device is checked. If it is still not updated in KV store
800 //then the initialisation fails.
801 time.Sleep(1 * time.Second)
802 dh.activateONU(onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn)
Matt Jeanneret53539512019-07-20 14:47:02 -0400803 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700804}
805
806func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
807 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
808
809 kwargs := make(map[string]interface{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700810 ponPort := IntfIDToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
Mahir Gunyele77977b2019-06-27 05:36:22 -0700811 var onuDevice *voltha.Device
812 foundInCache := false
Scott Baker7eb0a932019-07-26 10:33:22 -0700813 log.Debugw("ONU indication key create", log.Fields{"onuId": onuInd.OnuId,
814 "intfId": onuInd.GetIntfId()})
Mahir Gunyele77977b2019-06-27 05:36:22 -0700815 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.OnuId)
816 if onuInCache, ok := dh.onus[onuKey]; ok {
817 //If ONU id is discovered before then use GetDevice to get onuDevice because it is cheaper.
818 foundInCache = true
819 onuDevice, _ = dh.coreProxy.GetDevice(nil, dh.device.Id, onuInCache.deviceID)
cuilin20187b2a8c32019-03-26 19:52:28 -0700820 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700821 //If ONU not found in adapter cache then we have to use GetChildDevice to get onuDevice
822 if serialNumber != "" {
823 kwargs["serial_number"] = serialNumber
824 } else {
825 kwargs["onu_id"] = onuInd.OnuId
826 kwargs["parent_port_no"] = ponPort
827 }
828 onuDevice, _ = dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
cuilin20187b2a8c32019-03-26 19:52:28 -0700829 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700830
831 if onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400832 if onuDevice.ParentPortNo != ponPort {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700833 //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 -0400834 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700835 }
836
837 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
838 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})
839 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700840 if !foundInCache {
841 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId())
Matt Jeanneret53539512019-07-20 14:47:02 -0400842 dh.lockDevice.Lock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700843 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
Matt Jeanneret53539512019-07-20 14:47:02 -0400844 dh.lockDevice.Unlock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700845 }
Scott Baker7eb0a932019-07-26 10:33:22 -0700846 dh.updateOnuStates(onuDevice, onuInd, foundInCache)
cuilin20187b2a8c32019-03-26 19:52:28 -0700847
cuilin20187b2a8c32019-03-26 19:52:28 -0700848 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700849 log.Errorw("onu not found", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId})
cuilin20187b2a8c32019-03-26 19:52:28 -0700850 return
851 }
852
853}
854
Scott Baker7eb0a932019-07-26 10:33:22 -0700855func (dh *DeviceHandler) updateOnuStates(onuDevice *voltha.Device, onuInd *oop.OnuIndication, foundInCache bool) {
Matt Jeanneret53539512019-07-20 14:47:02 -0400856 log.Debugw("onu-indication-for-state", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700857 dh.updateOnuAdminState(onuInd)
858 // operState
859 if onuInd.OperState == "down" {
Matt Jeanneret53539512019-07-20 14:47:02 -0400860 log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700861 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
862 err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
863 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
864 if err != nil {
865 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
866 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
867 }
868 } else if onuInd.OperState == "up" {
Scott Baker7eb0a932019-07-26 10:33:22 -0700869 // Ignore operstatus if device was found in cache
870 if !foundInCache && onuDevice.OperStatus != common.OperStatus_DISCOVERED {
Matt Jeanneret53539512019-07-20 14:47:02 -0400871 log.Warnw("ignore-onu-indication", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700872 return
873 }
Matt Jeanneret53539512019-07-20 14:47:02 -0400874 log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState})
875 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700876 err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
877 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
878 if err != nil {
879 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
880 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
881 return
882 }
883 } else {
884 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
885 }
886}
887
888func (dh *DeviceHandler) updateOnuAdminState(onuInd *oop.OnuIndication) {
889 if onuInd.AdminState == "down" {
890 if onuInd.OperState != "down" {
891 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
892 // Forcing the oper state change code to execute
893 onuInd.OperState = "down"
894 }
895 // Port and logical port update is taken care of by oper state block
896 } else if onuInd.AdminState == "up" {
897 log.Debugln("received-onu-admin-state up")
898 } else {
899 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
900 }
901 log.Debugln("admin-state-dealt-with")
902}
903
cuilin20187b2a8c32019-03-26 19:52:28 -0700904func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
905 if serialNum != nil {
906 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
cuilin20187b2a8c32019-03-26 19:52:28 -0700907 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700908 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700909}
910
911func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
912 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700913 fmt.Sprintf("%x", uint32(vendorSpecific[0]&0x0f)) +
cuilin20187b2a8c32019-03-26 19:52:28 -0700914 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
915 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
916 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
917 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
918 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
919 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
920 return tmp
921}
922
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700923//UpdateFlowsBulk upates the bulk flow
924func (dh *DeviceHandler) UpdateFlowsBulk() error {
925 return errors.New("unimplemented")
cuilin20187b2a8c32019-03-26 19:52:28 -0700926}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700927
928//GetChildDevice returns the child device for given parent port and onu id
929func (dh *DeviceHandler) GetChildDevice(parentPort, onuID uint32) *voltha.Device {
930 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuID": onuID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400931 kwargs := make(map[string]interface{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700932 kwargs["onu_id"] = onuID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400933 kwargs["parent_port_no"] = parentPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700934 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400935 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700936 log.Errorw("onu not found", log.Fields{"intfID": parentPort, "onuID": onuID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400937 return nil
938 }
939 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
940 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530941}
942
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700943// SendPacketInToCore sends packet-in to core
944// For this, it calls SendPacketIn of the core-proxy which uses a device specific topic to send the request.
945// The adapter handling the device creates a device specific topic
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400946func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
947 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700948 if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPort, packetPayload); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400949 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
950 return
951 }
952 log.Debug("Sent packet-in to core successfully")
953}
954
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700955//UpdateFlowsIncrementally updates the device flow
manikkaraj kbf256be2019-03-25 00:13:48 +0530956func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700957 log.Debugw("In Update_flows_incrementally", log.Fields{"deviceID": device.Id, "flows": flows, "groups": groups})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400958 if flows != nil {
959 for _, flow := range flows.ToAdd.Items {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400960 log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400961 dh.flowMgr.AddFlow(flow)
962 }
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400963 for _, flow := range flows.ToRemove.Items {
964 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
965 dh.flowMgr.RemoveFlow(flow)
966 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400967 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700968 if groups != nil && flows != nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400969 for _, flow := range flows.ToRemove.Items {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700970 log.Debug("Removing flow", log.Fields{"deviceID": device.Id, "flowToRemove": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400971 // dh.flowMgr.RemoveFlow(flow)
972 }
973 }
974 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530975}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400976
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700977//DisableDevice disables the given device
978//It marks the following for the given device:
979//Device-Handler Admin-State : down
980//Device Port-State: UNKNOWN
981//Device Oper-State: UNKNOWN
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400982func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
983 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
984 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
985 return err
986 }
987 dh.lockDevice.Lock()
988 dh.adminState = "down"
989 dh.lockDevice.Unlock()
990 log.Debug("olt-disabled")
991
992 cloned := proto.Clone(device).(*voltha.Device)
993 // Update the all ports state on that device to disable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700994 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
995 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400996 return err
997 }
998
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700999 log.Debugw("Disable_device-end", log.Fields{"deviceID": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001000 return nil
1001}
1002
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001003//ReenableDevice re-enables the olt device after disable
1004//It marks the following for the given device:
1005//Device-Handler Admin-State : up
1006//Device Port-State: ACTIVE
1007//Device Oper-State: ACTIVE
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001008func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
1009 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
1010 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
1011 return err
1012 }
1013
1014 dh.lockDevice.Lock()
1015 dh.adminState = "up"
1016 dh.lockDevice.Unlock()
1017 log.Debug("olt-reenabled")
1018
1019 cloned := proto.Clone(device).(*voltha.Device)
1020 // Update the all ports state on that device to enable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001021 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
1022 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001023 return err
1024 }
1025
1026 //Update the device oper status as ACTIVE
1027 cloned.OperStatus = voltha.OperStatus_ACTIVE
1028 dh.device = cloned
1029
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001030 if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
1031 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001032 return err
1033 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001034 log.Debugw("ReEnableDevice-end", log.Fields{"deviceID": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001035
1036 return nil
1037}
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001038
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001039//RebootDevice reboots the given device
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -04001040func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
1041 if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
1042 log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
1043 return err
1044 }
1045
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001046 log.Debugw("rebooted-device-successfully", log.Fields{"deviceID": device.Id})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -04001047
1048 return nil
1049}
1050
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001051func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
1052 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
1053 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
1054 if err != nil {
1055 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
1056 return
1057 }
1058 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001059 if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001060 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
1061 return
1062 }
1063 log.Debug("Success sending packet-in to core!")
1064}
1065
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001066// PacketOut sends packet-out from VOLTHA to OLT on the egress port provided
1067func (dh *DeviceHandler) PacketOut(egressPortNo int, packet *of.OfpPacketOut) error {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001068 log.Debugw("incoming-packet-out", log.Fields{"deviceID": dh.deviceID, "egress_port_no": egressPortNo,
1069 "pkt-length": len(packet.Data), "packetData": hex.EncodeToString(packet.Data)})
1070
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001071 egressPortType := IntfIDToPortTypeName(uint32(egressPortNo))
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001072 if egressPortType == voltha.Port_ETHERNET_UNI {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001073 outerEthType := (uint16(packet.Data[12]) << 8) | uint16(packet.Data[13])
1074 innerEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
1075 if outerEthType == 0x88a8 || outerEthType == 0x8100 {
1076 if innerEthType == 0x8100 {
1077 // q-in-q 802.1ad or 802.1q double tagged packet.
1078 // slice out the outer tag.
1079 packet.Data = append(packet.Data[:12], packet.Data[16:]...)
1080 log.Debugw("packet-now-single-tagged", log.Fields{"packetData": hex.EncodeToString(packet.Data)})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001081 }
1082 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001083 intfID := IntfIDFromUniPortNum(uint32(egressPortNo))
1084 onuID := OnuIDFromPortNum(uint32(egressPortNo))
Matt Jeanneret1359c732019-08-01 21:40:02 -04001085 uniID := uint32(egressPortNo)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001086 onuPkt := oop.OnuPacket{IntfId: intfID, OnuId: onuID, PortNo: uint32(egressPortNo), Pkt: packet.Data}
Matt Jeanneret1359c732019-08-01 21:40:02 -04001087
1088 log.Debugw("sending-packet-to-onu", log.Fields{"egress_port_no": egressPortNo, "IntfId": intfID, "onuID": onuID,
1089 "uniID": uniID, "packet": hex.EncodeToString(packet.Data)})
1090
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001091 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
1092 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
1093 return err
1094 }
1095 } else if egressPortType == voltha.Port_ETHERNET_NNI {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001096 uplinkPkt := oop.UplinkPacket{IntfId: IntfIDFromNniPortNum(uint32(egressPortNo)), Pkt: packet.Data}
Matt Jeanneret1359c732019-08-01 21:40:02 -04001097
1098 log.Debugw("sending-packet-to-nni", log.Fields{"uplink_pkt": uplinkPkt, "packet": hex.EncodeToString(packet.Data)})
1099
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001100 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001101 log.Errorw("Error while sending packet-out to NNI", log.Fields{"error": err})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001102 return err
1103 }
1104 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001105 log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egressPortNo, "egressPortType": egressPortType})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001106 }
1107 return nil
1108}
Mahir Gunyela3f9add2019-06-06 15:13:19 -07001109
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001110func (dh *DeviceHandler) formOnuKey(intfID, onuID uint32) string {
1111 return "" + strconv.Itoa(int(intfID)) + "." + strconv.Itoa(int(onuID))
Mahir Gunyela3f9add2019-06-06 15:13:19 -07001112}