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 | |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 17 | //Package core provides the utility for olt devices, flows and statistics |
| 18 | package core |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 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 | "fmt" |
| 24 | "io" |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 25 | "net" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 26 | "strconv" |
| 27 | "strings" |
| 28 | "sync" |
| 29 | "time" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 30 | |
Chaitrashree G S | b2b62dd | 2019-07-24 21:47:04 -0400 | [diff] [blame] | 31 | "google.golang.org/grpc/codes" |
| 32 | |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 33 | "github.com/cenkalti/backoff/v3" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 34 | "github.com/gogo/protobuf/proto" |
| 35 | "github.com/golang/protobuf/ptypes" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 36 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
| 37 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 38 | "github.com/opencord/voltha-lib-go/v3/pkg/pmmetrics" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 39 | rsrcMgr "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 40 | "github.com/opencord/voltha-protos/v3/go/common" |
| 41 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 42 | of "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 43 | oop "github.com/opencord/voltha-protos/v3/go/openolt" |
| 44 | "github.com/opencord/voltha-protos/v3/go/voltha" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 45 | "google.golang.org/grpc" |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 46 | "google.golang.org/grpc/status" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 47 | ) |
| 48 | |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 49 | // Constants for number of retries and for timeout |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 50 | const ( |
salmansiddiqui | 7ac6213 | 2019-08-22 03:58:50 +0000 | [diff] [blame] | 51 | MaxRetry = 10 |
| 52 | MaxTimeOutInMs = 500 |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 53 | ) |
| 54 | |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 55 | //DeviceHandler will interact with the OLT device. |
| 56 | type DeviceHandler struct { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 57 | deviceID string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 58 | deviceType string |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 59 | adminState string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 60 | device *voltha.Device |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 61 | coreProxy adapterif.CoreProxy |
| 62 | AdapterProxy adapterif.AdapterProxy |
| 63 | EventProxy adapterif.EventProxy |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 64 | openOLT *OpenOLT |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 65 | exitChannel chan int |
| 66 | lockDevice sync.RWMutex |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 67 | Client oop.OpenoltClient |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 68 | transitionMap *TransitionMap |
| 69 | clientCon *grpc.ClientConn |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 70 | flowMgr *OpenOltFlowMgr |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 71 | eventMgr *OpenOltEventMgr |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 72 | resourceMgr *rsrcMgr.OpenOltResourceMgr |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 73 | |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 74 | discOnus sync.Map |
| 75 | onus sync.Map |
| 76 | portStats *OpenOltStatisticsMgr |
| 77 | metrics *pmmetrics.PmMetrics |
| 78 | stopCollector chan bool |
| 79 | stopHeartbeatCheck chan bool |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 80 | activePorts sync.Map |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 83 | //OnuDevice represents ONU related info |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 84 | type OnuDevice struct { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 85 | deviceID string |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 86 | deviceType string |
| 87 | serialNumber string |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 88 | onuID uint32 |
| 89 | intfID uint32 |
| 90 | proxyDeviceID string |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 91 | uniPorts map[uint32]struct{} |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 94 | var pmNames = []string{ |
| 95 | "rx_bytes", |
| 96 | "rx_packets", |
| 97 | "rx_mcast_packets", |
| 98 | "rx_bcast_packets", |
| 99 | "tx_bytes", |
| 100 | "tx_packets", |
| 101 | "tx_mcast_packets", |
| 102 | "tx_bcast_packets", |
| 103 | } |
| 104 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 105 | //NewOnuDevice creates a new Onu Device |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 106 | func NewOnuDevice(devID, deviceTp, serialNum string, onuID, intfID uint32, proxyDevID string) *OnuDevice { |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 107 | var device OnuDevice |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 108 | device.deviceID = devID |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 109 | device.deviceType = deviceTp |
| 110 | device.serialNumber = serialNum |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 111 | device.onuID = onuID |
| 112 | device.intfID = intfID |
| 113 | device.proxyDeviceID = proxyDevID |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 114 | device.uniPorts = make(map[uint32]struct{}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 115 | return &device |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | //NewDeviceHandler creates a new device handler |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 119 | 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] | 120 | var dh DeviceHandler |
| 121 | dh.coreProxy = cp |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 122 | dh.AdapterProxy = ap |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 123 | dh.EventProxy = ep |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 124 | cloned := (proto.Clone(device)).(*voltha.Device) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 125 | dh.deviceID = cloned.Id |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 126 | dh.deviceType = cloned.Type |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 127 | dh.adminState = "up" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 128 | dh.device = cloned |
| 129 | dh.openOLT = adapter |
| 130 | dh.exitChannel = make(chan int, 1) |
| 131 | dh.lockDevice = sync.RWMutex{} |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 132 | dh.stopCollector = make(chan bool, 2) |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 133 | dh.stopHeartbeatCheck = make(chan bool, 2) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 134 | dh.metrics = pmmetrics.NewPmMetrics(cloned.Id, pmmetrics.Frequency(150), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames)) |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 135 | dh.activePorts = sync.Map{} |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 136 | //TODO initialize the support classes. |
| 137 | return &dh |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // start save the device to the data model |
| 141 | func (dh *DeviceHandler) start(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 142 | dh.lockDevice.Lock() |
| 143 | defer dh.lockDevice.Unlock() |
| 144 | log.Debugw("starting-device-agent", log.Fields{"device": dh.device}) |
| 145 | // Add the initial device to the local model |
| 146 | log.Debug("device-agent-started") |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // stop stops the device dh. Not much to do for now |
| 150 | func (dh *DeviceHandler) stop(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 151 | dh.lockDevice.Lock() |
| 152 | defer dh.lockDevice.Unlock() |
| 153 | log.Debug("stopping-device-agent") |
| 154 | dh.exitChannel <- 1 |
| 155 | log.Debug("device-agent-stopped") |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 156 | } |
| 157 | |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 158 | func macifyIP(ip net.IP) string { |
| 159 | if len(ip) > 0 { |
| 160 | oct1 := strconv.FormatInt(int64(ip[12]), 16) |
| 161 | oct2 := strconv.FormatInt(int64(ip[13]), 16) |
| 162 | oct3 := strconv.FormatInt(int64(ip[14]), 16) |
| 163 | oct4 := strconv.FormatInt(int64(ip[15]), 16) |
| 164 | return fmt.Sprintf("00:00:%02v:%02v:%02v:%02v", oct1, oct2, oct3, oct4) |
| 165 | } |
| 166 | return "" |
| 167 | } |
| 168 | |
| 169 | func generateMacFromHost(host string) (string, error) { |
| 170 | var genmac string |
| 171 | var addr net.IP |
| 172 | var ips []string |
| 173 | var err error |
| 174 | |
| 175 | log.Debugw("generating-mac-from-host", log.Fields{"host": host}) |
| 176 | |
| 177 | if addr = net.ParseIP(host); addr == nil { |
| 178 | log.Debugw("looking-up-hostname", log.Fields{"host": host}) |
| 179 | |
| 180 | if ips, err = net.LookupHost(host); err == nil { |
| 181 | log.Debugw("dns-result-ips", log.Fields{"ips": ips}) |
| 182 | if addr = net.ParseIP(ips[0]); addr == nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 183 | return "", NewErrInvalidValue(log.Fields{"ip": ips[0]}, nil).Log() |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 184 | } |
| 185 | genmac = macifyIP(addr) |
| 186 | log.Debugw("using-ip-as-mac", log.Fields{"host": ips[0], "mac": genmac}) |
| 187 | return genmac, nil |
| 188 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 189 | return "", NewErrAdapter("cannot-resolve-hostname-to-ip", nil, err).Log() |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | genmac = macifyIP(addr) |
| 193 | log.Debugw("using-ip-as-mac", log.Fields{"host": host, "mac": genmac}) |
| 194 | return genmac, nil |
| 195 | } |
| 196 | |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 197 | func macAddressToUint32Array(mac string) []uint32 { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 198 | slist := strings.Split(mac, ":") |
| 199 | result := make([]uint32, len(slist)) |
| 200 | var err error |
| 201 | var tmp int64 |
| 202 | for index, val := range slist { |
| 203 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 204 | return []uint32{1, 2, 3, 4, 5, 6} |
| 205 | } |
| 206 | result[index] = uint32(tmp) |
| 207 | } |
| 208 | return result |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 209 | } |
| 210 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 211 | //GetportLabel returns the label for the NNI and the PON port based on port number and port type |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 212 | func GetportLabel(portNum uint32, portType voltha.Port_PortType) (string, error) { |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 213 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 214 | switch portType { |
| 215 | case voltha.Port_ETHERNET_NNI: |
| 216 | return fmt.Sprintf("nni-%d", portNum), nil |
| 217 | case voltha.Port_PON_OLT: |
| 218 | return fmt.Sprintf("pon-%d", portNum), nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 219 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 220 | |
| 221 | return "", NewErrInvalidValue(log.Fields{"port-type": portType}, nil).Log() |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 222 | } |
| 223 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 224 | func (dh *DeviceHandler) addPort(intfID uint32, portType voltha.Port_PortType, state string) error { |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 225 | var operStatus common.OperStatus_Types |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 226 | if state == "up" { |
| 227 | operStatus = voltha.OperStatus_ACTIVE |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 228 | //populating the intfStatus map |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 229 | dh.activePorts.Store(intfID, true) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 230 | } else { |
| 231 | operStatus = voltha.OperStatus_DISCOVERED |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 232 | dh.activePorts.Store(intfID, false) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 233 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 234 | portNum := IntfIDToPortNo(intfID, portType) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 235 | label, err := GetportLabel(portNum, portType) |
| 236 | if err != nil { |
| 237 | return NewErrNotFound("port-label", log.Fields{"port-number": portNum, "port-type": portType}, nil).Log() |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 238 | } |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 239 | |
| 240 | device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id) |
| 241 | if err != nil || device == nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 242 | return NewErrNotFound("device", log.Fields{"device-id": dh.device.Id}, err).Log() |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 243 | } |
| 244 | if device.Ports != nil { |
| 245 | for _, dPort := range device.Ports { |
| 246 | if dPort.Type == portType && dPort.PortNo == portNum { |
| 247 | log.Debug("port-already-exists-updating-oper-status-of-port") |
| 248 | if err := dh.coreProxy.PortStateUpdate(context.TODO(), dh.device.Id, portType, portNum, operStatus); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 249 | return NewErrAdapter("failed-to-update-port-state", log.Fields{ |
| 250 | "device-id": dh.device.Id, |
| 251 | "port-type": portType, |
| 252 | "port-number": portNum, |
| 253 | "oper-status": operStatus}, err).Log() |
| 254 | |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 255 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 256 | return nil |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 260 | // Now create Port |
| 261 | port := &voltha.Port{ |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 262 | PortNo: portNum, |
| 263 | Label: label, |
| 264 | Type: portType, |
| 265 | OperStatus: operStatus, |
| 266 | } |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 267 | log.Debugw("Sending-port-update-to-core", log.Fields{"port": port}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 268 | // 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] | 269 | if err := dh.coreProxy.PortCreated(context.TODO(), dh.device.Id, port); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 270 | return NewErrAdapter("Error-creating-port", log.Fields{ |
| 271 | "device-id": dh.device.Id, |
| 272 | "port-type": portType}, err).Log() |
Girish Gowdru | 1110ef2 | 2019-06-24 11:17:59 -0400 | [diff] [blame] | 273 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 274 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // readIndications to read the indications from the OLT device |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 278 | func (dh *DeviceHandler) readIndications(ctx context.Context) error { |
| 279 | defer log.Debugw("indications-ended", log.Fields{"device-id": dh.device.Id}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 280 | indications, err := dh.Client.EnableIndication(ctx, new(oop.Empty)) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 281 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 282 | return NewErrCommunication("fail-to-read-indications", log.Fields{"device-id": dh.device.Id}, err).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 283 | } |
| 284 | if indications == nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 285 | return NewErrInvalidValue(log.Fields{"indications": nil, "device-id": dh.device.Id}, nil).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 286 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 287 | /* get device state */ |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 288 | device, err := dh.coreProxy.GetDevice(ctx, dh.device.Id, dh.device.Id) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 289 | if err != nil || device == nil { |
| 290 | /*TODO: needs to handle error scenarios */ |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 291 | return NewErrNotFound("device", log.Fields{"device-id": dh.device.Id}, err).Log() |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 292 | } |
| 293 | // When the device is in DISABLED and Adapter container restarts, we need to |
| 294 | // rebuild the locally maintained admin state. |
| 295 | if device.AdminState == voltha.AdminState_DISABLED { |
| 296 | dh.lockDevice.Lock() |
| 297 | dh.adminState = "down" |
| 298 | dh.lockDevice.Unlock() |
| 299 | } |
| 300 | |
David Bainbridge | f5879ca | 2019-12-13 21:17:54 +0000 | [diff] [blame] | 301 | // Create an exponential backoff around re-enabling indications. The |
| 302 | // maximum elapsed time for the back off is set to 0 so that we will |
| 303 | // continue to retry. The max interval defaults to 1m, but is set |
| 304 | // here for code clarity |
| 305 | indicationBackoff := backoff.NewExponentialBackOff() |
| 306 | indicationBackoff.MaxElapsedTime = 0 |
| 307 | indicationBackoff.MaxInterval = 1 * time.Minute |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 308 | for { |
| 309 | indication, err := indications.Recv() |
| 310 | if err == io.EOF { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 311 | log.Infow("EOF for indications", log.Fields{"err": err}) |
David Bainbridge | f5879ca | 2019-12-13 21:17:54 +0000 | [diff] [blame] | 312 | // Use an exponential back off to prevent getting into a tight loop |
| 313 | duration := indicationBackoff.NextBackOff() |
| 314 | if duration == backoff.Stop { |
| 315 | // If we reach a maximum then warn and reset the backoff |
| 316 | // timer and keep attempting. |
| 317 | log.Warnw("Maximum indication backoff reached, resetting backoff timer", |
| 318 | log.Fields{"max_indication_backoff": indicationBackoff.MaxElapsedTime}) |
| 319 | indicationBackoff.Reset() |
| 320 | } |
| 321 | time.Sleep(indicationBackoff.NextBackOff()) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 322 | indications, err = dh.Client.EnableIndication(ctx, new(oop.Empty)) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 323 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 324 | return NewErrCommunication("indication-read-failure", log.Fields{"device-id": dh.device.Id}, err).Log() |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 325 | } |
| 326 | continue |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 327 | } |
| 328 | if err != nil { |
| 329 | log.Infow("Failed to read from indications", log.Fields{"err": err}) |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 330 | if dh.adminState == "deleted" { |
| 331 | log.Debug("Device deleted stoping the read indication thread") |
| 332 | break |
| 333 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 334 | dh.transitionMap.Handle(ctx, DeviceDownInd) |
| 335 | dh.transitionMap.Handle(ctx, DeviceInit) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 336 | return NewErrCommunication("indication-read-failure", log.Fields{"device-id": dh.device.Id}, err).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 337 | } |
David Bainbridge | f5879ca | 2019-12-13 21:17:54 +0000 | [diff] [blame] | 338 | // Reset backoff if we have a successful receive |
| 339 | indicationBackoff.Reset() |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 340 | dh.lockDevice.RLock() |
| 341 | adminState := dh.adminState |
| 342 | dh.lockDevice.RUnlock() |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 343 | // When OLT is admin down, ignore all indications. |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 344 | if adminState == "down" { |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 345 | |
Abhilash Laxmeshwar | 5b302e1 | 2020-01-09 15:15:14 +0530 | [diff] [blame] | 346 | log.Infow("olt is admin down, ignore indication", log.Fields{"indication": indication}) |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 347 | continue |
| 348 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 349 | dh.handleIndication(ctx, indication) |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 350 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 351 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 352 | return nil |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 353 | } |
| 354 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 355 | func (dh *DeviceHandler) handleOltIndication(ctx context.Context, oltIndication *oop.OltIndication) error { |
Daniele Rossi | 051466a | 2019-07-26 13:39:37 +0000 | [diff] [blame] | 356 | raisedTs := time.Now().UnixNano() |
Gamze Abaka | a1a5052 | 2019-10-03 19:28:27 +0000 | [diff] [blame] | 357 | if oltIndication.OperState == "up" && dh.transitionMap.currentDeviceState != deviceStateUp { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 358 | dh.transitionMap.Handle(ctx, DeviceUpInd) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 359 | } else if oltIndication.OperState == "down" { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 360 | dh.transitionMap.Handle(ctx, DeviceDownInd) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 361 | } |
Daniele Rossi | 051466a | 2019-07-26 13:39:37 +0000 | [diff] [blame] | 362 | // Send or clear Alarm |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 363 | if err := dh.eventMgr.oltUpDownIndication(oltIndication, dh.deviceID, raisedTs); err != nil { |
| 364 | return NewErrAdapter("failed-indication", log.Fields{ |
| 365 | "device_id": dh.deviceID, |
| 366 | "indication": oltIndication, |
| 367 | "timestamp": raisedTs}, err).Log() |
| 368 | } |
| 369 | return nil |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 370 | } |
| 371 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 372 | // nolint: gocyclo |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 373 | func (dh *DeviceHandler) handleIndication(ctx context.Context, indication *oop.Indication) { |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 374 | raisedTs := time.Now().UnixNano() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 375 | switch indication.Data.(type) { |
| 376 | case *oop.Indication_OltInd: |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 377 | if err := dh.handleOltIndication(ctx, indication.GetOltInd()); err != nil { |
| 378 | NewErrAdapter("handle-indication-error", log.Fields{"type": "olt"}, err).Log() |
| 379 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 380 | case *oop.Indication_IntfInd: |
| 381 | intfInd := indication.GetIntfInd() |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 382 | go func() { |
| 383 | if err := dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState()); err != nil { |
| 384 | NewErrAdapter("handle-indication-error", log.Fields{"type": "interface"}, err).Log() |
| 385 | } |
| 386 | }() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 387 | log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd}) |
| 388 | case *oop.Indication_IntfOperInd: |
| 389 | intfOperInd := indication.GetIntfOperInd() |
| 390 | if intfOperInd.GetType() == "nni" { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 391 | go func() { |
| 392 | if err := dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState()); err != nil { |
| 393 | NewErrAdapter("handle-indication-error", log.Fields{"type": "interface-oper-nni"}, err).Log() |
| 394 | } |
| 395 | }() |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 396 | dh.resourceMgr.AddNNIToKVStore(ctx, intfOperInd.GetIntfId()) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 397 | } else if intfOperInd.GetType() == "pon" { |
| 398 | // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down |
| 399 | // Handle pon port update |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 400 | go func() { |
| 401 | if err := dh.addPort(intfOperInd.GetIntfId(), voltha.Port_PON_OLT, intfOperInd.GetOperState()); err != nil { |
| 402 | NewErrAdapter("handle-indication-error", log.Fields{"type": "interface-oper-pon"}, err).Log() |
| 403 | } |
| 404 | }() |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 405 | go dh.eventMgr.oltIntfOperIndication(indication.GetIntfOperInd(), dh.deviceID, raisedTs) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 406 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 407 | log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd}) |
| 408 | case *oop.Indication_OnuDiscInd: |
| 409 | onuDiscInd := indication.GetOnuDiscInd() |
| 410 | log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 411 | sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 412 | go func() { |
| 413 | if err := dh.onuDiscIndication(ctx, onuDiscInd, sn); err != nil { |
| 414 | NewErrAdapter("handle-indication-error", log.Fields{"type": "onu-discovery"}, err).Log() |
| 415 | } |
| 416 | }() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 417 | case *oop.Indication_OnuInd: |
| 418 | onuInd := indication.GetOnuInd() |
| 419 | log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 420 | go func() { |
| 421 | if err := dh.onuIndication(onuInd); err != nil { |
| 422 | NewErrAdapter("handle-indication-error", log.Fields{"type": "onu"}, err).Log() |
| 423 | } |
| 424 | }() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 425 | case *oop.Indication_OmciInd: |
| 426 | omciInd := indication.GetOmciInd() |
lcui | e24ef18 | 2019-04-29 22:58:36 -0700 | [diff] [blame] | 427 | log.Debugw("Received Omci indication ", log.Fields{"IntfId": omciInd.IntfId, "OnuId": omciInd.OnuId, "pkt": hex.EncodeToString(omciInd.Pkt)}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 428 | go func() { |
| 429 | if err := dh.omciIndication(omciInd); err != nil { |
| 430 | NewErrAdapter("handle-indication-error", log.Fields{"type": "omci"}, err).Log() |
| 431 | } |
| 432 | }() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 433 | case *oop.Indication_PktInd: |
| 434 | pktInd := indication.GetPktInd() |
| 435 | log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 436 | go func() { |
| 437 | if err := dh.handlePacketIndication(ctx, pktInd); err != nil { |
| 438 | NewErrAdapter("handle-indication-error", log.Fields{"type": "packet"}, err).Log() |
| 439 | } |
| 440 | }() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 441 | case *oop.Indication_PortStats: |
| 442 | portStats := indication.GetPortStats() |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 443 | go dh.portStats.PortStatisticsIndication(portStats, dh.resourceMgr.DevInfo.GetPonPorts()) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 444 | case *oop.Indication_FlowStats: |
| 445 | flowStats := indication.GetFlowStats() |
| 446 | log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats}) |
| 447 | case *oop.Indication_AlarmInd: |
| 448 | alarmInd := indication.GetAlarmInd() |
| 449 | log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd}) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 450 | go dh.eventMgr.ProcessEvents(alarmInd, dh.deviceID, raisedTs) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 451 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // doStateUp handle the olt up indication and update to voltha core |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 455 | func (dh *DeviceHandler) doStateUp(ctx context.Context) error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 456 | // Synchronous call to update device state - this method is run in its own go routine |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 457 | if err := dh.coreProxy.DeviceStateUpdate(ctx, dh.device.Id, voltha.ConnectStatus_REACHABLE, |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 458 | voltha.OperStatus_ACTIVE); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 459 | return NewErrAdapter("device-update-failed", log.Fields{"device-id": dh.device.Id}, err).Log() |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 460 | } |
| 461 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // doStateDown handle the olt down indication |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 465 | func (dh *DeviceHandler) doStateDown(ctx context.Context) error { |
serkant.uluderya | 245caba | 2019-09-24 23:15:29 -0700 | [diff] [blame] | 466 | dh.lockDevice.Lock() |
| 467 | defer dh.lockDevice.Unlock() |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 468 | log.Debug("do-state-down-start") |
| 469 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 470 | device, err := dh.coreProxy.GetDevice(ctx, dh.device.Id, dh.device.Id) |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 471 | if err != nil || device == nil { |
| 472 | /*TODO: needs to handle error scenarios */ |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 473 | return NewErrNotFound("device", log.Fields{"device-id": dh.device.Id}, err).Log() |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | cloned := proto.Clone(device).(*voltha.Device) |
| 477 | // Update the all ports state on that device to disable |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 478 | if err = dh.coreProxy.PortsStateUpdate(ctx, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
| 479 | return NewErrAdapter("port-update-failed", log.Fields{"device-id": device.Id}, err).Log() |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | //Update the device oper state and connection status |
| 483 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 484 | cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE |
| 485 | dh.device = cloned |
| 486 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 487 | if err = dh.coreProxy.DeviceStateUpdate(ctx, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 488 | return NewErrAdapter("state-update-failed", log.Fields{"device-id": device.Id}, err).Log() |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 489 | } |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 490 | |
| 491 | //get the child device for the parent device |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 492 | onuDevices, err := dh.coreProxy.GetChildDevices(ctx, dh.device.Id) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 493 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 494 | return NewErrAdapter("child-device-fetch-failed", log.Fields{"device-id": dh.device.Id}, err).Log() |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 495 | } |
| 496 | for _, onuDevice := range onuDevices.Items { |
| 497 | |
| 498 | // Update onu state as down in onu adapter |
| 499 | onuInd := oop.OnuIndication{} |
| 500 | onuInd.OperState = "down" |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 501 | err := dh.AdapterProxy.SendInterAdapterMessage(ctx, &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 502 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 503 | if err != nil { |
| 504 | NewErrCommunication("inter-adapter-send-failed", log.Fields{ |
| 505 | "source": "openolt", |
| 506 | "onu-indicator": onuInd, |
| 507 | "device-type": onuDevice.Type, |
| 508 | "device-id": onuDevice.Id}, err).LogAt(log.ErrorLevel) |
serkant.uluderya | 245caba | 2019-09-24 23:15:29 -0700 | [diff] [blame] | 509 | //Do not return here and continue to process other ONUs |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 510 | } |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 511 | } |
serkant.uluderya | 245caba | 2019-09-24 23:15:29 -0700 | [diff] [blame] | 512 | /* Discovered ONUs entries need to be cleared , since after OLT |
| 513 | is up, it starts sending discovery indications again*/ |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 514 | dh.discOnus = sync.Map{} |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 515 | log.Debugw("do-state-down-end", log.Fields{"device-id": device.Id}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 516 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // doStateInit dial the grpc before going to init state |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 520 | func (dh *DeviceHandler) doStateInit(ctx context.Context) error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 521 | var err error |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 522 | if dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock()); err != nil { |
| 523 | return NewErrCommunication("dial-failure", log.Fields{ |
| 524 | "device-id": dh.deviceID, |
| 525 | "host-and-port": dh.device.GetHostAndPort()}, err).Log() |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 526 | } |
| 527 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | // postInit create olt client instance to invoke RPC on the olt device |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 531 | func (dh *DeviceHandler) postInit(ctx context.Context) error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 532 | dh.Client = oop.NewOpenoltClient(dh.clientCon) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 533 | dh.transitionMap.Handle(ctx, GrpcConnected) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 534 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | // doStateConnected get the device info and update to voltha core |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 538 | func (dh *DeviceHandler) doStateConnected(ctx context.Context) error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 539 | log.Debug("OLT device has been connected") |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 540 | |
| 541 | // Case where OLT is disabled and then rebooted. |
| 542 | if dh.adminState == "down" { |
| 543 | log.Debugln("do-state-connected--device-admin-state-down") |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 544 | device, err := dh.coreProxy.GetDevice(ctx, dh.device.Id, dh.device.Id) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 545 | if err != nil || device == nil { |
| 546 | /*TODO: needs to handle error scenarios */ |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 547 | NewErrAdapter("device-fetch-failed", log.Fields{"device-id": dh.device.Id}, err).LogAt(log.ErrorLevel) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | cloned := proto.Clone(device).(*voltha.Device) |
| 551 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 552 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 553 | dh.device = cloned |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 554 | if er := dh.coreProxy.DeviceStateUpdate(ctx, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 555 | NewErrAdapter("device-state-update-failed", log.Fields{"device-id": dh.device.Id}, err).LogAt(log.ErrorLevel) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 558 | // Since the device was disabled before the OLT was rebooted, enforce the OLT to be Disabled after re-connection. |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 559 | _, err = dh.Client.DisableOlt(ctx, new(oop.Empty)) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 560 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 561 | NewErrAdapter("olt-disable-failed", log.Fields{"device-id": dh.device.Id}, err).LogAt(log.ErrorLevel) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // Start reading indications |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 565 | go func() { |
| 566 | if err := dh.readIndications(ctx); err != nil { |
| 567 | NewErrAdapter("indication-read-failure", log.Fields{"device-id": dh.device.Id}, err).LogAt(log.ErrorLevel) |
| 568 | } |
| 569 | }() |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 570 | return nil |
| 571 | } |
| 572 | |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 573 | deviceInfo, err := dh.populateDeviceInfo() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 574 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 575 | return NewErrAdapter("populate-device-info-failed", log.Fields{"device-id": dh.device.Id}, err).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 576 | } |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 577 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 578 | 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] | 579 | if err != nil || device == nil { |
| 580 | /*TODO: needs to handle error scenarios */ |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 581 | return NewErrAdapter("fetch-device-failed", log.Fields{"device-id": dh.device.Id}, err).Log() |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 582 | } |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 583 | dh.populateActivePorts(device) |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 584 | if err := dh.disableAdminDownPorts(device); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 585 | return NewErrAdapter("port-status-update-failed", log.Fields{"device": device}, err).Log() |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 586 | } |
| 587 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 588 | KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort) |
| 589 | // Instantiate resource manager |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 590 | if dh.resourceMgr = rsrcMgr.NewResourceMgr(ctx, dh.deviceID, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 591 | return ErrResourceManagerInstantiating.Log() |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 592 | } |
| 593 | // Instantiate flow manager |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 594 | if dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr); dh.flowMgr == nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 595 | return ErrResourceManagerInstantiating.Log() |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 596 | } |
| 597 | /* TODO: Instantiate Alarm , stats , BW managers */ |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 598 | /* Instantiating Event Manager to handle Alarms and KPIs */ |
Devmalya Paul | 90ca301 | 2019-09-02 21:55:45 -0400 | [diff] [blame] | 599 | dh.eventMgr = NewEventMgr(dh.EventProxy, dh) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 600 | // Stats config for new device |
| 601 | dh.portStats = NewOpenOltStatsMgr(dh) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 602 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 603 | // Start reading indications |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 604 | go func() { |
| 605 | if err := dh.readIndications(ctx); err != nil { |
| 606 | NewErrAdapter("read-indications-failure", log.Fields{"device-id": dh.device.Id}, err).Log() |
| 607 | } |
| 608 | }() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 609 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 610 | } |
| 611 | |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 612 | func (dh *DeviceHandler) populateDeviceInfo() (*oop.DeviceInfo, error) { |
| 613 | var err error |
| 614 | var deviceInfo *oop.DeviceInfo |
| 615 | |
| 616 | deviceInfo, err = dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty)) |
| 617 | |
| 618 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 619 | return nil, NewErrPersistence("get", "device", 0, nil, err).Log() |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 620 | } |
| 621 | if deviceInfo == nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 622 | return nil, NewErrInvalidValue(log.Fields{"device": nil}, nil).Log() |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo}) |
| 626 | dh.device.Root = true |
| 627 | dh.device.Vendor = deviceInfo.Vendor |
| 628 | dh.device.Model = deviceInfo.Model |
| 629 | dh.device.SerialNumber = deviceInfo.DeviceSerialNumber |
| 630 | dh.device.HardwareVersion = deviceInfo.HardwareVersion |
| 631 | dh.device.FirmwareVersion = deviceInfo.FirmwareVersion |
| 632 | |
| 633 | if deviceInfo.DeviceId == "" { |
| 634 | log.Warnw("no-device-id-provided-using-host", log.Fields{"hostport": dh.device.GetHostAndPort()}) |
| 635 | host := strings.Split(dh.device.GetHostAndPort(), ":")[0] |
| 636 | genmac, err := generateMacFromHost(host) |
| 637 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 638 | return nil, NewErrAdapter("failed-to-generate-mac-host", log.Fields{"host": host}, err).Log() |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 639 | } |
| 640 | log.Debugw("using-host-for-mac-address", log.Fields{"host": host, "mac": genmac}) |
| 641 | dh.device.MacAddress = genmac |
| 642 | } else { |
| 643 | dh.device.MacAddress = deviceInfo.DeviceId |
| 644 | } |
| 645 | |
| 646 | // Synchronous call to update device - this method is run in its own go routine |
| 647 | if err := dh.coreProxy.DeviceUpdate(context.TODO(), dh.device); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 648 | return nil, NewErrAdapter("device-update-failed", log.Fields{"device-id": dh.device.Id}, err).Log() |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | return deviceInfo, nil |
| 652 | } |
| 653 | |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 654 | func startCollector(dh *DeviceHandler) { |
| 655 | // Initial delay for OLT initialization |
| 656 | time.Sleep(1 * time.Minute) |
| 657 | log.Debugf("Starting-Collector") |
| 658 | context := make(map[string]string) |
| 659 | for { |
| 660 | select { |
| 661 | case <-dh.stopCollector: |
| 662 | log.Debugw("Stopping-Collector-for-OLT", log.Fields{"deviceID:": dh.deviceID}) |
| 663 | return |
| 664 | default: |
| 665 | freq := dh.metrics.ToPmConfigs().DefaultFreq |
| 666 | time.Sleep(time.Duration(freq) * time.Second) |
| 667 | context["oltid"] = dh.deviceID |
| 668 | context["devicetype"] = dh.deviceType |
| 669 | // NNI Stats |
| 670 | cmnni := dh.portStats.collectNNIMetrics(uint32(0)) |
| 671 | log.Debugf("Collect-NNI-Metrics %v", cmnni) |
| 672 | go dh.portStats.publishMetrics("NNIStats", cmnni, uint32(0), context, dh.deviceID) |
| 673 | log.Debugf("Publish-NNI-Metrics") |
| 674 | // PON Stats |
| 675 | NumPonPORTS := dh.resourceMgr.DevInfo.GetPonPorts() |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 676 | for i := uint32(0); i < NumPonPORTS; i++ { |
| 677 | if val, ok := dh.activePorts.Load(i); ok && val == true { |
| 678 | cmpon := dh.portStats.collectPONMetrics(i) |
| 679 | log.Debugf("Collect-PON-Metrics %v", cmpon) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 680 | |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 681 | go dh.portStats.publishMetrics("PONStats", cmpon, i, context, dh.deviceID) |
| 682 | log.Debugf("Publish-PON-Metrics") |
| 683 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 689 | //AdoptDevice adopts the OLT device |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 690 | func (dh *DeviceHandler) AdoptDevice(ctx context.Context, device *voltha.Device) { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 691 | dh.transitionMap = NewTransitionMap(dh) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 692 | log.Infow("Adopt_device", log.Fields{"deviceID": device.Id, "Address": device.GetHostAndPort()}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 693 | dh.transitionMap.Handle(ctx, DeviceInit) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 694 | |
| 695 | // Now, set the initial PM configuration for that device |
| 696 | if err := dh.coreProxy.DevicePMConfigUpdate(nil, dh.metrics.ToPmConfigs()); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 697 | NewErrAdapter("error-updating-performance-metrics", log.Fields{"device-id": device.Id}, err).LogAt(log.ErrorLevel) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | go startCollector(dh) |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 701 | go startHeartbeatCheck(dh) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 702 | } |
| 703 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 704 | //GetOfpDeviceInfo Gets the Ofp information of the given device |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 705 | func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 706 | return &ic.SwitchCapability{ |
| 707 | Desc: &of.OfpDesc{ |
Devmalya Paul | 70dd497 | 2019-06-10 15:19:17 +0530 | [diff] [blame] | 708 | MfrDesc: "VOLTHA Project", |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 709 | HwDesc: "open_pon", |
| 710 | SwDesc: "open_pon", |
| 711 | SerialNum: dh.device.SerialNumber, |
| 712 | }, |
| 713 | SwitchFeatures: &of.OfpSwitchFeatures{ |
| 714 | NBuffers: 256, |
| 715 | NTables: 2, |
| 716 | Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS | |
| 717 | of.OfpCapabilities_OFPC_TABLE_STATS | |
| 718 | of.OfpCapabilities_OFPC_PORT_STATS | |
| 719 | of.OfpCapabilities_OFPC_GROUP_STATS), |
| 720 | }, |
| 721 | }, nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 722 | } |
| 723 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 724 | //GetOfpPortInfo Get Ofp port information |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 725 | func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 726 | capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 727 | return &ic.PortCapability{ |
| 728 | Port: &voltha.LogicalPort{ |
| 729 | OfpPort: &of.OfpPort{ |
| 730 | HwAddr: macAddressToUint32Array(dh.device.MacAddress), |
| 731 | Config: 0, |
| 732 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 733 | Curr: capacity, |
| 734 | Advertised: capacity, |
| 735 | Peer: capacity, |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 736 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 737 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 738 | }, |
| 739 | DeviceId: dh.device.Id, |
| 740 | DevicePortNo: uint32(portNo), |
| 741 | }, |
| 742 | }, nil |
| 743 | } |
| 744 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 745 | func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 746 | log.Debugw("omci indication", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 747 | var deviceType string |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 748 | var deviceID string |
| 749 | var proxyDeviceID string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 750 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 751 | onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId) |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 752 | |
| 753 | if onuInCache, ok := dh.onus.Load(onuKey); !ok { |
| 754 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 755 | log.Debugw("omci indication for a device not in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId}) |
| 756 | ponPort := IntfIDToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 757 | kwargs := make(map[string]interface{}) |
| 758 | kwargs["onu_id"] = omciInd.OnuId |
| 759 | kwargs["parent_port_no"] = ponPort |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 760 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 761 | onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
| 762 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 763 | return NewErrNotFound("onu", log.Fields{ |
| 764 | "interface-id": omciInd.IntfId, |
| 765 | "onu-id": omciInd.OnuId}, err).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 766 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 767 | deviceType = onuDevice.Type |
| 768 | deviceID = onuDevice.Id |
| 769 | proxyDeviceID = onuDevice.ProxyAddress.DeviceId |
| 770 | //if not exist in cache, then add to cache. |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 771 | dh.onus.Store(onuKey, NewOnuDevice(deviceID, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceID)) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 772 | } else { |
| 773 | //found in cache |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 774 | log.Debugw("omci indication for a device in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId}) |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 775 | deviceType = onuInCache.(*OnuDevice).deviceType |
| 776 | deviceID = onuInCache.(*OnuDevice).deviceID |
| 777 | proxyDeviceID = onuInCache.(*OnuDevice).proxyDeviceID |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 778 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 779 | |
| 780 | omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt} |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 781 | if err := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg, |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 782 | ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 783 | deviceID, proxyDeviceID, ""); err != nil { |
| 784 | return NewErrCommunication("omci-request", log.Fields{ |
| 785 | "source": dh.deviceType, |
| 786 | "destination": deviceType, |
| 787 | "onu-id": deviceID, |
| 788 | "proxy-device-id": proxyDeviceID}, err).Log() |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 789 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 790 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 791 | } |
| 792 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 793 | //ProcessInterAdapterMessage sends the proxied messages to the target device |
| 794 | // If the proxy address is not found in the unmarshalled message, it first fetches the onu device for which the message |
| 795 | // is meant, and then send the unmarshalled omci message to this onu |
| 796 | func (dh *DeviceHandler) ProcessInterAdapterMessage(msg *ic.InterAdapterMessage) error { |
| 797 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgID": msg.Header.Id}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 798 | if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 799 | msgID := msg.Header.Id |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 800 | fromTopic := msg.Header.FromTopic |
| 801 | toTopic := msg.Header.ToTopic |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 802 | toDeviceID := msg.Header.ToDeviceId |
| 803 | proxyDeviceID := msg.Header.ProxyDeviceId |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 804 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 805 | 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] | 806 | |
| 807 | msgBody := msg.GetBody() |
| 808 | |
| 809 | omciMsg := &ic.InterAdapterOmciMessage{} |
| 810 | if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil { |
| 811 | log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err}) |
| 812 | return err |
| 813 | } |
| 814 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 815 | if omciMsg.GetProxyAddress() == nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 816 | onuDevice, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, toDeviceID) |
| 817 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 818 | return NewErrNotFound("onu", log.Fields{ |
| 819 | "device-id": dh.device.Id, |
| 820 | "onu-device-id": toDeviceID}, err).Log() |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 821 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 822 | log.Debugw("device retrieved from core", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 823 | if err := dh.sendProxiedMessage(onuDevice, omciMsg); err != nil { |
| 824 | return NewErrCommunication("send-failed", log.Fields{ |
| 825 | "device-id": dh.device.Id, |
| 826 | "onu-device-id": toDeviceID}, err).Log() |
| 827 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 828 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 829 | log.Debugw("Proxy Address found in omci message", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 830 | if err := dh.sendProxiedMessage(nil, omciMsg); err != nil { |
| 831 | return NewErrCommunication("send-failed", log.Fields{ |
| 832 | "device-id": dh.device.Id, |
| 833 | "onu-device-id": toDeviceID}, err).Log() |
| 834 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | } else { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 838 | return NewErrInvalidValue(log.Fields{"inter-adapter-message-type": msg.Header.Type}, nil).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 839 | } |
| 840 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 841 | } |
| 842 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 843 | func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) error { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 844 | var intfID uint32 |
| 845 | var onuID uint32 |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 846 | var connectStatus common.ConnectStatus_Types |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 847 | if onuDevice != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 848 | intfID = onuDevice.ProxyAddress.GetChannelId() |
| 849 | onuID = onuDevice.ProxyAddress.GetOnuId() |
| 850 | connectStatus = onuDevice.ConnectStatus |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 851 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 852 | intfID = omciMsg.GetProxyAddress().GetChannelId() |
| 853 | onuID = omciMsg.GetProxyAddress().GetOnuId() |
| 854 | connectStatus = omciMsg.GetConnectStatus() |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 855 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 856 | if connectStatus != voltha.ConnectStatus_REACHABLE { |
| 857 | log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"intfID": intfID, "onuID": onuID}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 858 | |
| 859 | return NewErrCommunication("unreachable", log.Fields{ |
| 860 | "interface-id": intfID, |
| 861 | "onu-id": onuID}, nil).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 862 | } |
| 863 | |
lcui | e24ef18 | 2019-04-29 22:58:36 -0700 | [diff] [blame] | 864 | // TODO: Once we are sure openonu/openomci is sending only binary in omciMsg.Message, we can remove this check |
| 865 | isHexString := false |
| 866 | _, decodeerr := hex.DecodeString(string(omciMsg.Message)) |
| 867 | if decodeerr == nil { |
| 868 | isHexString = true |
| 869 | } |
| 870 | |
| 871 | // TODO: OpenOLT Agent expects a hex string for OMCI packets rather than binary. Fix this in the agent and then we can pass binary Pkt: omciMsg.Message. |
| 872 | var omciMessage *oop.OmciMsg |
| 873 | if isHexString { |
| 874 | omciMessage = &oop.OmciMsg{IntfId: intfID, OnuId: onuID, Pkt: omciMsg.Message} |
| 875 | } else { |
| 876 | hexPkt := make([]byte, hex.EncodedLen(len(omciMsg.Message))) |
| 877 | hex.Encode(hexPkt, omciMsg.Message) |
| 878 | omciMessage = &oop.OmciMsg{IntfId: intfID, OnuId: onuID, Pkt: hexPkt} |
| 879 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 880 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 881 | _, err := dh.Client.OmciMsgOut(context.Background(), omciMessage) |
| 882 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 883 | return NewErrCommunication("omci-send-failed", log.Fields{ |
| 884 | "interface-id": intfID, |
| 885 | "onu-id": onuID, |
| 886 | "message": omciMessage}, err).Log() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 887 | } |
lcui | e24ef18 | 2019-04-29 22:58:36 -0700 | [diff] [blame] | 888 | log.Debugw("Sent Omci message", log.Fields{"intfID": intfID, "onuID": onuID, "omciMsg": hex.EncodeToString(omciMsg.Message)}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 889 | return nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 890 | } |
| 891 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 892 | func (dh *DeviceHandler) activateONU(ctx context.Context, intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) error { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 893 | log.Debugw("activate-onu", log.Fields{"intfID": intfID, "onuID": onuID, "serialNum": serialNum, "serialNumber": serialNumber}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 894 | dh.flowMgr.UpdateOnuInfo(ctx, intfID, uint32(onuID), serialNumber) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 895 | // TODO: need resource manager |
| 896 | var pir uint32 = 1000000 |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 897 | Onu := oop.Onu{IntfId: intfID, OnuId: uint32(onuID), SerialNumber: serialNum, Pir: pir} |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 898 | if _, err := dh.Client.ActivateOnu(ctx, &Onu); err != nil { |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 899 | st, _ := status.FromError(err) |
| 900 | if st.Code() == codes.AlreadyExists { |
| 901 | log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber}) |
| 902 | } else { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 903 | return NewErrAdapter("onu-activate-failed", log.Fields{"onu": Onu}, err).Log() |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 904 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 905 | } else { |
| 906 | log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber}) |
| 907 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 908 | return nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 909 | } |
| 910 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 911 | func (dh *DeviceHandler) onuDiscIndication(ctx context.Context, onuDiscInd *oop.OnuDiscIndication, sn string) error { |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 912 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 913 | channelID := onuDiscInd.GetIntfId() |
| 914 | parentPortNo := IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 915 | |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 916 | log.Infow("new-discovery-indication", log.Fields{"sn": sn}) |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 917 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 918 | kwargs := make(map[string]interface{}) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 919 | if sn != "" { |
| 920 | kwargs["serial_number"] = sn |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 921 | } else { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 922 | return NewErrInvalidValue(log.Fields{"serial-number": sn}, nil).Log() |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 923 | } |
| 924 | |
Amit Ghosh | e5c6a85 | 2020-02-10 15:09:46 +0000 | [diff] [blame] | 925 | if _, loaded := dh.discOnus.LoadOrStore(sn, true); loaded { |
| 926 | log.Warnw("onu-sn-is-already-being-processed", log.Fields{"sn": sn}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 927 | return nil |
Amit Ghosh | e5c6a85 | 2020-02-10 15:09:46 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 930 | var onuID uint32 |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 931 | |
| 932 | // check the ONU is already know to the OLT |
| 933 | // NOTE the second time the ONU is discovered this should return a device |
| 934 | onuDevice, err := dh.coreProxy.GetChildDevice(ctx, dh.device.Id, kwargs) |
| 935 | |
| 936 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 937 | log.Warnw("core-proxy-get-child-device-failed", log.Fields{"parentDevice": dh.device.Id, "err": err, "sn": sn}) |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 938 | if e, ok := status.FromError(err); ok { |
| 939 | log.Warnw("core-proxy-get-child-device-failed-with-code", log.Fields{"errCode": e.Code(), "sn": sn}) |
| 940 | switch e.Code() { |
| 941 | case codes.Internal: |
| 942 | // this probably means NOT FOUND, so just create a new device |
| 943 | onuDevice = nil |
| 944 | case codes.DeadlineExceeded: |
| 945 | // if the call times out, cleanup and exit |
| 946 | dh.discOnus.Delete(sn) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 947 | return NewErrTimeout("get-child-device", log.Fields{"device-id": dh.device.Id}, err).Log() |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | if onuDevice == nil { |
| 953 | // NOTE this should happen a single time, and only if GetChildDevice returns NotFound |
| 954 | log.Infow("creating-new-onu", log.Fields{"sn": sn}) |
| 955 | // we need to create a new ChildDevice |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 956 | ponintfid := onuDiscInd.GetIntfId() |
| 957 | dh.lockDevice.Lock() |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 958 | onuID, err = dh.resourceMgr.GetONUID(ctx, ponintfid) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 959 | dh.lockDevice.Unlock() |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 960 | |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 961 | log.Infow("creating-new-onu-got-onu-id", log.Fields{"sn": sn, "onuId": onuID}) |
| 962 | |
| 963 | if err != nil { |
| 964 | // if we can't create an ID in resource manager, |
| 965 | // cleanup and exit |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 966 | dh.discOnus.Delete(sn) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 967 | return NewErrAdapter("resource-manage-get-onu-id-failed", log.Fields{ |
| 968 | "pon-interface-id": ponintfid, |
| 969 | "serial-number": sn}, err).Log() |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | if onuDevice, err = dh.coreProxy.ChildDeviceDetected(context.TODO(), dh.device.Id, int(parentPortNo), |
| 973 | "", int(channelID), string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuID)); err != nil { |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 974 | dh.discOnus.Delete(sn) |
| 975 | dh.resourceMgr.FreeonuID(ctx, ponintfid, []uint32{onuID}) // NOTE I'm not sure this method is actually cleaning up the right thing |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 976 | return NewErrAdapter("core-proxy-child-device-detected-failed", log.Fields{ |
| 977 | "pon-interface-id": ponintfid, |
| 978 | "serial-number": sn}, err).Log() |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | log.Infow("onu-child-device-added", log.Fields{"onuDevice": onuDevice, "sn": sn}) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 982 | } |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 983 | |
| 984 | // we can now use the existing ONU Id |
| 985 | onuID = onuDevice.ProxyAddress.OnuId |
| 986 | |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 987 | //Insert the ONU into cache to use in OnuIndication. |
| 988 | //TODO: Do we need to remove this from the cache on ONU change, or wait for overwritten on next discovery. |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 989 | log.Debugw("onu-discovery-indication-key-create", log.Fields{"onuID": onuID, |
| 990 | "intfId": onuDiscInd.GetIntfId(), "sn": sn}) |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 991 | onuKey := dh.formOnuKey(onuDiscInd.GetIntfId(), onuID) |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 992 | |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 993 | onuDev := NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuID, onuDiscInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId) |
| 994 | dh.onus.Store(onuKey, onuDev) |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 995 | log.Debugw("new-onu-device-discovered", log.Fields{"onu": onuDev, "sn": sn}) |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 996 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 997 | if err = dh.coreProxy.DeviceStateUpdate(ctx, onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED); err != nil { |
| 998 | return NewErrAdapter("failed-to-update-device-state", log.Fields{ |
| 999 | "device-id": onuDevice.Id, |
| 1000 | "serial-number": sn}, err).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1001 | } |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 1002 | log.Infow("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id, "sn": sn}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1003 | if err = dh.activateONU(ctx, onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn); err != nil { |
| 1004 | return NewErrAdapter("onu-activation-failed", log.Fields{ |
| 1005 | "device-id": onuDevice.Id, |
| 1006 | "serial-number": sn}, err).Log() |
| 1007 | } |
| 1008 | return nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1011 | func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) error { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1012 | serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber) |
| 1013 | |
| 1014 | kwargs := make(map[string]interface{}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1015 | ponPort := IntfIDToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT) |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 1016 | var onuDevice *voltha.Device |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1017 | var err error |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 1018 | foundInCache := false |
Scott Baker | 7eb0a93 | 2019-07-26 10:33:22 -0700 | [diff] [blame] | 1019 | log.Debugw("ONU indication key create", log.Fields{"onuId": onuInd.OnuId, |
| 1020 | "intfId": onuInd.GetIntfId()}) |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 1021 | onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.OnuId) |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 1022 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1023 | errFields := log.Fields{"device-id": dh.device.Id} |
| 1024 | |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 1025 | if onuInCache, ok := dh.onus.Load(onuKey); ok { |
| 1026 | |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 1027 | //If ONU id is discovered before then use GetDevice to get onuDevice because it is cheaper. |
| 1028 | foundInCache = true |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1029 | errFields["onu-id"] = onuInCache.(*OnuDevice).deviceID |
| 1030 | onuDevice, err = dh.coreProxy.GetDevice(nil, dh.device.Id, onuInCache.(*OnuDevice).deviceID) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1031 | } else { |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 1032 | //If ONU not found in adapter cache then we have to use GetChildDevice to get onuDevice |
| 1033 | if serialNumber != "" { |
| 1034 | kwargs["serial_number"] = serialNumber |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1035 | errFields["serial-number"] = serialNumber |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 1036 | } else { |
| 1037 | kwargs["onu_id"] = onuInd.OnuId |
| 1038 | kwargs["parent_port_no"] = ponPort |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1039 | errFields["onu-id"] = onuInd.OnuId |
| 1040 | errFields["parent-port-no"] = ponPort |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 1041 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1042 | onuDevice, err = dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1043 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 1044 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1045 | if err != nil || onuDevice == nil { |
| 1046 | return NewErrNotFound("onu-device", errFields, err).Log() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1049 | if onuDevice.ParentPortNo != ponPort { |
| 1050 | log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{ |
| 1051 | "previousIntfId": onuDevice.ParentPortNo, |
| 1052 | "currentIntfId": ponPort}) |
| 1053 | } |
| 1054 | |
| 1055 | if onuDevice.ProxyAddress.OnuId != onuInd.OnuId { |
| 1056 | log.Warnw("ONU-id-mismatch, can happen if both voltha and the olt rebooted", log.Fields{ |
| 1057 | "expected_onu_id": onuDevice.ProxyAddress.OnuId, |
| 1058 | "received_onu_id": onuInd.OnuId}) |
| 1059 | } |
| 1060 | if !foundInCache { |
| 1061 | onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId()) |
| 1062 | |
| 1063 | dh.onus.Store(onuKey, NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId)) |
| 1064 | |
| 1065 | } |
Amit Ghosh | 9bbc565 | 2020-02-17 13:37:32 +0000 | [diff] [blame] | 1066 | if err := dh.updateOnuStates(onuDevice, onuInd); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1067 | return NewErrCommunication("state-update-failed", errFields, err).Log() |
| 1068 | } |
| 1069 | return nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Amit Ghosh | 9bbc565 | 2020-02-17 13:37:32 +0000 | [diff] [blame] | 1072 | func (dh *DeviceHandler) updateOnuStates(onuDevice *voltha.Device, onuInd *oop.OnuIndication) error { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1073 | ctx := context.TODO() |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 1074 | log.Debugw("onu-indication-for-state", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState}) |
Amit Ghosh | 9bbc565 | 2020-02-17 13:37:32 +0000 | [diff] [blame] | 1075 | if onuInd.AdminState == "down" { |
| 1076 | // Tests have shown that we sometimes get OperState as NOT down even if AdminState is down, forcing it |
| 1077 | if onuInd.OperState != "down" { |
| 1078 | log.Warnw("ONU-admin-state-down", log.Fields{"operState": onuInd.OperState}) |
| 1079 | onuInd.OperState = "down" |
| 1080 | } |
| 1081 | } |
| 1082 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1083 | switch onuInd.OperState { |
| 1084 | case "down": |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 1085 | 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] | 1086 | // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1087 | err := dh.AdapterProxy.SendInterAdapterMessage(ctx, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1088 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 1089 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1090 | return NewErrCommunication("inter-adapter-send-failed", log.Fields{ |
| 1091 | "onu-indicator": onuInd, |
| 1092 | "source": "openolt", |
| 1093 | "device-type": onuDevice.Type, |
| 1094 | "device-id": onuDevice.Id}, err).Log() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1095 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1096 | case "up": |
Matt Jeanneret | 5353951 | 2019-07-20 14:47:02 -0400 | [diff] [blame] | 1097 | log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState}) |
| 1098 | // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1099 | err := dh.AdapterProxy.SendInterAdapterMessage(ctx, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1100 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 1101 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1102 | return NewErrCommunication("inter-adapter-send-failed", log.Fields{ |
| 1103 | "onu-indicator": onuInd, |
| 1104 | "source": "openolt", |
| 1105 | "device-type": onuDevice.Type, |
| 1106 | "device-id": onuDevice.Id}, err).Log() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1107 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1108 | default: |
| 1109 | return NewErrInvalidValue(log.Fields{"oper-state": onuInd.OperState}, nil).Log() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1110 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1111 | return nil |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1114 | func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string { |
| 1115 | if serialNum != nil { |
| 1116 | return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1117 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1118 | return "" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1119 | } |
Chaitrashree G S | 1a55b88 | 2020-02-04 17:35:35 -0500 | [diff] [blame] | 1120 | func (dh *DeviceHandler) deStringifySerialNumber(serialNum string) (*oop.SerialNumber, error) { |
| 1121 | decodedStr, err := hex.DecodeString(serialNum[4:]) |
| 1122 | if err != nil { |
| 1123 | return nil, err |
| 1124 | } |
| 1125 | return &oop.SerialNumber{ |
| 1126 | VendorId: []byte(serialNum[:4]), |
| 1127 | VendorSpecific: []byte(decodedStr), |
| 1128 | }, nil |
| 1129 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1130 | |
| 1131 | func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string { |
| 1132 | tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) + |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1133 | fmt.Sprintf("%x", uint32(vendorSpecific[0]&0x0f)) + |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1134 | fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) + |
| 1135 | fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) + |
| 1136 | fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) + |
| 1137 | fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) + |
| 1138 | fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) + |
| 1139 | fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f) |
| 1140 | return tmp |
| 1141 | } |
| 1142 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1143 | //UpdateFlowsBulk upates the bulk flow |
| 1144 | func (dh *DeviceHandler) UpdateFlowsBulk() error { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1145 | return ErrNotImplemented |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 1146 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1147 | |
| 1148 | //GetChildDevice returns the child device for given parent port and onu id |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1149 | func (dh *DeviceHandler) GetChildDevice(parentPort, onuID uint32) (*voltha.Device, error) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1150 | log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuID": onuID}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1151 | kwargs := make(map[string]interface{}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1152 | kwargs["onu_id"] = onuID |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1153 | kwargs["parent_port_no"] = parentPort |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1154 | onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1155 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1156 | return nil, NewErrNotFound("onu", log.Fields{ |
| 1157 | "interface-id": parentPort, |
| 1158 | "onu-id": onuID}, err).Log() |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1159 | } |
| 1160 | log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1161 | return onuDevice, nil |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 1162 | } |
| 1163 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1164 | // SendPacketInToCore sends packet-in to core |
| 1165 | // For this, it calls SendPacketIn of the core-proxy which uses a device specific topic to send the request. |
| 1166 | // The adapter handling the device creates a device specific topic |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1167 | func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) error { |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1168 | log.Debugw("send-packet-in-to-core", log.Fields{ |
| 1169 | "port": logicalPort, |
| 1170 | "packet": hex.EncodeToString(packetPayload), |
| 1171 | }) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1172 | if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPort, packetPayload); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1173 | return NewErrCommunication("packet-send-failed", log.Fields{ |
| 1174 | "source": "adapter", |
| 1175 | "destination": "core", |
| 1176 | "device-id": dh.device.Id, |
| 1177 | "logical-port": logicalPort, |
| 1178 | "packet": hex.EncodeToString(packetPayload)}, err).Log() |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1179 | } |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1180 | log.Debugw("Sent packet-in to core successfully", log.Fields{ |
| 1181 | "packet": hex.EncodeToString(packetPayload), |
| 1182 | }) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1183 | return nil |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1184 | } |
| 1185 | |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1186 | // AddUniPortToOnu adds the uni port to the onu device |
| 1187 | func (dh *DeviceHandler) AddUniPortToOnu(intfID, onuID, uniPort uint32) { |
| 1188 | onuKey := dh.formOnuKey(intfID, onuID) |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 1189 | |
| 1190 | if onuDevice, ok := dh.onus.Load(onuKey); ok { |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1191 | // add it to the uniPort map for the onu device |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 1192 | if _, ok = onuDevice.(*OnuDevice).uniPorts[uniPort]; !ok { |
| 1193 | onuDevice.(*OnuDevice).uniPorts[uniPort] = struct{}{} |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1194 | log.Debugw("adding-uni-port", log.Fields{"port": uniPort, "intfID": intfID, "onuId": onuID}) |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1199 | //UpdateFlowsIncrementally updates the device flow |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1200 | func (dh *DeviceHandler) UpdateFlowsIncrementally(ctx context.Context, device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 1201 | 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] | 1202 | if flows != nil { |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 1203 | for _, flow := range flows.ToRemove.Items { |
| 1204 | log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1205 | dh.flowMgr.RemoveFlow(ctx, flow) |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 1206 | } |
Girish Gowdra | 3d63303 | 2019-12-10 16:37:05 +0530 | [diff] [blame] | 1207 | |
| 1208 | for _, flow := range flows.ToAdd.Items { |
| 1209 | log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1210 | dh.flowMgr.AddFlow(ctx, flow, flowMetadata) |
Girish Gowdra | 3d63303 | 2019-12-10 16:37:05 +0530 | [diff] [blame] | 1211 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1212 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1213 | if groups != nil && flows != nil { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1214 | for _, flow := range flows.ToRemove.Items { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1215 | log.Debug("Removing flow", log.Fields{"deviceID": device.Id, "flowToRemove": flow}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1216 | // dh.flowMgr.RemoveFlow(flow) |
| 1217 | } |
| 1218 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1219 | |
| 1220 | if groups != nil { |
| 1221 | for _, group := range groups.ToAdd.Items { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1222 | dh.flowMgr.AddGroup(ctx, group) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1223 | } |
| 1224 | for _, group := range groups.ToUpdate.Items { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1225 | dh.flowMgr.ModifyGroup(ctx, group) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1226 | } |
| 1227 | if len(groups.ToRemove.Items) != 0 { |
| 1228 | log.Debug("Group delete operation is not supported for now") |
| 1229 | } |
| 1230 | } |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 1231 | log.Debug("UpdateFlowsIncrementally done successfully") |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 1232 | return nil |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 1233 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1234 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1235 | //DisableDevice disables the given device |
| 1236 | //It marks the following for the given device: |
| 1237 | //Device-Handler Admin-State : down |
| 1238 | //Device Port-State: UNKNOWN |
| 1239 | //Device Oper-State: UNKNOWN |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1240 | func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error { |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 1241 | /* On device disable ,admin state update has to be done prior sending request to agent since |
| 1242 | the indication thread may processes invalid indications of ONU and OLT*/ |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1243 | dh.lockDevice.Lock() |
| 1244 | dh.adminState = "down" |
| 1245 | dh.lockDevice.Unlock() |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1246 | if dh.Client != nil { |
| 1247 | if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil { |
| 1248 | if e, ok := status.FromError(err); ok && e.Code() == codes.Internal { |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1249 | dh.lockDevice.Lock() |
| 1250 | dh.adminState = "up" |
| 1251 | dh.lockDevice.Unlock() |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1252 | return NewErrAdapter("olt-disable-failed", log.Fields{"device-id": device.Id}, err).Log() |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1253 | } |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1254 | } |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 1255 | } |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1256 | log.Debugw("olt-disabled", log.Fields{"deviceID": device.Id}) |
Chaitrashree G S | 4412419 | 2019-08-07 20:21:36 -0400 | [diff] [blame] | 1257 | /* Discovered ONUs entries need to be cleared , since on device disable the child devices goes to |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1258 | UNREACHABLE state which needs to be configured again*/ |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 1259 | |
| 1260 | dh.discOnus = sync.Map{} |
| 1261 | dh.onus = sync.Map{} |
| 1262 | |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 1263 | go dh.notifyChildDevices("unreachable") |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1264 | cloned := proto.Clone(device).(*voltha.Device) |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 1265 | // Update the all pon ports state on that device to disable and NNI remains active as NNI remains active in openolt agent. |
| 1266 | for _, port := range cloned.Ports { |
| 1267 | if port.GetType() == voltha.Port_PON_OLT { |
| 1268 | if err := dh.coreProxy.PortStateUpdate(context.TODO(), cloned.Id, |
| 1269 | voltha.Port_PON_OLT, port.GetPortNo(), voltha.OperStatus_UNKNOWN); err != nil { |
| 1270 | return err |
| 1271 | } |
| 1272 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1273 | } |
| 1274 | |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1275 | log.Debugw("disable-device-end", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1276 | return nil |
| 1277 | } |
| 1278 | |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 1279 | func (dh *DeviceHandler) notifyChildDevices(state string) { |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1280 | |
| 1281 | // Update onu state as unreachable in onu adapter |
| 1282 | onuInd := oop.OnuIndication{} |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 1283 | onuInd.OperState = state |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 1284 | //get the child device for the parent device |
| 1285 | onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id) |
| 1286 | if err != nil { |
| 1287 | log.Errorw("failed-to-get-child-devices-information", log.Fields{"deviceID": dh.device.Id, "error": err}) |
| 1288 | } |
| 1289 | if onuDevices != nil { |
| 1290 | for _, onuDevice := range onuDevices.Items { |
| 1291 | err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
| 1292 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 1293 | if err != nil { |
| 1294 | log.Errorw("failed-to-send-inter-adapter-message", log.Fields{"OnuInd": onuInd, |
| 1295 | "From Adapter": "openolt", "DeviceType": onuDevice.Type, "DeviceID": onuDevice.Id}) |
| 1296 | } |
| 1297 | |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | } |
| 1302 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1303 | //ReenableDevice re-enables the olt device after disable |
| 1304 | //It marks the following for the given device: |
| 1305 | //Device-Handler Admin-State : up |
| 1306 | //Device Port-State: ACTIVE |
| 1307 | //Device Oper-State: ACTIVE |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1308 | func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error { |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1309 | dh.lockDevice.Lock() |
| 1310 | dh.adminState = "up" |
| 1311 | dh.lockDevice.Unlock() |
Abhilash Laxmeshwar | 5b302e1 | 2020-01-09 15:15:14 +0530 | [diff] [blame] | 1312 | |
| 1313 | if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil { |
| 1314 | if e, ok := status.FromError(err); ok && e.Code() == codes.Internal { |
Abhilash Laxmeshwar | 5b302e1 | 2020-01-09 15:15:14 +0530 | [diff] [blame] | 1315 | dh.lockDevice.Lock() |
| 1316 | dh.adminState = "down" |
| 1317 | dh.lockDevice.Unlock() |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1318 | return NewErrAdapter("olt-reenable-failed", log.Fields{"device-id": dh.device.Id}, err).Log() |
Abhilash Laxmeshwar | 5b302e1 | 2020-01-09 15:15:14 +0530 | [diff] [blame] | 1319 | } |
| 1320 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1321 | log.Debug("olt-reenabled") |
| 1322 | |
| 1323 | cloned := proto.Clone(device).(*voltha.Device) |
| 1324 | // Update the all ports state on that device to enable |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1325 | |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 1326 | if err := dh.disableAdminDownPorts(device); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1327 | return NewErrAdapter("port-status-update-failed-after-olt-reenable", log.Fields{"device": device}, err).Log() |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1328 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1329 | //Update the device oper status as ACTIVE |
| 1330 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 1331 | dh.device = cloned |
| 1332 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1333 | if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1334 | return NewErrAdapter("state-update-failed", log.Fields{ |
| 1335 | "device-id": device.Id, |
| 1336 | "connect-status": cloned.ConnectStatus, |
| 1337 | "oper-status": cloned.OperStatus}, err).Log() |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1338 | } |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1339 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1340 | log.Debugw("ReEnableDevice-end", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1341 | |
| 1342 | return nil |
| 1343 | } |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1344 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1345 | func (dh *DeviceHandler) clearUNIData(ctx context.Context, onu *rsrcMgr.OnuGemInfo) error { |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1346 | var uniID uint32 |
| 1347 | var err error |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1348 | for _, port := range onu.UniPorts { |
| 1349 | uniID = UniIDFromPortNum(uint32(port)) |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1350 | log.Debugw("clearing-resource-data-for-uni-port", log.Fields{"port": port, "uniID": uniID}) |
| 1351 | /* Delete tech-profile instance from the KV store */ |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1352 | if err = dh.flowMgr.DeleteTechProfileInstances(ctx, onu.IntfID, onu.OnuID, uniID, onu.SerialNumber); err != nil { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1353 | 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] | 1354 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1355 | log.Debugw("Deleted-tech-profile-instance-for-onu", log.Fields{"onu-id": onu.OnuID}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1356 | flowIDs := dh.resourceMgr.GetCurrentFlowIDsForOnu(ctx, onu.IntfID, int32(onu.OnuID), int32(uniID)) |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1357 | for _, flowID := range flowIDs { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1358 | dh.resourceMgr.FreeFlowID(ctx, onu.IntfID, int32(onu.OnuID), int32(uniID), flowID) |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1359 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1360 | tpIDList := dh.resourceMgr.GetTechProfileIDForOnu(ctx, onu.IntfID, onu.OnuID, uniID) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 1361 | for _, tpID := range tpIDList { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1362 | if err = dh.resourceMgr.RemoveMeterIDForOnu(ctx, "upstream", onu.IntfID, onu.OnuID, uniID, tpID); err != nil { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1363 | log.Debugw("Failed-to-remove-meter-id-for-onu-upstream", log.Fields{"onu-id": onu.OnuID}) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 1364 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1365 | log.Debugw("Removed-meter-id-for-onu-upstream", log.Fields{"onu-id": onu.OnuID}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1366 | if err = dh.resourceMgr.RemoveMeterIDForOnu(ctx, "downstream", onu.IntfID, onu.OnuID, uniID, tpID); err != nil { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1367 | log.Debugw("Failed-to-remove-meter-id-for-onu-downstream", log.Fields{"onu-id": onu.OnuID}) |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 1368 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1369 | log.Debugw("Removed-meter-id-for-onu-downstream", log.Fields{"onu-id": onu.OnuID}) |
| 1370 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1371 | dh.resourceMgr.FreePONResourcesForONU(ctx, onu.IntfID, onu.OnuID, uniID) |
| 1372 | if err = dh.resourceMgr.RemoveTechProfileIDsForOnu(ctx, onu.IntfID, onu.OnuID, uniID); err != nil { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1373 | log.Debugw("Failed-to-remove-tech-profile-id-for-onu", log.Fields{"onu-id": onu.OnuID}) |
| 1374 | } |
| 1375 | log.Debugw("Removed-tech-profile-id-for-onu", log.Fields{"onu-id": onu.OnuID}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1376 | if err = dh.resourceMgr.DelGemPortPktIn(ctx, onu.IntfID, onu.OnuID, uint32(port)); err != nil { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1377 | log.Debugw("Failed-to-remove-gemport-pkt-in", log.Fields{"intfid": onu.IntfID, "onuid": onu.OnuID, "uniId": uniID}) |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1378 | } |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1379 | } |
| 1380 | return nil |
| 1381 | } |
| 1382 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1383 | func (dh *DeviceHandler) clearNNIData(ctx context.Context) error { |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1384 | nniUniID := -1 |
| 1385 | nniOnuID := -1 |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1386 | |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1387 | if dh.resourceMgr == nil { |
| 1388 | return fmt.Errorf("no resource manager for deviceID %s", dh.deviceID) |
| 1389 | } |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1390 | //Free the flow-ids for the NNI port |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1391 | nni, err := dh.resourceMgr.GetNNIFromKVStore(ctx) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1392 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1393 | return NewErrPersistence("get", "nni", 0, nil, err).Log() |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1394 | } |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1395 | log.Debugw("NNI are ", log.Fields{"nni": nni}) |
| 1396 | for _, nniIntfID := range nni { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1397 | flowIDs := dh.resourceMgr.GetCurrentFlowIDsForOnu(ctx, uint32(nniIntfID), int32(nniOnuID), int32(nniUniID)) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1398 | log.Debugw("Current flow ids for nni", log.Fields{"flow-ids": flowIDs}) |
| 1399 | for _, flowID := range flowIDs { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1400 | dh.resourceMgr.FreeFlowID(ctx, uint32(nniIntfID), -1, -1, uint32(flowID)) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1401 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1402 | dh.resourceMgr.RemoveResourceMap(ctx, nniIntfID, int32(nniOnuID), int32(nniUniID)) |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1403 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1404 | if err = dh.resourceMgr.DelNNiFromKVStore(ctx); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1405 | return NewErrPersistence("clear", "nni", 0, nil, err).Log() |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1406 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1407 | return nil |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | // DeleteDevice deletes the device instance from openolt handler array. Also clears allocated resource manager resources. Also reboots the OLT hardware! |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1411 | func (dh *DeviceHandler) DeleteDevice(ctx context.Context, device *voltha.Device) error { |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1412 | log.Debug("Function entry delete device") |
| 1413 | dh.lockDevice.Lock() |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1414 | if dh.adminState == "deleted" { |
| 1415 | dh.lockDevice.Unlock() |
| 1416 | return nil |
| 1417 | } |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1418 | dh.adminState = "deleted" |
| 1419 | dh.lockDevice.Unlock() |
| 1420 | /* Clear the KV store data associated with the all the UNI ports |
| 1421 | This clears up flow data and also resource map data for various |
| 1422 | other pon resources like alloc_id and gemport_id |
| 1423 | */ |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1424 | if dh.resourceMgr != nil { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1425 | noOfPonPorts := dh.resourceMgr.DevInfo.GetPonPorts() |
| 1426 | var ponPort uint32 |
| 1427 | for ponPort = 0; ponPort < noOfPonPorts; ponPort++ { |
| 1428 | var onuGemData []rsrcMgr.OnuGemInfo |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1429 | err := dh.resourceMgr.ResourceMgrs[ponPort].GetOnuGemInfo(ctx, ponPort, &onuGemData) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1430 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1431 | return NewErrNotFound("onu", log.Fields{ |
| 1432 | "device-id": dh.device.Id, |
| 1433 | "pon-port": ponPort}, err).Log() |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1434 | } |
| 1435 | for _, onu := range onuGemData { |
Abhilash Laxmeshwar | 6d1acb9 | 2020-01-17 15:43:03 +0530 | [diff] [blame] | 1436 | onuID := make([]uint32, 1) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1437 | log.Debugw("onu data ", log.Fields{"onu": onu}) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1438 | if err = dh.clearUNIData(ctx, &onu); err != nil { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1439 | log.Errorw("Failed to clear data for onu", log.Fields{"onu-device": onu}) |
| 1440 | } |
Abhilash Laxmeshwar | 6d1acb9 | 2020-01-17 15:43:03 +0530 | [diff] [blame] | 1441 | // Clear flowids for gem cache. |
| 1442 | for _, gem := range onu.GemPorts { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1443 | dh.resourceMgr.DeleteFlowIDsForGem(ctx, ponPort, gem) |
Abhilash Laxmeshwar | 6d1acb9 | 2020-01-17 15:43:03 +0530 | [diff] [blame] | 1444 | } |
| 1445 | onuID[0] = onu.OnuID |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1446 | dh.resourceMgr.FreeonuID(ctx, ponPort, onuID) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1447 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1448 | dh.resourceMgr.DeleteIntfIDGempMapPath(ctx, ponPort) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1449 | onuGemData = nil |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1450 | err = dh.resourceMgr.DelOnuGemInfoForIntf(ctx, ponPort) |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1451 | if err != nil { |
| 1452 | log.Errorw("Failed to update onugem info", log.Fields{"intfid": ponPort, "onugeminfo": onuGemData}) |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1453 | } |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1454 | } |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1455 | /* Clear the flows from KV store associated with NNI port. |
| 1456 | There are mostly trap rules from NNI port (like LLDP) |
| 1457 | */ |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1458 | if err := dh.clearNNIData(ctx); err != nil { |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1459 | log.Errorw("Failed to clear data for NNI port", log.Fields{"device-id": dh.deviceID}) |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1460 | } |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1461 | |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1462 | /* Clear the resource pool for each PON port in the background */ |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1463 | go dh.resourceMgr.Delete(ctx) |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1464 | } |
A R Karthick | 1f85b80 | 2019-10-11 05:06:05 +0000 | [diff] [blame] | 1465 | |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1466 | /*Delete ONU map for the device*/ |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 1467 | dh.onus.Range(func(key interface{}, value interface{}) bool { |
| 1468 | dh.onus.Delete(key) |
| 1469 | return true |
| 1470 | }) |
| 1471 | |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1472 | log.Debug("Removed-device-from-Resource-manager-KV-store") |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1473 | // Stop the Stats collector |
| 1474 | dh.stopCollector <- true |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 1475 | // stop the heartbeat check routine |
| 1476 | dh.stopHeartbeatCheck <- true |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1477 | //Reset the state |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1478 | if dh.Client != nil { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1479 | if _, err := dh.Client.Reboot(ctx, new(oop.Empty)); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1480 | return NewErrAdapter("olt-reboot-failed", log.Fields{"device-id": dh.deviceID}, err).Log() |
Serkant Uluderya | 89ff40c | 2019-10-17 16:02:25 -0700 | [diff] [blame] | 1481 | } |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1482 | } |
| 1483 | cloned := proto.Clone(device).(*voltha.Device) |
| 1484 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 1485 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1486 | if err := dh.coreProxy.DeviceStateUpdate(ctx, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1487 | return NewErrAdapter("device-state-update-failed", log.Fields{ |
| 1488 | "device-id": device.Id, |
| 1489 | "connect-status": cloned.ConnectStatus, |
| 1490 | "oper-status": cloned.OperStatus}, err).Log() |
Devmalya Paul | 495b94a | 2019-08-27 19:42:00 -0400 | [diff] [blame] | 1491 | } |
| 1492 | return nil |
| 1493 | } |
| 1494 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1495 | //RebootDevice reboots the given device |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 1496 | func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error { |
| 1497 | if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1498 | return NewErrAdapter("olt-reboot-failed", log.Fields{"device-id": dh.deviceID}, err).Log() |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 1499 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1500 | log.Debugw("rebooted-device-successfully", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 1501 | return nil |
| 1502 | } |
| 1503 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1504 | func (dh *DeviceHandler) handlePacketIndication(ctx context.Context, packetIn *oop.PacketIndication) error { |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1505 | log.Debugw("Received packet-in", log.Fields{ |
| 1506 | "packet-indication": *packetIn, |
| 1507 | "packet": hex.EncodeToString(packetIn.Pkt), |
| 1508 | }) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1509 | logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(ctx, packetIn) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1510 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1511 | return NewErrNotFound("logical-port", log.Fields{"packet": hex.EncodeToString(packetIn.Pkt)}, err).Log() |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1512 | } |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1513 | log.Debugw("sending packet-in to core", log.Fields{ |
| 1514 | "logicalPortNum": logicalPortNum, |
| 1515 | "packet": hex.EncodeToString(packetIn.Pkt), |
| 1516 | }) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1517 | if err := dh.coreProxy.SendPacketIn(context.TODO(), dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1518 | return NewErrCommunication("send-packet-in", log.Fields{ |
| 1519 | "destination": "core", |
| 1520 | "source": dh.deviceType, |
| 1521 | "packet": hex.EncodeToString(packetIn.Pkt)}, err).Log() |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1522 | } |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1523 | log.Debugw("Success sending packet-in to core!", log.Fields{ |
| 1524 | "packet": hex.EncodeToString(packetIn.Pkt), |
| 1525 | }) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1526 | return nil |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1527 | } |
| 1528 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1529 | // PacketOut sends packet-out from VOLTHA to OLT on the egress port provided |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1530 | func (dh *DeviceHandler) PacketOut(ctx context.Context, egressPortNo int, packet *of.OfpPacketOut) error { |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1531 | log.Debugw("incoming-packet-out", log.Fields{ |
| 1532 | "deviceID": dh.deviceID, |
| 1533 | "egress_port_no": egressPortNo, |
| 1534 | "pkt-length": len(packet.Data), |
| 1535 | "packet": hex.EncodeToString(packet.Data), |
| 1536 | }) |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1537 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1538 | egressPortType := IntfIDToPortTypeName(uint32(egressPortNo)) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1539 | if egressPortType == voltha.Port_ETHERNET_UNI { |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1540 | outerEthType := (uint16(packet.Data[12]) << 8) | uint16(packet.Data[13]) |
| 1541 | innerEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17]) |
Girish Gowdra | 6e1534a | 2019-11-15 19:24:04 +0530 | [diff] [blame] | 1542 | if outerEthType == 0x8942 || outerEthType == 0x88cc { |
| 1543 | // Do not packet-out lldp packets on uni port. |
| 1544 | // ONOS has no clue about uni/nni ports, it just packets out on all |
| 1545 | // available ports on the Logical Switch. It should not be interested |
| 1546 | // in the UNI links. |
| 1547 | log.Debug("dropping-lldp-packet-out-on-uni") |
| 1548 | return nil |
| 1549 | } |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1550 | if outerEthType == 0x88a8 || outerEthType == 0x8100 { |
| 1551 | if innerEthType == 0x8100 { |
| 1552 | // q-in-q 802.1ad or 802.1q double tagged packet. |
| 1553 | // slice out the outer tag. |
| 1554 | packet.Data = append(packet.Data[:12], packet.Data[16:]...) |
| 1555 | 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] | 1556 | } |
| 1557 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1558 | intfID := IntfIDFromUniPortNum(uint32(egressPortNo)) |
| 1559 | onuID := OnuIDFromPortNum(uint32(egressPortNo)) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 1560 | uniID := UniIDFromPortNum(uint32(egressPortNo)) |
| 1561 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1562 | gemPortID, err := dh.flowMgr.GetPacketOutGemPortID(ctx, intfID, onuID, uint32(egressPortNo)) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 1563 | if err != nil { |
| 1564 | // In this case the openolt agent will receive the gemPortID as 0. |
| 1565 | // The agent tries to retrieve the gemPortID in this case. |
| 1566 | // This may not always succeed at the agent and packetOut may fail. |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1567 | log.Errorw("failed-to-retrieve-gemport-id-for-packet-out", log.Fields{ |
| 1568 | "packet": hex.EncodeToString(packet.Data), |
| 1569 | }) |
Manikkaraj k | b1d5144 | 2019-07-23 10:41:02 -0400 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | 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] | 1573 | |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1574 | log.Debugw("sending-packet-to-onu", log.Fields{ |
| 1575 | "egress_port_no": egressPortNo, |
| 1576 | "IntfId": intfID, |
| 1577 | "onuID": onuID, |
| 1578 | "uniID": uniID, |
| 1579 | "gemPortID": gemPortID, |
| 1580 | "packet": hex.EncodeToString(packet.Data), |
| 1581 | }) |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1582 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1583 | if _, err := dh.Client.OnuPacketOut(ctx, &onuPkt); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1584 | return NewErrCommunication("packet-out-send", log.Fields{ |
| 1585 | "source": "adapter", |
| 1586 | "destination": "onu", |
| 1587 | "egress-port-number": egressPortNo, |
| 1588 | "interface-id": intfID, |
| 1589 | "oni-id": onuID, |
| 1590 | "uni-id": uniID, |
| 1591 | "gem-port-id": gemPortID, |
| 1592 | "packet": hex.EncodeToString(packet.Data)}, err).Log() |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1593 | } |
| 1594 | } else if egressPortType == voltha.Port_ETHERNET_NNI { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1595 | nniIntfID, err := IntfIDFromNniPortNum(uint32(egressPortNo)) |
| 1596 | if err != nil { |
| 1597 | return NewErrInvalidValue(log.Fields{"egress-nni-port": egressPortNo}, err).Log() |
| 1598 | } |
| 1599 | uplinkPkt := oop.UplinkPacket{IntfId: nniIntfID, Pkt: packet.Data} |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1600 | |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1601 | log.Debugw("sending-packet-to-nni", log.Fields{ |
| 1602 | "uplink_pkt": uplinkPkt, |
| 1603 | "packet": hex.EncodeToString(packet.Data), |
| 1604 | }) |
Matt Jeanneret | 1359c73 | 2019-08-01 21:40:02 -0400 | [diff] [blame] | 1605 | |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1606 | if _, err := dh.Client.UplinkPacketOut(ctx, &uplinkPkt); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1607 | return NewErrCommunication("packet-out-to-nni", log.Fields{"packet": hex.EncodeToString(packet.Data)}, err).Log() |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1608 | } |
| 1609 | } else { |
Matteo Scandolo | 6056e82 | 2019-11-13 14:05:29 -0800 | [diff] [blame] | 1610 | log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{ |
| 1611 | "egress_port_no": egressPortNo, |
| 1612 | "egressPortType": egressPortType, |
| 1613 | "packet": hex.EncodeToString(packet.Data), |
| 1614 | }) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1615 | } |
| 1616 | return nil |
| 1617 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 1618 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1619 | func (dh *DeviceHandler) formOnuKey(intfID, onuID uint32) string { |
| 1620 | return "" + strconv.Itoa(int(intfID)) + "." + strconv.Itoa(int(onuID)) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 1621 | } |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 1622 | |
| 1623 | func startHeartbeatCheck(dh *DeviceHandler) { |
| 1624 | // start the heartbeat check towards the OLT. |
| 1625 | var timerCheck *time.Timer |
| 1626 | |
| 1627 | for { |
| 1628 | heartbeatTimer := time.NewTimer(dh.openOLT.HeartbeatCheckInterval) |
| 1629 | select { |
| 1630 | case <-heartbeatTimer.C: |
| 1631 | ctx, cancel := context.WithTimeout(context.Background(), dh.openOLT.GrpcTimeoutInterval) |
| 1632 | if heartBeat, err := dh.Client.HeartbeatCheck(ctx, new(oop.Empty)); err != nil { |
| 1633 | log.Error("Hearbeat failed") |
| 1634 | if timerCheck == nil { |
| 1635 | // start a after func, when expired will update the state to the core |
| 1636 | timerCheck = time.AfterFunc(dh.openOLT.HeartbeatFailReportInterval, dh.updateStateUnreachable) |
| 1637 | } |
| 1638 | } else { |
| 1639 | if timerCheck != nil { |
| 1640 | if timerCheck.Stop() { |
| 1641 | log.Debug("We got hearbeat within the timeout") |
| 1642 | } else { |
| 1643 | |
| 1644 | log.Debug("We got hearbeat after the timeout expired, changing the states") |
| 1645 | go dh.notifyChildDevices("up") |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1646 | if err := dh.coreProxy.DeviceStateUpdate(ctx, dh.device.Id, voltha.ConnectStatus_REACHABLE, |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 1647 | voltha.OperStatus_ACTIVE); err != nil { |
| 1648 | log.Errorw("Failed to update device state", log.Fields{"deviceID": dh.device.Id, "error": err}) |
| 1649 | } |
| 1650 | } |
| 1651 | timerCheck = nil |
| 1652 | } |
| 1653 | log.Debugw("Hearbeat", log.Fields{"signature": heartBeat}) |
| 1654 | } |
| 1655 | cancel() |
| 1656 | case <-dh.stopHeartbeatCheck: |
| 1657 | log.Debug("Stopping heart beat check") |
| 1658 | return |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | func (dh *DeviceHandler) updateStateUnreachable() { |
| 1664 | |
| 1665 | go dh.notifyChildDevices("unreachable") |
| 1666 | if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), dh.device.Id, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1667 | NewErrAdapter("device-state-update-failed", log.Fields{"device-id": dh.device.Id}, err).LogAt(log.ErrorLevel) |
Abhilash Laxmeshwar | f9942e9 | 2020-01-07 15:32:44 +0530 | [diff] [blame] | 1668 | } |
| 1669 | } |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1670 | |
| 1671 | // EnablePort to enable Pon interface |
| 1672 | func (dh *DeviceHandler) EnablePort(port *voltha.Port) error { |
| 1673 | log.Debugw("enable-port", log.Fields{"Device": dh.device, "port": port}) |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 1674 | return dh.modifyPhyPort(port, true) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | // DisablePort to disable pon interface |
| 1678 | func (dh *DeviceHandler) DisablePort(port *voltha.Port) error { |
| 1679 | log.Debugw("disable-port", log.Fields{"Device": dh.device, "port": port}) |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 1680 | return dh.modifyPhyPort(port, false) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1681 | } |
| 1682 | |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 1683 | //modifyPhyPort is common function to enable and disable the port. parm :enablePort, true to enablePort and false to disablePort. |
| 1684 | func (dh *DeviceHandler) modifyPhyPort(port *voltha.Port, enablePort bool) error { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1685 | ctx := context.Background() |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 1686 | log.Infow("modifyPhyPort", log.Fields{"port": port, "Enable": enablePort}) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1687 | if port.GetType() == voltha.Port_ETHERNET_NNI { |
| 1688 | // Bug is opened for VOL-2505 to support NNI disable feature. |
| 1689 | log.Infow("voltha-supports-single-nni-hence-disable-of-nni-not-allowed", |
| 1690 | log.Fields{"Device": dh.device, "port": port}) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1691 | return NewErrAdapter("illegal-port-request", log.Fields{ |
| 1692 | "port-type": port.GetType, |
| 1693 | "enable-state": enablePort}, nil).Log() |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1694 | } |
| 1695 | // fetch interfaceid from PortNo |
| 1696 | ponID := PortNoToIntfID(port.GetPortNo(), voltha.Port_PON_OLT) |
| 1697 | ponIntf := &oop.Interface{IntfId: ponID} |
| 1698 | var operStatus voltha.OperStatus_Types |
| 1699 | if enablePort { |
| 1700 | operStatus = voltha.OperStatus_ACTIVE |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1701 | out, err := dh.Client.EnablePonIf(ctx, ponIntf) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1702 | |
| 1703 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1704 | return NewErrAdapter("pon-port-enable-failed", log.Fields{ |
| 1705 | "device-id": dh.device.Id, |
| 1706 | "port": port}, err).Log() |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1707 | } |
| 1708 | // updating interface local cache for collecting stats |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 1709 | dh.activePorts.Store(ponID, true) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1710 | log.Infow("enabled-pon-port", log.Fields{"out": out, "DeviceID": dh.device, "Port": port}) |
| 1711 | } else { |
| 1712 | operStatus = voltha.OperStatus_UNKNOWN |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1713 | out, err := dh.Client.DisablePonIf(ctx, ponIntf) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1714 | if err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1715 | return NewErrAdapter("pon-port-disable-failed", log.Fields{ |
| 1716 | "device-id": dh.device.Id, |
| 1717 | "port": port}, err).Log() |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1718 | } |
| 1719 | // updating interface local cache for collecting stats |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 1720 | dh.activePorts.Store(ponID, false) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1721 | log.Infow("disabled-pon-port", log.Fields{"out": out, "DeviceID": dh.device, "Port": port}) |
| 1722 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1723 | if err := dh.coreProxy.PortStateUpdate(ctx, dh.deviceID, voltha.Port_PON_OLT, port.PortNo, operStatus); err != nil { |
| 1724 | return NewErrAdapter("port-state-update-failed", log.Fields{ |
| 1725 | "device-id": dh.deviceID, |
| 1726 | "port": port.PortNo}, err).Log() |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1727 | } |
| 1728 | return nil |
| 1729 | } |
| 1730 | |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 1731 | //disableAdminDownPorts disables the ports, if the corresponding port Adminstate is disabled on reboot and Renable device. |
| 1732 | func (dh *DeviceHandler) disableAdminDownPorts(device *voltha.Device) error { |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1733 | cloned := proto.Clone(device).(*voltha.Device) |
| 1734 | // Disable the port and update the oper_port_status to core |
| 1735 | // if the Admin state of the port is disabled on reboot and re-enable device. |
| 1736 | for _, port := range cloned.Ports { |
| 1737 | if port.AdminState == common.AdminState_DISABLED { |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 1738 | if err := dh.DisablePort(port); err != nil { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 1739 | return NewErrAdapter("port-disable-failed", log.Fields{ |
| 1740 | "device-id": dh.deviceID, |
| 1741 | "port": port}, err).Log() |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | return nil |
| 1746 | } |
| 1747 | |
| 1748 | //populateActivePorts to populate activePorts map |
| 1749 | func (dh *DeviceHandler) populateActivePorts(device *voltha.Device) { |
| 1750 | log.Info("populateActiveports", log.Fields{"Device": device}) |
| 1751 | for _, port := range device.Ports { |
| 1752 | if port.Type == voltha.Port_ETHERNET_NNI { |
| 1753 | if port.OperStatus == voltha.OperStatus_ACTIVE { |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 1754 | dh.activePorts.Store(PortNoToIntfID(port.PortNo, voltha.Port_ETHERNET_NNI), true) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1755 | } else { |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 1756 | dh.activePorts.Store(PortNoToIntfID(port.PortNo, voltha.Port_ETHERNET_NNI), false) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | if port.Type == voltha.Port_PON_OLT { |
| 1760 | if port.OperStatus == voltha.OperStatus_ACTIVE { |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 1761 | dh.activePorts.Store(PortNoToIntfID(port.PortNo, voltha.Port_PON_OLT), true) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1762 | } else { |
Chaitrashree G S | ef08811 | 2020-02-03 21:39:27 -0500 | [diff] [blame] | 1763 | dh.activePorts.Store(PortNoToIntfID(port.PortNo, voltha.Port_PON_OLT), false) |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | } |
Chaitrashree G S | 1a55b88 | 2020-02-04 17:35:35 -0500 | [diff] [blame] | 1768 | |
| 1769 | // ChildDeviceLost deletes ONU and clears pon resources related to it. |
| 1770 | func (dh *DeviceHandler) ChildDeviceLost(ctx context.Context, pPortNo uint32, onuID uint32) error { |
| 1771 | log.Debugw("child-device-lost", log.Fields{"pdeviceID": dh.device.Id}) |
| 1772 | IntfID := PortNoToIntfID(pPortNo, voltha.Port_PON_OLT) |
| 1773 | onuKey := dh.formOnuKey(IntfID, onuID) |
| 1774 | onuDevice, ok := dh.onus.Load(onuKey) |
| 1775 | if !ok { |
| 1776 | return NewErrAdapter("failed-to-load-onu-details", |
| 1777 | log.Fields{ |
| 1778 | "device-id": dh.deviceID, |
| 1779 | "onu-id": onuID, |
| 1780 | "interface-id": IntfID}, nil).Log() |
| 1781 | } |
| 1782 | var sn *oop.SerialNumber |
| 1783 | var err error |
| 1784 | if sn, err = dh.deStringifySerialNumber(onuDevice.(*OnuDevice).serialNumber); err != nil { |
| 1785 | return NewErrAdapter("failed-to-destringify-serial-number", |
| 1786 | log.Fields{ |
| 1787 | "devicer-id": dh.deviceID, |
| 1788 | "serial-number": onuDevice.(*OnuDevice).serialNumber}, err).Log() |
| 1789 | } |
| 1790 | onu := &oop.Onu{IntfId: IntfID, OnuId: onuID, SerialNumber: sn} |
| 1791 | if _, err := dh.Client.DeleteOnu(context.Background(), onu); err != nil { |
| 1792 | return NewErrAdapter("failed-to-delete-onu", log.Fields{ |
| 1793 | "device-id": dh.deviceID, |
| 1794 | "onu-id": onuID}, err).Log() |
| 1795 | } |
| 1796 | //clear PON resources associated with ONU |
| 1797 | var onuGemData []rsrcMgr.OnuGemInfo |
| 1798 | if err := dh.resourceMgr.ResourceMgrs[IntfID].GetOnuGemInfo(ctx, IntfID, &onuGemData); err != nil { |
| 1799 | log.Errorw("Failed-to-get-onu-info-for-pon-port ", log.Fields{ |
| 1800 | "device-id": dh.deviceID, |
| 1801 | "interface-id": IntfID, |
| 1802 | "error": err}) |
| 1803 | } |
| 1804 | |
| 1805 | for i, onu := range onuGemData { |
| 1806 | if onu.OnuID == onuID && onu.SerialNumber == onuDevice.(*OnuDevice).serialNumber { |
| 1807 | log.Debugw("onu-data ", log.Fields{"onu": onu}) |
| 1808 | if err := dh.clearUNIData(ctx, &onu); err != nil { |
| 1809 | log.Errorw("Failed-to-clear-uni-data-for-onu", log.Fields{ |
| 1810 | "device-id": dh.deviceID, |
| 1811 | "onu-device": onu, |
| 1812 | "error": err}) |
| 1813 | } |
| 1814 | // Clear flowids for gem cache. |
| 1815 | for _, gem := range onu.GemPorts { |
| 1816 | dh.resourceMgr.DeleteFlowIDsForGem(ctx, IntfID, gem) |
| 1817 | } |
| 1818 | onuGemData = append(onuGemData[:i], onuGemData[i+1:]...) |
| 1819 | dh.resourceMgr.UpdateOnuGemInfo(ctx, IntfID, onuGemData) |
| 1820 | |
| 1821 | dh.resourceMgr.FreeonuID(ctx, IntfID, []uint32{onu.OnuID}) |
| 1822 | break |
| 1823 | } |
| 1824 | } |
| 1825 | dh.onus.Delete(onuKey) |
| 1826 | dh.discOnus.Delete(onuDevice.(*OnuDevice).serialNumber) |
| 1827 | return nil |
| 1828 | } |