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" |
| 22 | "errors" |
| 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 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 33 | "github.com/gogo/protobuf/proto" |
| 34 | "github.com/golang/protobuf/ptypes" |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 35 | "github.com/mdlayher/ethernet" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 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 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 275 | // When OLT is admin down, allow only NNI operation status change indications. |
| 276 | if dh.adminState == "down" { |
| 277 | _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd) |
| 278 | if isIntfOperInd { |
| 279 | intfOperInd := indication.GetIntfOperInd() |
| 280 | if intfOperInd.GetType() == "nni" { |
| 281 | log.Infow("olt is admin down, allow nni ind", log.Fields{}) |
| 282 | } |
| 283 | } else { |
| 284 | log.Infow("olt is admin down, ignore indication", log.Fields{}) |
| 285 | continue |
| 286 | } |
| 287 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 288 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 289 | dh.handleIndication(indication) |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 290 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | func (dh *DeviceHandler) handleOltIndication(oltIndication *oop.OltIndication) { |
| 295 | if oltIndication.OperState == "up" { |
| 296 | dh.transitionMap.Handle(DeviceUpInd) |
| 297 | } else if oltIndication.OperState == "down" { |
| 298 | dh.transitionMap.Handle(DeviceDownInd) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func (dh *DeviceHandler) handleIndication(indication *oop.Indication) { |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 303 | raisedTs := time.Now().UnixNano() |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 304 | switch indication.Data.(type) { |
| 305 | case *oop.Indication_OltInd: |
| 306 | dh.handleOltIndication(indication.GetOltInd()) |
| 307 | case *oop.Indication_IntfInd: |
| 308 | intfInd := indication.GetIntfInd() |
| 309 | go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState()) |
| 310 | log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd}) |
| 311 | case *oop.Indication_IntfOperInd: |
| 312 | intfOperInd := indication.GetIntfOperInd() |
| 313 | if intfOperInd.GetType() == "nni" { |
| 314 | go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState()) |
| 315 | } else if intfOperInd.GetType() == "pon" { |
| 316 | // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down |
| 317 | // Handle pon port update |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 318 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 319 | log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd}) |
| 320 | case *oop.Indication_OnuDiscInd: |
| 321 | onuDiscInd := indication.GetOnuDiscInd() |
| 322 | log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 323 | sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber) |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 324 | dh.onuDiscIndication(onuDiscInd, sn) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 325 | case *oop.Indication_OnuInd: |
| 326 | onuInd := indication.GetOnuInd() |
| 327 | log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd}) |
| 328 | go dh.onuIndication(onuInd) |
| 329 | case *oop.Indication_OmciInd: |
| 330 | omciInd := indication.GetOmciInd() |
| 331 | log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd}) |
| 332 | if err := dh.omciIndication(omciInd); err != nil { |
| 333 | log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd}) |
| 334 | } |
| 335 | case *oop.Indication_PktInd: |
| 336 | pktInd := indication.GetPktInd() |
| 337 | log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd}) |
| 338 | go dh.handlePacketIndication(pktInd) |
| 339 | case *oop.Indication_PortStats: |
| 340 | portStats := indication.GetPortStats() |
| 341 | log.Infow("Received port stats indication", log.Fields{"PortStats": portStats}) |
| 342 | case *oop.Indication_FlowStats: |
| 343 | flowStats := indication.GetFlowStats() |
| 344 | log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats}) |
| 345 | case *oop.Indication_AlarmInd: |
| 346 | alarmInd := indication.GetAlarmInd() |
| 347 | log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd}) |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 348 | dh.eventMgr.ProcessEvents(alarmInd, dh.deviceID, raisedTs) |
| 349 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 350 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // doStateUp handle the olt up indication and update to voltha core |
| 354 | func (dh *DeviceHandler) doStateUp() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 355 | // 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] | 356 | 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] | 357 | voltha.OperStatus_ACTIVE); err != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 358 | 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] | 359 | return err |
| 360 | } |
| 361 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // doStateDown handle the olt down indication |
| 365 | func (dh *DeviceHandler) doStateDown() error { |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 366 | log.Debug("do-state-down-start") |
| 367 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 368 | 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] | 369 | if err != nil || device == nil { |
| 370 | /*TODO: needs to handle error scenarios */ |
| 371 | log.Errorw("Failed to fetch device device", log.Fields{"err": err}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 372 | return errors.New("failed to fetch device device") |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | cloned := proto.Clone(device).(*voltha.Device) |
| 376 | // Update the all ports state on that device to disable |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 377 | if er := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); er != nil { |
| 378 | log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": er}) |
| 379 | return er |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | //Update the device oper state and connection status |
| 383 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 384 | cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE |
| 385 | dh.device = cloned |
| 386 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 387 | if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil { |
| 388 | log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": er}) |
| 389 | return er |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 390 | } |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 391 | |
| 392 | //get the child device for the parent device |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 393 | onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 394 | if err != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 395 | 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] | 396 | return err |
| 397 | } |
| 398 | for _, onuDevice := range onuDevices.Items { |
| 399 | |
| 400 | // Update onu state as down in onu adapter |
| 401 | onuInd := oop.OnuIndication{} |
| 402 | onuInd.OperState = "down" |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 403 | er := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
| 404 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 405 | if er != nil { |
| 406 | log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd, |
| 407 | "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id}) |
| 408 | return er |
| 409 | } |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 410 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 411 | log.Debugw("do-state-down-end", log.Fields{"deviceID": device.Id}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 412 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // doStateInit dial the grpc before going to init state |
| 416 | func (dh *DeviceHandler) doStateInit() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 417 | var err error |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 418 | dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock()) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 419 | if err != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 420 | 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] | 421 | return err |
| 422 | } |
| 423 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // postInit create olt client instance to invoke RPC on the olt device |
| 427 | func (dh *DeviceHandler) postInit() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 428 | dh.Client = oop.NewOpenoltClient(dh.clientCon) |
| 429 | dh.transitionMap.Handle(GrpcConnected) |
| 430 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // doStateConnected get the device info and update to voltha core |
| 434 | func (dh *DeviceHandler) doStateConnected() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 435 | log.Debug("OLT device has been connected") |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 436 | |
| 437 | // Case where OLT is disabled and then rebooted. |
| 438 | if dh.adminState == "down" { |
| 439 | log.Debugln("do-state-connected--device-admin-state-down") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 440 | 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] | 441 | if err != nil || device == nil { |
| 442 | /*TODO: needs to handle error scenarios */ |
| 443 | log.Errorw("Failed to fetch device device", log.Fields{"err": err}) |
| 444 | } |
| 445 | |
| 446 | cloned := proto.Clone(device).(*voltha.Device) |
| 447 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 448 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 449 | dh.device = cloned |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 450 | if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil { |
| 451 | 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] | 452 | } |
| 453 | |
| 454 | // Since the device was disabled before the OLT was rebooted, enfore the OLT to be Disabled after re-connection. |
| 455 | _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty)) |
| 456 | if err != nil { |
| 457 | log.Errorw("Failed to disable olt ", log.Fields{"err": err}) |
| 458 | } |
| 459 | |
| 460 | // Start reading indications |
| 461 | go dh.readIndications() |
| 462 | return nil |
| 463 | } |
| 464 | |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 465 | deviceInfo, err := dh.populateDeviceInfo() |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 466 | if err != nil { |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 467 | log.Errorw("Unable to populate Device Info", log.Fields{"err": err}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 468 | return err |
| 469 | } |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 470 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 471 | 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] | 472 | if err != nil || device == nil { |
| 473 | /*TODO: needs to handle error scenarios */ |
| 474 | log.Errorw("Failed to fetch device device", log.Fields{"err": err}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 475 | return err |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 476 | } |
| 477 | cloned := proto.Clone(device).(*voltha.Device) |
| 478 | // Update the all ports (if available) on that device to ACTIVE. |
| 479 | // 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] | 480 | if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 481 | log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err}) |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 482 | return err |
| 483 | } |
| 484 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 485 | KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort) |
| 486 | // Instantiate resource manager |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 487 | 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] | 488 | log.Error("Error while instantiating resource manager") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 489 | return errors.New("instantiating resource manager failed") |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 490 | } |
| 491 | // Instantiate flow manager |
| 492 | if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil { |
| 493 | log.Error("Error while instantiating flow manager") |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 494 | return errors.New("instantiating flow manager failed") |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 495 | } |
| 496 | /* TODO: Instantiate Alarm , stats , BW managers */ |
Devmalya Paul | fb990a5 | 2019-07-09 10:01:49 -0400 | [diff] [blame] | 497 | /* Instantiating Event Manager to handle Alarms and KPIs */ |
| 498 | dh.eventMgr = NewEventMgr(dh.EventProxy) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 499 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 500 | // Start reading indications |
| 501 | go dh.readIndications() |
| 502 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 503 | } |
| 504 | |
Matt Jeanneret | f4fdcd7 | 2019-07-19 20:03:23 -0400 | [diff] [blame] | 505 | func (dh *DeviceHandler) populateDeviceInfo() (*oop.DeviceInfo, error) { |
| 506 | var err error |
| 507 | var deviceInfo *oop.DeviceInfo |
| 508 | |
| 509 | deviceInfo, err = dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty)) |
| 510 | |
| 511 | if err != nil { |
| 512 | log.Errorw("Failed to fetch device info", log.Fields{"err": err}) |
| 513 | return nil, err |
| 514 | } |
| 515 | if deviceInfo == nil { |
| 516 | log.Errorw("Device info is nil", log.Fields{}) |
| 517 | return nil, errors.New("failed to get device info from OLT") |
| 518 | } |
| 519 | |
| 520 | log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo}) |
| 521 | dh.device.Root = true |
| 522 | dh.device.Vendor = deviceInfo.Vendor |
| 523 | dh.device.Model = deviceInfo.Model |
| 524 | dh.device.SerialNumber = deviceInfo.DeviceSerialNumber |
| 525 | dh.device.HardwareVersion = deviceInfo.HardwareVersion |
| 526 | dh.device.FirmwareVersion = deviceInfo.FirmwareVersion |
| 527 | |
| 528 | if deviceInfo.DeviceId == "" { |
| 529 | log.Warnw("no-device-id-provided-using-host", log.Fields{"hostport": dh.device.GetHostAndPort()}) |
| 530 | host := strings.Split(dh.device.GetHostAndPort(), ":")[0] |
| 531 | genmac, err := generateMacFromHost(host) |
| 532 | if err != nil { |
| 533 | return nil, err |
| 534 | } |
| 535 | log.Debugw("using-host-for-mac-address", log.Fields{"host": host, "mac": genmac}) |
| 536 | dh.device.MacAddress = genmac |
| 537 | } else { |
| 538 | dh.device.MacAddress = deviceInfo.DeviceId |
| 539 | } |
| 540 | |
| 541 | // Synchronous call to update device - this method is run in its own go routine |
| 542 | if err := dh.coreProxy.DeviceUpdate(context.TODO(), dh.device); err != nil { |
| 543 | log.Errorw("error-updating-device", log.Fields{"deviceID": dh.device.Id, "error": err}) |
| 544 | return nil, err |
| 545 | } |
| 546 | |
| 547 | return deviceInfo, nil |
| 548 | } |
| 549 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 550 | //AdoptDevice adopts the OLT device |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 551 | func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 552 | dh.transitionMap = NewTransitionMap(dh) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 553 | log.Infow("Adopt_device", log.Fields{"deviceID": device.Id, "Address": device.GetHostAndPort()}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 554 | dh.transitionMap.Handle(DeviceInit) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 555 | } |
| 556 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 557 | //GetOfpDeviceInfo Gets the Ofp information of the given device |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 558 | func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 559 | return &ic.SwitchCapability{ |
| 560 | Desc: &of.OfpDesc{ |
Devmalya Paul | 70dd497 | 2019-06-10 15:19:17 +0530 | [diff] [blame] | 561 | MfrDesc: "VOLTHA Project", |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 562 | HwDesc: "open_pon", |
| 563 | SwDesc: "open_pon", |
| 564 | SerialNum: dh.device.SerialNumber, |
| 565 | }, |
| 566 | SwitchFeatures: &of.OfpSwitchFeatures{ |
| 567 | NBuffers: 256, |
| 568 | NTables: 2, |
| 569 | Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS | |
| 570 | of.OfpCapabilities_OFPC_TABLE_STATS | |
| 571 | of.OfpCapabilities_OFPC_PORT_STATS | |
| 572 | of.OfpCapabilities_OFPC_GROUP_STATS), |
| 573 | }, |
| 574 | }, nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 575 | } |
| 576 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 577 | //GetOfpPortInfo Get Ofp port information |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 578 | func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 579 | capacity := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 580 | return &ic.PortCapability{ |
| 581 | Port: &voltha.LogicalPort{ |
| 582 | OfpPort: &of.OfpPort{ |
| 583 | HwAddr: macAddressToUint32Array(dh.device.MacAddress), |
| 584 | Config: 0, |
| 585 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 586 | Curr: capacity, |
| 587 | Advertised: capacity, |
| 588 | Peer: capacity, |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 589 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 590 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 591 | }, |
| 592 | DeviceId: dh.device.Id, |
| 593 | DevicePortNo: uint32(portNo), |
| 594 | }, |
| 595 | }, nil |
| 596 | } |
| 597 | |
| 598 | func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 599 | log.Debugw("omci indication", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 600 | var deviceType string |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 601 | var deviceID string |
| 602 | var proxyDeviceID string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 603 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 604 | onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId) |
| 605 | if onuInCache, ok := dh.onus[onuKey]; !ok { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 606 | log.Debugw("omci indication for a device not in cache.", log.Fields{"intfID": omciInd.IntfId, "onuID": omciInd.OnuId}) |
| 607 | ponPort := IntfIDToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 608 | kwargs := make(map[string]interface{}) |
| 609 | kwargs["onu_id"] = omciInd.OnuId |
| 610 | kwargs["parent_port_no"] = ponPort |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 611 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 612 | onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
| 613 | if err != nil { |
| 614 | 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] | 615 | return err |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 616 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 617 | deviceType = onuDevice.Type |
| 618 | deviceID = onuDevice.Id |
| 619 | proxyDeviceID = onuDevice.ProxyAddress.DeviceId |
| 620 | //if not exist in cache, then add to cache. |
| 621 | 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] | 622 | } else { |
| 623 | //found in cache |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 624 | 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] | 625 | deviceType = onuInCache.deviceType |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 626 | deviceID = onuInCache.deviceID |
| 627 | proxyDeviceID = onuInCache.proxyDeviceID |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 628 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 629 | |
| 630 | omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt} |
| 631 | if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg, |
| 632 | ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType, |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 633 | deviceID, proxyDeviceID, ""); sendErr != nil { |
| 634 | 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] | 635 | return sendErr |
| 636 | } |
| 637 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 638 | } |
| 639 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 640 | //ProcessInterAdapterMessage sends the proxied messages to the target device |
| 641 | // If the proxy address is not found in the unmarshalled message, it first fetches the onu device for which the message |
| 642 | // is meant, and then send the unmarshalled omci message to this onu |
| 643 | func (dh *DeviceHandler) ProcessInterAdapterMessage(msg *ic.InterAdapterMessage) error { |
| 644 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgID": msg.Header.Id}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 645 | if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 646 | msgID := msg.Header.Id |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 647 | fromTopic := msg.Header.FromTopic |
| 648 | toTopic := msg.Header.ToTopic |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 649 | toDeviceID := msg.Header.ToDeviceId |
| 650 | proxyDeviceID := msg.Header.ProxyDeviceId |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 651 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 652 | 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] | 653 | |
| 654 | msgBody := msg.GetBody() |
| 655 | |
| 656 | omciMsg := &ic.InterAdapterOmciMessage{} |
| 657 | if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil { |
| 658 | log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err}) |
| 659 | return err |
| 660 | } |
| 661 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 662 | if omciMsg.GetProxyAddress() == nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 663 | onuDevice, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, toDeviceID) |
| 664 | if err != nil { |
| 665 | log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceID, "error": err}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 666 | return err |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 667 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 668 | log.Debugw("device retrieved from core", log.Fields{"msgID": msgID, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceID": toDeviceID, "proxyDeviceID": proxyDeviceID}) |
| 669 | dh.sendProxiedMessage(onuDevice, omciMsg) |
| 670 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 671 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 672 | 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] | 673 | dh.sendProxiedMessage(nil, omciMsg) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | } else { |
| 677 | log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type}) |
| 678 | } |
| 679 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 680 | } |
| 681 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 682 | func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 683 | var intfID uint32 |
| 684 | var onuID uint32 |
| 685 | var connectStatus common.ConnectStatus_ConnectStatus |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 686 | if onuDevice != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 687 | intfID = onuDevice.ProxyAddress.GetChannelId() |
| 688 | onuID = onuDevice.ProxyAddress.GetOnuId() |
| 689 | connectStatus = onuDevice.ConnectStatus |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 690 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 691 | intfID = omciMsg.GetProxyAddress().GetChannelId() |
| 692 | onuID = omciMsg.GetProxyAddress().GetOnuId() |
| 693 | connectStatus = omciMsg.GetConnectStatus() |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 694 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 695 | if connectStatus != voltha.ConnectStatus_REACHABLE { |
| 696 | 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] | 697 | return |
| 698 | } |
| 699 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 700 | omciMessage := &oop.OmciMsg{IntfId: intfID, OnuId: onuID, Pkt: omciMsg.Message} |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 701 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 702 | _, err := dh.Client.OmciMsgOut(context.Background(), omciMessage) |
| 703 | if err != nil { |
| 704 | log.Errorw("unable to send omci-msg-out", log.Fields{"IntfID": intfID, "OnuID": onuID, "Msg": omciMessage}) |
| 705 | return |
| 706 | } |
| 707 | 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] | 708 | } |
| 709 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 710 | func (dh *DeviceHandler) activateONU(intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) { |
| 711 | log.Debugw("activate-onu", log.Fields{"intfID": intfID, "onuID": onuID, "serialNum": serialNum, "serialNumber": serialNumber}) |
| 712 | dh.flowMgr.UpdateOnuInfo(intfID, uint32(onuID), serialNumber) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 713 | // TODO: need resource manager |
| 714 | var pir uint32 = 1000000 |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 715 | Onu := oop.Onu{IntfId: intfID, OnuId: uint32(onuID), SerialNumber: serialNum, Pir: pir} |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 716 | if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil { |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 717 | st, _ := status.FromError(err) |
| 718 | if st.Code() == codes.AlreadyExists { |
| 719 | log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber}) |
| 720 | } else { |
| 721 | log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err}) |
| 722 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 723 | } else { |
| 724 | log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber}) |
| 725 | } |
| 726 | } |
| 727 | |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 728 | func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, sn string) error { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 729 | channelID := onuDiscInd.GetIntfId() |
| 730 | parentPortNo := IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 731 | if _, ok := dh.discOnus[sn]; ok { |
| 732 | log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn}) |
| 733 | return nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 736 | dh.lockDevice.Lock() |
| 737 | dh.discOnus[sn] = true |
| 738 | dh.lockDevice.Unlock() |
| 739 | // evict the onu serial number from local cache |
| 740 | defer func() { |
| 741 | delete(dh.discOnus, sn) |
| 742 | }() |
| 743 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 744 | kwargs := make(map[string]interface{}) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 745 | if sn != "" { |
| 746 | kwargs["serial_number"] = sn |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 747 | } else { |
| 748 | log.Error("invalid onu serial number") |
| 749 | return errors.New("failed to fetch onu serial number") |
| 750 | } |
| 751 | |
| 752 | onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
| 753 | var onuID uint32 |
| 754 | if onuDevice == nil || err != nil { |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 755 | //This is the first time ONU discovered. Create an OnuID for it. |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 756 | onuID, err = dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId()) |
| 757 | if err != nil { |
| 758 | log.Errorw("failed to fetch onuID from resource manager", log.Fields{"err": err}) |
| 759 | return err |
| 760 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 761 | if onuDevice, err = dh.coreProxy.ChildDeviceDetected(context.TODO(), dh.device.Id, int(parentPortNo), |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 762 | "brcm_openomci_onu", int(channelID), |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 763 | string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuID)); onuDevice == nil { |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 764 | log.Errorw("Create onu error", |
| 765 | log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), |
| 766 | "onuID": onuID, "sn": sn, "error": err}) |
| 767 | return err |
| 768 | } |
| 769 | |
| 770 | } else { |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 771 | //ONU already discovered before. Use the same OnuID. |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 772 | onuID = onuDevice.ProxyAddress.OnuId |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 773 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 774 | //Insert the ONU into cache to use in OnuIndication. |
| 775 | //TODO: Do we need to remove this from the cache on ONU change, or wait for overwritten on next discovery. |
| 776 | onuKey := dh.formOnuKey(onuDiscInd.GetIntfId(), onuID) |
| 777 | dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuID, onuDiscInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId) |
Chaitrashree G S | 35b5d80 | 2019-07-08 23:12:03 -0400 | [diff] [blame] | 778 | |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 779 | err = dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED) |
| 780 | if err != nil { |
| 781 | log.Errorw("failed to update device state", log.Fields{"DeviceID": onuDevice.Id}) |
| 782 | return err |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 783 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 784 | log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id}) |
| 785 | //TODO: We put this sleep here to prevent the race between state update and onuIndication |
| 786 | //In onuIndication the operStatus of device is checked. If it is still not updated in KV store |
| 787 | //then the initialisation fails. |
| 788 | time.Sleep(1 * time.Second) |
| 789 | dh.activateONU(onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn) |
| 790 | return nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) { |
| 794 | serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber) |
| 795 | |
| 796 | kwargs := make(map[string]interface{}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 797 | ponPort := IntfIDToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT) |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 798 | var onuDevice *voltha.Device |
| 799 | foundInCache := false |
| 800 | onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.OnuId) |
| 801 | if onuInCache, ok := dh.onus[onuKey]; ok { |
| 802 | //If ONU id is discovered before then use GetDevice to get onuDevice because it is cheaper. |
| 803 | foundInCache = true |
| 804 | onuDevice, _ = dh.coreProxy.GetDevice(nil, dh.device.Id, onuInCache.deviceID) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 805 | } else { |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 806 | //If ONU not found in adapter cache then we have to use GetChildDevice to get onuDevice |
| 807 | if serialNumber != "" { |
| 808 | kwargs["serial_number"] = serialNumber |
| 809 | } else { |
| 810 | kwargs["onu_id"] = onuInd.OnuId |
| 811 | kwargs["parent_port_no"] = ponPort |
| 812 | } |
| 813 | onuDevice, _ = dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 814 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 815 | |
| 816 | if onuDevice != nil { |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 817 | if onuDevice.ParentPortNo != ponPort { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 818 | //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] | 819 | 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] | 820 | } |
| 821 | |
| 822 | if onuDevice.ProxyAddress.OnuId != onuInd.OnuId { |
| 823 | 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}) |
| 824 | } |
Mahir Gunyel | e77977b | 2019-06-27 05:36:22 -0700 | [diff] [blame] | 825 | if !foundInCache { |
| 826 | onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId()) |
| 827 | dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId) |
| 828 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 829 | dh.updateOnuStates(onuDevice, onuInd) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 830 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 831 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 832 | log.Errorw("onu not found", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 833 | return |
| 834 | } |
| 835 | |
| 836 | } |
| 837 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 838 | func (dh *DeviceHandler) updateOnuStates(onuDevice *voltha.Device, onuInd *oop.OnuIndication) { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 839 | dh.updateOnuAdminState(onuInd) |
| 840 | // operState |
| 841 | if onuInd.OperState == "down" { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 842 | log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd}) |
| 843 | |
| 844 | // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
| 845 | err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
| 846 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 847 | if err != nil { |
| 848 | log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd, |
| 849 | "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id}) |
| 850 | } |
| 851 | } else if onuInd.OperState == "up" { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 852 | if onuDevice.OperStatus != common.OperStatus_DISCOVERED { |
| 853 | log.Warnw("ignore onu indication", log.Fields{"intfID": onuInd.IntfId, "onuID": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState}) |
| 854 | return |
| 855 | } |
| 856 | err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, |
| 857 | "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 858 | if err != nil { |
| 859 | log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd, |
| 860 | "From Adapter": "openolt", "DevieType": onuDevice.Type, "DeviceID": onuDevice.Id}) |
| 861 | return |
| 862 | } |
| 863 | } else { |
| 864 | log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState}) |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | func (dh *DeviceHandler) updateOnuAdminState(onuInd *oop.OnuIndication) { |
| 869 | if onuInd.AdminState == "down" { |
| 870 | if onuInd.OperState != "down" { |
| 871 | log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState}) |
| 872 | // Forcing the oper state change code to execute |
| 873 | onuInd.OperState = "down" |
| 874 | } |
| 875 | // Port and logical port update is taken care of by oper state block |
| 876 | } else if onuInd.AdminState == "up" { |
| 877 | log.Debugln("received-onu-admin-state up") |
| 878 | } else { |
| 879 | log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState}) |
| 880 | } |
| 881 | log.Debugln("admin-state-dealt-with") |
| 882 | } |
| 883 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 884 | func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string { |
| 885 | if serialNum != nil { |
| 886 | return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 887 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 888 | return "" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string { |
| 892 | tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) + |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 893 | fmt.Sprintf("%x", uint32(vendorSpecific[0]&0x0f)) + |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 894 | fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) + |
| 895 | fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) + |
| 896 | fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) + |
| 897 | fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) + |
| 898 | fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) + |
| 899 | fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f) |
| 900 | return tmp |
| 901 | } |
| 902 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 903 | //UpdateFlowsBulk upates the bulk flow |
| 904 | func (dh *DeviceHandler) UpdateFlowsBulk() error { |
| 905 | return errors.New("unimplemented") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 906 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 907 | |
| 908 | //GetChildDevice returns the child device for given parent port and onu id |
| 909 | func (dh *DeviceHandler) GetChildDevice(parentPort, onuID uint32) *voltha.Device { |
| 910 | log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuID": onuID}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 911 | kwargs := make(map[string]interface{}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 912 | kwargs["onu_id"] = onuID |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 913 | kwargs["parent_port_no"] = parentPort |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 914 | onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 915 | if err != nil { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 916 | log.Errorw("onu not found", log.Fields{"intfID": parentPort, "onuID": onuID}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 917 | return nil |
| 918 | } |
| 919 | log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice}) |
| 920 | return onuDevice |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 921 | } |
| 922 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 923 | // SendPacketInToCore sends packet-in to core |
| 924 | // For this, it calls SendPacketIn of the core-proxy which uses a device specific topic to send the request. |
| 925 | // The adapter handling the device creates a device specific topic |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 926 | func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) { |
| 927 | log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 928 | 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] | 929 | log.Errorw("Error sending packetin to core", log.Fields{"error": err}) |
| 930 | return |
| 931 | } |
| 932 | log.Debug("Sent packet-in to core successfully") |
| 933 | } |
| 934 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 935 | //UpdateFlowsIncrementally updates the device flow |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 936 | 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] | 937 | 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] | 938 | if flows != nil { |
| 939 | for _, flow := range flows.ToAdd.Items { |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 940 | log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 941 | dh.flowMgr.AddFlow(flow) |
| 942 | } |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame] | 943 | for _, flow := range flows.ToRemove.Items { |
| 944 | log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow}) |
| 945 | dh.flowMgr.RemoveFlow(flow) |
| 946 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 947 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 948 | if groups != nil && flows != nil { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 949 | for _, flow := range flows.ToRemove.Items { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 950 | log.Debug("Removing flow", log.Fields{"deviceID": device.Id, "flowToRemove": flow}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 951 | // dh.flowMgr.RemoveFlow(flow) |
| 952 | } |
| 953 | } |
| 954 | return nil |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 955 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 956 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 957 | //DisableDevice disables the given device |
| 958 | //It marks the following for the given device: |
| 959 | //Device-Handler Admin-State : down |
| 960 | //Device Port-State: UNKNOWN |
| 961 | //Device Oper-State: UNKNOWN |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 962 | func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error { |
| 963 | if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil { |
| 964 | log.Errorw("Failed to disable olt ", log.Fields{"err": err}) |
| 965 | return err |
| 966 | } |
| 967 | dh.lockDevice.Lock() |
| 968 | dh.adminState = "down" |
| 969 | dh.lockDevice.Unlock() |
| 970 | log.Debug("olt-disabled") |
| 971 | |
| 972 | cloned := proto.Clone(device).(*voltha.Device) |
| 973 | // Update the all ports state on that device to disable |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 974 | if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
| 975 | log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 976 | return err |
| 977 | } |
| 978 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 979 | log.Debugw("Disable_device-end", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 980 | return nil |
| 981 | } |
| 982 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 983 | //ReenableDevice re-enables the olt device after disable |
| 984 | //It marks the following for the given device: |
| 985 | //Device-Handler Admin-State : up |
| 986 | //Device Port-State: ACTIVE |
| 987 | //Device Oper-State: ACTIVE |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 988 | func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error { |
| 989 | if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil { |
| 990 | log.Errorw("Failed to reenable olt ", log.Fields{"err": err}) |
| 991 | return err |
| 992 | } |
| 993 | |
| 994 | dh.lockDevice.Lock() |
| 995 | dh.adminState = "up" |
| 996 | dh.lockDevice.Unlock() |
| 997 | log.Debug("olt-reenabled") |
| 998 | |
| 999 | cloned := proto.Clone(device).(*voltha.Device) |
| 1000 | // Update the all ports state on that device to enable |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1001 | if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 1002 | log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1003 | return err |
| 1004 | } |
| 1005 | |
| 1006 | //Update the device oper status as ACTIVE |
| 1007 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 1008 | dh.device = cloned |
| 1009 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1010 | if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 1011 | 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] | 1012 | return err |
| 1013 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1014 | log.Debugw("ReEnableDevice-end", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 1015 | |
| 1016 | return nil |
| 1017 | } |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1018 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1019 | //RebootDevice reboots the given device |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 1020 | func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error { |
| 1021 | if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil { |
| 1022 | log.Errorw("Failed to reboot olt ", log.Fields{"err": err}) |
| 1023 | return err |
| 1024 | } |
| 1025 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1026 | log.Debugw("rebooted-device-successfully", log.Fields{"deviceID": device.Id}) |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 1027 | |
| 1028 | return nil |
| 1029 | } |
| 1030 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1031 | func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) { |
| 1032 | log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn}) |
| 1033 | logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn) |
| 1034 | if err != nil { |
| 1035 | log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err}) |
| 1036 | return |
| 1037 | } |
| 1038 | 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] | 1039 | 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] | 1040 | log.Errorw("Error sending packet-in to core", log.Fields{"error": err}) |
| 1041 | return |
| 1042 | } |
| 1043 | log.Debug("Success sending packet-in to core!") |
| 1044 | } |
| 1045 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1046 | // PacketOut sends packet-out from VOLTHA to OLT on the egress port provided |
| 1047 | func (dh *DeviceHandler) PacketOut(egressPortNo int, packet *of.OfpPacketOut) error { |
| 1048 | log.Debugw("PacketOut", log.Fields{"deviceID": dh.deviceID, "egress_port_no": egressPortNo, "pkt-length": len(packet.Data)}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1049 | var etherFrame ethernet.Frame |
| 1050 | err := (ðerFrame).UnmarshalBinary(packet.Data) |
| 1051 | if err != nil { |
| 1052 | log.Errorw("Failed to unmarshal into ethernet frame", log.Fields{"err": err, "pkt-length": len(packet.Data)}) |
| 1053 | return err |
| 1054 | } |
| 1055 | log.Debugw("Ethernet Frame", log.Fields{"Frame": etherFrame}) |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1056 | egressPortType := IntfIDToPortTypeName(uint32(egressPortNo)) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1057 | if egressPortType == voltha.Port_ETHERNET_UNI { |
| 1058 | if etherFrame.VLAN != nil { // If double tag, remove the outer tag |
| 1059 | nextEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17]) |
| 1060 | if nextEthType == 0x8100 { |
| 1061 | etherFrame.VLAN = nil |
| 1062 | packet.Data, err = etherFrame.MarshalBinary() |
| 1063 | if err != nil { |
| 1064 | log.Fatalf("failed to marshal frame: %v", err) |
| 1065 | return err |
| 1066 | } |
| 1067 | if err := (ðerFrame).UnmarshalBinary(packet.Data); err != nil { |
| 1068 | log.Fatalf("failed to unmarshal frame: %v", err) |
| 1069 | return err |
| 1070 | } |
| 1071 | log.Debug("Double tagged packet , removed outer vlan", log.Fields{"New frame": etherFrame}) |
| 1072 | } |
| 1073 | } |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1074 | intfID := IntfIDFromUniPortNum(uint32(egressPortNo)) |
| 1075 | onuID := OnuIDFromPortNum(uint32(egressPortNo)) |
| 1076 | uniID := UniIDFromPortNum(uint32(egressPortNo)) |
| 1077 | /*gemPortId, err := dh.flowMgr.GetPacketOutGemPortId(intfID, onuID, uint32(egress_port_no)) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1078 | if err != nil{ |
| 1079 | log.Errorw("Error while getting gemport to packet-out",log.Fields{"error": err}) |
| 1080 | return err |
| 1081 | }*/ |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1082 | onuPkt := oop.OnuPacket{IntfId: intfID, OnuId: onuID, PortNo: uint32(egressPortNo), Pkt: packet.Data} |
| 1083 | log.Debug("sending-packet-to-ONU", log.Fields{"egress_port_no": egressPortNo, "IntfId": intfID, "onuID": onuID, |
| 1084 | "uniID": uniID, "packet": packet.Data}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1085 | if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil { |
| 1086 | log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err}) |
| 1087 | return err |
| 1088 | } |
| 1089 | } else if egressPortType == voltha.Port_ETHERNET_NNI { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1090 | uplinkPkt := oop.UplinkPacket{IntfId: IntfIDFromNniPortNum(uint32(egressPortNo)), Pkt: packet.Data} |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 1091 | log.Debug("sending-packet-to-uplink", log.Fields{"uplink_pkt": uplinkPkt}) |
| 1092 | if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil { |
| 1093 | log.Errorw("Error while sending packet-out to uplink", log.Fields{"error": err}) |
| 1094 | return err |
| 1095 | } |
| 1096 | } else { |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1097 | 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] | 1098 | } |
| 1099 | return nil |
| 1100 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 1101 | |
Girish Gowdru | 6a80bbd | 2019-07-02 07:36:09 -0700 | [diff] [blame] | 1102 | func (dh *DeviceHandler) formOnuKey(intfID, onuID uint32) string { |
| 1103 | return "" + strconv.Itoa(int(intfID)) + "." + strconv.Itoa(int(onuID)) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 1104 | } |