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