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" |
khenaidoo | 54544ae | 2019-03-18 13:22:39 -0400 | [diff] [blame] | 20 | "fmt" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 21 | "github.com/gogo/protobuf/proto" |
| 22 | com "github.com/opencord/voltha-go/adapters/common" |
| 23 | "github.com/opencord/voltha-go/common/log" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 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" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 27 | "strconv" |
| 28 | "strings" |
| 29 | "sync" |
| 30 | ) |
| 31 | |
| 32 | //DeviceHandler follows the same patterns as ponsim_olt. The only difference is that it does not |
| 33 | // interact with an OLT device. |
| 34 | type DeviceHandler struct { |
| 35 | deviceId string |
| 36 | deviceType string |
| 37 | device *voltha.Device |
| 38 | coreProxy *com.CoreProxy |
| 39 | simulatedOLT *SimulatedONU |
| 40 | uniPort *voltha.Port |
| 41 | ponPort *voltha.Port |
| 42 | exitChannel chan int |
| 43 | lockDevice sync.RWMutex |
| 44 | } |
| 45 | |
| 46 | //NewDeviceHandler creates a new device handler |
| 47 | func NewDeviceHandler(cp *com.CoreProxy, device *voltha.Device, adapter *SimulatedONU) *DeviceHandler { |
| 48 | var dh DeviceHandler |
| 49 | dh.coreProxy = cp |
| 50 | cloned := (proto.Clone(device)).(*voltha.Device) |
| 51 | dh.deviceId = cloned.Id |
| 52 | dh.deviceType = cloned.Type |
| 53 | dh.device = cloned |
| 54 | dh.simulatedOLT = adapter |
| 55 | dh.exitChannel = make(chan int, 1) |
| 56 | dh.lockDevice = sync.RWMutex{} |
| 57 | return &dh |
| 58 | } |
| 59 | |
| 60 | // start save the device to the data model |
| 61 | func (dh *DeviceHandler) start(ctx context.Context) { |
| 62 | dh.lockDevice.Lock() |
| 63 | defer dh.lockDevice.Unlock() |
| 64 | log.Debugw("starting-device-agent", log.Fields{"device": dh.device}) |
| 65 | // Add the initial device to the local model |
| 66 | log.Debug("device-agent-started") |
| 67 | } |
| 68 | |
| 69 | // stop stops the device dh. Not much to do for now |
| 70 | func (dh *DeviceHandler) stop(ctx context.Context) { |
| 71 | dh.lockDevice.Lock() |
| 72 | defer dh.lockDevice.Unlock() |
| 73 | log.Debug("stopping-device-agent") |
| 74 | dh.exitChannel <- 1 |
| 75 | log.Debug("device-agent-stopped") |
| 76 | } |
| 77 | |
| 78 | func macAddressToUint32Array(mac string) []uint32 { |
| 79 | slist := strings.Split(mac, ":") |
| 80 | result := make([]uint32, len(slist)) |
| 81 | var err error |
| 82 | var tmp int64 |
| 83 | for index, val := range slist { |
| 84 | if tmp, err = strconv.ParseInt(val, 16, 32); err != nil { |
| 85 | return []uint32{1, 2, 3, 4, 5, 6} |
| 86 | } |
| 87 | result[index] = uint32(tmp) |
| 88 | } |
| 89 | return result |
| 90 | } |
| 91 | |
| 92 | func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) { |
| 93 | log.Debugw("AdoptDevice", log.Fields{"deviceId": device.Id}) |
| 94 | |
| 95 | // Update the device info |
| 96 | cloned := proto.Clone(device).(*voltha.Device) |
| 97 | cloned.Root = false |
| 98 | cloned.Vendor = "simulators" |
| 99 | cloned.Model = "go-simulators" |
khenaidoo | cee54fd | 2019-03-06 12:03:03 -0500 | [diff] [blame] | 100 | //cloned.SerialNumber = com.GetRandomSerialNumber() |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 101 | cloned.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 102 | |
| 103 | // Synchronous call to update device - this method is run in its own go routine |
| 104 | if err := dh.coreProxy.DeviceUpdate(nil, cloned); err != nil { |
| 105 | log.Errorw("error-updating-device", log.Fields{"deviceId": device.Id, "error": err}) |
| 106 | } |
| 107 | |
khenaidoo | 54544ae | 2019-03-18 13:22:39 -0400 | [diff] [blame] | 108 | // Use the channel Id, assigned by the parent device to me, as the port number |
| 109 | uni_port := uint32(2) |
| 110 | if device.ProxyAddress != nil { |
| 111 | if device.ProxyAddress.ChannelId != 0 { |
| 112 | uni_port = device.ProxyAddress.ChannelId |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Now create the UNI Port |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 117 | dh.uniPort = &voltha.Port{ |
khenaidoo | 54544ae | 2019-03-18 13:22:39 -0400 | [diff] [blame] | 118 | PortNo: uni_port, |
| 119 | Label: fmt.Sprintf("uni-%d", uni_port), |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 120 | Type: voltha.Port_ETHERNET_UNI, |
| 121 | AdminState: voltha.AdminState_ENABLED, |
| 122 | OperStatus: voltha.OperStatus_ACTIVE, |
| 123 | } |
| 124 | |
| 125 | // Synchronous call to update device - this method is run in its own go routine |
| 126 | if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.uniPort); err != nil { |
| 127 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 128 | } |
khenaidoo | ce5969c | 2019-04-26 12:07:21 -0400 | [diff] [blame] | 129 | // use Pon port number on which this ONU has been detected |
| 130 | ponPortNo := uint32(1) |
| 131 | if device.ParentPortNo != 0 { |
| 132 | ponPortNo = device.ParentPortNo |
mkoodali | 252f767 | 2019-03-25 12:13:12 +0530 | [diff] [blame] | 133 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 134 | // Now create the PON Port |
| 135 | dh.ponPort = &voltha.Port{ |
mkoodali | 252f767 | 2019-03-25 12:13:12 +0530 | [diff] [blame] | 136 | PortNo: ponPortNo, |
| 137 | Label: fmt.Sprintf("pon-%d", ponPortNo), |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 138 | Type: voltha.Port_PON_ONU, |
| 139 | AdminState: voltha.AdminState_ENABLED, |
| 140 | OperStatus: voltha.OperStatus_ACTIVE, |
mkoodali | 252f767 | 2019-03-25 12:13:12 +0530 | [diff] [blame] | 141 | Peers: []*voltha.Port_PeerPort{{DeviceId: cloned.ParentId, // Peer device is OLT |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 142 | PortNo: uni_port}}, // Peer port is UNI port |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Synchronous call to update device - this method is run in its own go routine |
| 146 | if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.ponPort); err != nil { |
| 147 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 148 | } |
| 149 | |
| 150 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 151 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 152 | |
| 153 | // Update the device state |
| 154 | if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 155 | log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err}) |
| 156 | } |
| 157 | |
| 158 | dh.device = cloned |
| 159 | } |
| 160 | |
| 161 | func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { |
| 162 | cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 163 | return &ic.PortCapability{ |
| 164 | Port: &voltha.LogicalPort{ |
| 165 | OfpPort: &of.OfpPort{ |
| 166 | HwAddr: macAddressToUint32Array(dh.device.MacAddress), |
| 167 | Config: 0, |
| 168 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 169 | Curr: cap, |
| 170 | Advertised: cap, |
| 171 | Peer: cap, |
| 172 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 173 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 174 | }, |
| 175 | DeviceId: dh.device.Id, |
| 176 | DevicePortNo: uint32(portNo), |
| 177 | }, |
| 178 | }, nil |
| 179 | } |
| 180 | |
| 181 | func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { |
| 182 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id}) |
| 183 | return nil |
| 184 | } |