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 | */ |
| 16 | package adaptercore |
| 17 | |
| 18 | import ( |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 19 | "context" |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | "sync" |
| 26 | "time" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 27 | |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 30 | "github.com/gogo/protobuf/proto" |
| 31 | "github.com/golang/protobuf/ptypes" |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 32 | "github.com/mdlayher/ethernet" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 33 | com "github.com/opencord/voltha-go/adapters/common" |
| 34 | "github.com/opencord/voltha-go/common/log" |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 35 | rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager" |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 36 | "github.com/opencord/voltha-protos/go/common" |
| 37 | ic "github.com/opencord/voltha-protos/go/inter_container" |
| 38 | of "github.com/opencord/voltha-protos/go/openflow_13" |
| 39 | oop "github.com/opencord/voltha-protos/go/openolt" |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 40 | "github.com/opencord/voltha-protos/go/voltha" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 41 | "google.golang.org/grpc" |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 42 | "google.golang.org/grpc/status" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | //DeviceHandler will interact with the OLT device. |
| 46 | type DeviceHandler struct { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 47 | deviceId string |
| 48 | deviceType string |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 49 | adminState string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 50 | device *voltha.Device |
| 51 | coreProxy *com.CoreProxy |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 52 | AdapterProxy *com.AdapterProxy |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 53 | openOLT *OpenOLT |
| 54 | nniPort *voltha.Port |
| 55 | ponPort *voltha.Port |
| 56 | exitChannel chan int |
| 57 | lockDevice sync.RWMutex |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 58 | Client oop.OpenoltClient |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 59 | transitionMap *TransitionMap |
| 60 | clientCon *grpc.ClientConn |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 61 | flowMgr *OpenOltFlowMgr |
| 62 | resourceMgr *rsrcMgr.OpenOltResourceMgr |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 63 | discOnus map[string]bool |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 64 | onus map[string]*OnuDevice |
| 65 | } |
| 66 | |
| 67 | type OnuDevice struct { |
| 68 | deviceId string |
| 69 | deviceType string |
| 70 | serialNumber string |
| 71 | onuId uint32 |
| 72 | intfId uint32 |
| 73 | proxyDeviceId string |
| 74 | } |
| 75 | |
| 76 | //NewOnuDevice creates a new Onu Device |
| 77 | func NewOnuDevice(devId string, deviceTp string, serialNum string, onuId uint32, intfId uint32, proxyDevId string) *OnuDevice { |
| 78 | var device OnuDevice |
| 79 | device.deviceId = devId |
| 80 | device.deviceType = deviceTp |
| 81 | device.serialNumber = serialNum |
| 82 | device.onuId = onuId |
| 83 | device.intfId = intfId |
| 84 | device.proxyDeviceId = proxyDevId |
| 85 | return &device |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | //NewDeviceHandler creates a new device handler |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 89 | func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler { |
| 90 | var dh DeviceHandler |
| 91 | dh.coreProxy = cp |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 92 | dh.AdapterProxy = ap |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 93 | cloned := (proto.Clone(device)).(*voltha.Device) |
| 94 | dh.deviceId = cloned.Id |
| 95 | dh.deviceType = cloned.Type |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 96 | dh.adminState = "up" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 97 | dh.device = cloned |
| 98 | dh.openOLT = adapter |
| 99 | dh.exitChannel = make(chan int, 1) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 100 | dh.discOnus = make(map[string]bool) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 101 | dh.lockDevice = sync.RWMutex{} |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 102 | dh.onus = make(map[string]*OnuDevice) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 103 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 104 | //TODO initialize the support classes. |
| 105 | return &dh |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // start save the device to the data model |
| 109 | func (dh *DeviceHandler) start(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 110 | dh.lockDevice.Lock() |
| 111 | defer dh.lockDevice.Unlock() |
| 112 | log.Debugw("starting-device-agent", log.Fields{"device": dh.device}) |
| 113 | // Add the initial device to the local model |
| 114 | log.Debug("device-agent-started") |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // stop stops the device dh. Not much to do for now |
| 118 | func (dh *DeviceHandler) stop(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 119 | dh.lockDevice.Lock() |
| 120 | defer dh.lockDevice.Unlock() |
| 121 | log.Debug("stopping-device-agent") |
| 122 | dh.exitChannel <- 1 |
| 123 | log.Debug("device-agent-stopped") |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | func macAddressToUint32Array(mac string) []uint32 { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 127 | slist := strings.Split(mac, ":") |
| 128 | result := make([]uint32, len(slist)) |
| 129 | var err error |
| 130 | var tmp int64 |
| 131 | for index, val := range slist { |
| 132 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 133 | return []uint32{1, 2, 3, 4, 5, 6} |
| 134 | } |
| 135 | result[index] = uint32(tmp) |
| 136 | } |
| 137 | return result |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 138 | } |
| 139 | |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 140 | func GetportLabel(portNum uint32, portType voltha.Port_PortType) string { |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 141 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 142 | if portType == voltha.Port_ETHERNET_NNI { |
| 143 | return fmt.Sprintf("nni-%d", portNum) |
| 144 | } else if portType == voltha.Port_PON_OLT { |
| 145 | return fmt.Sprintf("pon-%d", portNum) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 146 | } else if portType == voltha.Port_ETHERNET_UNI { |
| 147 | log.Errorw("local UNI management not supported", log.Fields{}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 148 | return "" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 149 | } |
| 150 | return "" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | func (dh *DeviceHandler) addPort(intfId uint32, portType voltha.Port_PortType, state string) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 154 | var operStatus common.OperStatus_OperStatus |
| 155 | if state == "up" { |
| 156 | operStatus = voltha.OperStatus_ACTIVE |
| 157 | } else { |
| 158 | operStatus = voltha.OperStatus_DISCOVERED |
| 159 | } |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 160 | portNum := IntfIdToPortNo(intfId, portType) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 161 | label := GetportLabel(portNum, portType) |
| 162 | if len(label) == 0 { |
| 163 | log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType}) |
| 164 | return |
| 165 | } |
| 166 | // Now create Port |
| 167 | port := &voltha.Port{ |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 168 | PortNo: portNum, |
| 169 | Label: label, |
| 170 | Type: portType, |
| 171 | OperStatus: operStatus, |
| 172 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 173 | log.Debugw("Sending port update to core", log.Fields{"port": port}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 174 | // Synchronous call to update device - this method is run in its own go routine |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 175 | if err := dh.coreProxy.PortCreated(nil, dh.device.Id, port); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 176 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": dh.device.Id, "error": err}) |
| 177 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // readIndications to read the indications from the OLT device |
| 181 | func (dh *DeviceHandler) readIndications() { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 182 | indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty)) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 183 | if err != nil { |
| 184 | log.Errorw("Failed to read indications", log.Fields{"err": err}) |
| 185 | return |
| 186 | } |
| 187 | if indications == nil { |
| 188 | log.Errorw("Indications is nil", log.Fields{}) |
| 189 | return |
| 190 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 191 | /* get device state */ |
| 192 | device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id) |
| 193 | if err != nil || device == nil { |
| 194 | /*TODO: needs to handle error scenarios */ |
| 195 | log.Errorw("Failed to fetch device info", log.Fields{"err": err}) |
| 196 | |
| 197 | } |
| 198 | // When the device is in DISABLED and Adapter container restarts, we need to |
| 199 | // rebuild the locally maintained admin state. |
| 200 | if device.AdminState == voltha.AdminState_DISABLED { |
| 201 | dh.lockDevice.Lock() |
| 202 | dh.adminState = "down" |
| 203 | dh.lockDevice.Unlock() |
| 204 | } |
| 205 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 206 | for { |
| 207 | indication, err := indications.Recv() |
| 208 | if err == io.EOF { |
| 209 | break |
| 210 | } |
| 211 | if err != nil { |
| 212 | log.Infow("Failed to read from indications", log.Fields{"err": err}) |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 213 | dh.transitionMap.Handle(DeviceDownInd) |
| 214 | dh.transitionMap.Handle(DeviceInit) |
| 215 | break |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 216 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 217 | // When OLT is admin down, allow only NNI operation status change indications. |
| 218 | if dh.adminState == "down" { |
| 219 | _, isIntfOperInd := indication.Data.(*oop.Indication_IntfOperInd) |
| 220 | if isIntfOperInd { |
| 221 | intfOperInd := indication.GetIntfOperInd() |
| 222 | if intfOperInd.GetType() == "nni" { |
| 223 | log.Infow("olt is admin down, allow nni ind", log.Fields{}) |
| 224 | } |
| 225 | } else { |
| 226 | log.Infow("olt is admin down, ignore indication", log.Fields{}) |
| 227 | continue |
| 228 | } |
| 229 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 230 | switch indication.Data.(type) { |
| 231 | case *oop.Indication_OltInd: |
| 232 | oltInd := indication.GetOltInd() |
| 233 | if oltInd.OperState == "up" { |
| 234 | dh.transitionMap.Handle(DeviceUpInd) |
| 235 | } else if oltInd.OperState == "down" { |
| 236 | dh.transitionMap.Handle(DeviceDownInd) |
| 237 | } |
| 238 | case *oop.Indication_IntfInd: |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 239 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 240 | intfInd := indication.GetIntfInd() |
| 241 | go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState()) |
| 242 | log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd}) |
| 243 | case *oop.Indication_IntfOperInd: |
| 244 | intfOperInd := indication.GetIntfOperInd() |
| 245 | if intfOperInd.GetType() == "nni" { |
| 246 | go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState()) |
| 247 | } else if intfOperInd.GetType() == "pon" { |
| 248 | // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down |
| 249 | // Handle pon port update |
| 250 | } |
| 251 | log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd}) |
| 252 | case *oop.Indication_OnuDiscInd: |
| 253 | onuDiscInd := indication.GetOnuDiscInd() |
| 254 | log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 255 | //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId()) |
| 256 | //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId()) |
| 257 | // TODO Get onu ID from the resource manager |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 258 | var onuId uint32 = 1 |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 259 | /*if err != nil{ |
| 260 | log.Errorw("onu-id-unavailable",log.Fields{"intfId":onuDiscInd.GetIntfId()}) |
| 261 | return |
| 262 | }*/ |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 263 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 264 | sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 265 | go dh.onuDiscIndication(onuDiscInd, onuId, sn) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 266 | case *oop.Indication_OnuInd: |
| 267 | onuInd := indication.GetOnuInd() |
| 268 | log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd}) |
| 269 | go dh.onuIndication(onuInd) |
| 270 | case *oop.Indication_OmciInd: |
| 271 | omciInd := indication.GetOmciInd() |
| 272 | log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd}) |
| 273 | if err := dh.omciIndication(omciInd); err != nil { |
| 274 | log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd}) |
| 275 | } |
| 276 | case *oop.Indication_PktInd: |
| 277 | pktInd := indication.GetPktInd() |
| 278 | log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 279 | go dh.handlePacketIndication(pktInd) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 280 | case *oop.Indication_PortStats: |
| 281 | portStats := indication.GetPortStats() |
| 282 | log.Infow("Received port stats indication", log.Fields{"PortStats": portStats}) |
| 283 | case *oop.Indication_FlowStats: |
| 284 | flowStats := indication.GetFlowStats() |
| 285 | log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats}) |
| 286 | case *oop.Indication_AlarmInd: |
| 287 | alarmInd := indication.GetAlarmInd() |
| 288 | log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd}) |
| 289 | } |
| 290 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // doStateUp handle the olt up indication and update to voltha core |
| 294 | func (dh *DeviceHandler) doStateUp() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 295 | // 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] | 296 | 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] | 297 | voltha.OperStatus_ACTIVE); err != nil { |
| 298 | log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceId": dh.device.Id, "error": err}) |
| 299 | return err |
| 300 | } |
| 301 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // doStateDown handle the olt down indication |
| 305 | func (dh *DeviceHandler) doStateDown() error { |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 306 | log.Debug("do-state-down-start") |
| 307 | |
| 308 | device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id) |
| 309 | if err != nil || device == nil { |
| 310 | /*TODO: needs to handle error scenarios */ |
| 311 | log.Errorw("Failed to fetch device device", log.Fields{"err": err}) |
| 312 | } |
| 313 | |
| 314 | cloned := proto.Clone(device).(*voltha.Device) |
| 315 | // Update the all ports state on that device to disable |
| 316 | if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
| 317 | log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 318 | return err |
| 319 | } |
| 320 | |
| 321 | //Update the device oper state and connection status |
| 322 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 323 | cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE |
| 324 | dh.device = cloned |
| 325 | |
| 326 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 327 | log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err}) |
| 328 | return err |
| 329 | } |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 330 | |
| 331 | //get the child device for the parent device |
| 332 | onuDevices, err := dh.coreProxy.GetChildDevices(nil, dh.device.Id) |
| 333 | if err != nil { |
| 334 | log.Errorw("failed to get child devices information", log.Fields{"deviceId": dh.device.Id, "error": err}) |
| 335 | return err |
| 336 | } |
| 337 | for _, onuDevice := range onuDevices.Items { |
| 338 | |
| 339 | // Update onu state as down in onu adapter |
| 340 | onuInd := oop.OnuIndication{} |
| 341 | onuInd.OperState = "down" |
| 342 | dh.AdapterProxy.SendInterAdapterMessage(nil, &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
| 343 | |
| 344 | } |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 345 | log.Debugw("do-state-down-end", log.Fields{"deviceId": device.Id}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 346 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // doStateInit dial the grpc before going to init state |
| 350 | func (dh *DeviceHandler) doStateInit() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 351 | var err error |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 352 | dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock()) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 353 | if err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 354 | 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] | 355 | return err |
| 356 | } |
| 357 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // postInit create olt client instance to invoke RPC on the olt device |
| 361 | func (dh *DeviceHandler) postInit() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 362 | dh.Client = oop.NewOpenoltClient(dh.clientCon) |
| 363 | dh.transitionMap.Handle(GrpcConnected) |
| 364 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // doStateConnected get the device info and update to voltha core |
| 368 | func (dh *DeviceHandler) doStateConnected() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 369 | log.Debug("OLT device has been connected") |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 370 | |
| 371 | // Case where OLT is disabled and then rebooted. |
| 372 | if dh.adminState == "down" { |
| 373 | log.Debugln("do-state-connected--device-admin-state-down") |
| 374 | device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id) |
| 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}) |
| 378 | } |
| 379 | |
| 380 | cloned := proto.Clone(device).(*voltha.Device) |
| 381 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 382 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 383 | dh.device = cloned |
| 384 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 385 | log.Errorw("error-updating-device-state", log.Fields{"deviceId": dh.device.Id, "error": err}) |
| 386 | } |
| 387 | |
| 388 | // Since the device was disabled before the OLT was rebooted, enfore the OLT to be Disabled after re-connection. |
| 389 | _, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty)) |
| 390 | if err != nil { |
| 391 | log.Errorw("Failed to disable olt ", log.Fields{"err": err}) |
| 392 | } |
| 393 | |
| 394 | // Start reading indications |
| 395 | go dh.readIndications() |
| 396 | return nil |
| 397 | } |
| 398 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 399 | deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty)) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 400 | if err != nil { |
| 401 | log.Errorw("Failed to fetch device info", log.Fields{"err": err}) |
| 402 | return err |
| 403 | } |
| 404 | if deviceInfo == nil { |
| 405 | log.Errorw("Device info is nil", log.Fields{}) |
| 406 | return errors.New("Failed to get device info from OLT") |
| 407 | } |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 408 | log.Debugw("Fetched device info", log.Fields{"deviceInfo": deviceInfo}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 409 | dh.device.Root = true |
| 410 | dh.device.Vendor = deviceInfo.Vendor |
| 411 | dh.device.Model = deviceInfo.Model |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 412 | dh.device.SerialNumber = deviceInfo.DeviceSerialNumber |
| 413 | dh.device.HardwareVersion = deviceInfo.HardwareVersion |
| 414 | dh.device.FirmwareVersion = deviceInfo.FirmwareVersion |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 415 | // FIXME: Remove Hardcodings |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 416 | dh.device.MacAddress = "0a:0b:0c:0d:0e:0f" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 417 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 418 | // Synchronous call to update device - this method is run in its own go routine |
| 419 | if err := dh.coreProxy.DeviceUpdate(nil, dh.device); err != nil { |
| 420 | log.Errorw("error-updating-device", log.Fields{"deviceId": dh.device.Id, "error": err}) |
| 421 | } |
Girish Gowdru | d424515 | 2019-05-10 00:47:31 -0400 | [diff] [blame] | 422 | |
| 423 | device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id) |
| 424 | if err != nil || device == nil { |
| 425 | /*TODO: needs to handle error scenarios */ |
| 426 | log.Errorw("Failed to fetch device device", log.Fields{"err": err}) |
| 427 | } |
| 428 | cloned := proto.Clone(device).(*voltha.Device) |
| 429 | // Update the all ports (if available) on that device to ACTIVE. |
| 430 | // The ports do not normally exist, unless the device is coming back from a reboot |
| 431 | if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 432 | log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 433 | return err |
| 434 | } |
| 435 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 436 | KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort) |
| 437 | // Instantiate resource manager |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 438 | 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] | 439 | log.Error("Error while instantiating resource manager") |
| 440 | return errors.New("Instantiating resource manager failed") |
| 441 | } |
| 442 | // Instantiate flow manager |
| 443 | if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil { |
| 444 | log.Error("Error while instantiating flow manager") |
| 445 | return errors.New("Instantiating flow manager failed") |
| 446 | } |
| 447 | /* TODO: Instantiate Alarm , stats , BW managers */ |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 448 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 449 | // Start reading indications |
| 450 | go dh.readIndications() |
| 451 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // AdoptDevice adopts the OLT device |
| 455 | func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 456 | dh.transitionMap = NewTransitionMap(dh) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 457 | log.Infow("AdoptDevice", log.Fields{"deviceId": device.Id, "Address": device.GetHostAndPort()}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 458 | dh.transitionMap.Handle(DeviceInit) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // GetOfpDeviceInfo Get the Ofp device information |
| 462 | func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 463 | return &ic.SwitchCapability{ |
| 464 | Desc: &of.OfpDesc{ |
Devmalya Paul | 70dd497 | 2019-06-10 15:19:17 +0530 | [diff] [blame] | 465 | MfrDesc: "VOLTHA Project", |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 466 | HwDesc: "open_pon", |
| 467 | SwDesc: "open_pon", |
| 468 | SerialNum: dh.device.SerialNumber, |
| 469 | }, |
| 470 | SwitchFeatures: &of.OfpSwitchFeatures{ |
| 471 | NBuffers: 256, |
| 472 | NTables: 2, |
| 473 | Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS | |
| 474 | of.OfpCapabilities_OFPC_TABLE_STATS | |
| 475 | of.OfpCapabilities_OFPC_PORT_STATS | |
| 476 | of.OfpCapabilities_OFPC_GROUP_STATS), |
| 477 | }, |
| 478 | }, nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // GetOfpPortInfo Get Ofp port information |
| 482 | func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 483 | cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 484 | return &ic.PortCapability{ |
| 485 | Port: &voltha.LogicalPort{ |
| 486 | OfpPort: &of.OfpPort{ |
| 487 | HwAddr: macAddressToUint32Array(dh.device.MacAddress), |
| 488 | Config: 0, |
| 489 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 490 | Curr: cap, |
| 491 | Advertised: cap, |
| 492 | Peer: cap, |
| 493 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 494 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 495 | }, |
| 496 | DeviceId: dh.device.Id, |
| 497 | DevicePortNo: uint32(portNo), |
| 498 | }, |
| 499 | }, nil |
| 500 | } |
| 501 | |
| 502 | func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error { |
| 503 | log.Debugw("omci indication", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId}) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 504 | var deviceType string |
| 505 | var deviceId string |
| 506 | var proxyDeviceId string |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 507 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 508 | onuKey := dh.formOnuKey(omciInd.IntfId, omciInd.OnuId) |
| 509 | if onuInCache, ok := dh.onus[onuKey]; !ok { |
| 510 | log.Debugw("omci indication for a device not in cache.", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId}) |
| 511 | ponPort := IntfIdToPortNo(omciInd.GetIntfId(), voltha.Port_PON_OLT) |
| 512 | kwargs := make(map[string]interface{}) |
| 513 | kwargs["onu_id"] = omciInd.OnuId |
| 514 | kwargs["parent_port_no"] = ponPort |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 515 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 516 | if onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); err != nil { |
| 517 | log.Errorw("onu not found", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId}) |
| 518 | return err |
| 519 | } else { |
| 520 | deviceType = onuDevice.Type |
| 521 | deviceId = onuDevice.Id |
| 522 | proxyDeviceId = onuDevice.ProxyAddress.DeviceId |
| 523 | //if not exist in cache, then add to cache. |
| 524 | dh.onus[onuKey] = NewOnuDevice(deviceId, deviceType, onuDevice.SerialNumber, omciInd.OnuId, omciInd.IntfId, proxyDeviceId) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 525 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 526 | } else { |
| 527 | //found in cache |
| 528 | log.Debugw("omci indication for a device in cache.", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId}) |
| 529 | deviceType = onuInCache.deviceType |
| 530 | deviceId = onuInCache.deviceId |
| 531 | proxyDeviceId = onuInCache.proxyDeviceId |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 532 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 533 | |
| 534 | omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt} |
| 535 | if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg, |
| 536 | ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, deviceType, |
| 537 | deviceId, proxyDeviceId, ""); sendErr != nil { |
| 538 | log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": deviceType, "onuId": deviceId, "proxyDeviceId": proxyDeviceId}) |
| 539 | return sendErr |
| 540 | } |
| 541 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | // Process_inter_adapter_message process inter adater message |
| 545 | func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 546 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id}) |
| 547 | if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST { |
| 548 | msgId := msg.Header.Id |
| 549 | fromTopic := msg.Header.FromTopic |
| 550 | toTopic := msg.Header.ToTopic |
| 551 | toDeviceId := msg.Header.ToDeviceId |
| 552 | proxyDeviceId := msg.Header.ProxyDeviceId |
| 553 | |
| 554 | log.Debugw("omci request message header", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId}) |
| 555 | |
| 556 | msgBody := msg.GetBody() |
| 557 | |
| 558 | omciMsg := &ic.InterAdapterOmciMessage{} |
| 559 | if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil { |
| 560 | log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err}) |
| 561 | return err |
| 562 | } |
| 563 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 564 | if omciMsg.GetProxyAddress() == nil { |
| 565 | if onuDevice, err := dh.coreProxy.GetDevice(nil, dh.device.Id, toDeviceId); err != nil { |
| 566 | log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceId, "error": err}) |
| 567 | return err |
| 568 | } else { |
| 569 | log.Debugw("device retrieved from core", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId}) |
| 570 | dh.sendProxiedMessage(onuDevice, omciMsg) |
| 571 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 572 | } else { |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 573 | log.Debugw("Proxy Address found in omci message", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId}) |
| 574 | dh.sendProxiedMessage(nil, omciMsg) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | } else { |
| 578 | log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type}) |
| 579 | } |
| 580 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 581 | } |
| 582 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 583 | func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) { |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 584 | var intfId uint32 |
| 585 | var onuId uint32 |
| 586 | var status common.ConnectStatus_ConnectStatus |
| 587 | if onuDevice != nil { |
| 588 | intfId = onuDevice.ProxyAddress.GetChannelId() |
| 589 | onuId = onuDevice.ProxyAddress.GetOnuId() |
| 590 | status = onuDevice.ConnectStatus |
| 591 | } else { |
| 592 | intfId = omciMsg.GetProxyAddress().GetChannelId() |
| 593 | onuId = omciMsg.GetProxyAddress().GetOnuId() |
| 594 | status = omciMsg.GetConnectStatus() |
| 595 | } |
| 596 | if status != voltha.ConnectStatus_REACHABLE { |
| 597 | 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] | 598 | return |
| 599 | } |
| 600 | |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 601 | omciMessage := &oop.OmciMsg{IntfId: intfId, OnuId: onuId, Pkt: omciMsg.Message} |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 602 | |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 603 | dh.Client.OmciMsgOut(context.Background(), omciMessage) |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 604 | 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] | 605 | } |
| 606 | |
| 607 | func (dh *DeviceHandler) activateONU(intfId uint32, onuId int64, serialNum *oop.SerialNumber, serialNumber string) { |
| 608 | log.Debugw("activate-onu", log.Fields{"intfId": intfId, "onuId": onuId, "serialNum": serialNum, "serialNumber": serialNumber}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 609 | dh.flowMgr.UpdateOnuInfo(intfId, uint32(onuId), serialNumber) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 610 | // TODO: need resource manager |
| 611 | var pir uint32 = 1000000 |
| 612 | Onu := oop.Onu{IntfId: intfId, OnuId: uint32(onuId), SerialNumber: serialNum, Pir: pir} |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 613 | if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil { |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 614 | st, _ := status.FromError(err) |
| 615 | if st.Code() == codes.AlreadyExists { |
| 616 | log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber}) |
| 617 | } else { |
| 618 | log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu, "err ": err}) |
| 619 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 620 | } else { |
| 621 | log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber}) |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, onuId uint32, sn string) error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 626 | channelId := onuDiscInd.GetIntfId() |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 627 | parentPortNo := IntfIdToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 628 | if _, ok := dh.discOnus[sn]; ok { |
| 629 | log.Debugw("onu-sn-is-already-being-processed", log.Fields{"sn": sn}) |
| 630 | return nil |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 633 | dh.lockDevice.Lock() |
| 634 | dh.discOnus[sn] = true |
| 635 | dh.lockDevice.Unlock() |
| 636 | // evict the onu serial number from local cache |
| 637 | defer func() { |
| 638 | delete(dh.discOnus, sn) |
| 639 | }() |
| 640 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 641 | kwargs := make(map[string]interface{}) |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 642 | if sn != "" { |
| 643 | kwargs["serial_number"] = sn |
| 644 | } |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 645 | kwargs["onu_id"] = onuId |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 646 | kwargs["parent_port_no"] = parentPortNo |
Chaitrashree G S | be6ab94 | 2019-05-24 06:42:49 -0400 | [diff] [blame] | 647 | onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs) |
| 648 | if onuDevice == nil { |
| 649 | if err := dh.coreProxy.ChildDeviceDetected(nil, dh.device.Id, int(parentPortNo), "brcm_openomci_onu", int(channelId), string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuId)); err != nil { |
| 650 | log.Errorw("Create onu error", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn, "error": err}) |
| 651 | return err |
| 652 | } |
| 653 | } |
| 654 | onuDevice, err = dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs) |
| 655 | if err != nil { |
| 656 | log.Errorw("failed to get ONU device information", log.Fields{"err": err}) |
| 657 | return err |
| 658 | } |
| 659 | dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED) |
| 660 | log.Debugw("onu-discovered-reachable", log.Fields{"deviceId": onuDevice.Id}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 661 | |
| 662 | for i := 0; i < 10; i++ { |
| 663 | if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil { |
| 664 | dh.activateONU(onuDiscInd.IntfId, int64(onuId), onuDiscInd.SerialNumber, sn) |
| 665 | return nil |
| 666 | } else { |
| 667 | time.Sleep(1 * time.Second) |
| 668 | log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1) |
| 669 | } |
| 670 | } |
| 671 | log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn}) |
| 672 | return errors.New("Failed to activate onu") |
| 673 | } |
| 674 | |
| 675 | func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) { |
| 676 | serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber) |
| 677 | |
| 678 | kwargs := make(map[string]interface{}) |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 679 | ponPort := IntfIdToPortNo(onuInd.GetIntfId(), voltha.Port_PON_OLT) |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 680 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 681 | if serialNumber != "" { |
| 682 | kwargs["serial_number"] = serialNumber |
| 683 | } else { |
| 684 | kwargs["onu_id"] = onuInd.OnuId |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 685 | kwargs["parent_port_no"] = ponPort |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 686 | } |
| 687 | if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil { |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 688 | if onuDevice.ParentPortNo != ponPort { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 689 | //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] | 690 | 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] | 691 | } |
| 692 | |
| 693 | if onuDevice.ProxyAddress.OnuId != onuInd.OnuId { |
| 694 | 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}) |
| 695 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 696 | onuKey := dh.formOnuKey(onuInd.GetIntfId(), onuInd.GetOnuId()) |
| 697 | dh.onus[onuKey] = NewOnuDevice(onuDevice.Id, onuDevice.Type, onuDevice.SerialNumber, onuInd.GetOnuId(), onuInd.GetIntfId(), onuDevice.ProxyAddress.DeviceId) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 698 | |
| 699 | // adminState |
| 700 | if onuInd.AdminState == "down" { |
| 701 | if onuInd.OperState != "down" { |
| 702 | log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState}) |
| 703 | // Forcing the oper state change code to execute |
| 704 | onuInd.OperState = "down" |
| 705 | } |
| 706 | // Port and logical port update is taken care of by oper state block |
| 707 | } else if onuInd.AdminState == "up" { |
| 708 | log.Debugln("received-onu-admin-state up") |
| 709 | } else { |
| 710 | log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState}) |
| 711 | } |
| 712 | log.Debugln("admin-state-dealt-with") |
| 713 | |
| 714 | // operState |
| 715 | if onuInd.OperState == "down" { |
| 716 | if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE { |
| 717 | dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, onuDevice.OperStatus) |
| 718 | log.Debugln("onu-oper-state-is-down") |
| 719 | } |
| 720 | if onuDevice.OperStatus != common.OperStatus_DISCOVERED { |
| 721 | dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, common.OperStatus_DISCOVERED) |
| 722 | } |
| 723 | log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd}) |
| 724 | |
| 725 | // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 726 | dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 727 | } else if onuInd.OperState == "up" { |
| 728 | if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE { |
| 729 | dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus) |
| 730 | |
| 731 | } |
| 732 | if onuDevice.OperStatus != common.OperStatus_DISCOVERED { |
| 733 | log.Warnw("ignore onu indication", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState}) |
| 734 | return |
| 735 | } |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 736 | dh.AdapterProxy.SendInterAdapterMessage(nil, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST, "openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "") |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 737 | } else { |
| 738 | log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState}) |
| 739 | } |
| 740 | } else { |
| 741 | log.Errorw("onu not found", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId}) |
| 742 | return |
| 743 | } |
| 744 | |
| 745 | } |
| 746 | |
| 747 | func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string { |
| 748 | if serialNum != nil { |
| 749 | return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific) |
| 750 | } else { |
| 751 | return "" |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string { |
| 756 | tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) + |
| 757 | fmt.Sprintf("%x", (uint32(vendorSpecific[0]&0x0f))) + |
| 758 | fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) + |
| 759 | fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) + |
| 760 | fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) + |
| 761 | fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) + |
| 762 | fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) + |
| 763 | fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f) |
| 764 | return tmp |
| 765 | } |
| 766 | |
| 767 | // flows |
| 768 | func (dh *DeviceHandler) Update_flows_bulk() error { |
| 769 | return errors.New("UnImplemented") |
| 770 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 771 | func (dh *DeviceHandler) GetChildDevice(parentPort uint32, onuId uint32) *voltha.Device { |
| 772 | log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuId": onuId}) |
| 773 | kwargs := make(map[string]interface{}) |
| 774 | kwargs["onu_id"] = onuId |
| 775 | kwargs["parent_port_no"] = parentPort |
| 776 | onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs) |
| 777 | if err != nil { |
| 778 | log.Errorw("onu not found", log.Fields{"intfId": parentPort, "onuId": onuId}) |
| 779 | return nil |
| 780 | } |
| 781 | log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice}) |
| 782 | return onuDevice |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 783 | } |
| 784 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 785 | func (dh *DeviceHandler) SendPacketInToCore(logicalPort uint32, packetPayload []byte) { |
| 786 | log.Debugw("SendPacketInToCore", log.Fields{"port": logicalPort, "packetPayload": packetPayload}) |
| 787 | if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPort, packetPayload); err != nil { |
| 788 | log.Errorw("Error sending packetin to core", log.Fields{"error": err}) |
| 789 | return |
| 790 | } |
| 791 | log.Debug("Sent packet-in to core successfully") |
| 792 | } |
| 793 | |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 794 | func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges) error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 795 | log.Debugw("In UpdateFlowsIncrementally", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups}) |
| 796 | if flows != nil { |
| 797 | for _, flow := range flows.ToAdd.Items { |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame^] | 798 | log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 799 | dh.flowMgr.AddFlow(flow) |
| 800 | } |
Manjunath Vanarajulu | 28c3e82 | 2019-05-16 11:14:28 -0400 | [diff] [blame^] | 801 | for _, flow := range flows.ToRemove.Items { |
| 802 | log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow}) |
| 803 | dh.flowMgr.RemoveFlow(flow) |
| 804 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 805 | } |
| 806 | if groups != nil { |
| 807 | for _, flow := range flows.ToRemove.Items { |
| 808 | log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow}) |
| 809 | // dh.flowMgr.RemoveFlow(flow) |
| 810 | } |
| 811 | } |
| 812 | return nil |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 813 | } |
Girish Gowdru | 5ba46c9 | 2019-04-25 05:00:05 -0400 | [diff] [blame] | 814 | |
| 815 | func (dh *DeviceHandler) DisableDevice(device *voltha.Device) error { |
| 816 | if _, err := dh.Client.DisableOlt(context.Background(), new(oop.Empty)); err != nil { |
| 817 | log.Errorw("Failed to disable olt ", log.Fields{"err": err}) |
| 818 | return err |
| 819 | } |
| 820 | dh.lockDevice.Lock() |
| 821 | dh.adminState = "down" |
| 822 | dh.lockDevice.Unlock() |
| 823 | log.Debug("olt-disabled") |
| 824 | |
| 825 | cloned := proto.Clone(device).(*voltha.Device) |
| 826 | // Update the all ports state on that device to disable |
| 827 | if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
| 828 | log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 829 | return err |
| 830 | } |
| 831 | |
| 832 | //Update the device oper state |
| 833 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 834 | dh.device = cloned |
| 835 | |
| 836 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 837 | log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err}) |
| 838 | return err |
| 839 | } |
| 840 | log.Debugw("DisableDevice-end", log.Fields{"deviceId": device.Id}) |
| 841 | return nil |
| 842 | } |
| 843 | |
| 844 | func (dh *DeviceHandler) ReenableDevice(device *voltha.Device) error { |
| 845 | if _, err := dh.Client.ReenableOlt(context.Background(), new(oop.Empty)); err != nil { |
| 846 | log.Errorw("Failed to reenable olt ", log.Fields{"err": err}) |
| 847 | return err |
| 848 | } |
| 849 | |
| 850 | dh.lockDevice.Lock() |
| 851 | dh.adminState = "up" |
| 852 | dh.lockDevice.Unlock() |
| 853 | log.Debug("olt-reenabled") |
| 854 | |
| 855 | cloned := proto.Clone(device).(*voltha.Device) |
| 856 | // Update the all ports state on that device to enable |
| 857 | if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 858 | log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 859 | return err |
| 860 | } |
| 861 | |
| 862 | //Update the device oper status as ACTIVE |
| 863 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 864 | dh.device = cloned |
| 865 | |
| 866 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 867 | log.Errorw("error-updating-device-state", log.Fields{"deviceId": device.Id, "error": err}) |
| 868 | return err |
| 869 | } |
| 870 | log.Debugw("ReEnableDevice-end", log.Fields{"deviceId": device.Id}) |
| 871 | |
| 872 | return nil |
| 873 | } |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 874 | |
Girish Gowdru | 0fe5f7e | 2019-05-28 05:12:27 -0400 | [diff] [blame] | 875 | func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error { |
| 876 | if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil { |
| 877 | log.Errorw("Failed to reboot olt ", log.Fields{"err": err}) |
| 878 | return err |
| 879 | } |
| 880 | |
| 881 | log.Debugw("rebooted-device-successfully", log.Fields{"deviceId": device.Id}) |
| 882 | |
| 883 | return nil |
| 884 | } |
| 885 | |
manikkaraj k | 9eb6cac | 2019-05-09 12:32:03 -0400 | [diff] [blame] | 886 | func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) { |
| 887 | log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn}) |
| 888 | logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn) |
| 889 | if err != nil { |
| 890 | log.Errorw("Error getting logical port from packet-in", log.Fields{"error": err}) |
| 891 | return |
| 892 | } |
| 893 | log.Debugw("sending packet-in to core", log.Fields{"logicalPortNum": logicalPortNum, "packet": *packetIn}) |
| 894 | if err := dh.coreProxy.SendPacketIn(nil, dh.device.Id, logicalPortNum, packetIn.Pkt); err != nil { |
| 895 | log.Errorw("Error sending packet-in to core", log.Fields{"error": err}) |
| 896 | return |
| 897 | } |
| 898 | log.Debug("Success sending packet-in to core!") |
| 899 | } |
| 900 | |
| 901 | func (dh *DeviceHandler) PacketOut(egress_port_no int, packet *of.OfpPacketOut) error { |
| 902 | log.Debugw("PacketOut", log.Fields{"deviceId": dh.deviceId, "egress_port_no": egress_port_no, "pkt-length": len(packet.Data)}) |
| 903 | var etherFrame ethernet.Frame |
| 904 | err := (ðerFrame).UnmarshalBinary(packet.Data) |
| 905 | if err != nil { |
| 906 | log.Errorw("Failed to unmarshal into ethernet frame", log.Fields{"err": err, "pkt-length": len(packet.Data)}) |
| 907 | return err |
| 908 | } |
| 909 | log.Debugw("Ethernet Frame", log.Fields{"Frame": etherFrame}) |
| 910 | egressPortType := IntfIdToPortTypeName(uint32(egress_port_no)) |
| 911 | if egressPortType == voltha.Port_ETHERNET_UNI { |
| 912 | if etherFrame.VLAN != nil { // If double tag, remove the outer tag |
| 913 | nextEthType := (uint16(packet.Data[16]) << 8) | uint16(packet.Data[17]) |
| 914 | if nextEthType == 0x8100 { |
| 915 | etherFrame.VLAN = nil |
| 916 | packet.Data, err = etherFrame.MarshalBinary() |
| 917 | if err != nil { |
| 918 | log.Fatalf("failed to marshal frame: %v", err) |
| 919 | return err |
| 920 | } |
| 921 | if err := (ðerFrame).UnmarshalBinary(packet.Data); err != nil { |
| 922 | log.Fatalf("failed to unmarshal frame: %v", err) |
| 923 | return err |
| 924 | } |
| 925 | log.Debug("Double tagged packet , removed outer vlan", log.Fields{"New frame": etherFrame}) |
| 926 | } |
| 927 | } |
| 928 | intfId := IntfIdFromUniPortNum(uint32(egress_port_no)) |
| 929 | onuId := OnuIdFromPortNum(uint32(egress_port_no)) |
| 930 | uniId := UniIdFromPortNum(uint32(egress_port_no)) |
| 931 | /*gemPortId, err := dh.flowMgr.GetPacketOutGemPortId(intfId, onuId, uint32(egress_port_no)) |
| 932 | if err != nil{ |
| 933 | log.Errorw("Error while getting gemport to packet-out",log.Fields{"error": err}) |
| 934 | return err |
| 935 | }*/ |
| 936 | onuPkt := oop.OnuPacket{IntfId: intfId, OnuId: onuId, PortNo: uint32(egress_port_no), Pkt: packet.Data} |
| 937 | log.Debug("sending-packet-to-ONU", log.Fields{"egress_port_no": egress_port_no, "IntfId": intfId, "onuId": onuId, |
| 938 | "uniId": uniId, "packet": packet.Data}) |
| 939 | if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil { |
| 940 | log.Errorw("Error while sending packet-out to ONU", log.Fields{"error": err}) |
| 941 | return err |
| 942 | } |
| 943 | } else if egressPortType == voltha.Port_ETHERNET_NNI { |
| 944 | uplinkPkt := oop.UplinkPacket{IntfId: IntfIdFromNniPortNum(uint32(egress_port_no)), Pkt: packet.Data} |
| 945 | log.Debug("sending-packet-to-uplink", log.Fields{"uplink_pkt": uplinkPkt}) |
| 946 | if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil { |
| 947 | log.Errorw("Error while sending packet-out to uplink", log.Fields{"error": err}) |
| 948 | return err |
| 949 | } |
| 950 | } else { |
| 951 | log.Warnw("Packet-out-to-this-interface-type-not-implemented", log.Fields{"egress_port_no": egress_port_no, "egressPortType": egressPortType}) |
| 952 | } |
| 953 | return nil |
| 954 | } |
Mahir Gunyel | a3f9add | 2019-06-06 15:13:19 -0700 | [diff] [blame] | 955 | |
| 956 | func (dh *DeviceHandler) formOnuKey(intfId uint32, onuId uint32) string { |
| 957 | return ("" + strconv.Itoa(int(intfId)) + "." + strconv.Itoa(int(onuId))) |
| 958 | |
| 959 | } |