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" |
| 30 | com "github.com/opencord/voltha-go/adapters/common" |
| 31 | "github.com/opencord/voltha-go/common/log" |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 32 | rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager" |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 33 | "github.com/opencord/voltha-protos/go/common" |
| 34 | ic "github.com/opencord/voltha-protos/go/inter_container" |
| 35 | of "github.com/opencord/voltha-protos/go/openflow_13" |
| 36 | oop "github.com/opencord/voltha-protos/go/openolt" |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 37 | "github.com/opencord/voltha-protos/go/voltha" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 38 | "google.golang.org/grpc" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | //DeviceHandler will interact with the OLT device. |
| 42 | type DeviceHandler struct { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 43 | deviceId string |
| 44 | deviceType string |
| 45 | device *voltha.Device |
| 46 | coreProxy *com.CoreProxy |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 47 | AdapterProxy *com.AdapterProxy |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 48 | openOLT *OpenOLT |
| 49 | nniPort *voltha.Port |
| 50 | ponPort *voltha.Port |
| 51 | exitChannel chan int |
| 52 | lockDevice sync.RWMutex |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 53 | Client oop.OpenoltClient |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 54 | transitionMap *TransitionMap |
| 55 | clientCon *grpc.ClientConn |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 56 | flowMgr *OpenOltFlowMgr |
| 57 | resourceMgr *rsrcMgr.OpenOltResourceMgr |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | //NewDeviceHandler creates a new device handler |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 61 | func NewDeviceHandler(cp *com.CoreProxy, ap *com.AdapterProxy, device *voltha.Device, adapter *OpenOLT) *DeviceHandler { |
| 62 | var dh DeviceHandler |
| 63 | dh.coreProxy = cp |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 64 | dh.AdapterProxy = ap |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 65 | cloned := (proto.Clone(device)).(*voltha.Device) |
| 66 | dh.deviceId = cloned.Id |
| 67 | dh.deviceType = cloned.Type |
| 68 | dh.device = cloned |
| 69 | dh.openOLT = adapter |
| 70 | dh.exitChannel = make(chan int, 1) |
| 71 | dh.lockDevice = sync.RWMutex{} |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 72 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 73 | //TODO initialize the support classes. |
| 74 | return &dh |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // start save the device to the data model |
| 78 | func (dh *DeviceHandler) start(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 79 | dh.lockDevice.Lock() |
| 80 | defer dh.lockDevice.Unlock() |
| 81 | log.Debugw("starting-device-agent", log.Fields{"device": dh.device}) |
| 82 | // Add the initial device to the local model |
| 83 | log.Debug("device-agent-started") |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // stop stops the device dh. Not much to do for now |
| 87 | func (dh *DeviceHandler) stop(ctx context.Context) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 88 | dh.lockDevice.Lock() |
| 89 | defer dh.lockDevice.Unlock() |
| 90 | log.Debug("stopping-device-agent") |
| 91 | dh.exitChannel <- 1 |
| 92 | log.Debug("device-agent-stopped") |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func macAddressToUint32Array(mac string) []uint32 { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 96 | slist := strings.Split(mac, ":") |
| 97 | result := make([]uint32, len(slist)) |
| 98 | var err error |
| 99 | var tmp int64 |
| 100 | for index, val := range slist { |
| 101 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 102 | return []uint32{1, 2, 3, 4, 5, 6} |
| 103 | } |
| 104 | result[index] = uint32(tmp) |
| 105 | } |
| 106 | return result |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 107 | } |
| 108 | |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 109 | func GetportLabel(portNum uint32, portType voltha.Port_PortType) string { |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 110 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 111 | if portType == voltha.Port_ETHERNET_NNI { |
| 112 | return fmt.Sprintf("nni-%d", portNum) |
| 113 | } else if portType == voltha.Port_PON_OLT { |
| 114 | return fmt.Sprintf("pon-%d", portNum) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 115 | } else if portType == voltha.Port_ETHERNET_UNI { |
| 116 | log.Errorw("local UNI management not supported", log.Fields{}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 117 | return "" |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 118 | } |
| 119 | return "" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | func (dh *DeviceHandler) addPort(intfId uint32, portType voltha.Port_PortType, state string) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 123 | var operStatus common.OperStatus_OperStatus |
| 124 | if state == "up" { |
| 125 | operStatus = voltha.OperStatus_ACTIVE |
| 126 | } else { |
| 127 | operStatus = voltha.OperStatus_DISCOVERED |
| 128 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 129 | // portNum := IntfIdToPortNo(intfId,portType) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 130 | portNum := intfId |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 131 | label := GetportLabel(portNum, portType) |
| 132 | if len(label) == 0 { |
| 133 | log.Errorw("Invalid-port-label", log.Fields{"portNum": portNum, "portType": portType}) |
| 134 | return |
| 135 | } |
| 136 | // Now create Port |
| 137 | port := &voltha.Port{ |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 138 | PortNo: portNum, |
| 139 | Label: label, |
| 140 | Type: portType, |
| 141 | OperStatus: operStatus, |
| 142 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 143 | log.Debugw("Sending port update to core", log.Fields{"port": port}) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 144 | // 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] | 145 | if err := dh.coreProxy.PortCreated(nil, dh.device.Id, port); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 146 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": dh.device.Id, "error": err}) |
| 147 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // readIndications to read the indications from the OLT device |
| 151 | func (dh *DeviceHandler) readIndications() { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 152 | indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty)) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 153 | if err != nil { |
| 154 | log.Errorw("Failed to read indications", log.Fields{"err": err}) |
| 155 | return |
| 156 | } |
| 157 | if indications == nil { |
| 158 | log.Errorw("Indications is nil", log.Fields{}) |
| 159 | return |
| 160 | } |
| 161 | for { |
| 162 | indication, err := indications.Recv() |
| 163 | if err == io.EOF { |
| 164 | break |
| 165 | } |
| 166 | if err != nil { |
| 167 | log.Infow("Failed to read from indications", log.Fields{"err": err}) |
| 168 | continue |
| 169 | } |
| 170 | switch indication.Data.(type) { |
| 171 | case *oop.Indication_OltInd: |
| 172 | oltInd := indication.GetOltInd() |
| 173 | if oltInd.OperState == "up" { |
| 174 | dh.transitionMap.Handle(DeviceUpInd) |
| 175 | } else if oltInd.OperState == "down" { |
| 176 | dh.transitionMap.Handle(DeviceDownInd) |
| 177 | } |
| 178 | case *oop.Indication_IntfInd: |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 179 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 180 | intfInd := indication.GetIntfInd() |
| 181 | go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState()) |
| 182 | log.Infow("Received interface indication ", log.Fields{"InterfaceInd": intfInd}) |
| 183 | case *oop.Indication_IntfOperInd: |
| 184 | intfOperInd := indication.GetIntfOperInd() |
| 185 | if intfOperInd.GetType() == "nni" { |
| 186 | go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState()) |
| 187 | } else if intfOperInd.GetType() == "pon" { |
| 188 | // TODO: Check what needs to be handled here for When PON PORT down, ONU will be down |
| 189 | // Handle pon port update |
| 190 | } |
| 191 | log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd}) |
| 192 | case *oop.Indication_OnuDiscInd: |
| 193 | onuDiscInd := indication.GetOnuDiscInd() |
| 194 | log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 195 | //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId()) |
| 196 | //onuId,err := dh.resourceMgr.GetONUID(onuDiscInd.GetIntfId()) |
| 197 | // TODO Get onu ID from the resource manager |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 198 | var onuId uint32 = 1 |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 199 | /*if err != nil{ |
| 200 | log.Errorw("onu-id-unavailable",log.Fields{"intfId":onuDiscInd.GetIntfId()}) |
| 201 | return |
| 202 | }*/ |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 203 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 204 | sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 205 | //FIXME: Duplicate child devices being create in go routine |
| 206 | dh.onuDiscIndication(onuDiscInd, onuId, sn) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 207 | case *oop.Indication_OnuInd: |
| 208 | onuInd := indication.GetOnuInd() |
| 209 | log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd}) |
| 210 | go dh.onuIndication(onuInd) |
| 211 | case *oop.Indication_OmciInd: |
| 212 | omciInd := indication.GetOmciInd() |
| 213 | log.Infow("Received Omci indication ", log.Fields{"OmciInd": omciInd}) |
| 214 | if err := dh.omciIndication(omciInd); err != nil { |
| 215 | log.Errorw("send-omci-indication-errr", log.Fields{"error": err, "omciInd": omciInd}) |
| 216 | } |
| 217 | case *oop.Indication_PktInd: |
| 218 | pktInd := indication.GetPktInd() |
| 219 | log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd}) |
| 220 | case *oop.Indication_PortStats: |
| 221 | portStats := indication.GetPortStats() |
| 222 | log.Infow("Received port stats indication", log.Fields{"PortStats": portStats}) |
| 223 | case *oop.Indication_FlowStats: |
| 224 | flowStats := indication.GetFlowStats() |
| 225 | log.Infow("Received flow stats", log.Fields{"FlowStats": flowStats}) |
| 226 | case *oop.Indication_AlarmInd: |
| 227 | alarmInd := indication.GetAlarmInd() |
| 228 | log.Infow("Received alarm indication ", log.Fields{"AlarmInd": alarmInd}) |
| 229 | } |
| 230 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // doStateUp handle the olt up indication and update to voltha core |
| 234 | func (dh *DeviceHandler) doStateUp() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 235 | // 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] | 236 | 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] | 237 | voltha.OperStatus_ACTIVE); err != nil { |
| 238 | log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceId": dh.device.Id, "error": err}) |
| 239 | return err |
| 240 | } |
| 241 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // doStateDown handle the olt down indication |
| 245 | func (dh *DeviceHandler) doStateDown() error { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 246 | //TODO Handle oper state down |
| 247 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // doStateInit dial the grpc before going to init state |
| 251 | func (dh *DeviceHandler) doStateInit() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 252 | var err error |
| 253 | dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure()) |
| 254 | if err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 255 | 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] | 256 | return err |
| 257 | } |
| 258 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // postInit create olt client instance to invoke RPC on the olt device |
| 262 | func (dh *DeviceHandler) postInit() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 263 | dh.Client = oop.NewOpenoltClient(dh.clientCon) |
| 264 | dh.transitionMap.Handle(GrpcConnected) |
| 265 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // doStateConnected get the device info and update to voltha core |
| 269 | func (dh *DeviceHandler) doStateConnected() error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 270 | log.Debug("OLT device has been connected") |
| 271 | deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty)) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 272 | if err != nil { |
| 273 | log.Errorw("Failed to fetch device info", log.Fields{"err": err}) |
| 274 | return err |
| 275 | } |
| 276 | if deviceInfo == nil { |
| 277 | log.Errorw("Device info is nil", log.Fields{}) |
| 278 | return errors.New("Failed to get device info from OLT") |
| 279 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 280 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 281 | dh.device.Root = true |
| 282 | dh.device.Vendor = deviceInfo.Vendor |
| 283 | dh.device.Model = deviceInfo.Model |
| 284 | dh.device.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 285 | dh.device.SerialNumber = deviceInfo.DeviceSerialNumber |
| 286 | dh.device.HardwareVersion = deviceInfo.HardwareVersion |
| 287 | dh.device.FirmwareVersion = deviceInfo.FirmwareVersion |
| 288 | // TODO : Check whether this MAC address is learnt from SDPON or need to send from device |
| 289 | dh.device.MacAddress = "0a:0b:0c:0d:0e:0f" |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 290 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 291 | // Synchronous call to update device - this method is run in its own go routine |
| 292 | if err := dh.coreProxy.DeviceUpdate(nil, dh.device); err != nil { |
| 293 | log.Errorw("error-updating-device", log.Fields{"deviceId": dh.device.Id, "error": err}) |
| 294 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 295 | KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort) |
| 296 | // Instantiate resource manager |
Abhilash S.L | 8ee9071 | 2019-04-29 16:24:22 +0530 | [diff] [blame] | 297 | 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] | 298 | log.Error("Error while instantiating resource manager") |
| 299 | return errors.New("Instantiating resource manager failed") |
| 300 | } |
| 301 | // Instantiate flow manager |
| 302 | if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil { |
| 303 | log.Error("Error while instantiating flow manager") |
| 304 | return errors.New("Instantiating flow manager failed") |
| 305 | } |
| 306 | /* TODO: Instantiate Alarm , stats , BW managers */ |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 307 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 308 | // Start reading indications |
| 309 | go dh.readIndications() |
| 310 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // AdoptDevice adopts the OLT device |
| 314 | func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 315 | dh.transitionMap = NewTransitionMap(dh) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 316 | log.Infow("AdoptDevice", log.Fields{"deviceId": device.Id, "Address": device.GetHostAndPort()}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 317 | dh.transitionMap.Handle(DeviceInit) |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // GetOfpDeviceInfo Get the Ofp device information |
| 321 | func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 322 | return &ic.SwitchCapability{ |
| 323 | Desc: &of.OfpDesc{ |
| 324 | HwDesc: "open_pon", |
| 325 | SwDesc: "open_pon", |
| 326 | SerialNum: dh.device.SerialNumber, |
| 327 | }, |
| 328 | SwitchFeatures: &of.OfpSwitchFeatures{ |
| 329 | NBuffers: 256, |
| 330 | NTables: 2, |
| 331 | Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS | |
| 332 | of.OfpCapabilities_OFPC_TABLE_STATS | |
| 333 | of.OfpCapabilities_OFPC_PORT_STATS | |
| 334 | of.OfpCapabilities_OFPC_GROUP_STATS), |
| 335 | }, |
| 336 | }, nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | // GetOfpPortInfo Get Ofp port information |
| 340 | func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 341 | cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 342 | return &ic.PortCapability{ |
| 343 | Port: &voltha.LogicalPort{ |
| 344 | OfpPort: &of.OfpPort{ |
| 345 | HwAddr: macAddressToUint32Array(dh.device.MacAddress), |
| 346 | Config: 0, |
| 347 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 348 | Curr: cap, |
| 349 | Advertised: cap, |
| 350 | Peer: cap, |
| 351 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 352 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 353 | }, |
| 354 | DeviceId: dh.device.Id, |
| 355 | DevicePortNo: uint32(portNo), |
| 356 | }, |
| 357 | }, nil |
| 358 | } |
| 359 | |
| 360 | func (dh *DeviceHandler) omciIndication(omciInd *oop.OmciIndication) error { |
| 361 | log.Debugw("omci indication", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId}) |
| 362 | |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 363 | // ponPort := IntfIdToPortNo(omciInd.GetIntfId(),voltha.Port_PON_OLT) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 364 | kwargs := make(map[string]interface{}) |
| 365 | kwargs["onu_id"] = omciInd.OnuId |
| 366 | kwargs["parent_port_no"] = omciInd.GetIntfId() |
| 367 | |
| 368 | if onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); err != nil { |
| 369 | log.Errorw("onu not found", log.Fields{"intfId": omciInd.IntfId, "onuId": omciInd.OnuId}) |
| 370 | return err |
| 371 | } else { |
| 372 | omciMsg := &ic.InterAdapterOmciMessage{Message: omciInd.Pkt} |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 373 | if sendErr := dh.AdapterProxy.SendInterAdapterMessage(context.Background(), omciMsg, |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 374 | ic.InterAdapterMessageType_OMCI_REQUEST, dh.deviceType, onuDevice.Type, |
| 375 | onuDevice.Id, onuDevice.ProxyAddress.DeviceId, ""); sendErr != nil { |
| 376 | log.Errorw("send omci request error", log.Fields{"fromAdapter": dh.deviceType, "toAdapter": onuDevice.Type, "onuId": onuDevice.Id, "proxyDeviceId": onuDevice.ProxyAddress.DeviceId}) |
| 377 | return sendErr |
| 378 | } |
| 379 | return nil |
| 380 | } |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // Process_inter_adapter_message process inter adater message |
| 384 | func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 385 | // TODO |
| 386 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id}) |
| 387 | if msg.Header.Type == ic.InterAdapterMessageType_OMCI_REQUEST { |
| 388 | msgId := msg.Header.Id |
| 389 | fromTopic := msg.Header.FromTopic |
| 390 | toTopic := msg.Header.ToTopic |
| 391 | toDeviceId := msg.Header.ToDeviceId |
| 392 | proxyDeviceId := msg.Header.ProxyDeviceId |
| 393 | |
| 394 | log.Debugw("omci request message header", log.Fields{"msgId": msgId, "fromTopic": fromTopic, "toTopic": toTopic, "toDeviceId": toDeviceId, "proxyDeviceId": proxyDeviceId}) |
| 395 | |
| 396 | msgBody := msg.GetBody() |
| 397 | |
| 398 | omciMsg := &ic.InterAdapterOmciMessage{} |
| 399 | if err := ptypes.UnmarshalAny(msgBody, omciMsg); err != nil { |
| 400 | log.Warnw("cannot-unmarshal-omci-msg-body", log.Fields{"error": err}) |
| 401 | return err |
| 402 | } |
| 403 | |
| 404 | if onuDevice, err := dh.coreProxy.GetDevice(nil, dh.device.Id, toDeviceId); err != nil { |
| 405 | log.Errorw("onu not found", log.Fields{"onuDeviceId": toDeviceId, "error": err}) |
| 406 | return err |
| 407 | } else { |
| 408 | dh.sendProxiedMessage(onuDevice, omciMsg) |
| 409 | } |
| 410 | |
| 411 | } else { |
| 412 | log.Errorw("inter-adapter-unhandled-type", log.Fields{"msgType": msg.Header.Type}) |
| 413 | } |
| 414 | return nil |
Phaneendra Manda | 4c62c80 | 2019-03-06 21:37:49 +0530 | [diff] [blame] | 415 | } |
| 416 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 417 | func (dh *DeviceHandler) sendProxiedMessage(onuDevice *voltha.Device, omciMsg *ic.InterAdapterOmciMessage) { |
| 418 | if onuDevice.ConnectStatus != voltha.ConnectStatus_REACHABLE { |
| 419 | log.Debugw("ONU is not reachable, cannot send OMCI", log.Fields{"serialNumber": onuDevice.SerialNumber, "intfId": onuDevice.ProxyAddress.GetChannelId(), "onuId": onuDevice.ProxyAddress.GetOnuId()}) |
| 420 | return |
| 421 | } |
| 422 | |
| 423 | omciMessage := &oop.OmciMsg{IntfId: onuDevice.ProxyAddress.GetChannelId(), OnuId: onuDevice.ProxyAddress.GetOnuId(), Pkt: omciMsg.Message} |
| 424 | |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 425 | dh.Client.OmciMsgOut(context.Background(), omciMessage) |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 426 | log.Debugw("omci-message-sent", log.Fields{"serialNumber": onuDevice.SerialNumber, "intfId": onuDevice.ProxyAddress.GetChannelId(), "omciMsg": string(omciMsg.Message)}) |
| 427 | } |
| 428 | |
| 429 | func (dh *DeviceHandler) activateONU(intfId uint32, onuId int64, serialNum *oop.SerialNumber, serialNumber string) { |
| 430 | log.Debugw("activate-onu", log.Fields{"intfId": intfId, "onuId": onuId, "serialNum": serialNum, "serialNumber": serialNumber}) |
| 431 | // TODO: need resource manager |
| 432 | var pir uint32 = 1000000 |
| 433 | Onu := oop.Onu{IntfId: intfId, OnuId: uint32(onuId), SerialNumber: serialNum, Pir: pir} |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 434 | if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil { |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 435 | log.Errorw("activate-onu-failed", log.Fields{"Onu": Onu}) |
| 436 | } else { |
| 437 | log.Infow("activated-onu", log.Fields{"SerialNumber": serialNumber}) |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, onuId uint32, sn string) error { |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 442 | //channelId := MkUniPortNum(onuDiscInd.GetIntfId(), onuId, uint32(0)) |
| 443 | //parentPortNo := IntfIdToPortNo(onuDiscInd.GetIntfId(),voltha.Port_PON_OLT) |
| 444 | channelId := onuDiscInd.GetIntfId() |
| 445 | parentPortNo := onuDiscInd.GetIntfId() |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 446 | 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] | 447 | log.Errorw("Create onu error", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn, "error": err}) |
| 448 | return err |
| 449 | } |
| 450 | |
| 451 | kwargs := make(map[string]interface{}) |
| 452 | kwargs["onu_id"] = onuId |
| 453 | kwargs["parent_port_no"] = onuDiscInd.GetIntfId() |
| 454 | |
| 455 | for i := 0; i < 10; i++ { |
| 456 | if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil { |
| 457 | dh.activateONU(onuDiscInd.IntfId, int64(onuId), onuDiscInd.SerialNumber, sn) |
| 458 | return nil |
| 459 | } else { |
| 460 | time.Sleep(1 * time.Second) |
| 461 | log.Debugln("Sleep 1 seconds to active onu, retry times ", i+1) |
| 462 | } |
| 463 | } |
| 464 | log.Errorw("Cannot query onu, dont activate it.", log.Fields{"parent_id": dh.device.Id, "ponPort": onuDiscInd.GetIntfId(), "onuId": onuId, "sn": sn}) |
| 465 | return errors.New("Failed to activate onu") |
| 466 | } |
| 467 | |
| 468 | func (dh *DeviceHandler) onuIndication(onuInd *oop.OnuIndication) { |
| 469 | serialNumber := dh.stringifySerialNumber(onuInd.SerialNumber) |
| 470 | |
| 471 | kwargs := make(map[string]interface{}) |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 472 | // ponPort := IntfIdToPortNo(onuInd.GetIntfId(),voltha.Port_PON_OLT) |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 473 | |
cuilin2018 | 7b2a8c3 | 2019-03-26 19:52:28 -0700 | [diff] [blame] | 474 | if serialNumber != "" { |
| 475 | kwargs["serial_number"] = serialNumber |
| 476 | } else { |
| 477 | kwargs["onu_id"] = onuInd.OnuId |
| 478 | kwargs["parent_port_no"] = onuInd.GetIntfId() |
| 479 | } |
| 480 | if onuDevice, _ := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs); onuDevice != nil { |
| 481 | //if intfIdFromPortNo(onuDevice.ParentPortNo) != onuInd.GetIntfId() { |
| 482 | if onuDevice.ParentPortNo != onuInd.GetIntfId() { |
| 483 | //log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": intfIdFromPortNo(onuDevice.ParentPortNo), "currentIntfId": onuInd.GetIntfId()}) |
| 484 | log.Warnw("ONU-is-on-a-different-intf-id-now", log.Fields{"previousIntfId": onuDevice.ParentPortNo, "currentIntfId": onuInd.GetIntfId()}) |
| 485 | } |
| 486 | |
| 487 | if onuDevice.ProxyAddress.OnuId != onuInd.OnuId { |
| 488 | 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}) |
| 489 | } |
| 490 | |
| 491 | // adminState |
| 492 | if onuInd.AdminState == "down" { |
| 493 | if onuInd.OperState != "down" { |
| 494 | log.Errorw("ONU-admin-state-down-and-oper-status-not-down", log.Fields{"operState": onuInd.OperState}) |
| 495 | // Forcing the oper state change code to execute |
| 496 | onuInd.OperState = "down" |
| 497 | } |
| 498 | // Port and logical port update is taken care of by oper state block |
| 499 | } else if onuInd.AdminState == "up" { |
| 500 | log.Debugln("received-onu-admin-state up") |
| 501 | } else { |
| 502 | log.Errorw("Invalid-or-not-implemented-admin-state", log.Fields{"received-admin-state": onuInd.AdminState}) |
| 503 | } |
| 504 | log.Debugln("admin-state-dealt-with") |
| 505 | |
| 506 | // operState |
| 507 | if onuInd.OperState == "down" { |
| 508 | if onuDevice.ConnectStatus != common.ConnectStatus_UNREACHABLE { |
| 509 | dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, onuDevice.OperStatus) |
| 510 | log.Debugln("onu-oper-state-is-down") |
| 511 | } |
| 512 | if onuDevice.OperStatus != common.OperStatus_DISCOVERED { |
| 513 | dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_UNREACHABLE, common.OperStatus_DISCOVERED) |
| 514 | } |
| 515 | log.Debugw("inter-adapter-send-onu-ind", log.Fields{"onuIndication": onuInd}) |
| 516 | |
| 517 | // TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 518 | 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] | 519 | } else if onuInd.OperState == "up" { |
| 520 | if onuDevice.ConnectStatus != common.ConnectStatus_REACHABLE { |
| 521 | dh.coreProxy.DeviceStateUpdate(nil, onuDevice.Id, common.ConnectStatus_REACHABLE, onuDevice.OperStatus) |
| 522 | |
| 523 | } |
| 524 | if onuDevice.OperStatus != common.OperStatus_DISCOVERED { |
| 525 | log.Warnw("ignore onu indication", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId, "operStatus": onuDevice.OperStatus, "msgOperStatus": onuInd.OperState}) |
| 526 | return |
| 527 | } |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 528 | 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] | 529 | } else { |
| 530 | log.Warnw("Not-implemented-or-invalid-value-of-oper-state", log.Fields{"operState": onuInd.OperState}) |
| 531 | } |
| 532 | } else { |
| 533 | log.Errorw("onu not found", log.Fields{"intfId": onuInd.IntfId, "onuId": onuInd.OnuId}) |
| 534 | return |
| 535 | } |
| 536 | |
| 537 | } |
| 538 | |
| 539 | func (dh *DeviceHandler) stringifySerialNumber(serialNum *oop.SerialNumber) string { |
| 540 | if serialNum != nil { |
| 541 | return string(serialNum.VendorId) + dh.stringifyVendorSpecific(serialNum.VendorSpecific) |
| 542 | } else { |
| 543 | return "" |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | func (dh *DeviceHandler) stringifyVendorSpecific(vendorSpecific []byte) string { |
| 548 | tmp := fmt.Sprintf("%x", (uint32(vendorSpecific[0])>>4)&0x0f) + |
| 549 | fmt.Sprintf("%x", (uint32(vendorSpecific[0]&0x0f))) + |
| 550 | fmt.Sprintf("%x", (uint32(vendorSpecific[1])>>4)&0x0f) + |
| 551 | fmt.Sprintf("%x", (uint32(vendorSpecific[1]))&0x0f) + |
| 552 | fmt.Sprintf("%x", (uint32(vendorSpecific[2])>>4)&0x0f) + |
| 553 | fmt.Sprintf("%x", (uint32(vendorSpecific[2]))&0x0f) + |
| 554 | fmt.Sprintf("%x", (uint32(vendorSpecific[3])>>4)&0x0f) + |
| 555 | fmt.Sprintf("%x", (uint32(vendorSpecific[3]))&0x0f) |
| 556 | return tmp |
| 557 | } |
| 558 | |
| 559 | // flows |
| 560 | func (dh *DeviceHandler) Update_flows_bulk() error { |
| 561 | return errors.New("UnImplemented") |
| 562 | } |
Girish Gowdru | 0c588b2 | 2019-04-23 23:24:56 -0400 | [diff] [blame] | 563 | func (dh *DeviceHandler) GetChildDevice(parentPort uint32, onuId uint32) *voltha.Device { |
| 564 | log.Debugw("GetChildDevice", log.Fields{"pon port": parentPort, "onuId": onuId}) |
| 565 | kwargs := make(map[string]interface{}) |
| 566 | kwargs["onu_id"] = onuId |
| 567 | kwargs["parent_port_no"] = parentPort |
| 568 | onuDevice, err := dh.coreProxy.GetChildDevice(nil, dh.device.Id, kwargs) |
| 569 | if err != nil { |
| 570 | log.Errorw("onu not found", log.Fields{"intfId": parentPort, "onuId": onuId}) |
| 571 | return nil |
| 572 | } |
| 573 | log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice}) |
| 574 | return onuDevice |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | 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] | 578 | log.Debugw("In UpdateFlowsIncrementally", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups}) |
| 579 | if flows != nil { |
| 580 | for _, flow := range flows.ToAdd.Items { |
| 581 | dh.flowMgr.AddFlow(flow) |
| 582 | } |
| 583 | } |
| 584 | if groups != nil { |
| 585 | for _, flow := range flows.ToRemove.Items { |
| 586 | log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow}) |
| 587 | // dh.flowMgr.RemoveFlow(flow) |
| 588 | } |
| 589 | } |
| 590 | return nil |
manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame] | 591 | } |