blob: 287f9277225259ede898cd6711fbda1fa5461dc6 [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
salmansiddiqui7ac62132019-08-22 03:58:50 +000048// Constants for number of retries and for timeout
Manikkaraj kb1d51442019-07-23 10:41:02 -040049const (
salmansiddiqui7ac62132019-08-22 03:58:50 +000050 MaxRetry = 10
51 MaxTimeOutInMs = 500
Manikkaraj kb1d51442019-07-23 10:41:02 -040052)
53
Phaneendra Manda4c62c802019-03-06 21:37:49 +053054//DeviceHandler will interact with the OLT device.
55type DeviceHandler struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070056 deviceID string
cuilin20187b2a8c32019-03-26 19:52:28 -070057 deviceType string
Girish Gowdru5ba46c92019-04-25 05:00:05 -040058 adminState string
cuilin20187b2a8c32019-03-26 19:52:28 -070059 device *voltha.Device
60 coreProxy *com.CoreProxy
manikkaraj kbf256be2019-03-25 00:13:48 +053061 AdapterProxy *com.AdapterProxy
Devmalya Paulfb990a52019-07-09 10:01:49 -040062 EventProxy *com.EventProxy
cuilin20187b2a8c32019-03-26 19:52:28 -070063 openOLT *OpenOLT
cuilin20187b2a8c32019-03-26 19:52:28 -070064 exitChannel chan int
65 lockDevice sync.RWMutex
manikkaraj kbf256be2019-03-25 00:13:48 +053066 Client oop.OpenoltClient
cuilin20187b2a8c32019-03-26 19:52:28 -070067 transitionMap *TransitionMap
68 clientCon *grpc.ClientConn
manikkaraj kbf256be2019-03-25 00:13:48 +053069 flowMgr *OpenOltFlowMgr
Devmalya Paulfb990a52019-07-09 10:01:49 -040070 eventMgr *OpenOltEventMgr
manikkaraj kbf256be2019-03-25 00:13:48 +053071 resourceMgr *rsrcMgr.OpenOltResourceMgr
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -040072 discOnus map[string]bool
Mahir Gunyela3f9add2019-06-06 15:13:19 -070073 onus map[string]*OnuDevice
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070074 nniIntfID int
Mahir Gunyela3f9add2019-06-06 15:13:19 -070075}
76
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070077//OnuDevice represents ONU related info
Mahir Gunyela3f9add2019-06-06 15:13:19 -070078type OnuDevice struct {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070079 deviceID string
Mahir Gunyela3f9add2019-06-06 15:13:19 -070080 deviceType string
81 serialNumber string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070082 onuID uint32
83 intfID uint32
84 proxyDeviceID string
Mahir Gunyela3f9add2019-06-06 15:13:19 -070085}
86
87//NewOnuDevice creates a new Onu Device
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070088func NewOnuDevice(devID, deviceTp, serialNum string, onuID, intfID uint32, proxyDevID string) *OnuDevice {
Mahir Gunyela3f9add2019-06-06 15:13:19 -070089 var device OnuDevice
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070090 device.deviceID = devID
Mahir Gunyela3f9add2019-06-06 15:13:19 -070091 device.deviceType = deviceTp
92 device.serialNumber = serialNum
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070093 device.onuID = onuID
94 device.intfID = intfID
95 device.proxyDeviceID = proxyDevID
Mahir Gunyela3f9add2019-06-06 15:13:19 -070096 return &device
Phaneendra Manda4c62c802019-03-06 21:37:49 +053097}
98
99//NewDeviceHandler creates a new device handler
Devmalya Paulfb990a52019-07-09 10:01:49 -0400100func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, ep *com.EventProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler {
cuilin20187b2a8c32019-03-26 19:52:28 -0700101 var dh DeviceHandler
102 dh.coreProxy = cp
Girish Gowdru0c588b22019-04-23 23:24:56 -0400103 dh.AdapterProxy = ap
Devmalya Paulfb990a52019-07-09 10:01:49 -0400104 dh.EventProxy = ep
cuilin20187b2a8c32019-03-26 19:52:28 -0700105 cloned := (proto.Clone(device)).(*voltha.Device)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700106 dh.deviceID = cloned.Id
cuilin20187b2a8c32019-03-26 19:52:28 -0700107 dh.deviceType = cloned.Type
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400108 dh.adminState = "up"
cuilin20187b2a8c32019-03-26 19:52:28 -0700109 dh.device = cloned
110 dh.openOLT = adapter
111 dh.exitChannel = make(chan int, 1)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400112 dh.discOnus = make(map[string]bool)
cuilin20187b2a8c32019-03-26 19:52:28 -0700113 dh.lockDevice = sync.RWMutex{}
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700114 dh.onus = make(map[string]*OnuDevice)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700115 // The nniIntfID is initialized to -1 (invalid) and set to right value
Girish Gowdru1110ef22019-06-24 11:17:59 -0400116 // when the first IntfOperInd with status as "up" is received for
117 // any one of the available NNI port on the OLT device.
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700118 dh.nniIntfID = -1
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530119
cuilin20187b2a8c32019-03-26 19:52:28 -0700120 //TODO initialize the support classes.
121 return &dh
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530122}
123
124// start save the device to the data model
125func (dh *DeviceHandler) start(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700126 dh.lockDevice.Lock()
127 defer dh.lockDevice.Unlock()
128 log.Debugw("starting-device-agent", log.Fields{"device": dh.device})
129 // Add the initial device to the local model
130 log.Debug("device-agent-started")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530131}
132
133// stop stops the device dh. Not much to do for now
134func (dh *DeviceHandler) stop(ctx context.Context) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700135 dh.lockDevice.Lock()
136 defer dh.lockDevice.Unlock()
137 log.Debug("stopping-device-agent")
138 dh.exitChannel <- 1
139 log.Debug("device-agent-stopped")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530140}
141
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400142func macifyIP(ip net.IP) string {
143 if len(ip) > 0 {
144 oct1 := strconv.FormatInt(int64(ip[12]), 16)
145 oct2 := strconv.FormatInt(int64(ip[13]), 16)
146 oct3 := strconv.FormatInt(int64(ip[14]), 16)
147 oct4 := strconv.FormatInt(int64(ip[15]), 16)
148 return fmt.Sprintf("00:00:%02v:%02v:%02v:%02v", oct1, oct2, oct3, oct4)
149 }
150 return ""
151}
152
153func generateMacFromHost(host string) (string, error) {
154 var genmac string
155 var addr net.IP
156 var ips []string
157 var err error
158
159 log.Debugw("generating-mac-from-host", log.Fields{"host": host})
160
161 if addr = net.ParseIP(host); addr == nil {
162 log.Debugw("looking-up-hostname", log.Fields{"host": host})
163
164 if ips, err = net.LookupHost(host); err == nil {
165 log.Debugw("dns-result-ips", log.Fields{"ips": ips})
166 if addr = net.ParseIP(ips[0]); addr == nil {
167 log.Errorw("unable-to-parse-ip", log.Fields{"ip": ips[0]})
168 return "", errors.New("unable-to-parse-ip")
169 }
170 genmac = macifyIP(addr)
171 log.Debugw("using-ip-as-mac", log.Fields{"host": ips[0], "mac": genmac})
172 return genmac, nil
173 }
174 log.Errorw("cannot-resolve-hostname-to-ip", log.Fields{"host": host})
175 return "", err
176 }
177
178 genmac = macifyIP(addr)
179 log.Debugw("using-ip-as-mac", log.Fields{"host": host, "mac": genmac})
180 return genmac, nil
181}
182
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530183func macAddressToUint32Array(mac string) []uint32 {
cuilin20187b2a8c32019-03-26 19:52:28 -0700184 slist := strings.Split(mac, ":")
185 result := make([]uint32, len(slist))
186 var err error
187 var tmp int64
188 for index, val := range slist {
189 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
190 return []uint32{1, 2, 3, 4, 5, 6}
191 }
192 result[index] = uint32(tmp)
193 }
194 return result
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530195}
196
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700197//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 +0530198func GetportLabel(portNum uint32, portType voltha.Port_PortType) string {
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530199
Girish Gowdru0c588b22019-04-23 23:24:56 -0400200 if portType == voltha.Port_ETHERNET_NNI {
201 return fmt.Sprintf("nni-%d", portNum)
202 } else if portType == voltha.Port_PON_OLT {
203 return fmt.Sprintf("pon-%d", portNum)
cuilin20187b2a8c32019-03-26 19:52:28 -0700204 } else if portType == voltha.Port_ETHERNET_UNI {
205 log.Errorw("local UNI management not supported", log.Fields{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700207 }
208 return ""
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530209}
210
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700211func (dh *DeviceHandler) addPort(intfID uint32, portType voltha.Port_PortType, state string) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700212 var operStatus common.OperStatus_OperStatus
213 if state == "up" {
214 operStatus = voltha.OperStatus_ACTIVE
215 } else {
216 operStatus = voltha.OperStatus_DISCOVERED
217 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700218 portNum := IntfIDToPortNo(intfID, portType)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400219 label := GetportLabel(portNum, portType)
220 if len(label) == 0 {
221 log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType})
222 return
223 }
224 // Now create Port
225 port := &voltha.Port{
cuilin20187b2a8c32019-03-26 19:52:28 -0700226 PortNo: portNum,
227 Label: label,
228 Type: portType,
229 OperStatus: operStatus,
230 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400231 log.Debugw("Sending port update to core", log.Fields{"port": port})
cuilin20187b2a8c32019-03-26 19:52:28 -0700232 // Synchronous call to update device - this method is run in its own go routine
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700233 if err := dh.coreProxy.PortCreated(context.TODO(), dh.device.Id, port); err != nil {
234 log.Errorw("error-creating-nni-port", log.Fields{"deviceID": dh.device.Id, "portType": portType, "error": err})
Girish Gowdru1110ef22019-06-24 11:17:59 -0400235 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236
Girish Gowdru1110ef22019-06-24 11:17:59 -0400237 // Once we have successfully added the NNI port to the core, if the
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700238 // locally cached nniIntfID is set to invalid (-1), set it to the right value.
239 if portType == voltha.Port_ETHERNET_NNI && dh.nniIntfID == -1 {
240 dh.nniIntfID = int(intfID)
cuilin20187b2a8c32019-03-26 19:52:28 -0700241 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530242}
243
244// readIndications to read the indications from the OLT device
245func (dh *DeviceHandler) readIndications() {
William Kurkianff524662019-08-20 10:34:30 -0400246 defer log.Errorw("Indications ended", log.Fields{})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247 indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty))
cuilin20187b2a8c32019-03-26 19:52:28 -0700248 if err != nil {
249 log.Errorw("Failed to read indications", log.Fields{"err": err})
250 return
251 }
252 if indications == nil {
253 log.Errorw("Indications is nil", log.Fields{})
254 return
255 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400256 /* get device state */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700257 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400258 if err != nil || device == nil {
259 /*TODO: needs to handle error scenarios */
260 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700261 return
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400262 }
263 // When the device is in DISABLED and Adapter container restarts, we need to
264 // rebuild the locally maintained admin state.
265 if device.AdminState == voltha.AdminState_DISABLED {
266 dh.lockDevice.Lock()
267 dh.adminState = "down"
268 dh.lockDevice.Unlock()
269 }
270
cuilin20187b2a8c32019-03-26 19:52:28 -0700271 for {
272 indication, err := indications.Recv()
273 if err == io.EOF {
274 break
275 }
276 if err != nil {
277 log.Infow("Failed to read from indications", log.Fields{"err": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400278 dh.transitionMap.Handle(DeviceDownInd)
279 dh.transitionMap.Handle(DeviceInit)
280 break
cuilin20187b2a8c32019-03-26 19:52:28 -0700281 }
Chaitrashree G S44124192019-08-07 20:21:36 -0400282 dh.lockDevice.RLock()
283 adminState := dh.adminState
284 dh.lockDevice.RUnlock()
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400285 // When OLT is admin down, allow only NNI operation status change indications.
Chaitrashree G S44124192019-08-07 20:21:36 -0400286 if adminState == "down" {
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400287 _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd)
288 if isIntfOperInd {
289 intfOperInd := indication.GetIntfOperInd()
290 if intfOperInd.GetType() == "nni" {
291 log.Infow("olt is admin down, allow nni ind", log.Fields{})
292 }
293 } else {
294 log.Infow("olt is admin down, ignore indication", log.Fields{})
295 continue
296 }
297 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530298
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700299 dh.handleIndication(indication)
manikkaraj kbf256be2019-03-25 00:13:48 +0530300
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700301 }
302}
303
304func (dh *DeviceHandler) handleOltIndication(oltIndication *oop.OltIndication) {
Daniele Rossi051466a2019-07-26 13:39:37 +0000305 raisedTs := time.Now().UnixNano()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700306 if oltIndication.OperState == "up" {
307 dh.transitionMap.Handle(DeviceUpInd)
308 } else if oltIndication.OperState == "down" {
309 dh.transitionMap.Handle(DeviceDownInd)
310 }
Daniele Rossi051466a2019-07-26 13:39:37 +0000311 // Send or clear Alarm
312 dh.eventMgr.oltUpDownIndication(oltIndication, dh.deviceID, raisedTs)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700313}
314
315func (dh *DeviceHandler) handleIndication(indication *oop.Indication) {
Devmalya Paulfb990a52019-07-09 10:01:49 -0400316 raisedTs := time.Now().UnixNano()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700317 switch indication.Data.(type) {
318 case *oop.Indication_OltInd:
319 dh.handleOltIndication(indication.GetOltInd())
320 case *oop.Indication_IntfInd:
321 intfInd := indication.GetIntfInd()
322 go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
323 log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd})
324 case *oop.Indication_IntfOperInd:
325 intfOperInd := indication.GetIntfOperInd()
326 if intfOperInd.GetType() == "nni" {
327 go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
328 } else if intfOperInd.GetType() == "pon" {
329 // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
330 // Handle pon port update
cuilin20187b2a8c32019-03-26 19:52:28 -0700331 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700332 log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
333 case *oop.Indication_OnuDiscInd:
334 onuDiscInd := indication.GetOnuDiscInd()
335 log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700336 sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
Matt Jeanneret53539512019-07-20 14:47:02 -0400337 go dh.onuDiscIndication(onuDiscInd, sn)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700338 case *oop.Indication_OnuInd:
339 onuInd := indication.GetOnuInd()
340 log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
341 go dh.onuIndication(onuInd)
342 case *oop.Indication_OmciInd:
343 omciInd := indication.GetOmciInd()
344 log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd})
William Kurkianff524662019-08-20 10:34:30 -0400345 go dh.omciIndication(omciInd)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700346 case *oop.Indication_PktInd:
347 pktInd := indication.GetPktInd()
348 log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
349 go dh.handlePacketIndication(pktInd)
350 case *oop.Indication_PortStats:
351 portStats := indication.GetPortStats()
352 log.Infow("Received port stats indication", log.Fields{"PortStats": portStats})
353 case *oop.Indication_FlowStats:
354 flowStats := indication.GetFlowStats()
355 log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats})
356 case *oop.Indication_AlarmInd:
357 alarmInd := indication.GetAlarmInd()
358 log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd})
Devmalya Paulfb990a52019-07-09 10:01:49 -0400359 dh.eventMgr.ProcessEvents(alarmInd, dh.deviceID, raisedTs)
360
cuilin20187b2a8c32019-03-26 19:52:28 -0700361 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530362}
363
364// doStateUp handle the olt up indication and update to voltha core
365func (dh *DeviceHandler) doStateUp() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400366 // Synchronous call to update device state - this method is run in its own go routine
cuilin20187b2a8c32019-03-26 19:52:28 -0700367 if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400368 voltha.OperStatus_ACTIVE); err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700369 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 -0400370 return err
371 }
372 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530373}
374
375// doStateDown handle the olt down indication
376func (dh *DeviceHandler) doStateDown() error {
Girish Gowdrud4245152019-05-10 00:47:31 -0400377 log.Debug("do-state-down-start")
378
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700379 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdrud4245152019-05-10 00:47:31 -0400380 if err != nil || device == nil {
381 /*TODO: needs to handle error scenarios */
382 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 return errors.New("failed to fetch device device")
Girish Gowdrud4245152019-05-10 00:47:31 -0400384 }
385
386 cloned := proto.Clone(device).(*voltha.Device)
387 // Update the all ports state on that device to disable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700388 if er := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); er != nil {
389 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": er})
390 return er
Girish Gowdrud4245152019-05-10 00:47:31 -0400391 }
392
393 //Update the device oper state and connection status
394 cloned.OperStatus = voltha.OperStatus_UNKNOWN
395 cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
396 dh.device = cloned
397
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700398 if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
399 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": er})
400 return er
Girish Gowdrud4245152019-05-10 00:47:31 -0400401 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400402
403 //get the child device for the parent device
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700404 onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400405 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700406 log.Errorw("failed to get child devices information", log.Fields{"deviceID": dh.device.Id, "error": err})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400407 return err
408 }
409 for _, onuDevice := range onuDevices.Items {
410
411 // Update onu state as down in onu adapter
412 onuInd := oop.OnuIndication{}
413 onuInd.OperState = "down"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700414 er := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
415 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
416 if er != nil {
417 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
418 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
419 return er
420 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400421 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700422 log.Debugw("do-state-down-end", log.Fields{"deviceID": device.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700423 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530424}
425
426// doStateInit dial the grpc before going to init state
427func (dh *DeviceHandler) doStateInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400428 var err error
Girish Gowdrud4245152019-05-10 00:47:31 -0400429 dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock())
Girish Gowdru0c588b22019-04-23 23:24:56 -0400430 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700431 log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceID, "HostAndPort": dh.device.GetHostAndPort(), "err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400432 return err
433 }
434 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530435}
436
437// postInit create olt client instance to invoke RPC on the olt device
438func (dh *DeviceHandler) postInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400439 dh.Client = oop.NewOpenoltClient(dh.clientCon)
440 dh.transitionMap.Handle(GrpcConnected)
441 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530442}
443
444// doStateConnected get the device info and update to voltha core
445func (dh *DeviceHandler) doStateConnected() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400446 log.Debug("OLT device has been connected")
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400447
448 // Case where OLT is disabled and then rebooted.
449 if dh.adminState == "down" {
450 log.Debugln("do-state-connected--device-admin-state-down")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700451 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400452 if err != nil || device == nil {
453 /*TODO: needs to handle error scenarios */
454 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
455 }
456
457 cloned := proto.Clone(device).(*voltha.Device)
458 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
459 cloned.OperStatus = voltha.OperStatus_UNKNOWN
460 dh.device = cloned
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700461 if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
462 log.Errorw("error-updating-device-state", log.Fields{"deviceID": dh.device.Id, "error": er})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400463 }
464
Chaitrashree G S44124192019-08-07 20:21:36 -0400465 // Since the device was disabled before the OLT was rebooted, enforce the OLT to be Disabled after re-connection.
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400466 _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
467 if err != nil {
468 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
469 }
470
471 // Start reading indications
472 go dh.readIndications()
473 return nil
474 }
475
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400476 deviceInfo, err := dh.populateDeviceInfo()
cuilin20187b2a8c32019-03-26 19:52:28 -0700477 if err != nil {
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400478 log.Errorw("Unable to populate Device Info", log.Fields{"err": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700479 return err
480 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400481
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700482 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdrud4245152019-05-10 00:47:31 -0400483 if err != nil || device == nil {
484 /*TODO: needs to handle error scenarios */
485 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700486 return err
Girish Gowdrud4245152019-05-10 00:47:31 -0400487 }
488 cloned := proto.Clone(device).(*voltha.Device)
489 // Update the all ports (if available) on that device to ACTIVE.
490 // The ports do not normally exist, unless the device is coming back from a reboot
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700491 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
492 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400493 return err
494 }
495
Girish Gowdru0c588b22019-04-23 23:24:56 -0400496 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
497 // Instantiate resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700498 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceID, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400499 log.Error("Error while instantiating resource manager")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700500 return errors.New("instantiating resource manager failed")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400501 }
502 // Instantiate flow manager
503 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
504 log.Error("Error while instantiating flow manager")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700505 return errors.New("instantiating flow manager failed")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400506 }
507 /* TODO: Instantiate Alarm , stats , BW managers */
Devmalya Paulfb990a52019-07-09 10:01:49 -0400508 /* Instantiating Event Manager to handle Alarms and KPIs */
509 dh.eventMgr = NewEventMgr(dh.EventProxy)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530510
cuilin20187b2a8c32019-03-26 19:52:28 -0700511 // Start reading indications
512 go dh.readIndications()
513 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530514}
515
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400516func (dh *DeviceHandler) populateDeviceInfo() (*oop.DeviceInfo, error) {
517 var err error
518 var deviceInfo *oop.DeviceInfo
519
520 deviceInfo, err = dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
521
522 if err != nil {
523 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
524 return nil, err
525 }
526 if deviceInfo == nil {
527 log.Errorw("Device info is nil", log.Fields{})
528 return nil, errors.New("failed to get device info from OLT")
529 }
530
531 log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo})
532 dh.device.Root = true
533 dh.device.Vendor = deviceInfo.Vendor
534 dh.device.Model = deviceInfo.Model
535 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
536 dh.device.HardwareVersion = deviceInfo.HardwareVersion
537 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
538
539 if deviceInfo.DeviceId == "" {
540 log.Warnw("no-device-id-provided-using-host", log.Fields{"hostport": dh.device.GetHostAndPort()})
541 host := strings.Split(dh.device.GetHostAndPort(), ":")[0]
542 genmac, err := generateMacFromHost(host)
543 if err != nil {
544 return nil, err
545 }
546 log.Debugw("using-host-for-mac-address", log.Fields{"host": host, "mac": genmac})
547 dh.device.MacAddress = genmac
548 } else {
549 dh.device.MacAddress = deviceInfo.DeviceId
550 }
551
552 // Synchronous call to update device - this method is run in its own go routine
553 if err := dh.coreProxy.DeviceUpdate(context.TODO(), dh.device); err != nil {
554 log.Errorw("error-updating-device", log.Fields{"deviceID": dh.device.Id, "error": err})
555 return nil, err
556 }
557
558 return deviceInfo, nil
559}
560
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700561//AdoptDevice adopts the OLT device
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530562func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400563 dh.transitionMap = NewTransitionMap(dh)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700564 log.Infow("Adopt_device", log.Fields{"deviceID": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400565 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530566}
567
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700568//GetOfpDeviceInfo Gets the Ofp information of the given device
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530569func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700570 return &ic.SwitchCapability{
571 Desc: &of.OfpDesc{
Devmalya Paul70dd4972019-06-10 15:19:17 +0530572 MfrDesc: "VOLTHA Project",
cuilin20187b2a8c32019-03-26 19:52:28 -0700573 HwDesc: "open_pon",
574 SwDesc: "open_pon",
575 SerialNum: dh.device.SerialNumber,
576 },
577 SwitchFeatures: &of.OfpSwitchFeatures{
578 NBuffers: 256,
579 NTables: 2,
580 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
581 of.OfpCapabilities_OFPC_TABLE_STATS |
582 of.OfpCapabilities_OFPC_PORT_STATS |
583 of.OfpCapabilities_OFPC_GROUP_STATS),
584 },
585 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530586}
587
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700588//GetOfpPortInfo Get Ofp port information
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530589func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700590 capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
cuilin20187b2a8c32019-03-26 19:52:28 -0700591 return &ic.PortCapability{
592 Port: &voltha.LogicalPort{
593 OfpPort: &of.OfpPort{
594 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
595 Config: 0,
596 State: uint32(of.OfpPortState_OFPPS_LIVE),
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700597 Curr: capacity,
598 Advertised: capacity,
599 Peer: capacity,
cuilin20187b2a8c32019-03-26 19:52:28 -0700600 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
601 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
602 },
603 DeviceId: dh.device.Id,
604 DevicePortNo: uint32(portNo),
605 },
606 }, nil
607}
608
William Kurkianff524662019-08-20 10:34:30 -0400609func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700610 log.Debugw("omci indication", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700611 var deviceType string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700612 var deviceID string
613 var proxyDeviceID string
cuilin20187b2a8c32019-03-26 19:52:28 -0700614
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700615 onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId)
616 if onuInCache, ok := dh.onus[onuKey]; !ok {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700617 log.Debugw("omci indication for a device not in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
618 ponPort := IntfIDToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700619 kwargs := make(map[string]interface{})
620 kwargs["onu_id"] = omciInd.OnuId
621 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700622
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700623 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
624 if err != nil {
William Kurkianff524662019-08-20 10:34:30 -0400625 log.Errorw("onu not found", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId, "error": err})
626 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700627 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700628 deviceType = onuDevice.Type
629 deviceID = onuDevice.Id
630 proxyDeviceID = onuDevice.ProxyAddress.DeviceId
631 //if not exist in cache, then add to cache.
632 dh.onus[onuKey] = NewOnuDevice(deviceID, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceID)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700633 } else {
634 //found in cache
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700635 log.Debugw("omci indication for a device in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700636 deviceType = onuInCache.deviceType
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700637 deviceID = onuInCache.deviceID
638 proxyDeviceID = onuInCache.proxyDeviceID
cuilin20187b2a8c32019-03-26 19:52:28 -0700639 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700640
641 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
642 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
643 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700644 deviceID, proxyDeviceID, ""); sendErr != nil {
William Kurkianff524662019-08-20 10:34:30 -0400645 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuID": deviceID, "proxyDeviceID": proxyDeviceID, "error": sendErr})
646 return
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700647 }
William Kurkianff524662019-08-20 10:34:30 -0400648 return
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530649}
650
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700651//ProcessInterAdapterMessage sends the proxied messages to the target device
652// If the proxy address is not found in the unmarshalled message, it first fetches the onu device for which the message
653// is meant, and then send the unmarshalled omci message to this onu
654func (dh *DeviceHandler) ProcessInterAdapterMessage(msg *ic.InterAdapterMessage) error {
655 log.Debugw("Process_inter_adapter_message", log.Fields{"msgID": msg.Header.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700656 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700657 msgID := msg.Header.Id
cuilin20187b2a8c32019-03-26 19:52:28 -0700658 fromTopic := msg.Header.FromTopic
659 toTopic := msg.Header.ToTopic
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700660 toDeviceID := msg.Header.ToDeviceId
661 proxyDeviceID := msg.Header.ProxyDeviceId
cuilin20187b2a8c32019-03-26 19:52:28 -0700662
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700663 log.Debugw("omci request message header", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
cuilin20187b2a8c32019-03-26 19:52:28 -0700664
665 msgBody := msg.GetBody()
666
667 omciMsg := &ic.InterAdapterOmciMessage{}
668 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
669 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
670 return err
671 }
672
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700673 if omciMsg.GetProxyAddress() == nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700674 onuDevice, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, toDeviceID)
675 if err != nil {
676 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceID, "error": err})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700677 return err
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700678 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700679 log.Debugw("device retrieved from core", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
680 dh.sendProxiedMessage(onuDevice, omciMsg)
681
cuilin20187b2a8c32019-03-26 19:52:28 -0700682 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700683 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 -0700684 dh.sendProxiedMessage(nil, omciMsg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700685 }
686
687 } else {
688 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
689 }
690 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530691}
692
cuilin20187b2a8c32019-03-26 19:52:28 -0700693func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700694 var intfID uint32
695 var onuID uint32
696 var connectStatus common.ConnectStatus_ConnectStatus
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700697 if onuDevice != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700698 intfID = onuDevice.ProxyAddress.GetChannelId()
699 onuID = onuDevice.ProxyAddress.GetOnuId()
700 connectStatus = onuDevice.ConnectStatus
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700701 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700702 intfID = omciMsg.GetProxyAddress().GetChannelId()
703 onuID = omciMsg.GetProxyAddress().GetOnuId()
704 connectStatus = omciMsg.GetConnectStatus()
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700705 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700706 if connectStatus != voltha.ConnectStatus_REACHABLE {
707 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfID": intfID, "onuID": onuID})
cuilin20187b2a8c32019-03-26 19:52:28 -0700708 return
709 }
710
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700711 omciMessage := &oop.OmciMsg{IntfId: intfID, OnuId: onuID, Pkt: omciMsg.Message}
cuilin20187b2a8c32019-03-26 19:52:28 -0700712
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700713 _, err := dh.Client.OmciMsgOut(context.Background(), omciMessage)
714 if err != nil {
715 log.Errorw("unable to send omci-msg-out", log.Fields{"IntfID": intfID, "OnuID": onuID, "Msg": omciMessage})
716 return
717 }
718 log.Debugw("omci-message-sent", log.Fields{"intfID": intfID, "onuID": onuID, "omciMsg": string(omciMsg.GetMessage())})
cuilin20187b2a8c32019-03-26 19:52:28 -0700719}
720
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700721func (dh *DeviceHandler) activateONU(intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) {
722 log.Debugw("activate-onu", log.Fields{"intfID": intfID, "onuID": onuID, "serialNum": serialNum, "serialNumber": serialNumber})
723 dh.flowMgr.UpdateOnuInfo(intfID, uint32(onuID), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700724 // TODO: need resource manager
725 var pir uint32 = 1000000
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700726 Onu := oop.Onu{IntfId: intfID, OnuId: uint32(onuID), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530727 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400728 st, _ := status.FromError(err)
729 if st.Code() == codes.AlreadyExists {
730 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
731 } else {
732 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
733 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700734 } else {
735 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
736 }
737}
738
Matt Jeanneret53539512019-07-20 14:47:02 -0400739func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, sn string) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700740 channelID := onuDiscInd.GetIntfId()
741 parentPortNo := IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Matt Jeanneret53539512019-07-20 14:47:02 -0400742
743 log.Debugw("new-discovery-indication", log.Fields{"sn": sn})
744 dh.lockDevice.Lock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400745 if _, ok := dh.discOnus[sn]; ok {
Matt Jeanneret53539512019-07-20 14:47:02 -0400746 dh.lockDevice.Unlock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400747 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
Matt Jeanneret53539512019-07-20 14:47:02 -0400748 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700749 }
750
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400751 dh.discOnus[sn] = true
Matt Jeanneret53539512019-07-20 14:47:02 -0400752 log.Debugw("new-discovery-indications-list", log.Fields{"discOnus": dh.discOnus})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400753 dh.lockDevice.Unlock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400754
cuilin20187b2a8c32019-03-26 19:52:28 -0700755 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400756 if sn != "" {
757 kwargs["serial_number"] = sn
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400758 } else {
Matt Jeanneret53539512019-07-20 14:47:02 -0400759 log.Errorw("invalid onu serial number", log.Fields{"sn": sn})
760 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400761 }
762
763 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
764 var onuID uint32
765 if onuDevice == nil || err != nil {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700766 //This is the first time ONU discovered. Create an OnuID for it.
Matt Jeanneret53539512019-07-20 14:47:02 -0400767 ponintfid := onuDiscInd.GetIntfId()
768 dh.lockDevice.Lock()
769 onuID, err = dh.resourceMgr.GetONUID(ponintfid)
770 dh.lockDevice.Unlock()
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400771 if err != nil {
Matt Jeanneret53539512019-07-20 14:47:02 -0400772 log.Errorw("failed to fetch onuID from resource manager", log.Fields{"pon-intf-id": ponintfid, "err": err})
773 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400774 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700775 if onuDevice, err = dh.coreProxy.ChildDeviceDetected(context.TODO(), dh.device.Id, int(parentPortNo),
Chaitrashree G See824a22019-07-28 18:28:27 -0400776 "", int(channelID),
Mahir Gunyele77977b2019-06-27 05:36:22 -0700777 string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuID)); onuDevice == nil {
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400778 log.Errorw("Create onu error",
779 log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(),
780 "onuID": onuID, "sn": sn, "error": err})
Matt Jeanneret53539512019-07-20 14:47:02 -0400781 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400782 }
Matt Jeanneret53539512019-07-20 14:47:02 -0400783 log.Debugw("onu-child-device-added", log.Fields{"onuDevice": onuDevice})
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400784
785 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700786 //ONU already discovered before. Use the same OnuID.
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400787 onuID = onuDevice.ProxyAddress.OnuId
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400788 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700789 //Insert the ONU into cache to use in OnuIndication.
790 //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 -0700791 log.Debugw("ONU discovery indication key create", log.Fields{"onuID": onuID,
792 "intfId": onuDiscInd.GetIntfId()})
Mahir Gunyele77977b2019-06-27 05:36:22 -0700793 onuKey := dh.formOnuKey(onuDiscInd.GetIntfId(), onuID)
Matt Jeanneret53539512019-07-20 14:47:02 -0400794
795 dh.lockDevice.Lock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700796 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuID, onuDiscInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
Matt Jeanneret53539512019-07-20 14:47:02 -0400797 log.Debugw("new-onu-device-discovered", log.Fields{"onu": dh.onus[onuKey]})
798 dh.lockDevice.Unlock()
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400799
Mahir Gunyele77977b2019-06-27 05:36:22 -0700800 err = dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
801 if err != nil {
Matt Jeanneret53539512019-07-20 14:47:02 -0400802 log.Errorw("failed to update device state", log.Fields{"DeviceID": onuDevice.Id, "err": err})
803 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700804 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700805 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
806 //TODO: We put this sleep here to prevent the race between state update and onuIndication
807 //In onuIndication the operStatus of device is checked. If it is still not updated in KV store
808 //then the initialisation fails.
809 time.Sleep(1 * time.Second)
810 dh.activateONU(onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn)
Matt Jeanneret53539512019-07-20 14:47:02 -0400811 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700812}
813
814func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
815 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
816
817 kwargs := make(map[string]interface{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700818 ponPort := IntfIDToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
Mahir Gunyele77977b2019-06-27 05:36:22 -0700819 var onuDevice *voltha.Device
820 foundInCache := false
Scott Baker7eb0a932019-07-26 10:33:22 -0700821 log.Debugw("ONU indication key create", log.Fields{"onuId": onuInd.OnuId,
822 "intfId": onuInd.GetIntfId()})
Mahir Gunyele77977b2019-06-27 05:36:22 -0700823 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.OnuId)
824 if onuInCache, ok := dh.onus[onuKey]; ok {
825 //If ONU id is discovered before then use GetDevice to get onuDevice because it is cheaper.
826 foundInCache = true
827 onuDevice, _ = dh.coreProxy.GetDevice(nil, dh.device.Id, onuInCache.deviceID)
cuilin20187b2a8c32019-03-26 19:52:28 -0700828 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700829 //If ONU not found in adapter cache then we have to use GetChildDevice to get onuDevice
830 if serialNumber != "" {
831 kwargs["serial_number"] = serialNumber
832 } else {
833 kwargs["onu_id"] = onuInd.OnuId
834 kwargs["parent_port_no"] = ponPort
835 }
836 onuDevice, _ = dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
cuilin20187b2a8c32019-03-26 19:52:28 -0700837 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700838
839 if onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400840 if onuDevice.ParentPortNo != ponPort {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700841 //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 -0400842 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700843 }
844
845 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
846 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})
847 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700848 if !foundInCache {
849 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId())
Matt Jeanneret53539512019-07-20 14:47:02 -0400850 dh.lockDevice.Lock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700851 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
Matt Jeanneret53539512019-07-20 14:47:02 -0400852 dh.lockDevice.Unlock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700853 }
Scott Baker7eb0a932019-07-26 10:33:22 -0700854 dh.updateOnuStates(onuDevice, onuInd, foundInCache)
cuilin20187b2a8c32019-03-26 19:52:28 -0700855
cuilin20187b2a8c32019-03-26 19:52:28 -0700856 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700857 log.Errorw("onu not found", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId})
cuilin20187b2a8c32019-03-26 19:52:28 -0700858 return
859 }
860
861}
862
Scott Baker7eb0a932019-07-26 10:33:22 -0700863func (dh *DeviceHandler) updateOnuStates(onuDevice *voltha.Device, onuInd *oop.OnuIndication, foundInCache bool) {
Matt Jeanneret53539512019-07-20 14:47:02 -0400864 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 -0700865 dh.updateOnuAdminState(onuInd)
866 // operState
867 if onuInd.OperState == "down" {
Matt Jeanneret53539512019-07-20 14:47:02 -0400868 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 -0700869 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
870 err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
871 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
872 if err != nil {
873 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
874 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
875 }
876 } else if onuInd.OperState == "up" {
Scott Baker7eb0a932019-07-26 10:33:22 -0700877 // Ignore operstatus if device was found in cache
878 if !foundInCache && onuDevice.OperStatus != common.OperStatus_DISCOVERED {
Matt Jeanneret53539512019-07-20 14:47:02 -0400879 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 -0700880 return
881 }
Matt Jeanneret53539512019-07-20 14:47:02 -0400882 log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState})
883 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700884 err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
885 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
886 if err != nil {
887 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
888 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
889 return
890 }
891 } else {
892 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
893 }
894}
895
896func (dh *DeviceHandler) updateOnuAdminState(onuInd *oop.OnuIndication) {
897 if onuInd.AdminState == "down" {
898 if onuInd.OperState != "down" {
899 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
900 // Forcing the oper state change code to execute
901 onuInd.OperState = "down"
902 }
903 // Port and logical port update is taken care of by oper state block
904 } else if onuInd.AdminState == "up" {
905 log.Debugln("received-onu-admin-state up")
906 } else {
907 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
908 }
909 log.Debugln("admin-state-dealt-with")
910}
911
cuilin20187b2a8c32019-03-26 19:52:28 -0700912func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
913 if serialNum != nil {
914 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
cuilin20187b2a8c32019-03-26 19:52:28 -0700915 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700916 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700917}
918
919func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
920 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700921 fmt.Sprintf("%x", uint32(vendorSpecific[0]&0x0f)) +
cuilin20187b2a8c32019-03-26 19:52:28 -0700922 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
923 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
924 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
925 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
926 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
927 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
928 return tmp
929}
930
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700931//UpdateFlowsBulk upates the bulk flow
932func (dh *DeviceHandler) UpdateFlowsBulk() error {
933 return errors.New("unimplemented")
cuilin20187b2a8c32019-03-26 19:52:28 -0700934}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700935
936//GetChildDevice returns the child device for given parent port and onu id
937func (dh *DeviceHandler) GetChildDevice(parentPort, onuID uint32) *voltha.Device {
938 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuID": onuID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400939 kwargs := make(map[string]interface{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700940 kwargs["onu_id"] = onuID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400941 kwargs["parent_port_no"] = parentPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700942 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400943 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700944 log.Errorw("onu not found", log.Fields{"intfID": parentPort, "onuID": onuID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400945 return nil
946 }
947 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
948 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530949}
950
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700951// SendPacketInToCore sends packet-in to core
952// For this, it calls SendPacketIn of the core-proxy which uses a device specific topic to send the request.
953// The adapter handling the device creates a device specific topic
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400954func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
955 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700956 if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPort, packetPayload); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400957 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
958 return
959 }
960 log.Debug("Sent packet-in to core successfully")
961}
962
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700963//UpdateFlowsIncrementally updates the device flow
Manikkaraj kb1d51442019-07-23 10:41:02 -0400964func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error {
965 log.Debugw("Received-incremental-flowupdate-in-device-handler", log.Fields{"deviceID": device.Id, "flows": flows, "groups": groups, "flowMetadata": flowMetadata})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400966 if flows != nil {
967 for _, flow := range flows.ToAdd.Items {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400968 log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400969 dh.flowMgr.AddFlow(flow, flowMetadata)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400970 }
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400971 for _, flow := range flows.ToRemove.Items {
972 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
973 dh.flowMgr.RemoveFlow(flow)
974 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400975 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700976 if groups != nil && flows != nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400977 for _, flow := range flows.ToRemove.Items {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700978 log.Debug("Removing flow", log.Fields{"deviceID": device.Id, "flowToRemove": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400979 // dh.flowMgr.RemoveFlow(flow)
980 }
981 }
Manikkaraj kb1d51442019-07-23 10:41:02 -0400982 log.Debug("UpdateFlowsIncrementally done successfully")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400983 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530984}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400985
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700986//DisableDevice disables the given device
987//It marks the following for the given device:
988//Device-Handler Admin-State : down
989//Device Port-State: UNKNOWN
990//Device Oper-State: UNKNOWN
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400991func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
Chaitrashree G S44124192019-08-07 20:21:36 -0400992 /* On device disable ,admin state update has to be done prior sending request to agent since
993 the indication thread may processes invalid indications of ONU and OLT*/
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400994 dh.lockDevice.Lock()
995 dh.adminState = "down"
996 dh.lockDevice.Unlock()
Chaitrashree G S44124192019-08-07 20:21:36 -0400997 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
998 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
999 dh.lockDevice.Lock()
1000 dh.adminState = "up"
1001 dh.lockDevice.Unlock()
1002 return err
1003 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001004 log.Debug("olt-disabled")
Chaitrashree G S44124192019-08-07 20:21:36 -04001005 dh.lockDevice.Lock()
1006 /* Discovered ONUs entries need to be cleared , since on device disable the child devices goes to
1007 UNREACHABLE state which needs to be configured again*/
1008 dh.discOnus = make(map[string]bool)
1009 dh.lockDevice.Unlock()
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001010
1011 cloned := proto.Clone(device).(*voltha.Device)
1012 // Update the all ports state on that device to disable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001013 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
1014 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001015 return err
1016 }
1017
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001018 log.Debugw("Disable_device-end", log.Fields{"deviceID": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001019 return nil
1020}
1021
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001022//ReenableDevice re-enables the olt device after disable
1023//It marks the following for the given device:
1024//Device-Handler Admin-State : up
1025//Device Port-State: ACTIVE
1026//Device Oper-State: ACTIVE
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001027func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
1028 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
1029 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
1030 return err
1031 }
1032
1033 dh.lockDevice.Lock()
1034 dh.adminState = "up"
1035 dh.lockDevice.Unlock()
1036 log.Debug("olt-reenabled")
1037
1038 cloned := proto.Clone(device).(*voltha.Device)
1039 // Update the all ports state on that device to enable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001040 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
1041 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001042 return err
1043 }
1044
1045 //Update the device oper status as ACTIVE
1046 cloned.OperStatus = voltha.OperStatus_ACTIVE
1047 dh.device = cloned
1048
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001049 if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
1050 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001051 return err
1052 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001053 log.Debugw("ReEnableDevice-end", log.Fields{"deviceID": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001054
1055 return nil
1056}
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001057
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001058//RebootDevice reboots the given device
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -04001059func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
1060 if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
1061 log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
1062 return err
1063 }
1064
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001065 log.Debugw("rebooted-device-successfully", log.Fields{"deviceID": device.Id})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -04001066
1067 return nil
1068}
1069
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001070func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
1071 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
1072 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
1073 if err != nil {
1074 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
1075 return
1076 }
1077 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001078 if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001079 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
1080 return
1081 }
1082 log.Debug("Success sending packet-in to core!")
1083}
1084
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001085// PacketOut sends packet-out from VOLTHA to OLT on the egress port provided
1086func (dh *DeviceHandler) PacketOut(egressPortNo int, packet *of.OfpPacketOut) error {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001087 log.Debugw("incoming-packet-out", log.Fields{"deviceID": dh.deviceID, "egress_port_no": egressPortNo,
1088 "pkt-length": len(packet.Data), "packetData": hex.EncodeToString(packet.Data)})
1089
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001090 egressPortType := IntfIDToPortTypeName(uint32(egressPortNo))
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001091 if egressPortType == voltha.Port_ETHERNET_UNI {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001092 outerEthType := (uint16(packet.Data[12]) << 8) | uint16(packet.Data[13])
1093 innerEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
1094 if outerEthType == 0x88a8 || outerEthType == 0x8100 {
1095 if innerEthType == 0x8100 {
1096 // q-in-q 802.1ad or 802.1q double tagged packet.
1097 // slice out the outer tag.
1098 packet.Data = append(packet.Data[:12], packet.Data[16:]...)
1099 log.Debugw("packet-now-single-tagged", log.Fields{"packetData": hex.EncodeToString(packet.Data)})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001100 }
1101 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001102 intfID := IntfIDFromUniPortNum(uint32(egressPortNo))
1103 onuID := OnuIDFromPortNum(uint32(egressPortNo))
Manikkaraj kb1d51442019-07-23 10:41:02 -04001104 uniID := UniIDFromPortNum(uint32(egressPortNo))
1105
1106 gemPortID, err := dh.flowMgr.GetPacketOutGemPortID(intfID, onuID, uint32(egressPortNo))
1107 if err != nil {
1108 // In this case the openolt agent will receive the gemPortID as 0.
1109 // The agent tries to retrieve the gemPortID in this case.
1110 // This may not always succeed at the agent and packetOut may fail.
1111 log.Error("failed-to-retrieve-gemport-id-for-packet-out")
1112 }
1113
1114 onuPkt := oop.OnuPacket{IntfId: intfID, OnuId: onuID, PortNo: uint32(egressPortNo), GemportId: gemPortID, Pkt: packet.Data}
Matt Jeanneret1359c732019-08-01 21:40:02 -04001115
1116 log.Debugw("sending-packet-to-onu", log.Fields{"egress_port_no": egressPortNo, "IntfId": intfID, "onuID": onuID,
Manikkaraj kb1d51442019-07-23 10:41:02 -04001117 "uniID": uniID, "gemPortID": gemPortID, "packet": hex.EncodeToString(packet.Data)})
Matt Jeanneret1359c732019-08-01 21:40:02 -04001118
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001119 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
1120 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
1121 return err
1122 }
1123 } else if egressPortType == voltha.Port_ETHERNET_NNI {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001124 uplinkPkt := oop.UplinkPacket{IntfId: IntfIDFromNniPortNum(uint32(egressPortNo)), Pkt: packet.Data}
Matt Jeanneret1359c732019-08-01 21:40:02 -04001125
1126 log.Debugw("sending-packet-to-nni", log.Fields{"uplink_pkt": uplinkPkt, "packet": hex.EncodeToString(packet.Data)})
1127
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001128 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001129 log.Errorw("Error while sending packet-out to NNI", log.Fields{"error": err})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001130 return err
1131 }
1132 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001133 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 -04001134 }
1135 return nil
1136}
Mahir Gunyela3f9add2019-06-06 15:13:19 -07001137
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001138func (dh *DeviceHandler) formOnuKey(intfID, onuID uint32) string {
1139 return "" + strconv.Itoa(int(intfID)) + "." + strconv.Itoa(int(onuID))
Mahir Gunyela3f9add2019-06-06 15:13:19 -07001140}