blob: 2e3271c145dffc00c6a54ce80dd611c8bb6dfc66 [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 }
129
130 // Now create the PON Port
131 dh.ponPort = &voltha.Port{
132 PortNo: 1,
khenaidoo54544ae2019-03-18 13:22:39 -0400133 Label: fmt.Sprintf("pon-%d", 1),
khenaidood2b6df92018-12-13 16:37:20 -0500134 Type: voltha.Port_PON_ONU,
135 AdminState: voltha.AdminState_ENABLED,
136 OperStatus: voltha.OperStatus_ACTIVE,
137 Peers: []*voltha.Port_PeerPort{{DeviceId: cloned.ParentId,
138 PortNo: cloned.ParentPortNo}},
139 }
140
141 // Synchronous call to update device - this method is run in its own go routine
142 if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.ponPort); err != nil {
143 log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err})
144 }
145
146 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
147 cloned.OperStatus = voltha.OperStatus_ACTIVE
148
149 // Update the device state
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 dh.device = cloned
155}
156
157func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
158 cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
159 return &ic.PortCapability{
160 Port: &voltha.LogicalPort{
161 OfpPort: &of.OfpPort{
162 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
163 Config: 0,
164 State: uint32(of.OfpPortState_OFPPS_LIVE),
165 Curr: cap,
166 Advertised: cap,
167 Peer: cap,
168 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
169 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
170 },
171 DeviceId: dh.device.Id,
172 DevicePortNo: uint32(portNo),
173 },
174 }, nil
175}
176
177func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
178 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
179 return nil
180}