blob: a84542757465de28f7b22d1ede7ab964d97aaa98 [file] [log] [blame]
khenaidood2b6df92018-12-13 16:37:20 -05001/*
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 */
16package adaptercore
17
18import (
19 "context"
khenaidoo54544ae2019-03-18 13:22:39 -040020 "fmt"
khenaidood2b6df92018-12-13 16:37:20 -050021 "github.com/gogo/protobuf/proto"
22 com "github.com/opencord/voltha-go/adapters/common"
23 "github.com/opencord/voltha-go/common/log"
William Kurkiandaa6bb22019-03-07 12:26:28 -050024 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"
khenaidood2b6df92018-12-13 16:37:20 -050027 "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.
34type 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
47func 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
61func (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
70func (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
78func 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
92func (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"
khenaidoocee54fd2019-03-06 12:03:03 -0500100 //cloned.SerialNumber = com.GetRandomSerialNumber()
khenaidood2b6df92018-12-13 16:37:20 -0500101 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
khenaidoo54544ae2019-03-18 13:22:39 -0400108 // 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
khenaidood2b6df92018-12-13 16:37:20 -0500117 dh.uniPort = &voltha.Port{
khenaidoo54544ae2019-03-18 13:22:39 -0400118 PortNo: uni_port,
119 Label: fmt.Sprintf("uni-%d", uni_port),
khenaidood2b6df92018-12-13 16:37:20 -0500120 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 }
khenaidooce5969c2019-04-26 12:07:21 -0400129 // use Pon port number on which this ONU has been detected
130 ponPortNo := uint32(1)
131 if device.ParentPortNo != 0 {
132 ponPortNo = device.ParentPortNo
mkoodali252f7672019-03-25 12:13:12 +0530133 }
khenaidood2b6df92018-12-13 16:37:20 -0500134 // Now create the PON Port
135 dh.ponPort = &voltha.Port{
mkoodali252f7672019-03-25 12:13:12 +0530136 PortNo: ponPortNo,
137 Label: fmt.Sprintf("pon-%d", ponPortNo),
khenaidood2b6df92018-12-13 16:37:20 -0500138 Type: voltha.Port_PON_ONU,
139 AdminState: voltha.AdminState_ENABLED,
140 OperStatus: voltha.OperStatus_ACTIVE,
mkoodali252f7672019-03-25 12:13:12 +0530141 Peers: []*voltha.Port_PeerPort{{DeviceId: cloned.ParentId, // Peer device is OLT
khenaidoo2c6a0992019-04-29 13:46:56 -0400142 PortNo: uni_port}}, // Peer port is UNI port
khenaidood2b6df92018-12-13 16:37:20 -0500143 }
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
161func (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
181func (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}
khenaidoo3ab34882019-05-02 21:33:30 -0400185
186func (dh *DeviceHandler) DisableDevice(device *voltha.Device) {
187 cloned := proto.Clone(device).(*voltha.Device)
188 // Update the all ports state on that device to disable
189 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
190 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
191 return
192 }
193
194 //Update the device state
195 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
196 cloned.OperStatus = voltha.OperStatus_UNKNOWN
197 dh.device = cloned
198
199 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
200 log.Errorw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
201 return
202 }
203 log.Debugw("DisableDevice-end", log.Fields{"deviceId": device.Id})
204}
205
206func (dh *DeviceHandler) ReEnableDevice(device *voltha.Device) {
207
208 cloned := proto.Clone(device).(*voltha.Device)
209 // Update the all ports state on that device to enable
210 if err := dh.coreProxy.PortsStateUpdate(nil, cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
211 log.Errorw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
212 return
213 }
214
215 //Update the device state
216 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
217 cloned.OperStatus = voltha.OperStatus_ACTIVE
218 dh.device = cloned
219
220 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
221 log.Errorw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
222 return
223 }
224 log.Debugw("ReEnableDevice-end", log.Fields{"deviceId": device.Id})
225}