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