blob: 51a0667524c0cff5e806e44f456090a8948dbbe7 [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"
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.
33type DeviceHandler struct {
34 deviceId string
35 deviceType string
36 device *voltha.Device
37 coreProxy *com.CoreProxy
38 simulatedOLT *SimulatedONU
39 uniPort *voltha.Port
40 ponPort *voltha.Port
41 exitChannel chan int
42 lockDevice sync.RWMutex
43}
44
45//NewDeviceHandler creates a new device handler
46func NewDeviceHandler(cp *com.CoreProxy, device *voltha.Device, adapter *SimulatedONU) *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
60func (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
69func (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
77func 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
91func (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 = false
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.uniPort = &voltha.Port{
109 PortNo: 2,
110 Label: "UNI facing Ethernet port",
111 Type: voltha.Port_ETHERNET_UNI,
112 AdminState: voltha.AdminState_ENABLED,
113 OperStatus: voltha.OperStatus_ACTIVE,
114 }
115
116 // Synchronous call to update device - this method is run in its own go routine
117 if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.uniPort); err != nil {
118 log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err})
119 }
120
121 // Now create the PON Port
122 dh.ponPort = &voltha.Port{
123 PortNo: 1,
124 Label: "PON port",
125 Type: voltha.Port_PON_ONU,
126 AdminState: voltha.AdminState_ENABLED,
127 OperStatus: voltha.OperStatus_ACTIVE,
128 Peers: []*voltha.Port_PeerPort{{DeviceId: cloned.ParentId,
129 PortNo: cloned.ParentPortNo}},
130 }
131
132 // Synchronous call to update device - this method is run in its own go routine
133 if err := dh.coreProxy.PortCreated(nil, cloned.Id, dh.ponPort); err != nil {
134 log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err})
135 }
136
137 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
138 cloned.OperStatus = voltha.OperStatus_ACTIVE
139
140 // Update the device state
141 if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
142 log.Errorw("error-creating-nni-port", log.Fields{"deviceId": device.Id, "error": err})
143 }
144
145 dh.device = cloned
146}
147
148func (dh *DeviceHandler) GetOfpPortInfo(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
149 cap := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
150 return &ic.PortCapability{
151 Port: &voltha.LogicalPort{
152 OfpPort: &of.OfpPort{
153 HwAddr: macAddressToUint32Array(dh.device.MacAddress),
154 Config: 0,
155 State: uint32(of.OfpPortState_OFPPS_LIVE),
156 Curr: cap,
157 Advertised: cap,
158 Peer: cap,
159 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
160 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
161 },
162 DeviceId: dh.device.Id,
163 DevicePortNo: uint32(portNo),
164 },
165 }, nil
166}
167
168func (dh *DeviceHandler) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
169 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
170 return nil
171}