blob: ea02210ad6c6acc1048ce15268a24f26f687aecf [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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidooab1f7bd2019-11-14 14:00:27 -050017package mocks
18
19import (
20 "context"
21 "fmt"
npujar1d86a522019-11-14 17:11:16 +053022 "strings"
23
khenaidooab1f7bd2019-11-14 14:00:27 -050024 "github.com/gogo/protobuf/proto"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
26 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
27 "github.com/opencord/voltha-lib-go/v3/pkg/log"
28 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
29 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
30 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050031)
32
npujar1d86a522019-11-14 17:11:16 +053033// ONUAdapter represent ONU adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050034type ONUAdapter struct {
35 coreProxy adapterif.CoreProxy
36 Adapter
37}
38
npujar1d86a522019-11-14 17:11:16 +053039// NewONUAdapter creates ONU adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050040func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter {
41 a := &ONUAdapter{}
42 a.coreProxy = cp
43 return a
44}
45
npujar1d86a522019-11-14 17:11:16 +053046// Adopt_device creates new handler for added device
47func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050048 go func() {
49 d := proto.Clone(device).(*voltha.Device)
50 d.Root = false
51 d.Vendor = "onu_adapter_mock"
52 d.Model = "go-mock"
53 d.SerialNumber = com.GetRandomSerialNumber()
54 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
55 onuA.storeDevice(d)
56 if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
57 log.Fatalf("deviceUpdate-failed-%s", res)
58 }
khenaidooab1f7bd2019-11-14 14:00:27 -050059
khenaidoo93181522020-01-23 12:43:21 -050060 // Updating the device states twice, once with oper status to discovered and followed by active may cause
61 // a failure for unit tests when these requests reaches the Core within a millisecond of each other (with real
62 // hardware will not happen as the time between these requests is much higher than 1 millisecond). For
63 // some reasons this issue is seen on Jenkins but not when running the tests locally. The issue
64 // in the core is triggered when these requests are processed out of order (an issue in the Core that is
65 // being handled by https://jira.opencord.org/browse/VOL-2164).
66 // TODO: Once the above change is completed then this code can be uncommented.
67
68 //d.ConnectStatus = voltha.ConnectStatus_REACHABLE
69 //d.OperStatus = voltha.OperStatus_DISCOVERED
70
71 //if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
72 // log.Fatalf("device-state-update-failed-%s", err)
73 //}
khenaidooab1f7bd2019-11-14 14:00:27 -050074
75 uniPortNo := uint32(2)
76 if device.ProxyAddress != nil {
77 if device.ProxyAddress.ChannelId != 0 {
78 uniPortNo = device.ProxyAddress.ChannelId
79 }
80 }
81
82 uniPort := &voltha.Port{
83 PortNo: uniPortNo,
84 Label: fmt.Sprintf("uni-%d", uniPortNo),
85 Type: voltha.Port_ETHERNET_UNI,
86 OperStatus: voltha.OperStatus_ACTIVE,
87 }
88 var err error
89 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
90 log.Fatalf("PortCreated-failed-%s", err)
91 }
92
93 ponPortNo := uint32(1)
94 if device.ParentPortNo != 0 {
95 ponPortNo = device.ParentPortNo
96 }
97
98 ponPort := &voltha.Port{
99 PortNo: ponPortNo,
100 Label: fmt.Sprintf("pon-%d", ponPortNo),
101 Type: voltha.Port_PON_ONU,
102 OperStatus: voltha.OperStatus_ACTIVE,
103 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -0500104 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -0500105 }
npujar1d86a522019-11-14 17:11:16 +0530106 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
khenaidooab1f7bd2019-11-14 14:00:27 -0500107 log.Fatalf("PortCreated-failed-%s", err)
108 }
109
110 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
111 d.OperStatus = voltha.OperStatus_ACTIVE
112
113 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
114 log.Fatalf("device-state-update-failed-%s", err)
115 }
116 //Get the latest device data from the Core
117 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
118 log.Fatalf("getting-device-failed-%s", err)
119 }
120
121 if err = onuA.updateDevice(d); err != nil {
122 log.Fatalf("saving-device-failed-%s", err)
123 }
124 }()
125 return nil
126}
127
npujar1d86a522019-11-14 17:11:16 +0530128// Get_ofp_port_info returns ofp device info
129func (onuA *ONUAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500130 if d := onuA.getDevice(device.Id); d == nil {
131 log.Fatalf("device-not-found-%s", device.Id)
132 }
133 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
134 return &ic.PortCapability{
135 Port: &voltha.LogicalPort{
136 OfpPort: &of.OfpPort{
137 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
138 Config: 0,
139 State: uint32(of.OfpPortState_OFPPS_LIVE),
140 Curr: capability,
141 Advertised: capability,
142 Peer: capability,
143 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
144 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
145 },
146 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530147 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500148 },
149 }, nil
150}
151
npujar1d86a522019-11-14 17:11:16 +0530152// Disable_device disables device
153func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500154 go func() {
155 if d := onuA.getDevice(device.Id); d == nil {
156 log.Fatalf("device-not-found-%s", device.Id)
157 }
158 cloned := proto.Clone(device).(*voltha.Device)
159 // Update the all ports state on that device to disable
160 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
161 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
162 }
163 //Update the device state
164 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
165 cloned.OperStatus = voltha.OperStatus_UNKNOWN
166
167 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
168 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
169 }
170 if err := onuA.updateDevice(cloned); err != nil {
171 log.Fatalf("saving-device-failed-%s", err)
172 }
173 }()
174 return nil
175}
176
npujar1d86a522019-11-14 17:11:16 +0530177// Reenable_device reenables device
178func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500179 go func() {
180 if d := onuA.getDevice(device.Id); d == nil {
181 log.Fatalf("device-not-found-%s", device.Id)
182 }
183
184 cloned := proto.Clone(device).(*voltha.Device)
185 // Update the all ports state on that device to enable
186 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
187 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
188 }
189
190 //Update the device state
191 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
192 cloned.OperStatus = voltha.OperStatus_ACTIVE
193
194 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
195 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
196 }
197 if err := onuA.updateDevice(cloned); err != nil {
198 log.Fatalf("saving-device-failed-%s", err)
199 }
200 }()
201 return nil
202}