blob: 1b2195c4cc48894a030635c45695459f033e14d2 [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 }
Chaitrashree G S44124192019-08-07 20:21:36 -0400275 dh.lockDevice.RLock()
276 adminState := dh.adminState
277 dh.lockDevice.RUnlock()
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400278 // When OLT is admin down, allow only NNI operation status change indications.
Chaitrashree G S44124192019-08-07 20:21:36 -0400279 if adminState == "down" {
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400280 _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd)
281 if isIntfOperInd {
282 intfOperInd := indication.GetIntfOperInd()
283 if intfOperInd.GetType() == "nni" {
284 log.Infow("olt is admin down, allow nni ind", log.Fields{})
285 }
286 } else {
287 log.Infow("olt is admin down, ignore indication", log.Fields{})
288 continue
289 }
290 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530291
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700292 dh.handleIndication(indication)
manikkaraj kbf256be2019-03-25 00:13:48 +0530293
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700294 }
295}
296
297func (dh *DeviceHandler) handleOltIndication(oltIndication *oop.OltIndication) {
Daniele Rossi051466a2019-07-26 13:39:37 +0000298 raisedTs := time.Now().UnixNano()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700299 if oltIndication.OperState == "up" {
300 dh.transitionMap.Handle(DeviceUpInd)
301 } else if oltIndication.OperState == "down" {
302 dh.transitionMap.Handle(DeviceDownInd)
303 }
Daniele Rossi051466a2019-07-26 13:39:37 +0000304 // Send or clear Alarm
305 dh.eventMgr.oltUpDownIndication(oltIndication, dh.deviceID, raisedTs)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700306}
307
308func (dh *DeviceHandler) handleIndication(indication *oop.Indication) {
Devmalya Paulfb990a52019-07-09 10:01:49 -0400309 raisedTs := time.Now().UnixNano()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700310 switch indication.Data.(type) {
311 case *oop.Indication_OltInd:
312 dh.handleOltIndication(indication.GetOltInd())
313 case *oop.Indication_IntfInd:
314 intfInd := indication.GetIntfInd()
315 go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
316 log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd})
317 case *oop.Indication_IntfOperInd:
318 intfOperInd := indication.GetIntfOperInd()
319 if intfOperInd.GetType() == "nni" {
320 go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
321 } else if intfOperInd.GetType() == "pon" {
322 // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
323 // Handle pon port update
cuilin20187b2a8c32019-03-26 19:52:28 -0700324 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700325 log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
326 case *oop.Indication_OnuDiscInd:
327 onuDiscInd := indication.GetOnuDiscInd()
328 log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700329 sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
Matt Jeanneret53539512019-07-20 14:47:02 -0400330 go dh.onuDiscIndication(onuDiscInd, sn)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700331 case *oop.Indication_OnuInd:
332 onuInd := indication.GetOnuInd()
333 log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
334 go dh.onuIndication(onuInd)
335 case *oop.Indication_OmciInd:
336 omciInd := indication.GetOmciInd()
337 log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd})
338 if err := dh.omciIndication(omciInd); err != nil {
339 log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd})
340 }
341 case *oop.Indication_PktInd:
342 pktInd := indication.GetPktInd()
343 log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
344 go dh.handlePacketIndication(pktInd)
345 case *oop.Indication_PortStats:
346 portStats := indication.GetPortStats()
347 log.Infow("Received port stats indication", log.Fields{"PortStats": portStats})
348 case *oop.Indication_FlowStats:
349 flowStats := indication.GetFlowStats()
350 log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats})
351 case *oop.Indication_AlarmInd:
352 alarmInd := indication.GetAlarmInd()
353 log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd})
Devmalya Paulfb990a52019-07-09 10:01:49 -0400354 dh.eventMgr.ProcessEvents(alarmInd, dh.deviceID, raisedTs)
355
cuilin20187b2a8c32019-03-26 19:52:28 -0700356 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530357}
358
359// doStateUp handle the olt up indication and update to voltha core
360func (dh *DeviceHandler) doStateUp() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400361 // Synchronous call to update device state - this method is run in its own go routine
cuilin20187b2a8c32019-03-26 19:52:28 -0700362 if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
Girish Gowdru0c588b22019-04-23 23:24:56 -0400363 voltha.OperStatus_ACTIVE); err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700364 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 -0400365 return err
366 }
367 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530368}
369
370// doStateDown handle the olt down indication
371func (dh *DeviceHandler) doStateDown() error {
Girish Gowdrud4245152019-05-10 00:47:31 -0400372 log.Debug("do-state-down-start")
373
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700374 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdrud4245152019-05-10 00:47:31 -0400375 if err != nil || device == nil {
376 /*TODO: needs to handle error scenarios */
377 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700378 return errors.New("failed to fetch device device")
Girish Gowdrud4245152019-05-10 00:47:31 -0400379 }
380
381 cloned := proto.Clone(device).(*voltha.Device)
382 // Update the all ports state on that device to disable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700383 if er := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); er != nil {
384 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": er})
385 return er
Girish Gowdrud4245152019-05-10 00:47:31 -0400386 }
387
388 //Update the device oper state and connection status
389 cloned.OperStatus = voltha.OperStatus_UNKNOWN
390 cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
391 dh.device = cloned
392
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700393 if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
394 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": er})
395 return er
Girish Gowdrud4245152019-05-10 00:47:31 -0400396 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400397
398 //get the child device for the parent device
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700399 onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id)
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400400 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700401 log.Errorw("failed to get child devices information", log.Fields{"deviceID": dh.device.Id, "error": err})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400402 return err
403 }
404 for _, onuDevice := range onuDevices.Items {
405
406 // Update onu state as down in onu adapter
407 onuInd := oop.OnuIndication{}
408 onuInd.OperState = "down"
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700409 er := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
410 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
411 if er != nil {
412 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
413 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
414 return er
415 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400416 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700417 log.Debugw("do-state-down-end", log.Fields{"deviceID": device.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700418 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530419}
420
421// doStateInit dial the grpc before going to init state
422func (dh *DeviceHandler) doStateInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400423 var err error
Girish Gowdrud4245152019-05-10 00:47:31 -0400424 dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock())
Girish Gowdru0c588b22019-04-23 23:24:56 -0400425 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700426 log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceID, "HostAndPort": dh.device.GetHostAndPort(), "err": err})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400427 return err
428 }
429 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530430}
431
432// postInit create olt client instance to invoke RPC on the olt device
433func (dh *DeviceHandler) postInit() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400434 dh.Client = oop.NewOpenoltClient(dh.clientCon)
435 dh.transitionMap.Handle(GrpcConnected)
436 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530437}
438
439// doStateConnected get the device info and update to voltha core
440func (dh *DeviceHandler) doStateConnected() error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400441 log.Debug("OLT device has been connected")
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400442
443 // Case where OLT is disabled and then rebooted.
444 if dh.adminState == "down" {
445 log.Debugln("do-state-connected--device-admin-state-down")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700446 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400447 if err != nil || device == nil {
448 /*TODO: needs to handle error scenarios */
449 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
450 }
451
452 cloned := proto.Clone(device).(*voltha.Device)
453 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
454 cloned.OperStatus = voltha.OperStatus_UNKNOWN
455 dh.device = cloned
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700456 if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
457 log.Errorw("error-updating-device-state", log.Fields{"deviceID": dh.device.Id, "error": er})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400458 }
459
Chaitrashree G S44124192019-08-07 20:21:36 -0400460 // 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 -0400461 _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
462 if err != nil {
463 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
464 }
465
466 // Start reading indications
467 go dh.readIndications()
468 return nil
469 }
470
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400471 deviceInfo, err := dh.populateDeviceInfo()
cuilin20187b2a8c32019-03-26 19:52:28 -0700472 if err != nil {
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400473 log.Errorw("Unable to populate Device Info", log.Fields{"err": err})
cuilin20187b2a8c32019-03-26 19:52:28 -0700474 return err
475 }
Girish Gowdrud4245152019-05-10 00:47:31 -0400476
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700477 device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
Girish Gowdrud4245152019-05-10 00:47:31 -0400478 if err != nil || device == nil {
479 /*TODO: needs to handle error scenarios */
480 log.Errorw("Failed to fetch device device", log.Fields{"err": err})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700481 return err
Girish Gowdrud4245152019-05-10 00:47:31 -0400482 }
483 cloned := proto.Clone(device).(*voltha.Device)
484 // Update the all ports (if available) on that device to ACTIVE.
485 // The ports do not normally exist, unless the device is coming back from a reboot
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700486 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
487 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdrud4245152019-05-10 00:47:31 -0400488 return err
489 }
490
Girish Gowdru0c588b22019-04-23 23:24:56 -0400491 KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
492 // Instantiate resource manager
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700493 if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceID, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400494 log.Error("Error while instantiating resource manager")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700495 return errors.New("instantiating resource manager failed")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400496 }
497 // Instantiate flow manager
498 if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
499 log.Error("Error while instantiating flow manager")
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700500 return errors.New("instantiating flow manager failed")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400501 }
502 /* TODO: Instantiate Alarm , stats , BW managers */
Devmalya Paulfb990a52019-07-09 10:01:49 -0400503 /* Instantiating Event Manager to handle Alarms and KPIs */
504 dh.eventMgr = NewEventMgr(dh.EventProxy)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530505
cuilin20187b2a8c32019-03-26 19:52:28 -0700506 // Start reading indications
507 go dh.readIndications()
508 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530509}
510
Matt Jeanneretf4fdcd72019-07-19 20:03:23 -0400511func (dh *DeviceHandler) populateDeviceInfo() (*oop.DeviceInfo, error) {
512 var err error
513 var deviceInfo *oop.DeviceInfo
514
515 deviceInfo, err = dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
516
517 if err != nil {
518 log.Errorw("Failed to fetch device info", log.Fields{"err": err})
519 return nil, err
520 }
521 if deviceInfo == nil {
522 log.Errorw("Device info is nil", log.Fields{})
523 return nil, errors.New("failed to get device info from OLT")
524 }
525
526 log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo})
527 dh.device.Root = true
528 dh.device.Vendor = deviceInfo.Vendor
529 dh.device.Model = deviceInfo.Model
530 dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
531 dh.device.HardwareVersion = deviceInfo.HardwareVersion
532 dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
533
534 if deviceInfo.DeviceId == "" {
535 log.Warnw("no-device-id-provided-using-host", log.Fields{"hostport": dh.device.GetHostAndPort()})
536 host := strings.Split(dh.device.GetHostAndPort(), ":")[0]
537 genmac, err := generateMacFromHost(host)
538 if err != nil {
539 return nil, err
540 }
541 log.Debugw("using-host-for-mac-address", log.Fields{"host": host, "mac": genmac})
542 dh.device.MacAddress = genmac
543 } else {
544 dh.device.MacAddress = deviceInfo.DeviceId
545 }
546
547 // Synchronous call to update device - this method is run in its own go routine
548 if err := dh.coreProxy.DeviceUpdate(context.TODO(), dh.device); err != nil {
549 log.Errorw("error-updating-device", log.Fields{"deviceID": dh.device.Id, "error": err})
550 return nil, err
551 }
552
553 return deviceInfo, nil
554}
555
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700556//AdoptDevice adopts the OLT device
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530557func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400558 dh.transitionMap = NewTransitionMap(dh)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700559 log.Infow("Adopt_device", log.Fields{"deviceID": device.Id, "Address": device.GetHostAndPort()})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400560 dh.transitionMap.Handle(DeviceInit)
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530561}
562
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700563//GetOfpDeviceInfo Gets the Ofp information of the given device
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530564func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700565 return &ic.SwitchCapability{
566 Desc: &of.OfpDesc{
Devmalya Paul70dd4972019-06-10 15:19:17 +0530567 MfrDesc: "VOLTHA Project",
cuilin20187b2a8c32019-03-26 19:52:28 -0700568 HwDesc: "open_pon",
569 SwDesc: "open_pon",
570 SerialNum: dh.device.SerialNumber,
571 },
572 SwitchFeatures: &of.OfpSwitchFeatures{
573 NBuffers: 256,
574 NTables: 2,
575 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
576 of.OfpCapabilities_OFPC_TABLE_STATS |
577 of.OfpCapabilities_OFPC_PORT_STATS |
578 of.OfpCapabilities_OFPC_GROUP_STATS),
579 },
580 }, nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530581}
582
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700583//GetOfpPortInfo Get Ofp port information
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530584func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700585 capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
cuilin20187b2a8c32019-03-26 19:52:28 -0700586 return &ic.PortCapability{
587 Port: &voltha.LogicalPort{
588 OfpPort: &of.OfpPort{
589 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
590 Config: 0,
591 State: uint32(of.OfpPortState_OFPPS_LIVE),
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700592 Curr: capacity,
593 Advertised: capacity,
594 Peer: capacity,
cuilin20187b2a8c32019-03-26 19:52:28 -0700595 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
596 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
597 },
598 DeviceId: dh.device.Id,
599 DevicePortNo: uint32(portNo),
600 },
601 }, nil
602}
603
604func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700605 log.Debugw("omci indication", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700606 var deviceType string
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700607 var deviceID string
608 var proxyDeviceID string
cuilin20187b2a8c32019-03-26 19:52:28 -0700609
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700610 onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId)
611 if onuInCache, ok := dh.onus[onuKey]; !ok {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700612 log.Debugw("omci indication for a device not in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
613 ponPort := IntfIDToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700614 kwargs := make(map[string]interface{})
615 kwargs["onu_id"] = omciInd.OnuId
616 kwargs["parent_port_no"] = ponPort
cuilin20187b2a8c32019-03-26 19:52:28 -0700617
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700618 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
619 if err != nil {
620 log.Errorw("onu not found", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700621 return err
cuilin20187b2a8c32019-03-26 19:52:28 -0700622 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700623 deviceType = onuDevice.Type
624 deviceID = onuDevice.Id
625 proxyDeviceID = onuDevice.ProxyAddress.DeviceId
626 //if not exist in cache, then add to cache.
627 dh.onus[onuKey] = NewOnuDevice(deviceID, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceID)
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700628 } else {
629 //found in cache
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700630 log.Debugw("omci indication for a device in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700631 deviceType = onuInCache.deviceType
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700632 deviceID = onuInCache.deviceID
633 proxyDeviceID = onuInCache.proxyDeviceID
cuilin20187b2a8c32019-03-26 19:52:28 -0700634 }
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700635
636 omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt}
637 if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg,
638 ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType,
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700639 deviceID, proxyDeviceID, ""); sendErr != nil {
640 log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuID": deviceID, "proxyDeviceID": proxyDeviceID})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700641 return sendErr
642 }
643 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530644}
645
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700646//ProcessInterAdapterMessage sends the proxied messages to the target device
647// If the proxy address is not found in the unmarshalled message, it first fetches the onu device for which the message
648// is meant, and then send the unmarshalled omci message to this onu
649func (dh *DeviceHandler) ProcessInterAdapterMessage(msg *ic.InterAdapterMessage) error {
650 log.Debugw("Process_inter_adapter_message", log.Fields{"msgID": msg.Header.Id})
cuilin20187b2a8c32019-03-26 19:52:28 -0700651 if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700652 msgID := msg.Header.Id
cuilin20187b2a8c32019-03-26 19:52:28 -0700653 fromTopic := msg.Header.FromTopic
654 toTopic := msg.Header.ToTopic
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700655 toDeviceID := msg.Header.ToDeviceId
656 proxyDeviceID := msg.Header.ProxyDeviceId
cuilin20187b2a8c32019-03-26 19:52:28 -0700657
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700658 log.Debugw("omci request message header", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
cuilin20187b2a8c32019-03-26 19:52:28 -0700659
660 msgBody := msg.GetBody()
661
662 omciMsg := &ic.InterAdapterOmciMessage{}
663 if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil {
664 log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err})
665 return err
666 }
667
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700668 if omciMsg.GetProxyAddress() == nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700669 onuDevice, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, toDeviceID)
670 if err != nil {
671 log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceID, "error": err})
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700672 return err
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700673 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700674 log.Debugw("device retrieved from core", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID})
675 dh.sendProxiedMessage(onuDevice, omciMsg)
676
cuilin20187b2a8c32019-03-26 19:52:28 -0700677 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700678 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 -0700679 dh.sendProxiedMessage(nil, omciMsg)
cuilin20187b2a8c32019-03-26 19:52:28 -0700680 }
681
682 } else {
683 log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type})
684 }
685 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530686}
687
cuilin20187b2a8c32019-03-26 19:52:28 -0700688func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700689 var intfID uint32
690 var onuID uint32
691 var connectStatus common.ConnectStatus_ConnectStatus
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700692 if onuDevice != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700693 intfID = onuDevice.ProxyAddress.GetChannelId()
694 onuID = onuDevice.ProxyAddress.GetOnuId()
695 connectStatus = onuDevice.ConnectStatus
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700696 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700697 intfID = omciMsg.GetProxyAddress().GetChannelId()
698 onuID = omciMsg.GetProxyAddress().GetOnuId()
699 connectStatus = omciMsg.GetConnectStatus()
Mahir Gunyela3f9add2019-06-06 15:13:19 -0700700 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700701 if connectStatus != voltha.ConnectStatus_REACHABLE {
702 log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfID": intfID, "onuID": onuID})
cuilin20187b2a8c32019-03-26 19:52:28 -0700703 return
704 }
705
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700706 omciMessage := &oop.OmciMsg{IntfId: intfID, OnuId: onuID, Pkt: omciMsg.Message}
cuilin20187b2a8c32019-03-26 19:52:28 -0700707
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700708 _, err := dh.Client.OmciMsgOut(context.Background(), omciMessage)
709 if err != nil {
710 log.Errorw("unable to send omci-msg-out", log.Fields{"IntfID": intfID, "OnuID": onuID, "Msg": omciMessage})
711 return
712 }
713 log.Debugw("omci-message-sent", log.Fields{"intfID": intfID, "onuID": onuID, "omciMsg": string(omciMsg.GetMessage())})
cuilin20187b2a8c32019-03-26 19:52:28 -0700714}
715
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700716func (dh *DeviceHandler) activateONU(intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) {
717 log.Debugw("activate-onu", log.Fields{"intfID": intfID, "onuID": onuID, "serialNum": serialNum, "serialNumber": serialNumber})
718 dh.flowMgr.UpdateOnuInfo(intfID, uint32(onuID), serialNumber)
cuilin20187b2a8c32019-03-26 19:52:28 -0700719 // TODO: need resource manager
720 var pir uint32 = 1000000
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700721 Onu := oop.Onu{IntfId: intfID, OnuId: uint32(onuID), SerialNumber: serialNum, Pir: pir}
manikkaraj kbf256be2019-03-25 00:13:48 +0530722 if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400723 st, _ := status.FromError(err)
724 if st.Code() == codes.AlreadyExists {
725 log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
726 } else {
727 log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err})
728 }
cuilin20187b2a8c32019-03-26 19:52:28 -0700729 } else {
730 log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber})
731 }
732}
733
Matt Jeanneret53539512019-07-20 14:47:02 -0400734func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, sn string) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700735 channelID := onuDiscInd.GetIntfId()
736 parentPortNo := IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
Matt Jeanneret53539512019-07-20 14:47:02 -0400737
738 log.Debugw("new-discovery-indication", log.Fields{"sn": sn})
739 dh.lockDevice.Lock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400740 if _, ok := dh.discOnus[sn]; ok {
Matt Jeanneret53539512019-07-20 14:47:02 -0400741 dh.lockDevice.Unlock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400742 log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn})
Matt Jeanneret53539512019-07-20 14:47:02 -0400743 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700744 }
745
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400746 dh.discOnus[sn] = true
Matt Jeanneret53539512019-07-20 14:47:02 -0400747 log.Debugw("new-discovery-indications-list", log.Fields{"discOnus": dh.discOnus})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400748 dh.lockDevice.Unlock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400749
cuilin20187b2a8c32019-03-26 19:52:28 -0700750 kwargs := make(map[string]interface{})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400751 if sn != "" {
752 kwargs["serial_number"] = sn
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400753 } else {
Matt Jeanneret53539512019-07-20 14:47:02 -0400754 log.Errorw("invalid onu serial number", log.Fields{"sn": sn})
755 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400756 }
757
758 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
759 var onuID uint32
760 if onuDevice == nil || err != nil {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700761 //This is the first time ONU discovered. Create an OnuID for it.
Matt Jeanneret53539512019-07-20 14:47:02 -0400762 ponintfid := onuDiscInd.GetIntfId()
763 dh.lockDevice.Lock()
764 onuID, err = dh.resourceMgr.GetONUID(ponintfid)
765 dh.lockDevice.Unlock()
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400766 if err != nil {
Matt Jeanneret53539512019-07-20 14:47:02 -0400767 log.Errorw("failed to fetch onuID from resource manager", log.Fields{"pon-intf-id": ponintfid, "err": err})
768 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400769 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700770 if onuDevice, err = dh.coreProxy.ChildDeviceDetected(context.TODO(), dh.device.Id, int(parentPortNo),
Chaitrashree G See824a22019-07-28 18:28:27 -0400771 "", int(channelID),
Mahir Gunyele77977b2019-06-27 05:36:22 -0700772 string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuID)); onuDevice == nil {
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400773 log.Errorw("Create onu error",
774 log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(),
775 "onuID": onuID, "sn": sn, "error": err})
Matt Jeanneret53539512019-07-20 14:47:02 -0400776 return
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400777 }
Matt Jeanneret53539512019-07-20 14:47:02 -0400778 log.Debugw("onu-child-device-added", log.Fields{"onuDevice": onuDevice})
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400779
780 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700781 //ONU already discovered before. Use the same OnuID.
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400782 onuID = onuDevice.ProxyAddress.OnuId
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400783 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700784 //Insert the ONU into cache to use in OnuIndication.
785 //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 -0700786 log.Debugw("ONU discovery indication key create", log.Fields{"onuID": onuID,
787 "intfId": onuDiscInd.GetIntfId()})
Mahir Gunyele77977b2019-06-27 05:36:22 -0700788 onuKey := dh.formOnuKey(onuDiscInd.GetIntfId(), onuID)
Matt Jeanneret53539512019-07-20 14:47:02 -0400789
790 dh.lockDevice.Lock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700791 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuID, onuDiscInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
Matt Jeanneret53539512019-07-20 14:47:02 -0400792 log.Debugw("new-onu-device-discovered", log.Fields{"onu": dh.onus[onuKey]})
793 dh.lockDevice.Unlock()
Chaitrashree G S35b5d802019-07-08 23:12:03 -0400794
Mahir Gunyele77977b2019-06-27 05:36:22 -0700795 err = dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
796 if err != nil {
Matt Jeanneret53539512019-07-20 14:47:02 -0400797 log.Errorw("failed to update device state", log.Fields{"DeviceID": onuDevice.Id, "err": err})
798 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700799 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700800 log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id})
801 //TODO: We put this sleep here to prevent the race between state update and onuIndication
802 //In onuIndication the operStatus of device is checked. If it is still not updated in KV store
803 //then the initialisation fails.
804 time.Sleep(1 * time.Second)
805 dh.activateONU(onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn)
Matt Jeanneret53539512019-07-20 14:47:02 -0400806 return
cuilin20187b2a8c32019-03-26 19:52:28 -0700807}
808
809func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) {
810 serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber)
811
812 kwargs := make(map[string]interface{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700813 ponPort := IntfIDToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT)
Mahir Gunyele77977b2019-06-27 05:36:22 -0700814 var onuDevice *voltha.Device
815 foundInCache := false
Scott Baker7eb0a932019-07-26 10:33:22 -0700816 log.Debugw("ONU indication key create", log.Fields{"onuId": onuInd.OnuId,
817 "intfId": onuInd.GetIntfId()})
Mahir Gunyele77977b2019-06-27 05:36:22 -0700818 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.OnuId)
819 if onuInCache, ok := dh.onus[onuKey]; ok {
820 //If ONU id is discovered before then use GetDevice to get onuDevice because it is cheaper.
821 foundInCache = true
822 onuDevice, _ = dh.coreProxy.GetDevice(nil, dh.device.Id, onuInCache.deviceID)
cuilin20187b2a8c32019-03-26 19:52:28 -0700823 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700824 //If ONU not found in adapter cache then we have to use GetChildDevice to get onuDevice
825 if serialNumber != "" {
826 kwargs["serial_number"] = serialNumber
827 } else {
828 kwargs["onu_id"] = onuInd.OnuId
829 kwargs["parent_port_no"] = ponPort
830 }
831 onuDevice, _ = dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
cuilin20187b2a8c32019-03-26 19:52:28 -0700832 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700833
834 if onuDevice != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400835 if onuDevice.ParentPortNo != ponPort {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700836 //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 -0400837 log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort})
cuilin20187b2a8c32019-03-26 19:52:28 -0700838 }
839
840 if onuDevice.ProxyAddress.OnuId != onuInd.OnuId {
841 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})
842 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700843 if !foundInCache {
844 onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId())
Matt Jeanneret53539512019-07-20 14:47:02 -0400845 dh.lockDevice.Lock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700846 dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)
Matt Jeanneret53539512019-07-20 14:47:02 -0400847 dh.lockDevice.Unlock()
Mahir Gunyele77977b2019-06-27 05:36:22 -0700848 }
Scott Baker7eb0a932019-07-26 10:33:22 -0700849 dh.updateOnuStates(onuDevice, onuInd, foundInCache)
cuilin20187b2a8c32019-03-26 19:52:28 -0700850
cuilin20187b2a8c32019-03-26 19:52:28 -0700851 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700852 log.Errorw("onu not found", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId})
cuilin20187b2a8c32019-03-26 19:52:28 -0700853 return
854 }
855
856}
857
Scott Baker7eb0a932019-07-26 10:33:22 -0700858func (dh *DeviceHandler) updateOnuStates(onuDevice *voltha.Device, onuInd *oop.OnuIndication, foundInCache bool) {
Matt Jeanneret53539512019-07-20 14:47:02 -0400859 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 -0700860 dh.updateOnuAdminState(onuInd)
861 // operState
862 if onuInd.OperState == "down" {
Matt Jeanneret53539512019-07-20 14:47:02 -0400863 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 -0700864 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
865 err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
866 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
867 if err != nil {
868 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
869 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
870 }
871 } else if onuInd.OperState == "up" {
Scott Baker7eb0a932019-07-26 10:33:22 -0700872 // Ignore operstatus if device was found in cache
873 if !foundInCache && onuDevice.OperStatus != common.OperStatus_DISCOVERED {
Matt Jeanneret53539512019-07-20 14:47:02 -0400874 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 -0700875 return
876 }
Matt Jeanneret53539512019-07-20 14:47:02 -0400877 log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState})
878 // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700879 err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
880 "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
881 if err != nil {
882 log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
883 "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id})
884 return
885 }
886 } else {
887 log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState})
888 }
889}
890
891func (dh *DeviceHandler) updateOnuAdminState(onuInd *oop.OnuIndication) {
892 if onuInd.AdminState == "down" {
893 if onuInd.OperState != "down" {
894 log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState})
895 // Forcing the oper state change code to execute
896 onuInd.OperState = "down"
897 }
898 // Port and logical port update is taken care of by oper state block
899 } else if onuInd.AdminState == "up" {
900 log.Debugln("received-onu-admin-state up")
901 } else {
902 log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState})
903 }
904 log.Debugln("admin-state-dealt-with")
905}
906
cuilin20187b2a8c32019-03-26 19:52:28 -0700907func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string {
908 if serialNum != nil {
909 return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific)
cuilin20187b2a8c32019-03-26 19:52:28 -0700910 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700911 return ""
cuilin20187b2a8c32019-03-26 19:52:28 -0700912}
913
914func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string {
915 tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) +
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700916 fmt.Sprintf("%x", uint32(vendorSpecific[0]&0x0f)) +
cuilin20187b2a8c32019-03-26 19:52:28 -0700917 fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) +
918 fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) +
919 fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) +
920 fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) +
921 fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) +
922 fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f)
923 return tmp
924}
925
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700926//UpdateFlowsBulk upates the bulk flow
927func (dh *DeviceHandler) UpdateFlowsBulk() error {
928 return errors.New("unimplemented")
cuilin20187b2a8c32019-03-26 19:52:28 -0700929}
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700930
931//GetChildDevice returns the child device for given parent port and onu id
932func (dh *DeviceHandler) GetChildDevice(parentPort, onuID uint32) *voltha.Device {
933 log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuID": onuID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400934 kwargs := make(map[string]interface{})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700935 kwargs["onu_id"] = onuID
Girish Gowdru0c588b22019-04-23 23:24:56 -0400936 kwargs["parent_port_no"] = parentPort
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700937 onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400938 if err != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700939 log.Errorw("onu not found", log.Fields{"intfID": parentPort, "onuID": onuID})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400940 return nil
941 }
942 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
943 return onuDevice
manikkaraj kbf256be2019-03-25 00:13:48 +0530944}
945
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700946// SendPacketInToCore sends packet-in to core
947// For this, it calls SendPacketIn of the core-proxy which uses a device specific topic to send the request.
948// The adapter handling the device creates a device specific topic
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400949func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) {
950 log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700951 if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPort, packetPayload); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400952 log.Errorw("Error sending packetin to core", log.Fields{"error": err})
953 return
954 }
955 log.Debug("Sent packet-in to core successfully")
956}
957
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700958//UpdateFlowsIncrementally updates the device flow
manikkaraj kbf256be2019-03-25 00:13:48 +0530959func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700960 log.Debugw("In Update_flows_incrementally", log.Fields{"deviceID": device.Id, "flows": flows, "groups": groups})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400961 if flows != nil {
962 for _, flow := range flows.ToAdd.Items {
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400963 log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400964 dh.flowMgr.AddFlow(flow)
965 }
Manjunath Vanarajulu28c3e822019-05-16 11:14:28 -0400966 for _, flow := range flows.ToRemove.Items {
967 log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
968 dh.flowMgr.RemoveFlow(flow)
969 }
Girish Gowdru0c588b22019-04-23 23:24:56 -0400970 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700971 if groups != nil && flows != nil {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400972 for _, flow := range flows.ToRemove.Items {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700973 log.Debug("Removing flow", log.Fields{"deviceID": device.Id, "flowToRemove": flow})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400974 // dh.flowMgr.RemoveFlow(flow)
975 }
976 }
977 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530978}
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400979
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700980//DisableDevice disables the given device
981//It marks the following for the given device:
982//Device-Handler Admin-State : down
983//Device Port-State: UNKNOWN
984//Device Oper-State: UNKNOWN
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400985func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error {
Chaitrashree G S44124192019-08-07 20:21:36 -0400986 /* On device disable ,admin state update has to be done prior sending request to agent since
987 the indication thread may processes invalid indications of ONU and OLT*/
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400988 dh.lockDevice.Lock()
989 dh.adminState = "down"
990 dh.lockDevice.Unlock()
Chaitrashree G S44124192019-08-07 20:21:36 -0400991 if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil {
992 log.Errorw("Failed to disable olt ", log.Fields{"err": err})
993 dh.lockDevice.Lock()
994 dh.adminState = "up"
995 dh.lockDevice.Unlock()
996 return err
997 }
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400998 log.Debug("olt-disabled")
Chaitrashree G S44124192019-08-07 20:21:36 -0400999 dh.lockDevice.Lock()
1000 /* Discovered ONUs entries need to be cleared , since on device disable the child devices goes to
1001 UNREACHABLE state which needs to be configured again*/
1002 dh.discOnus = make(map[string]bool)
1003 dh.lockDevice.Unlock()
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001004
1005 cloned := proto.Clone(device).(*voltha.Device)
1006 // Update the all ports state on that device to disable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001007 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
1008 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001009 return err
1010 }
1011
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001012 log.Debugw("Disable_device-end", log.Fields{"deviceID": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001013 return nil
1014}
1015
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001016//ReenableDevice re-enables the olt device after disable
1017//It marks the following for the given device:
1018//Device-Handler Admin-State : up
1019//Device Port-State: ACTIVE
1020//Device Oper-State: ACTIVE
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001021func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error {
1022 if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil {
1023 log.Errorw("Failed to reenable olt ", log.Fields{"err": err})
1024 return err
1025 }
1026
1027 dh.lockDevice.Lock()
1028 dh.adminState = "up"
1029 dh.lockDevice.Unlock()
1030 log.Debug("olt-reenabled")
1031
1032 cloned := proto.Clone(device).(*voltha.Device)
1033 // Update the all ports state on that device to enable
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001034 if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
1035 log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001036 return err
1037 }
1038
1039 //Update the device oper status as ACTIVE
1040 cloned.OperStatus = voltha.OperStatus_ACTIVE
1041 dh.device = cloned
1042
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001043 if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
1044 log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001045 return err
1046 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001047 log.Debugw("ReEnableDevice-end", log.Fields{"deviceID": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -04001048
1049 return nil
1050}
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001051
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001052//RebootDevice reboots the given device
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -04001053func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
1054 if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
1055 log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
1056 return err
1057 }
1058
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001059 log.Debugw("rebooted-device-successfully", log.Fields{"deviceID": device.Id})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -04001060
1061 return nil
1062}
1063
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001064func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
1065 log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
1066 logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
1067 if err != nil {
1068 log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err})
1069 return
1070 }
1071 log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn})
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001072 if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001073 log.Errorw("Error sending packet-in to core", log.Fields{"error": err})
1074 return
1075 }
1076 log.Debug("Success sending packet-in to core!")
1077}
1078
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001079// PacketOut sends packet-out from VOLTHA to OLT on the egress port provided
1080func (dh *DeviceHandler) PacketOut(egressPortNo int, packet *of.OfpPacketOut) error {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001081 log.Debugw("incoming-packet-out", log.Fields{"deviceID": dh.deviceID, "egress_port_no": egressPortNo,
1082 "pkt-length": len(packet.Data), "packetData": hex.EncodeToString(packet.Data)})
1083
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001084 egressPortType := IntfIDToPortTypeName(uint32(egressPortNo))
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001085 if egressPortType == voltha.Port_ETHERNET_UNI {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001086 outerEthType := (uint16(packet.Data[12]) << 8) | uint16(packet.Data[13])
1087 innerEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17])
1088 if outerEthType == 0x88a8 || outerEthType == 0x8100 {
1089 if innerEthType == 0x8100 {
1090 // q-in-q 802.1ad or 802.1q double tagged packet.
1091 // slice out the outer tag.
1092 packet.Data = append(packet.Data[:12], packet.Data[16:]...)
1093 log.Debugw("packet-now-single-tagged", log.Fields{"packetData": hex.EncodeToString(packet.Data)})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001094 }
1095 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001096 intfID := IntfIDFromUniPortNum(uint32(egressPortNo))
1097 onuID := OnuIDFromPortNum(uint32(egressPortNo))
Matt Jeanneret1359c732019-08-01 21:40:02 -04001098 uniID := uint32(egressPortNo)
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001099 onuPkt := oop.OnuPacket{IntfId: intfID, OnuId: onuID, PortNo: uint32(egressPortNo), Pkt: packet.Data}
Matt Jeanneret1359c732019-08-01 21:40:02 -04001100
1101 log.Debugw("sending-packet-to-onu", log.Fields{"egress_port_no": egressPortNo, "IntfId": intfID, "onuID": onuID,
1102 "uniID": uniID, "packet": hex.EncodeToString(packet.Data)})
1103
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001104 if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
1105 log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err})
1106 return err
1107 }
1108 } else if egressPortType == voltha.Port_ETHERNET_NNI {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001109 uplinkPkt := oop.UplinkPacket{IntfId: IntfIDFromNniPortNum(uint32(egressPortNo)), Pkt: packet.Data}
Matt Jeanneret1359c732019-08-01 21:40:02 -04001110
1111 log.Debugw("sending-packet-to-nni", log.Fields{"uplink_pkt": uplinkPkt, "packet": hex.EncodeToString(packet.Data)})
1112
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001113 if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
Matt Jeanneret1359c732019-08-01 21:40:02 -04001114 log.Errorw("Error while sending packet-out to NNI", log.Fields{"error": err})
manikkaraj k9eb6cac2019-05-09 12:32:03 -04001115 return err
1116 }
1117 } else {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001118 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 -04001119 }
1120 return nil
1121}
Mahir Gunyela3f9add2019-06-06 15:13:19 -07001122
Girish Gowdru6a80bbd2019-07-02 07:36:09 -07001123func (dh *DeviceHandler) formOnuKey(intfID, onuID uint32) string {
1124 return "" + strconv.Itoa(int(intfID)) + "." + strconv.Itoa(int(onuID))
Mahir Gunyela3f9add2019-06-06 15:13:19 -07001125}