khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [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 | "github.com/gogo/protobuf/proto" |
| 21 | com "github.com/opencord/voltha-go/adapters/common" |
| 22 | "github.com/opencord/voltha-go/common/log" |
| 23 | ic "github.com/opencord/voltha-go/protos/inter_container" |
| 24 | of "github.com/opencord/voltha-go/protos/openflow_13" |
| 25 | "github.com/opencord/voltha-go/protos/voltha" |
| 26 | "strconv" |
| 27 | "strings" |
| 28 | "sync" |
| 29 | ) |
| 30 | |
| 31 | //DeviceHandler follows the same patterns as ponsim_olt. The only difference is that it does not |
| 32 | // interact with an OLT device. |
| 33 | type DeviceHandler struct { |
| 34 | deviceId string |
| 35 | deviceType string |
| 36 | device *voltha.Device |
| 37 | coreProxy *com.CoreProxy |
| 38 | simulatedOLT *SimulatedOLT |
| 39 | nniPort *voltha.Port |
| 40 | ponPort *voltha.Port |
| 41 | exitChannel chan int |
| 42 | lockDevice sync.RWMutex |
| 43 | } |
| 44 | |
| 45 | //NewDeviceHandler creates a new device handler |
| 46 | func NewDeviceHandler(cp *com.CoreProxy, device *voltha.Device, adapter *SimulatedOLT) *DeviceHandler { |
| 47 | var dh DeviceHandler |
| 48 | dh.coreProxy = cp |
| 49 | cloned := (proto.Clone(device)).(*voltha.Device) |
| 50 | dh.deviceId = cloned.Id |
| 51 | dh.deviceType = cloned.Type |
| 52 | dh.device = cloned |
| 53 | dh.simulatedOLT = adapter |
| 54 | dh.exitChannel = make(chan int, 1) |
| 55 | dh.lockDevice = sync.RWMutex{} |
| 56 | return &dh |
| 57 | } |
| 58 | |
| 59 | // start save the device to the data model |
| 60 | func (dh *DeviceHandler) start(ctx context.Context) { |
| 61 | dh.lockDevice.Lock() |
| 62 | defer dh.lockDevice.Unlock() |
| 63 | log.Debugw("starting-device-agent", log.Fields{"device": dh.device}) |
| 64 | // Add the initial device to the local model |
| 65 | log.Debug("device-agent-started") |
| 66 | } |
| 67 | |
| 68 | // stop stops the device dh. Not much to do for now |
| 69 | func (dh *DeviceHandler) stop(ctx context.Context) { |
| 70 | dh.lockDevice.Lock() |
| 71 | defer dh.lockDevice.Unlock() |
| 72 | log.Debug("stopping-device-agent") |
| 73 | dh.exitChannel <- 1 |
| 74 | log.Debug("device-agent-stopped") |
| 75 | } |
| 76 | |
| 77 | func macAddressToUint32Array(mac string) []uint32 { |
| 78 | slist := strings.Split(mac, ":") |
| 79 | result := make([]uint32, len(slist)) |
| 80 | var err error |
| 81 | var tmp int64 |
| 82 | for index, val := range slist { |
| 83 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 84 | return []uint32{1, 2, 3, 4, 5, 6} |
| 85 | } |
| 86 | result[index] = uint32(tmp) |
| 87 | } |
| 88 | return result |
| 89 | } |
| 90 | |
| 91 | func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) { |
| 92 | log.Debugw("AdoptDevice", log.Fields{"deviceId": device.Id}) |
| 93 | |
| 94 | // Update the device info |
| 95 | cloned := proto.Clone(device).(*voltha.Device) |
| 96 | cloned.Root = true |
| 97 | cloned.Vendor = "simulators" |
| 98 | cloned.Model = "go-simulators" |
| 99 | cloned.SerialNumber = com.GetRandomSerialNumber() |
| 100 | cloned.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 101 | |
| 102 | // Synchronous call to update device - this method is run in its own go routine |
| 103 | if err := dh.coreProxy.DeviceUpdate(nil, cloned); err != nil { |
| 104 | log.Errorw("error-updating-device", log.Fields{"deviceId": device.Id, "error": err}) |
| 105 | } |
| 106 | |
| 107 | // Now create the NNI Port |
| 108 | dh.nniPort = &voltha.Port{ |
| 109 | PortNo: 2, |
| 110 | Label: "NNI facing Ethernet port", |
| 111 | Type: voltha.Port_ETHERNET_NNI, |
| 112 | OperStatus: voltha.OperStatus_ACTIVE, |
| 113 | } |
| 114 | |
| 115 | // Synchronous call to update device - this method is run in its own go routine |
| 116 | if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.nniPort); err != nil { |
| 117 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 118 | } |
| 119 | |
| 120 | // Now create the PON Port |
| 121 | dh.ponPort = &voltha.Port{ |
| 122 | PortNo: 1, |
| 123 | Label: "PON port", |
| 124 | Type: voltha.Port_PON_OLT, |
| 125 | OperStatus: voltha.OperStatus_ACTIVE, |
| 126 | } |
| 127 | |
| 128 | // Synchronous call to update device - this method is run in its own go routine |
| 129 | if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.ponPort); err != nil { |
| 130 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 131 | } |
| 132 | |
| 133 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 134 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 135 | |
| 136 | // Update the device state |
| 137 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 138 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 139 | } |
| 140 | |
| 141 | // Register Child device |
| 142 | initialUniPortNo := 100 |
| 143 | log.Debugw("registering-onus", log.Fields{"total": dh.simulatedOLT.numOnus}) |
| 144 | for i := 0; i < dh.simulatedOLT.numOnus; i++ { |
| 145 | go dh.coreProxy.ChildDeviceDetected( |
| 146 | nil, |
| 147 | cloned.Id, |
| 148 | 1, |
| 149 | "simulated_onu", |
| 150 | initialUniPortNo+i) |
| 151 | } |
| 152 | dh.device = cloned |
| 153 | } |
| 154 | |
| 155 | func (dh *DeviceHandler) GetOfpDeviceInfo(device *voltha.Device) (*ic.SwitchCapability, error) { |
| 156 | return &ic.SwitchCapability{ |
| 157 | Desc: &of.OfpDesc{ |
| 158 | HwDesc: "simulated_pon", |
| 159 | SwDesc: "simulated_pon", |
| 160 | SerialNum: dh.device.SerialNumber, |
| 161 | }, |
| 162 | SwitchFeatures: &of.OfpSwitchFeatures{ |
| 163 | NBuffers: 256, |
| 164 | NTables: 2, |
| 165 | Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS | |
| 166 | of.OfpCapabilities_OFPC_TABLE_STATS | |
| 167 | of.OfpCapabilities_OFPC_PORT_STATS | |
| 168 | of.OfpCapabilities_OFPC_GROUP_STATS), |
| 169 | }, |
| 170 | }, nil |
| 171 | } |
| 172 | |
| 173 | func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { |
| 174 | cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 175 | return &ic.PortCapability{ |
| 176 | Port: &voltha.LogicalPort{ |
| 177 | OfpPort: &of.OfpPort{ |
| 178 | HwAddr: macAddressToUint32Array(dh.device.MacAddress), |
| 179 | Config: 0, |
| 180 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 181 | Curr: cap, |
| 182 | Advertised: cap, |
| 183 | Peer: cap, |
| 184 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 185 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 186 | }, |
| 187 | DeviceId: dh.device.Id, |
| 188 | DevicePortNo: uint32(portNo), |
| 189 | }, |
| 190 | }, nil |
| 191 | } |
| 192 | |
| 193 | func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { |
| 194 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id}) |
| 195 | return nil |
| 196 | } |