Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [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 ( |
| 19 | "context" |
| 20 | "fmt" |
| 21 | "github.com/gogo/protobuf/proto" |
| 22 | com "github.com/opencord/voltha-go/adapters/common" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | ic "github.com/opencord/voltha-protos/go/inter_container" |
| 25 | of "github.com/opencord/voltha-protos/go/openflow_13" |
| 26 | "github.com/opencord/voltha-protos/go/voltha" |
| 27 | "strconv" |
| 28 | "strings" |
| 29 | "sync" |
| 30 | "time" |
| 31 | ) |
| 32 | |
| 33 | // A set of pm names to create the initial pm config. This is used only for testing in this simulated adapter |
| 34 | var pmNames = []string{ |
| 35 | "tx_64_pkts", |
| 36 | "tx_65_127_pkts", |
| 37 | "tx_128_255_pkts", |
| 38 | "tx_1024_1518_pkts", |
| 39 | "tx_1519_9k_pkts", |
| 40 | "rx_64_pkts", |
| 41 | "rx_64_pkts", |
| 42 | "rx_65_127_pkts", |
| 43 | "rx_128_255_pkts", |
| 44 | "rx_1024_1518_pkts", |
| 45 | "rx_1519_9k_pkts", |
| 46 | } |
| 47 | |
| 48 | //DeviceHandler follows the same patterns as ponsim_olt. The only difference is that it does not |
| 49 | // interact with an OLT device. |
| 50 | type DeviceHandler struct { |
| 51 | deviceId string |
| 52 | deviceType string |
| 53 | device *voltha.Device |
| 54 | coreProxy *com.CoreProxy |
| 55 | simulatedOLT *SimulatedONU |
| 56 | uniPort *voltha.Port |
| 57 | ponPort *voltha.Port |
| 58 | exitChannel chan int |
| 59 | lockDevice sync.RWMutex |
| 60 | metrics *com.PmMetrics |
| 61 | } |
| 62 | |
| 63 | //NewDeviceHandler creates a new device handler |
| 64 | func NewDeviceHandler(cp *com.CoreProxy, device *voltha.Device, adapter *SimulatedONU) *DeviceHandler { |
| 65 | var dh DeviceHandler |
| 66 | dh.coreProxy = cp |
| 67 | cloned := (proto.Clone(device)).(*voltha.Device) |
| 68 | dh.deviceId = cloned.Id |
| 69 | dh.deviceType = cloned.Type |
| 70 | dh.device = cloned |
| 71 | dh.simulatedOLT = adapter |
| 72 | dh.exitChannel = make(chan int, 1) |
| 73 | dh.lockDevice = sync.RWMutex{} |
| 74 | dh.metrics = com.NewPmMetrics( |
| 75 | cloned.Id, |
| 76 | com.Frequency(150), |
| 77 | com.Grouped(false), |
| 78 | com.FrequencyOverride(false), |
| 79 | com.Metrics(pmNames), |
| 80 | ) |
| 81 | return &dh |
| 82 | } |
| 83 | |
| 84 | // start save the device to the data model |
| 85 | func (dh *DeviceHandler) start(ctx context.Context) { |
| 86 | dh.lockDevice.Lock() |
| 87 | defer dh.lockDevice.Unlock() |
| 88 | log.Debugw("starting-device-agent", log.Fields{"device": dh.device}) |
| 89 | // Add the initial device to the local model |
| 90 | log.Debug("device-agent-started") |
| 91 | } |
| 92 | |
| 93 | // stop stops the device dh. Not much to do for now |
| 94 | func (dh *DeviceHandler) stop(ctx context.Context) { |
| 95 | dh.lockDevice.Lock() |
| 96 | defer dh.lockDevice.Unlock() |
| 97 | log.Debug("stopping-device-agent") |
| 98 | dh.exitChannel <- 1 |
| 99 | log.Debug("device-agent-stopped") |
| 100 | } |
| 101 | |
| 102 | func macAddressToUint32Array(mac string) []uint32 { |
| 103 | slist := strings.Split(mac, ":") |
| 104 | result := make([]uint32, len(slist)) |
| 105 | var err error |
| 106 | var tmp int64 |
| 107 | for index, val := range slist { |
| 108 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 109 | return []uint32{1, 2, 3, 4, 5, 6} |
| 110 | } |
| 111 | result[index] = uint32(tmp) |
| 112 | } |
| 113 | return result |
| 114 | } |
| 115 | |
| 116 | func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) { |
| 117 | log.Debugw("AdoptDevice", log.Fields{"deviceId": device.Id}) |
| 118 | |
| 119 | // Update the device info |
| 120 | cloned := proto.Clone(device).(*voltha.Device) |
| 121 | cloned.Root = false |
| 122 | cloned.Vendor = "simulators" |
| 123 | cloned.Model = "go-simulators" |
| 124 | //cloned.SerialNumber = com.GetRandomSerialNumber() |
| 125 | cloned.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 126 | |
| 127 | // Synchronous call to update device - this method is run in its own go routine |
| 128 | if err := dh.coreProxy.DeviceUpdate(nil, cloned); err != nil { |
| 129 | log.Errorw("error-updating-device", log.Fields{"deviceId": device.Id, "error": err}) |
| 130 | } |
| 131 | |
| 132 | // Update the device state to DISCOVERED |
| 133 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 134 | cloned.OperStatus = voltha.OperStatus_DISCOVERED |
| 135 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 136 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 137 | } |
| 138 | |
| 139 | // Now, set the initial PM configuration for that device |
| 140 | if err := dh.coreProxy.DevicePMConfigUpdate(nil, dh.metrics.ToPmConfigs()); err != nil { |
| 141 | log.Errorw("error-updating-PMs", log.Fields{"deviceId": device.Id, "error": err}) |
| 142 | } |
| 143 | |
| 144 | // Sleep to mimic the omci management channel creation with the OLT |
| 145 | time.Sleep(10 * time.Millisecond) |
| 146 | |
| 147 | // Update the device state to ACTIVATING |
| 148 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 149 | cloned.OperStatus = voltha.OperStatus_ACTIVATING |
| 150 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 151 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 152 | } |
| 153 | |
| 154 | // Sleep to mimic the omci discovery ( usually takes a few seconds but for ease of simulated environment we are |
| 155 | // setting it to 100ms only. |
| 156 | time.Sleep(100 * time.Millisecond) |
| 157 | |
| 158 | // Use the channel Id, assigned by the parent device to me, as the port number |
| 159 | uni_port := uint32(2) |
| 160 | if device.ProxyAddress != nil { |
| 161 | if device.ProxyAddress.ChannelId != 0 { |
| 162 | uni_port = device.ProxyAddress.ChannelId |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Now create the UNI Port |
| 167 | dh.uniPort = &voltha.Port{ |
| 168 | PortNo: uni_port, |
| 169 | Label: fmt.Sprintf("uni-%d", uni_port), |
| 170 | Type: voltha.Port_ETHERNET_UNI, |
| 171 | AdminState: voltha.AdminState_ENABLED, |
| 172 | OperStatus: voltha.OperStatus_ACTIVE, |
| 173 | } |
| 174 | |
| 175 | // Synchronous call to update device - this method is run in its own go routine |
| 176 | if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.uniPort); err != nil { |
| 177 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 178 | } |
| 179 | // use Pon port number on which this ONU has been detected |
| 180 | ponPortNo := uint32(1) |
| 181 | if device.ParentPortNo != 0 { |
| 182 | ponPortNo = device.ParentPortNo |
| 183 | } |
| 184 | // Now create the PON Port |
| 185 | dh.ponPort = &voltha.Port{ |
| 186 | PortNo: ponPortNo, |
| 187 | Label: fmt.Sprintf("pon-%d", ponPortNo), |
| 188 | Type: voltha.Port_PON_ONU, |
| 189 | AdminState: voltha.AdminState_ENABLED, |
| 190 | OperStatus: voltha.OperStatus_ACTIVE, |
| 191 | Peers: []*voltha.Port_PeerPort{{DeviceId: cloned.ParentId, // Peer device is OLT |
| 192 | PortNo: uni_port}}, // Peer port is UNI port |
| 193 | } |
| 194 | |
| 195 | // Synchronous call to update device - this method is run in its own go routine |
| 196 | if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.ponPort); err != nil { |
| 197 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 198 | } |
| 199 | |
| 200 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 201 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 202 | |
| 203 | // Update the device state |
| 204 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 205 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 206 | } |
| 207 | |
| 208 | dh.device = cloned |
| 209 | } |
| 210 | |
| 211 | func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { |
| 212 | cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 213 | return &ic.PortCapability{ |
| 214 | Port: &voltha.LogicalPort{ |
| 215 | OfpPort: &of.OfpPort{ |
| 216 | HwAddr: macAddressToUint32Array(dh.device.MacAddress), |
| 217 | Config: 0, |
| 218 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 219 | Curr: cap, |
| 220 | Advertised: cap, |
| 221 | Peer: cap, |
| 222 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 223 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 224 | }, |
| 225 | DeviceId: dh.device.Id, |
| 226 | DevicePortNo: uint32(portNo), |
| 227 | }, |
| 228 | }, nil |
| 229 | } |
| 230 | |
| 231 | func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { |
| 232 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id}) |
| 233 | return nil |
| 234 | } |
| 235 | |
| 236 | func (dh *DeviceHandler) DisableDevice(device *voltha.Device) { |
| 237 | cloned := proto.Clone(device).(*voltha.Device) |
| 238 | // Update the all ports state on that device to disable |
| 239 | if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
| 240 | log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 241 | return |
| 242 | } |
| 243 | |
| 244 | //Update the device state |
| 245 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
| 246 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 247 | dh.device = cloned |
| 248 | |
| 249 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 250 | log.Errorw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 251 | return |
| 252 | } |
| 253 | log.Debugw("DisableDevice-end", log.Fields{"deviceId": device.Id}) |
| 254 | } |
| 255 | |
| 256 | func (dh *DeviceHandler) ReEnableDevice(device *voltha.Device) { |
| 257 | |
| 258 | cloned := proto.Clone(device).(*voltha.Device) |
| 259 | // Update the all ports state on that device to enable |
| 260 | if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 261 | log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 262 | return |
| 263 | } |
| 264 | |
| 265 | //Update the device state |
| 266 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 267 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 268 | dh.device = cloned |
| 269 | |
| 270 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 271 | log.Errorw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 272 | return |
| 273 | } |
| 274 | log.Debugw("ReEnableDevice-end", log.Fields{"deviceId": device.Id}) |
| 275 | } |
| 276 | |
| 277 | func (dh *DeviceHandler) DeleteDevice(device *voltha.Device) { |
| 278 | cloned := proto.Clone(device).(*voltha.Device) |
| 279 | // Update the all ports state on that device to disable |
| 280 | if err := dh.coreProxy.DeleteAllPorts(nil, cloned.Id); err != nil { |
| 281 | log.Errorw("delete-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 282 | return |
| 283 | } |
| 284 | |
| 285 | log.Debugw("DeleteDevice-end", log.Fields{"deviceId": device.Id}) |
| 286 | } |
| 287 | |
| 288 | func (dh *DeviceHandler) UpdateFlowsBulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, metadata *voltha.FlowMetadata) { |
| 289 | log.Debugw("UpdateFlowsBulk", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups}) |
| 290 | // For now we do nothing with it |
| 291 | return |
| 292 | } |
| 293 | |
| 294 | func (dh *DeviceHandler) UpdateFlowsIncremental(device *voltha.Device, flowChanges *of.FlowChanges, groupChanges *of.FlowGroupChanges, metadata *voltha.FlowMetadata) { |
| 295 | log.Debugw("UpdateFlowsIncremental", log.Fields{"deviceId": device.Id, "flowChanges": flowChanges, "groupChanges": groupChanges}) |
| 296 | // For now we do nothing with it |
| 297 | return |
| 298 | } |
| 299 | |
| 300 | func (dh *DeviceHandler) UpdatePmConfigs(device *voltha.Device, pmConfigs *voltha.PmConfigs) { |
| 301 | log.Debugw("UpdatePmConfigs", log.Fields{"deviceId": device.Id, "pmConfigs": pmConfigs}) |
| 302 | // For now we do nothing with it |
| 303 | return |
| 304 | } |
| 305 | |
| 306 | func (dh *DeviceHandler) ReconcileDevice(device *voltha.Device) { |
| 307 | // Update the device info |
| 308 | cloned := proto.Clone(device).(*voltha.Device) |
| 309 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 310 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 311 | |
| 312 | dh.device = cloned |
| 313 | |
| 314 | // Update the device state |
| 315 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 316 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 317 | } |
| 318 | } |