blob: 66a3538422d77fbe49baad4cd0d9826e9127fea8 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001/*
2 * Copyright 2019-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 mocks
17
18import (
19 "context"
20 "fmt"
21 "github.com/gogo/protobuf/proto"
22 "github.com/opencord/voltha-lib-go/v2/pkg/adapters/adapterif"
23 com "github.com/opencord/voltha-lib-go/v2/pkg/adapters/common"
24 "github.com/opencord/voltha-lib-go/v2/pkg/log"
25 ic "github.com/opencord/voltha-protos/v2/go/inter_container"
26 of "github.com/opencord/voltha-protos/v2/go/openflow_13"
27 "github.com/opencord/voltha-protos/v2/go/voltha"
28 "strings"
29)
30
31type ONUAdapter struct {
32 coreProxy adapterif.CoreProxy
33 Adapter
34}
35
36func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter {
37 a := &ONUAdapter{}
38 a.coreProxy = cp
39 return a
40}
41
42func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error {
43 go func() {
44 d := proto.Clone(device).(*voltha.Device)
45 d.Root = false
46 d.Vendor = "onu_adapter_mock"
47 d.Model = "go-mock"
48 d.SerialNumber = com.GetRandomSerialNumber()
49 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
50 onuA.storeDevice(d)
51 if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
52 log.Fatalf("deviceUpdate-failed-%s", res)
53 }
54 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
55 d.OperStatus = voltha.OperStatus_DISCOVERED
56
57 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
58 log.Fatalf("device-state-update-failed-%s", err)
59 }
60
61 uniPortNo := uint32(2)
62 if device.ProxyAddress != nil {
63 if device.ProxyAddress.ChannelId != 0 {
64 uniPortNo = device.ProxyAddress.ChannelId
65 }
66 }
67
68 uniPort := &voltha.Port{
69 PortNo: uniPortNo,
70 Label: fmt.Sprintf("uni-%d", uniPortNo),
71 Type: voltha.Port_ETHERNET_UNI,
72 OperStatus: voltha.OperStatus_ACTIVE,
73 }
74 var err error
75 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
76 log.Fatalf("PortCreated-failed-%s", err)
77 }
78
79 ponPortNo := uint32(1)
80 if device.ParentPortNo != 0 {
81 ponPortNo = device.ParentPortNo
82 }
83
84 ponPort := &voltha.Port{
85 PortNo: ponPortNo,
86 Label: fmt.Sprintf("pon-%d", ponPortNo),
87 Type: voltha.Port_PON_ONU,
88 OperStatus: voltha.OperStatus_ACTIVE,
89 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
90 PortNo: uniPortNo}}, // Peer port is UNI port
91 }
92 if err := onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
93 log.Fatalf("PortCreated-failed-%s", err)
94 }
95
96 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
97 d.OperStatus = voltha.OperStatus_ACTIVE
98
99 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
100 log.Fatalf("device-state-update-failed-%s", err)
101 }
102 //Get the latest device data from the Core
103 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
104 log.Fatalf("getting-device-failed-%s", err)
105 }
106
107 if err = onuA.updateDevice(d); err != nil {
108 log.Fatalf("saving-device-failed-%s", err)
109 }
110 }()
111 return nil
112}
113
114func (onuA *ONUAdapter) Get_ofp_port_info(device *voltha.Device, port_no int64) (*ic.PortCapability, error) {
115 if d := onuA.getDevice(device.Id); d == nil {
116 log.Fatalf("device-not-found-%s", device.Id)
117 }
118 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
119 return &ic.PortCapability{
120 Port: &voltha.LogicalPort{
121 OfpPort: &of.OfpPort{
122 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
123 Config: 0,
124 State: uint32(of.OfpPortState_OFPPS_LIVE),
125 Curr: capability,
126 Advertised: capability,
127 Peer: capability,
128 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
129 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
130 },
131 DeviceId: device.Id,
132 DevicePortNo: uint32(port_no),
133 },
134 }, nil
135}
136
137func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error {
138 go func() {
139 if d := onuA.getDevice(device.Id); d == nil {
140 log.Fatalf("device-not-found-%s", device.Id)
141 }
142 cloned := proto.Clone(device).(*voltha.Device)
143 // Update the all ports state on that device to disable
144 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
145 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
146 }
147 //Update the device state
148 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
149 cloned.OperStatus = voltha.OperStatus_UNKNOWN
150
151 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
152 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
153 }
154 if err := onuA.updateDevice(cloned); err != nil {
155 log.Fatalf("saving-device-failed-%s", err)
156 }
157 }()
158 return nil
159}
160
161func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error {
162 go func() {
163 if d := onuA.getDevice(device.Id); d == nil {
164 log.Fatalf("device-not-found-%s", device.Id)
165 }
166
167 cloned := proto.Clone(device).(*voltha.Device)
168 // Update the all ports state on that device to enable
169 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
170 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
171 }
172
173 //Update the device state
174 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
175 cloned.OperStatus = voltha.OperStatus_ACTIVE
176
177 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
178 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
179 }
180 if err := onuA.updateDevice(cloned); err != nil {
181 log.Fatalf("saving-device-failed-%s", err)
182 }
183 }()
184 return nil
185}