Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 1 | /* |
| 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 Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 16 | |
| 17 | //Package adaptercore provides the utility for olt devices, flows and statistics |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 18 | package adaptercore |
| 19 | |
| 20 | import ( |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 21 | "context" |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 22 | "encoding/hex" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 23 | "errors" |
| 24 | "fmt" |
| 25 | "io" |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 26 | "net" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 27 | "strconv" |
| 28 | "strings" |
| 29 | "sync" |
| 30 | "time" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 31 | |
Chaitrashree G S | b2b62dd | 2019-07-24 21:47:04 -0400 | [diff] [blame] | 32 | "google.golang.org/grpc/codes" |
| 33 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 34 | "github.com/gogo/protobuf/proto" |
| 35 | "github.com/golang/protobuf/ptypes" |
Scott Baker | 5129015 | 2019-10-24 14:23:20 -0700 | [diff] [blame] | 36 | "github.com/opencord/voltha-lib-go/v2/pkg/adapters/adapterif" |
| 37 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 38 | rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager" |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 39 | "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 k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 43 | "github.com/opencord/voltha-protos/go/voltha" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 44 | "google.golang.org/grpc" |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 45 | "google.golang.org/grpc/status" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 46 | ) |
| 47 | |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 48 | // Constants for number of retries and for timeout |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 49 | const ( |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 50 | MaxRetry = 10 |
| 51 | MaxTimeOutInMs = 500 |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 52 | ) |
| 53 | |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 54 | func init() { |
| 55 | _, _ = log.AddPackage(log.JSON, log.DebugLevel, nil) |
| 56 | } |
| 57 | |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 58 | //DeviceHandler will interact with the OLT device. |
| 59 | type DeviceHandler struct { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 60 | deviceID string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 61 | deviceType string |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 62 | adminState string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 63 | device *voltha.Device |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 64 | coreProxy adapterif.CoreProxy |
| 65 | AdapterProxy adapterif.AdapterProxy |
| 66 | EventProxy adapterif.EventProxy |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 67 | openOLT *OpenOLT |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 68 | exitChannel chan int |
| 69 | lockDevice sync.RWMutex |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 70 | Client oop.OpenoltClient |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 71 | transitionMap *TransitionMap |
| 72 | clientCon *grpc.ClientConn |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 73 | flowMgr *OpenOltFlowMgr |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 74 | eventMgr *OpenOltEventMgr |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 75 | resourceMgr *rsrcMgr.OpenOltResourceMgr |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 76 | discOnus map[string]bool |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 77 | onus map[string]*OnuDevice |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 78 | nniIntfID int |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 81 | //OnuDevice represents ONU related info |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 82 | type OnuDevice struct { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 83 | deviceID string |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 84 | deviceType string |
| 85 | serialNumber string |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 86 | onuID uint32 |
| 87 | intfID uint32 |
| 88 | proxyDeviceID string |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 89 | uniPorts map[uint32]struct{} |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | //NewOnuDevice creates a new Onu Device |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 93 | func NewOnuDevice(devID, deviceTp, serialNum string, onuID, intfID uint32, proxyDevID string) *OnuDevice { |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 94 | var device OnuDevice |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 95 | device.deviceID = devID |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 96 | device.deviceType = deviceTp |
| 97 | device.serialNumber = serialNum |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 98 | device.onuID = onuID |
| 99 | device.intfID = intfID |
| 100 | device.proxyDeviceID = proxyDevID |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 101 | device.uniPorts = make(map[uint32]struct{}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 102 | return &device |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | //NewDeviceHandler creates a new device handler |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 106 | func NewDeviceHandler(cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep adapterif.EventProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 107 | var dh DeviceHandler |
| 108 | dh.coreProxy = cp |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 109 | dh.AdapterProxy = ap |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 110 | dh.EventProxy = ep |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 111 | cloned := (proto.Clone(device)).(*voltha.Device) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 112 | dh.deviceID = cloned.Id |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 113 | dh.deviceType = cloned.Type |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 114 | dh.adminState = "up" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 115 | dh.device = cloned |
| 116 | dh.openOLT = adapter |
| 117 | dh.exitChannel = make(chan int, 1) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 118 | dh.discOnus = make(map[string]bool) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 119 | dh.lockDevice = sync.RWMutex{} |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 120 | dh.onus = make(map[string]*OnuDevice) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 121 | // The nniIntfID is initialized to -1 (invalid) and set to right value |
Girish Gowdru | 1110ef2 | 2019-06-24 11:17:59 -0400 | [diff] [blame] | 122 | // when the first IntfOperInd with status as "up" is received for |
| 123 | // any one of the available NNI port on the OLT device. |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 124 | dh.nniIntfID = -1 |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 125 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 126 | //TODO initialize the support classes. |
| 127 | return &dh |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // start save the device to the data model |
| 131 | func (dh *DeviceHandler) start(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 132 | dh.lockDevice.Lock() |
| 133 | defer dh.lockDevice.Unlock() |
| 134 | log.Debugw("starting-device-agent", log.Fields{"device": dh.device}) |
| 135 | // Add the initial device to the local model |
| 136 | log.Debug("device-agent-started") |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // stop stops the device dh. Not much to do for now |
| 140 | func (dh *DeviceHandler) stop(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 141 | dh.lockDevice.Lock() |
| 142 | defer dh.lockDevice.Unlock() |
| 143 | log.Debug("stopping-device-agent") |
| 144 | dh.exitChannel <- 1 |
| 145 | log.Debug("device-agent-stopped") |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 146 | } |
| 147 | |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 148 | func macifyIP(ip net.IP) string { |
| 149 | if len(ip) > 0 { |
| 150 | oct1 := strconv.FormatInt(int64(ip[12]), 16) |
| 151 | oct2 := strconv.FormatInt(int64(ip[13]), 16) |
| 152 | oct3 := strconv.FormatInt(int64(ip[14]), 16) |
| 153 | oct4 := strconv.FormatInt(int64(ip[15]), 16) |
| 154 | return fmt.Sprintf("00:00:%02v:%02v:%02v:%02v", oct1, oct2, oct3, oct4) |
| 155 | } |
| 156 | return "" |
| 157 | } |
| 158 | |
| 159 | func generateMacFromHost(host string) (string, error) { |
| 160 | var genmac string |
| 161 | var addr net.IP |
| 162 | var ips []string |
| 163 | var err error |
| 164 | |
| 165 | log.Debugw("generating-mac-from-host", log.Fields{"host": host}) |
| 166 | |
| 167 | if addr = net.ParseIP(host); addr == nil { |
| 168 | log.Debugw("looking-up-hostname", log.Fields{"host": host}) |
| 169 | |
| 170 | if ips, err = net.LookupHost(host); err == nil { |
| 171 | log.Debugw("dns-result-ips", log.Fields{"ips": ips}) |
| 172 | if addr = net.ParseIP(ips[0]); addr == nil { |
| 173 | log.Errorw("unable-to-parse-ip", log.Fields{"ip": ips[0]}) |
| 174 | return "", errors.New("unable-to-parse-ip") |
| 175 | } |
| 176 | genmac = macifyIP(addr) |
| 177 | log.Debugw("using-ip-as-mac", log.Fields{"host": ips[0], "mac": genmac}) |
| 178 | return genmac, nil |
| 179 | } |
| 180 | log.Errorw("cannot-resolve-hostname-to-ip", log.Fields{"host": host}) |
| 181 | return "", err |
| 182 | } |
| 183 | |
| 184 | genmac = macifyIP(addr) |
| 185 | log.Debugw("using-ip-as-mac", log.Fields{"host": host, "mac": genmac}) |
| 186 | return genmac, nil |
| 187 | } |
| 188 | |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 189 | func macAddressToUint32Array(mac string) []uint32 { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 190 | slist := strings.Split(mac, ":") |
| 191 | result := make([]uint32, len(slist)) |
| 192 | var err error |
| 193 | var tmp int64 |
| 194 | for index, val := range slist { |
| 195 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 196 | return []uint32{1, 2, 3, 4, 5, 6} |
| 197 | } |
| 198 | result[index] = uint32(tmp) |
| 199 | } |
| 200 | return result |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 201 | } |
| 202 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 203 | //GetportLabel returns the label for the NNI and the PON port based on port number and port type |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 204 | func GetportLabel(portNum uint32, portType voltha.Port_PortType) string { |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 205 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 206 | if portType == voltha.Port_ETHERNET_NNI { |
| 207 | return fmt.Sprintf("nni-%d", portNum) |
| 208 | } else if portType == voltha.Port_PON_OLT { |
| 209 | return fmt.Sprintf("pon-%d", portNum) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 210 | } else if portType == voltha.Port_ETHERNET_UNI { |
| 211 | log.Errorw("local UNI management not supported", log.Fields{}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 212 | return "" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 213 | } |
| 214 | return "" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 215 | } |
| 216 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 217 | func (dh *DeviceHandler) addPort(intfID uint32, portType voltha.Port_PortType, state string) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 218 | var operStatus common.OperStatus_OperStatus |
| 219 | if state == "up" { |
| 220 | operStatus = voltha.OperStatus_ACTIVE |
| 221 | } else { |
| 222 | operStatus = voltha.OperStatus_DISCOVERED |
| 223 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 224 | portNum := IntfIDToPortNo(intfID, portType) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 225 | label := GetportLabel(portNum, portType) |
| 226 | if len(label) == 0 { |
| 227 | log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType}) |
| 228 | return |
| 229 | } |
| 230 | // Now create Port |
| 231 | port := &voltha.Port{ |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 232 | PortNo: portNum, |
| 233 | Label: label, |
| 234 | Type: portType, |
| 235 | OperStatus: operStatus, |
| 236 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 237 | log.Debugw("Sending port update to core", log.Fields{"port": port}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 238 | // Synchronous call to update device - this method is run in its own go routine |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 239 | if err := dh.coreProxy.PortCreated(context.TODO(), dh.device.Id, port); err != nil { |
| 240 | log.Errorw("error-creating-nni-port", log.Fields{"deviceID": dh.device.Id, "portType": portType, "error": err}) |
Girish Gowdru | 1110ef2 | 2019-06-24 11:17:59 -0400 | [diff] [blame] | 241 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 242 | |
Girish Gowdru | 1110ef2 | 2019-06-24 11:17:59 -0400 | [diff] [blame] | 243 | // Once we have successfully added the NNI port to the core, if the |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 244 | // locally cached nniIntfID is set to invalid (-1), set it to the right value. |
| 245 | if portType == voltha.Port_ETHERNET_NNI && dh.nniIntfID == -1 { |
| 246 | dh.nniIntfID = int(intfID) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 247 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // readIndications to read the indications from the OLT device |
| 251 | func (dh *DeviceHandler) readIndications() { |
William Kurkian | ff52466 | 2019-08-20 10:34:30 -0400 | [diff] [blame] | 252 | defer log.Errorw("Indications ended", log.Fields{}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 253 | indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty)) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 254 | if err != nil { |
| 255 | log.Errorw("Failed to read indications", log.Fields{"err": err}) |
| 256 | return |
| 257 | } |
| 258 | if indications == nil { |
| 259 | log.Errorw("Indications is nil", log.Fields{}) |
| 260 | return |
| 261 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 262 | /* get device state */ |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 263 | device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 264 | if err != nil || device == nil { |
| 265 | /*TODO: needs to handle error scenarios */ |
| 266 | log.Errorw("Failed to fetch device info", log.Fields{"err": err}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 267 | return |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 268 | } |
| 269 | // When the device is in DISABLED and Adapter container restarts, we need to |
| 270 | // rebuild the locally maintained admin state. |
| 271 | if device.AdminState == voltha.AdminState_DISABLED { |
| 272 | dh.lockDevice.Lock() |
| 273 | dh.adminState = "down" |
| 274 | dh.lockDevice.Unlock() |
| 275 | } |
| 276 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 277 | for { |
| 278 | indication, err := indications.Recv() |
| 279 | if err == io.EOF { |
| 280 | break |
| 281 | } |
| 282 | if err != nil { |
| 283 | log.Infow("Failed to read from indications", log.Fields{"err": err}) |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 284 | if dh.adminState == "deleted" { |
| 285 | log.Debug("Device deleted stoping the read indication thread") |
| 286 | break |
| 287 | } |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 288 | dh.transitionMap.Handle(DeviceDownInd) |
| 289 | dh.transitionMap.Handle(DeviceInit) |
| 290 | break |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 291 | } |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 292 | dh.lockDevice.RLock() |
| 293 | adminState := dh.adminState |
| 294 | dh.lockDevice.RUnlock() |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 295 | // When OLT is admin down, ignore all indications. |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 296 | if adminState == "down" { |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 297 | |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 298 | log.Infow("olt is admin down, ignore indication", log.Fields{}) |
| 299 | continue |
| 300 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 301 | dh.handleIndication(indication) |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 302 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | func (dh *DeviceHandler) handleOltIndication(oltIndication *oop.OltIndication) { |
Daniele Rossi | 051466a | 2019-07-26 13:39:37 +0000 | [diff] [blame] | 307 | raisedTs := time.Now().UnixNano() |
Gamze Abaka | a1a5052 | 2019-10-03 19:28:27 +0000 | [diff] [blame] | 308 | if oltIndication.OperState == "up" && dh.transitionMap.currentDeviceState != deviceStateUp { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 309 | dh.transitionMap.Handle(DeviceUpInd) |
| 310 | } else if oltIndication.OperState == "down" { |
| 311 | dh.transitionMap.Handle(DeviceDownInd) |
| 312 | } |
Daniele Rossi | 051466a | 2019-07-26 13:39:37 +0000 | [diff] [blame] | 313 | // Send or clear Alarm |
| 314 | dh.eventMgr.oltUpDownIndication(oltIndication, dh.deviceID, raisedTs) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | func (dh *DeviceHandler) handleIndication(indication *oop.Indication) { |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 318 | raisedTs := time.Now().UnixNano() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 319 | switch indication.Data.(type) { |
| 320 | case *oop.Indication_OltInd: |
| 321 | dh.handleOltIndication(indication.GetOltInd()) |
| 322 | case *oop.Indication_IntfInd: |
| 323 | intfInd := indication.GetIntfInd() |
| 324 | go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState()) |
| 325 | log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd}) |
| 326 | case *oop.Indication_IntfOperInd: |
| 327 | intfOperInd := indication.GetIntfOperInd() |
| 328 | if intfOperInd.GetType() == "nni" { |
| 329 | go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState()) |
| 330 | } else if intfOperInd.GetType() == "pon" { |
| 331 | // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down |
| 332 | // Handle pon port update |
Gamze Abaka | 8539e20 | 2019-10-03 19:22:48 +0000 | [diff] [blame] | 333 | go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_PON_OLT, intfOperInd.GetOperState()) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 334 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 335 | log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd}) |
| 336 | case *oop.Indication_OnuDiscInd: |
| 337 | onuDiscInd := indication.GetOnuDiscInd() |
| 338 | log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 339 | sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 340 | go dh.onuDiscIndication(onuDiscInd, sn) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 341 | case *oop.Indication_OnuInd: |
| 342 | onuInd := indication.GetOnuInd() |
| 343 | log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd}) |
| 344 | go dh.onuIndication(onuInd) |
| 345 | case *oop.Indication_OmciInd: |
| 346 | omciInd := indication.GetOmciInd() |
| 347 | log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd}) |
William Kurkian | ff52466 | 2019-08-20 10:34:30 -0400 | [diff] [blame] | 348 | go dh.omciIndication(omciInd) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 349 | case *oop.Indication_PktInd: |
| 350 | pktInd := indication.GetPktInd() |
| 351 | log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd}) |
| 352 | go dh.handlePacketIndication(pktInd) |
| 353 | case *oop.Indication_PortStats: |
| 354 | portStats := indication.GetPortStats() |
| 355 | log.Infow("Received port stats indication", log.Fields{"PortStats": portStats}) |
| 356 | case *oop.Indication_FlowStats: |
| 357 | flowStats := indication.GetFlowStats() |
| 358 | log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats}) |
| 359 | case *oop.Indication_AlarmInd: |
| 360 | alarmInd := indication.GetAlarmInd() |
| 361 | log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd}) |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 362 | dh.eventMgr.ProcessEvents(alarmInd, dh.deviceID, raisedTs) |
| 363 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 364 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // doStateUp handle the olt up indication and update to voltha core |
| 368 | func (dh *DeviceHandler) doStateUp() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 369 | // Synchronous call to update device state - this method is run in its own go routine |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 370 | if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE, |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 371 | voltha.OperStatus_ACTIVE); err != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 372 | log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceID": dh.device.Id, "error": err}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 373 | return err |
| 374 | } |
| 375 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // doStateDown handle the olt down indication |
| 379 | func (dh *DeviceHandler) doStateDown() error { |
serkant.uluderya | 245caba | 2019-09-24 23:15:29 -0700 | [diff] [blame] | 380 | dh.lockDevice.Lock() |
| 381 | defer dh.lockDevice.Unlock() |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 382 | log.Debug("do-state-down-start") |
| 383 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 384 | device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id) |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 385 | if err != nil || device == nil { |
| 386 | /*TODO: needs to handle error scenarios */ |
| 387 | log.Errorw("Failed to fetch device device", log.Fields{"err": err}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 388 | return errors.New("failed to fetch device device") |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | cloned := proto.Clone(device).(*voltha.Device) |
| 392 | // Update the all ports state on that device to disable |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 393 | if er := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); er != nil { |
| 394 | log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": er}) |
| 395 | return er |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | //Update the device oper state and connection status |
| 399 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 400 | cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE |
| 401 | dh.device = cloned |
| 402 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 403 | if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil { |
| 404 | log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": er}) |
| 405 | return er |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 406 | } |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 407 | |
| 408 | //get the child device for the parent device |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 409 | onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 410 | if err != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 411 | log.Errorw("failed to get child devices information", log.Fields{"deviceID": dh.device.Id, "error": err}) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 412 | return err |
| 413 | } |
| 414 | for _, onuDevice := range onuDevices.Items { |
| 415 | |
| 416 | // Update onu state as down in onu adapter |
| 417 | onuInd := oop.OnuIndication{} |
| 418 | onuInd.OperState = "down" |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 419 | er := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
| 420 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 421 | if er != nil { |
| 422 | log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd, |
| 423 | "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id}) |
serkant.uluderya | 245caba | 2019-09-24 23:15:29 -0700 | [diff] [blame] | 424 | //Do not return here and continue to process other ONUs |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 425 | } |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 426 | } |
serkant.uluderya | 245caba | 2019-09-24 23:15:29 -0700 | [diff] [blame] | 427 | /* Discovered ONUs entries need to be cleared , since after OLT |
| 428 | is up, it starts sending discovery indications again*/ |
| 429 | dh.discOnus = make(map[string]bool) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 430 | log.Debugw("do-state-down-end", log.Fields{"deviceID": device.Id}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 431 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // doStateInit dial the grpc before going to init state |
| 435 | func (dh *DeviceHandler) doStateInit() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 436 | var err error |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 437 | dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock()) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 438 | if err != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 439 | log.Errorw("Failed to dial device", log.Fields{"DeviceId": dh.deviceID, "HostAndPort": dh.device.GetHostAndPort(), "err": err}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 440 | return err |
| 441 | } |
| 442 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | // postInit create olt client instance to invoke RPC on the olt device |
| 446 | func (dh *DeviceHandler) postInit() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 447 | dh.Client = oop.NewOpenoltClient(dh.clientCon) |
| 448 | dh.transitionMap.Handle(GrpcConnected) |
| 449 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // doStateConnected get the device info and update to voltha core |
| 453 | func (dh *DeviceHandler) doStateConnected() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 454 | log.Debug("OLT device has been connected") |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 455 | |
| 456 | // Case where OLT is disabled and then rebooted. |
| 457 | if dh.adminState == "down" { |
| 458 | log.Debugln("do-state-connected--device-admin-state-down") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 459 | device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 460 | if err != nil || device == nil { |
| 461 | /*TODO: needs to handle error scenarios */ |
| 462 | log.Errorw("Failed to fetch device device", log.Fields{"err": err}) |
| 463 | } |
| 464 | |
| 465 | cloned := proto.Clone(device).(*voltha.Device) |
| 466 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 467 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 468 | dh.device = cloned |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 469 | if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil { |
| 470 | log.Errorw("error-updating-device-state", log.Fields{"deviceID": dh.device.Id, "error": er}) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 471 | } |
| 472 | |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 473 | // Since the device was disabled before the OLT was rebooted, enforce the OLT to be Disabled after re-connection. |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 474 | _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty)) |
| 475 | if err != nil { |
| 476 | log.Errorw("Failed to disable olt ", log.Fields{"err": err}) |
| 477 | } |
| 478 | |
| 479 | // Start reading indications |
| 480 | go dh.readIndications() |
| 481 | return nil |
| 482 | } |
| 483 | |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 484 | deviceInfo, err := dh.populateDeviceInfo() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 485 | if err != nil { |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 486 | log.Errorw("Unable to populate Device Info", log.Fields{"err": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 487 | return err |
| 488 | } |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 489 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 490 | device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id) |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 491 | if err != nil || device == nil { |
| 492 | /*TODO: needs to handle error scenarios */ |
| 493 | log.Errorw("Failed to fetch device device", log.Fields{"err": err}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 494 | return err |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 495 | } |
| 496 | cloned := proto.Clone(device).(*voltha.Device) |
| 497 | // Update the all ports (if available) on that device to ACTIVE. |
| 498 | // The ports do not normally exist, unless the device is coming back from a reboot |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 499 | if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 500 | log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err}) |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 501 | return err |
| 502 | } |
| 503 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 504 | KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort) |
| 505 | // Instantiate resource manager |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 506 | if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceID, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 507 | log.Error("Error while instantiating resource manager") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 508 | return errors.New("instantiating resource manager failed") |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 509 | } |
| 510 | // Instantiate flow manager |
| 511 | if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil { |
| 512 | log.Error("Error while instantiating flow manager") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 513 | return errors.New("instantiating flow manager failed") |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 514 | } |
| 515 | /* TODO: Instantiate Alarm , stats , BW managers */ |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 516 | /* Instantiating Event Manager to handle Alarms and KPIs */ |
Devmalya Paul | 90ca301 | 2019-09-02 21:55:45 -0400 | [diff] [blame] | 517 | dh.eventMgr = NewEventMgr(dh.EventProxy, dh) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 518 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 519 | // Start reading indications |
| 520 | go dh.readIndications() |
| 521 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 522 | } |
| 523 | |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 524 | func (dh *DeviceHandler) populateDeviceInfo() (*oop.DeviceInfo, error) { |
| 525 | var err error |
| 526 | var deviceInfo *oop.DeviceInfo |
| 527 | |
| 528 | deviceInfo, err = dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty)) |
| 529 | |
| 530 | if err != nil { |
| 531 | log.Errorw("Failed to fetch device info", log.Fields{"err": err}) |
| 532 | return nil, err |
| 533 | } |
| 534 | if deviceInfo == nil { |
| 535 | log.Errorw("Device info is nil", log.Fields{}) |
| 536 | return nil, errors.New("failed to get device info from OLT") |
| 537 | } |
| 538 | |
| 539 | log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo}) |
| 540 | dh.device.Root = true |
| 541 | dh.device.Vendor = deviceInfo.Vendor |
| 542 | dh.device.Model = deviceInfo.Model |
| 543 | dh.device.SerialNumber = deviceInfo.DeviceSerialNumber |
| 544 | dh.device.HardwareVersion = deviceInfo.HardwareVersion |
| 545 | dh.device.FirmwareVersion = deviceInfo.FirmwareVersion |
| 546 | |
| 547 | if deviceInfo.DeviceId == "" { |
| 548 | log.Warnw("no-device-id-provided-using-host", log.Fields{"hostport": dh.device.GetHostAndPort()}) |
| 549 | host := strings.Split(dh.device.GetHostAndPort(), ":")[0] |
| 550 | genmac, err := generateMacFromHost(host) |
| 551 | if err != nil { |
| 552 | return nil, err |
| 553 | } |
| 554 | log.Debugw("using-host-for-mac-address", log.Fields{"host": host, "mac": genmac}) |
| 555 | dh.device.MacAddress = genmac |
| 556 | } else { |
| 557 | dh.device.MacAddress = deviceInfo.DeviceId |
| 558 | } |
| 559 | |
| 560 | // Synchronous call to update device - this method is run in its own go routine |
| 561 | if err := dh.coreProxy.DeviceUpdate(context.TODO(), dh.device); err != nil { |
| 562 | log.Errorw("error-updating-device", log.Fields{"deviceID": dh.device.Id, "error": err}) |
| 563 | return nil, err |
| 564 | } |
| 565 | |
| 566 | return deviceInfo, nil |
| 567 | } |
| 568 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 569 | //AdoptDevice adopts the OLT device |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 570 | func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 571 | dh.transitionMap = NewTransitionMap(dh) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 572 | log.Infow("Adopt_device", log.Fields{"deviceID": device.Id, "Address": device.GetHostAndPort()}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 573 | dh.transitionMap.Handle(DeviceInit) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 574 | } |
| 575 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 576 | //GetOfpDeviceInfo Gets the Ofp information of the given device |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 577 | func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 578 | return &ic.SwitchCapability{ |
| 579 | Desc: &of.OfpDesc{ |
Devmalya Paul | 70dd497 | 2019-06-10 15:19:17 +0530 | [diff] [blame] | 580 | MfrDesc: "VOLTHA Project", |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 581 | HwDesc: "open_pon", |
| 582 | SwDesc: "open_pon", |
| 583 | SerialNum: dh.device.SerialNumber, |
| 584 | }, |
| 585 | SwitchFeatures: &of.OfpSwitchFeatures{ |
| 586 | NBuffers: 256, |
| 587 | NTables: 2, |
| 588 | Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS | |
| 589 | of.OfpCapabilities_OFPC_TABLE_STATS | |
| 590 | of.OfpCapabilities_OFPC_PORT_STATS | |
| 591 | of.OfpCapabilities_OFPC_GROUP_STATS), |
| 592 | }, |
| 593 | }, nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 594 | } |
| 595 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 596 | //GetOfpPortInfo Get Ofp port information |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 597 | func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 598 | capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 599 | return &ic.PortCapability{ |
| 600 | Port: &voltha.LogicalPort{ |
| 601 | OfpPort: &of.OfpPort{ |
| 602 | HwAddr: macAddressToUint32Array(dh.device.MacAddress), |
| 603 | Config: 0, |
| 604 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 605 | Curr: capacity, |
| 606 | Advertised: capacity, |
| 607 | Peer: capacity, |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 608 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 609 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 610 | }, |
| 611 | DeviceId: dh.device.Id, |
| 612 | DevicePortNo: uint32(portNo), |
| 613 | }, |
| 614 | }, nil |
| 615 | } |
| 616 | |
William Kurkian | ff52466 | 2019-08-20 10:34:30 -0400 | [diff] [blame] | 617 | func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 618 | log.Debugw("omci indication", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 619 | var deviceType string |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 620 | var deviceID string |
| 621 | var proxyDeviceID string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 622 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 623 | onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId) |
| 624 | if onuInCache, ok := dh.onus[onuKey]; !ok { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 625 | log.Debugw("omci indication for a device not in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId}) |
| 626 | ponPort := IntfIDToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 627 | kwargs := make(map[string]interface{}) |
| 628 | kwargs["onu_id"] = omciInd.OnuId |
| 629 | kwargs["parent_port_no"] = ponPort |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 630 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 631 | onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
| 632 | if err != nil { |
William Kurkian | ff52466 | 2019-08-20 10:34:30 -0400 | [diff] [blame] | 633 | log.Errorw("onu not found", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId, "error": err}) |
| 634 | return |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 635 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 636 | deviceType = onuDevice.Type |
| 637 | deviceID = onuDevice.Id |
| 638 | proxyDeviceID = onuDevice.ProxyAddress.DeviceId |
| 639 | //if not exist in cache, then add to cache. |
| 640 | dh.onus[onuKey] = NewOnuDevice(deviceID, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceID) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 641 | } else { |
| 642 | //found in cache |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 643 | log.Debugw("omci indication for a device in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 644 | deviceType = onuInCache.deviceType |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 645 | deviceID = onuInCache.deviceID |
| 646 | proxyDeviceID = onuInCache.proxyDeviceID |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 647 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 648 | |
| 649 | omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt} |
| 650 | if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg, |
| 651 | ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 652 | deviceID, proxyDeviceID, ""); sendErr != nil { |
William Kurkian | ff52466 | 2019-08-20 10:34:30 -0400 | [diff] [blame] | 653 | log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuID": deviceID, "proxyDeviceID": proxyDeviceID, "error": sendErr}) |
| 654 | return |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 655 | } |
William Kurkian | ff52466 | 2019-08-20 10:34:30 -0400 | [diff] [blame] | 656 | return |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 657 | } |
| 658 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 659 | //ProcessInterAdapterMessage sends the proxied messages to the target device |
| 660 | // If the proxy address is not found in the unmarshalled message, it first fetches the onu device for which the message |
| 661 | // is meant, and then send the unmarshalled omci message to this onu |
| 662 | func (dh *DeviceHandler) ProcessInterAdapterMessage(msg *ic.InterAdapterMessage) error { |
| 663 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgID": msg.Header.Id}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 664 | if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 665 | msgID := msg.Header.Id |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 666 | fromTopic := msg.Header.FromTopic |
| 667 | toTopic := msg.Header.ToTopic |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 668 | toDeviceID := msg.Header.ToDeviceId |
| 669 | proxyDeviceID := msg.Header.ProxyDeviceId |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 670 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 671 | log.Debugw("omci request message header", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 672 | |
| 673 | msgBody := msg.GetBody() |
| 674 | |
| 675 | omciMsg := &ic.InterAdapterOmciMessage{} |
| 676 | if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil { |
| 677 | log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err}) |
| 678 | return err |
| 679 | } |
| 680 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 681 | if omciMsg.GetProxyAddress() == nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 682 | onuDevice, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, toDeviceID) |
| 683 | if err != nil { |
| 684 | log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceID, "error": err}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 685 | return err |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 686 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 687 | log.Debugw("device retrieved from core", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID}) |
| 688 | dh.sendProxiedMessage(onuDevice, omciMsg) |
| 689 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 690 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 691 | log.Debugw("Proxy Address found in omci message", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 692 | dh.sendProxiedMessage(nil, omciMsg) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | } else { |
| 696 | log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type}) |
| 697 | } |
| 698 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 699 | } |
| 700 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 701 | func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 702 | var intfID uint32 |
| 703 | var onuID uint32 |
| 704 | var connectStatus common.ConnectStatus_ConnectStatus |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 705 | if onuDevice != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 706 | intfID = onuDevice.ProxyAddress.GetChannelId() |
| 707 | onuID = onuDevice.ProxyAddress.GetOnuId() |
| 708 | connectStatus = onuDevice.ConnectStatus |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 709 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 710 | intfID = omciMsg.GetProxyAddress().GetChannelId() |
| 711 | onuID = omciMsg.GetProxyAddress().GetOnuId() |
| 712 | connectStatus = omciMsg.GetConnectStatus() |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 713 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 714 | if connectStatus != voltha.ConnectStatus_REACHABLE { |
| 715 | log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfID": intfID, "onuID": onuID}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 716 | return |
| 717 | } |
| 718 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 719 | omciMessage := &oop.OmciMsg{IntfId: intfID, OnuId: onuID, Pkt: omciMsg.Message} |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 720 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 721 | _, err := dh.Client.OmciMsgOut(context.Background(), omciMessage) |
| 722 | if err != nil { |
| 723 | log.Errorw("unable to send omci-msg-out", log.Fields{"IntfID": intfID, "OnuID": onuID, "Msg": omciMessage}) |
| 724 | return |
| 725 | } |
| 726 | log.Debugw("omci-message-sent", log.Fields{"intfID": intfID, "onuID": onuID, "omciMsg": string(omciMsg.GetMessage())}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 729 | func (dh *DeviceHandler) activateONU(intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) { |
| 730 | log.Debugw("activate-onu", log.Fields{"intfID": intfID, "onuID": onuID, "serialNum": serialNum, "serialNumber": serialNumber}) |
| 731 | dh.flowMgr.UpdateOnuInfo(intfID, uint32(onuID), serialNumber) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 732 | // TODO: need resource manager |
| 733 | var pir uint32 = 1000000 |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 734 | Onu := oop.Onu{IntfId: intfID, OnuId: uint32(onuID), SerialNumber: serialNum, Pir: pir} |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 735 | if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil { |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 736 | st, _ := status.FromError(err) |
| 737 | if st.Code() == codes.AlreadyExists { |
| 738 | log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber}) |
| 739 | } else { |
| 740 | log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err}) |
| 741 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 742 | } else { |
| 743 | log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber}) |
| 744 | } |
| 745 | } |
| 746 | |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 747 | func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, sn string) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 748 | channelID := onuDiscInd.GetIntfId() |
| 749 | parentPortNo := IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 750 | |
| 751 | log.Debugw("new-discovery-indication", log.Fields{"sn": sn}) |
| 752 | dh.lockDevice.Lock() |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 753 | if _, ok := dh.discOnus[sn]; ok { |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 754 | dh.lockDevice.Unlock() |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 755 | log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn}) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 756 | return |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 759 | dh.discOnus[sn] = true |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 760 | log.Debugw("new-discovery-indications-list", log.Fields{"discOnus": dh.discOnus}) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 761 | dh.lockDevice.Unlock() |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 762 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 763 | kwargs := make(map[string]interface{}) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 764 | if sn != "" { |
| 765 | kwargs["serial_number"] = sn |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 766 | } else { |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 767 | log.Errorw("invalid onu serial number", log.Fields{"sn": sn}) |
| 768 | return |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
| 772 | var onuID uint32 |
| 773 | if onuDevice == nil || err != nil { |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 774 | //This is the first time ONU discovered. Create an OnuID for it. |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 775 | ponintfid := onuDiscInd.GetIntfId() |
| 776 | dh.lockDevice.Lock() |
| 777 | onuID, err = dh.resourceMgr.GetONUID(ponintfid) |
| 778 | dh.lockDevice.Unlock() |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 779 | if err != nil { |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 780 | log.Errorw("failed to fetch onuID from resource manager", log.Fields{"pon-intf-id": ponintfid, "err": err}) |
| 781 | return |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 782 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 783 | if onuDevice, err = dh.coreProxy.ChildDeviceDetected(context.TODO(), dh.device.Id, int(parentPortNo), |
Chaitrashree G S | ee824a2 | 2019-07-28 18:28:27 -0400 | [diff] [blame] | 784 | "", int(channelID), |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 785 | string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuID)); onuDevice == nil { |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 786 | log.Errorw("Create onu error", |
| 787 | log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), |
| 788 | "onuID": onuID, "sn": sn, "error": err}) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 789 | return |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 790 | } |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 791 | log.Debugw("onu-child-device-added", log.Fields{"onuDevice": onuDevice}) |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 792 | |
| 793 | } else { |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 794 | //ONU already discovered before. Use the same OnuID. |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 795 | onuID = onuDevice.ProxyAddress.OnuId |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 796 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 797 | //Insert the ONU into cache to use in OnuIndication. |
| 798 | //TODO: Do we need to remove this from the cache on ONU change, or wait for overwritten on next discovery. |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 799 | log.Debugw("ONU discovery indication key create", log.Fields{"onuID": onuID, |
| 800 | "intfId": onuDiscInd.GetIntfId()}) |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 801 | onuKey := dh.formOnuKey(onuDiscInd.GetIntfId(), onuID) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 802 | |
| 803 | dh.lockDevice.Lock() |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 804 | dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuID, onuDiscInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 805 | log.Debugw("new-onu-device-discovered", log.Fields{"onu": dh.onus[onuKey]}) |
| 806 | dh.lockDevice.Unlock() |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 807 | |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 808 | err = dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED) |
| 809 | if err != nil { |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 810 | log.Errorw("failed to update device state", log.Fields{"DeviceID": onuDevice.Id, "err": err}) |
| 811 | return |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 812 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 813 | log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id}) |
| 814 | //TODO: We put this sleep here to prevent the race between state update and onuIndication |
| 815 | //In onuIndication the operStatus of device is checked. If it is still not updated in KV store |
| 816 | //then the initialisation fails. |
| 817 | time.Sleep(1 * time.Second) |
| 818 | dh.activateONU(onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 819 | return |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) { |
| 823 | serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber) |
| 824 | |
| 825 | kwargs := make(map[string]interface{}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 826 | ponPort := IntfIDToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT) |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 827 | var onuDevice *voltha.Device |
| 828 | foundInCache := false |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 829 | log.Debugw("ONU indication key create", log.Fields{"onuId": onuInd.OnuId, |
| 830 | "intfId": onuInd.GetIntfId()}) |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 831 | onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.OnuId) |
| 832 | if onuInCache, ok := dh.onus[onuKey]; ok { |
| 833 | //If ONU id is discovered before then use GetDevice to get onuDevice because it is cheaper. |
| 834 | foundInCache = true |
| 835 | onuDevice, _ = dh.coreProxy.GetDevice(nil, dh.device.Id, onuInCache.deviceID) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 836 | } else { |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 837 | //If ONU not found in adapter cache then we have to use GetChildDevice to get onuDevice |
| 838 | if serialNumber != "" { |
| 839 | kwargs["serial_number"] = serialNumber |
| 840 | } else { |
| 841 | kwargs["onu_id"] = onuInd.OnuId |
| 842 | kwargs["parent_port_no"] = ponPort |
| 843 | } |
| 844 | onuDevice, _ = dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 845 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 846 | |
| 847 | if onuDevice != nil { |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 848 | if onuDevice.ParentPortNo != ponPort { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 849 | //log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": intfIDFromPortNo(onuDevice.ParentPortNo), "currentIntfId": onuInd.GetIntfId()}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 850 | log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": ponPort}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | if onuDevice.ProxyAddress.OnuId != onuInd.OnuId { |
| 854 | 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}) |
| 855 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 856 | if !foundInCache { |
| 857 | onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId()) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 858 | dh.lockDevice.Lock() |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 859 | dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 860 | dh.lockDevice.Unlock() |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 861 | } |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 862 | dh.updateOnuStates(onuDevice, onuInd, foundInCache) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 863 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 864 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 865 | log.Errorw("onu not found", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 866 | return |
| 867 | } |
| 868 | |
| 869 | } |
| 870 | |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 871 | func (dh *DeviceHandler) updateOnuStates(onuDevice *voltha.Device, onuInd *oop.OnuIndication, foundInCache bool) { |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 872 | log.Debugw("onu-indication-for-state", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 873 | dh.updateOnuAdminState(onuInd) |
| 874 | // operState |
| 875 | if onuInd.OperState == "down" { |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 876 | log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 877 | // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
| 878 | err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
| 879 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 880 | if err != nil { |
| 881 | log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd, |
| 882 | "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id}) |
| 883 | } |
| 884 | } else if onuInd.OperState == "up" { |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 885 | // Ignore operstatus if device was found in cache |
| 886 | if !foundInCache && onuDevice.OperStatus != common.OperStatus_DISCOVERED { |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 887 | log.Warnw("ignore-onu-indication", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 888 | return |
| 889 | } |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 890 | log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState}) |
| 891 | // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 892 | err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
| 893 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 894 | if err != nil { |
| 895 | log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd, |
William Kurkian | 740a09c | 2019-10-23 17:07:38 -0400 | [diff] [blame] | 896 | "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id, "Error": err}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 897 | return |
| 898 | } |
| 899 | } else { |
| 900 | log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState}) |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | func (dh *DeviceHandler) updateOnuAdminState(onuInd *oop.OnuIndication) { |
| 905 | if onuInd.AdminState == "down" { |
| 906 | if onuInd.OperState != "down" { |
| 907 | log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState}) |
| 908 | // Forcing the oper state change code to execute |
| 909 | onuInd.OperState = "down" |
| 910 | } |
| 911 | // Port and logical port update is taken care of by oper state block |
| 912 | } else if onuInd.AdminState == "up" { |
| 913 | log.Debugln("received-onu-admin-state up") |
| 914 | } else { |
| 915 | log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState}) |
| 916 | } |
| 917 | log.Debugln("admin-state-dealt-with") |
| 918 | } |
| 919 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 920 | func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string { |
| 921 | if serialNum != nil { |
| 922 | return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 923 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 924 | return "" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string { |
| 928 | tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) + |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 929 | fmt.Sprintf("%x", uint32(vendorSpecific[0]&0x0f)) + |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 930 | fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) + |
| 931 | fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) + |
| 932 | fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) + |
| 933 | fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) + |
| 934 | fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) + |
| 935 | fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f) |
| 936 | return tmp |
| 937 | } |
| 938 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 939 | //UpdateFlowsBulk upates the bulk flow |
| 940 | func (dh *DeviceHandler) UpdateFlowsBulk() error { |
| 941 | return errors.New("unimplemented") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 942 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 943 | |
| 944 | //GetChildDevice returns the child device for given parent port and onu id |
| 945 | func (dh *DeviceHandler) GetChildDevice(parentPort, onuID uint32) *voltha.Device { |
| 946 | log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuID": onuID}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 947 | kwargs := make(map[string]interface{}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 948 | kwargs["onu_id"] = onuID |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 949 | kwargs["parent_port_no"] = parentPort |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 950 | onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 951 | if err != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 952 | log.Errorw("onu not found", log.Fields{"intfID": parentPort, "onuID": onuID}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 953 | return nil |
| 954 | } |
| 955 | log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice}) |
| 956 | return onuDevice |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 957 | } |
| 958 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 959 | // SendPacketInToCore sends packet-in to core |
| 960 | // For this, it calls SendPacketIn of the core-proxy which uses a device specific topic to send the request. |
| 961 | // The adapter handling the device creates a device specific topic |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 962 | func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) { |
| 963 | log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 964 | if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPort, packetPayload); err != nil { |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 965 | log.Errorw("Error sending packetin to core", log.Fields{"error": err}) |
| 966 | return |
| 967 | } |
| 968 | log.Debug("Sent packet-in to core successfully") |
| 969 | } |
| 970 | |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 971 | // AddUniPortToOnu adds the uni port to the onu device |
| 972 | func (dh *DeviceHandler) AddUniPortToOnu(intfID, onuID, uniPort uint32) { |
| 973 | onuKey := dh.formOnuKey(intfID, onuID) |
| 974 | dh.lockDevice.Lock() |
| 975 | defer dh.lockDevice.Unlock() |
| 976 | if onuDevice, ok := dh.onus[onuKey]; ok { |
| 977 | // add it to the uniPort map for the onu device |
| 978 | if _, ok = onuDevice.uniPorts[uniPort]; !ok { |
| 979 | onuDevice.uniPorts[uniPort] = struct{}{} |
| 980 | log.Debugw("adding-uni-port", log.Fields{"port": uniPort, "intfID": intfID, "onuId": onuID}) |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 985 | //UpdateFlowsIncrementally updates the device flow |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 986 | func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { |
| 987 | log.Debugw("Received-incremental-flowupdate-in-device-handler", log.Fields{"deviceID": device.Id, "flows": flows, "groups": groups, "flowMetadata": flowMetadata}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 988 | if flows != nil { |
| 989 | for _, flow := range flows.ToAdd.Items { |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 990 | log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow}) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 991 | dh.flowMgr.AddFlow(flow, flowMetadata) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 992 | } |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 993 | for _, flow := range flows.ToRemove.Items { |
| 994 | log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow}) |
| 995 | dh.flowMgr.RemoveFlow(flow) |
| 996 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 997 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 998 | if groups != nil && flows != nil { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 999 | for _, flow := range flows.ToRemove.Items { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1000 | log.Debug("Removing flow", log.Fields{"deviceID": device.Id, "flowToRemove": flow}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1001 | // dh.flowMgr.RemoveFlow(flow) |
| 1002 | } |
| 1003 | } |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 1004 | log.Debug("UpdateFlowsIncrementally done successfully") |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1005 | return nil |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 1006 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1007 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1008 | //DisableDevice disables the given device |
| 1009 | //It marks the following for the given device: |
| 1010 | //Device-Handler Admin-State : down |
| 1011 | //Device Port-State: UNKNOWN |
| 1012 | //Device Oper-State: UNKNOWN |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1013 | func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error { |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 1014 | /* On device disable ,admin state update has to be done prior sending request to agent since |
| 1015 | the indication thread may processes invalid indications of ONU and OLT*/ |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1016 | dh.lockDevice.Lock() |
| 1017 | dh.adminState = "down" |
| 1018 | dh.lockDevice.Unlock() |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 1019 | if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil { |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1020 | if e, ok := status.FromError(err); ok && e.Code() == codes.Internal { |
| 1021 | log.Errorw("failed-to-disable-olt ", log.Fields{"err": err}) |
| 1022 | dh.lockDevice.Lock() |
| 1023 | dh.adminState = "up" |
| 1024 | dh.lockDevice.Unlock() |
| 1025 | return err |
| 1026 | } |
| 1027 | |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 1028 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1029 | log.Debug("olt-disabled") |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 1030 | dh.lockDevice.Lock() |
| 1031 | /* Discovered ONUs entries need to be cleared , since on device disable the child devices goes to |
| 1032 | UNREACHABLE state which needs to be configured again*/ |
| 1033 | dh.discOnus = make(map[string]bool) |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1034 | dh.onus = make(map[string]*OnuDevice) |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 1035 | dh.lockDevice.Unlock() |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1036 | go dh.notifyChildDevices() |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1037 | cloned := proto.Clone(device).(*voltha.Device) |
| 1038 | // Update the all ports state on that device to disable |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1039 | if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
| 1040 | log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1041 | return err |
| 1042 | } |
| 1043 | |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1044 | log.Debugw("disable-device-end", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1045 | return nil |
| 1046 | } |
| 1047 | |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1048 | func (dh *DeviceHandler) notifyChildDevices() { |
| 1049 | |
| 1050 | // Update onu state as unreachable in onu adapter |
| 1051 | onuInd := oop.OnuIndication{} |
| 1052 | onuInd.OperState = "unreachable" |
| 1053 | //get the child device for the parent device |
| 1054 | onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id) |
| 1055 | if err != nil { |
| 1056 | log.Errorw("failed-to-get-child-devices-information", log.Fields{"deviceID": dh.device.Id, "error": err}) |
| 1057 | } |
| 1058 | if onuDevices != nil { |
| 1059 | for _, onuDevice := range onuDevices.Items { |
| 1060 | err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
| 1061 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 1062 | if err != nil { |
| 1063 | log.Errorw("failed-to-send-inter-adapter-message", log.Fields{"OnuInd": onuInd, |
| 1064 | "From Adapter": "openolt", "DeviceType": onuDevice.Type, "DeviceID": onuDevice.Id}) |
| 1065 | } |
| 1066 | |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | } |
| 1071 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1072 | //ReenableDevice re-enables the olt device after disable |
| 1073 | //It marks the following for the given device: |
| 1074 | //Device-Handler Admin-State : up |
| 1075 | //Device Port-State: ACTIVE |
| 1076 | //Device Oper-State: ACTIVE |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1077 | func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error { |
| 1078 | if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil { |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1079 | if e, ok := status.FromError(err); ok && e.Code() == codes.Internal { |
| 1080 | log.Errorw("Failed to reenable olt ", log.Fields{"err": err}) |
| 1081 | return err |
| 1082 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | dh.lockDevice.Lock() |
| 1086 | dh.adminState = "up" |
| 1087 | dh.lockDevice.Unlock() |
| 1088 | log.Debug("olt-reenabled") |
| 1089 | |
| 1090 | cloned := proto.Clone(device).(*voltha.Device) |
| 1091 | // Update the all ports state on that device to enable |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1092 | if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 1093 | log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1094 | return err |
| 1095 | } |
| 1096 | |
| 1097 | //Update the device oper status as ACTIVE |
| 1098 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 1099 | dh.device = cloned |
| 1100 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1101 | if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 1102 | log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1103 | return err |
| 1104 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1105 | log.Debugw("ReEnableDevice-end", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1106 | |
| 1107 | return nil |
| 1108 | } |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1109 | |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1110 | func (dh *DeviceHandler) clearUNIData(onu *OnuDevice) error { |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1111 | var uniID uint32 |
| 1112 | var err error |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1113 | for port := range onu.uniPorts { |
| 1114 | delete(onu.uniPorts, port) |
| 1115 | uniID = UniIDFromPortNum(port) |
| 1116 | log.Debugw("clearing-resource-data-for-uni-port", log.Fields{"port": port, "uniID": uniID}) |
| 1117 | /* Delete tech-profile instance from the KV store */ |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame^] | 1118 | if err = dh.flowMgr.DeleteTechProfileInstances(onu.intfID, onu.onuID, uniID, onu.serialNumber); err != nil { |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1119 | log.Debugw("Failed-to-remove-tech-profile-instance-for-onu", log.Fields{"onu-id": onu.onuID}) |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1120 | } |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1121 | log.Debugw("Deleted-tech-profile-instance-for-onu", log.Fields{"onu-id": onu.onuID}) |
| 1122 | flowIDs := dh.resourceMgr.GetCurrentFlowIDsForOnu(onu.intfID, onu.onuID, uniID) |
| 1123 | for _, flowID := range flowIDs { |
| 1124 | dh.resourceMgr.FreeFlowID(onu.intfID, int32(onu.onuID), int32(uniID), flowID) |
| 1125 | } |
| 1126 | dh.resourceMgr.FreePONResourcesForONU(onu.intfID, onu.onuID, uniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame^] | 1127 | if err = dh.resourceMgr.RemoveTechProfileIDsForOnu(onu.intfID, onu.onuID, uniID); err != nil { |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1128 | log.Debugw("Failed-to-remove-tech-profile-id-for-onu", log.Fields{"onu-id": onu.onuID}) |
| 1129 | } |
| 1130 | log.Debugw("Removed-tech-profile-id-for-onu", log.Fields{"onu-id": onu.onuID}) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame^] | 1131 | tpIDList := dh.resourceMgr.GetTechProfileIDForOnu(onu.intfID, onu.onuID, uniID) |
| 1132 | for _, tpID := range tpIDList { |
| 1133 | if err = dh.resourceMgr.RemoveMeterIDForOnu("upstream", onu.intfID, onu.onuID, uniID, tpID); err != nil { |
| 1134 | log.Debugw("Failed-to-remove-meter-id-for-onu-upstream", log.Fields{"onu-id": onu.onuID}) |
| 1135 | } |
| 1136 | log.Debugw("Removed-meter-id-for-onu-upstream", log.Fields{"onu-id": onu.onuID}) |
| 1137 | if err = dh.resourceMgr.RemoveMeterIDForOnu("downstream", onu.intfID, onu.onuID, uniID, tpID); err != nil { |
| 1138 | log.Debugw("Failed-to-remove-meter-id-for-onu-downstream", log.Fields{"onu-id": onu.onuID}) |
| 1139 | } |
| 1140 | log.Debugw("Removed-meter-id-for-onu-downstream", log.Fields{"onu-id": onu.onuID}) |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1141 | } |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1142 | } |
| 1143 | return nil |
| 1144 | } |
| 1145 | |
| 1146 | func (dh *DeviceHandler) clearNNIData() error { |
| 1147 | nniUniID := -1 |
| 1148 | nniOnuID := -1 |
| 1149 | flowIDs := dh.resourceMgr.GetCurrentFlowIDsForOnu(uint32(dh.nniIntfID), uint32(nniOnuID), uint32(nniUniID)) |
| 1150 | log.Debugw("Current flow ids for nni", log.Fields{"flow-ids": flowIDs}) |
| 1151 | for _, flowID := range flowIDs { |
| 1152 | dh.resourceMgr.FreeFlowID(uint32(dh.nniIntfID), -1, -1, uint32(flowID)) |
| 1153 | } |
| 1154 | //Free the flow-ids for the NNI port |
| 1155 | dh.resourceMgr.FreePONResourcesForONU(uint32(dh.nniIntfID), uint32(nniOnuID), uint32(nniUniID)) |
| 1156 | /* Free ONU IDs for each pon port |
| 1157 | intfIDToONUIds is a map of intf-id: [onu-ids]*/ |
| 1158 | intfIDToONUIds := make(map[uint32][]uint32) |
| 1159 | for _, onu := range dh.onus { |
| 1160 | intfIDToONUIds[onu.intfID] = append(intfIDToONUIds[onu.intfID], onu.onuID) |
| 1161 | } |
| 1162 | for intfID, onuIds := range intfIDToONUIds { |
| 1163 | dh.resourceMgr.FreeonuID(intfID, onuIds) |
| 1164 | } |
| 1165 | return nil |
| 1166 | } |
| 1167 | |
| 1168 | // DeleteDevice deletes the device instance from openolt handler array. Also clears allocated resource manager resources. Also reboots the OLT hardware! |
| 1169 | func (dh *DeviceHandler) DeleteDevice(device *voltha.Device) error { |
| 1170 | log.Debug("Function entry delete device") |
| 1171 | dh.lockDevice.Lock() |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1172 | if dh.adminState == "deleted" { |
| 1173 | dh.lockDevice.Unlock() |
| 1174 | return nil |
| 1175 | } |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1176 | dh.adminState = "deleted" |
| 1177 | dh.lockDevice.Unlock() |
| 1178 | /* Clear the KV store data associated with the all the UNI ports |
| 1179 | This clears up flow data and also resource map data for various |
| 1180 | other pon resources like alloc_id and gemport_id |
| 1181 | */ |
| 1182 | for _, onu := range dh.onus { |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1183 | if err := dh.clearUNIData(onu); err != nil { |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1184 | log.Debugw("Failed to clear data for onu", log.Fields{"onu-device": onu}) |
| 1185 | } |
| 1186 | } |
| 1187 | /* Clear the flows from KV store associated with NNI port. |
| 1188 | There are mostly trap rules from NNI port (like LLDP) |
| 1189 | */ |
| 1190 | if err := dh.clearNNIData(); err != nil { |
| 1191 | log.Debugw("Failed to clear data for NNI port", log.Fields{"device-id": dh.deviceID}) |
| 1192 | } |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1193 | |
| 1194 | /* Clear the resource pool for each PON port in the background */ |
| 1195 | go dh.resourceMgr.Delete() |
| 1196 | |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1197 | /*Delete ONU map for the device*/ |
| 1198 | for onu := range dh.onus { |
| 1199 | delete(dh.onus, onu) |
| 1200 | } |
| 1201 | log.Debug("Removed-device-from-Resource-manager-KV-store") |
| 1202 | //Reset the state |
| 1203 | if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil { |
| 1204 | log.Errorw("Failed-to-reboot-olt ", log.Fields{"err": err}) |
| 1205 | return err |
| 1206 | } |
| 1207 | cloned := proto.Clone(device).(*voltha.Device) |
| 1208 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 1209 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
| 1210 | if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 1211 | log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err}) |
| 1212 | return err |
| 1213 | } |
| 1214 | return nil |
| 1215 | } |
| 1216 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1217 | //RebootDevice reboots the given device |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 1218 | func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error { |
| 1219 | if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil { |
| 1220 | log.Errorw("Failed to reboot olt ", log.Fields{"err": err}) |
| 1221 | return err |
| 1222 | } |
| 1223 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1224 | log.Debugw("rebooted-device-successfully", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 1225 | |
| 1226 | return nil |
| 1227 | } |
| 1228 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1229 | func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) { |
| 1230 | log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn}) |
| 1231 | logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn) |
| 1232 | if err != nil { |
| 1233 | log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err}) |
| 1234 | return |
| 1235 | } |
| 1236 | log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1237 | if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil { |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1238 | log.Errorw("Error sending packet-in to core", log.Fields{"error": err}) |
| 1239 | return |
| 1240 | } |
| 1241 | log.Debug("Success sending packet-in to core!") |
| 1242 | } |
| 1243 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1244 | // PacketOut sends packet-out from VOLTHA to OLT on the egress port provided |
| 1245 | func (dh *DeviceHandler) PacketOut(egressPortNo int, packet *of.OfpPacketOut) error { |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1246 | log.Debugw("incoming-packet-out", log.Fields{"deviceID": dh.deviceID, "egress_port_no": egressPortNo, |
| 1247 | "pkt-length": len(packet.Data), "packetData": hex.EncodeToString(packet.Data)}) |
| 1248 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1249 | egressPortType := IntfIDToPortTypeName(uint32(egressPortNo)) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1250 | if egressPortType == voltha.Port_ETHERNET_UNI { |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1251 | outerEthType := (uint16(packet.Data[12]) << 8) | uint16(packet.Data[13]) |
| 1252 | innerEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17]) |
| 1253 | if outerEthType == 0x88a8 || outerEthType == 0x8100 { |
| 1254 | if innerEthType == 0x8100 { |
| 1255 | // q-in-q 802.1ad or 802.1q double tagged packet. |
| 1256 | // slice out the outer tag. |
| 1257 | packet.Data = append(packet.Data[:12], packet.Data[16:]...) |
| 1258 | log.Debugw("packet-now-single-tagged", log.Fields{"packetData": hex.EncodeToString(packet.Data)}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1259 | } |
| 1260 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1261 | intfID := IntfIDFromUniPortNum(uint32(egressPortNo)) |
| 1262 | onuID := OnuIDFromPortNum(uint32(egressPortNo)) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 1263 | uniID := UniIDFromPortNum(uint32(egressPortNo)) |
| 1264 | |
| 1265 | gemPortID, err := dh.flowMgr.GetPacketOutGemPortID(intfID, onuID, uint32(egressPortNo)) |
| 1266 | if err != nil { |
| 1267 | // In this case the openolt agent will receive the gemPortID as 0. |
| 1268 | // The agent tries to retrieve the gemPortID in this case. |
| 1269 | // This may not always succeed at the agent and packetOut may fail. |
| 1270 | log.Error("failed-to-retrieve-gemport-id-for-packet-out") |
| 1271 | } |
| 1272 | |
| 1273 | onuPkt := oop.OnuPacket{IntfId: intfID, OnuId: onuID, PortNo: uint32(egressPortNo), GemportId: gemPortID, Pkt: packet.Data} |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1274 | |
| 1275 | log.Debugw("sending-packet-to-onu", log.Fields{"egress_port_no": egressPortNo, "IntfId": intfID, "onuID": onuID, |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 1276 | "uniID": uniID, "gemPortID": gemPortID, "packet": hex.EncodeToString(packet.Data)}) |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1277 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1278 | if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil { |
| 1279 | log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err}) |
| 1280 | return err |
| 1281 | } |
| 1282 | } else if egressPortType == voltha.Port_ETHERNET_NNI { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1283 | uplinkPkt := oop.UplinkPacket{IntfId: IntfIDFromNniPortNum(uint32(egressPortNo)), Pkt: packet.Data} |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1284 | |
| 1285 | log.Debugw("sending-packet-to-nni", log.Fields{"uplink_pkt": uplinkPkt, "packet": hex.EncodeToString(packet.Data)}) |
| 1286 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1287 | if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil { |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1288 | log.Errorw("Error while sending packet-out to NNI", log.Fields{"error": err}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1289 | return err |
| 1290 | } |
| 1291 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1292 | log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egressPortNo, "egressPortType": egressPortType}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1293 | } |
| 1294 | return nil |
| 1295 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 1296 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1297 | func (dh *DeviceHandler) formOnuKey(intfID, onuID uint32) string { |
| 1298 | return "" + strconv.Itoa(int(intfID)) + "." + strconv.Itoa(int(onuID)) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 1299 | } |