blob: 217f01ddd92517edf344e4c0cbac9dca0ae14166 [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"
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -080021 "errors"
khenaidooab1f7bd2019-11-14 14:00:27 -050022 "fmt"
23 "github.com/gogo/protobuf/proto"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080024 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
25 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
26 "github.com/opencord/voltha-lib-go/v3/pkg/log"
27 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
28 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
29 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo8b4abbf2020-04-24 17:04:30 -040030 "strings"
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 {
khenaidoo8b4abbf2020-04-24 17:04:30 -040035 *Adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050036}
37
npujar1d86a522019-11-14 17:11:16 +053038// NewONUAdapter creates ONU adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050039func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter {
khenaidoo67b22152020-03-02 16:01:25 -050040 return &ONUAdapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040041 Adapter: NewAdapter(cp),
khenaidoo67b22152020-03-02 16:01:25 -050042 }
khenaidooab1f7bd2019-11-14 14:00:27 -050043}
44
npujar1d86a522019-11-14 17:11:16 +053045// Adopt_device creates new handler for added device
46func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050047 go func() {
48 d := proto.Clone(device).(*voltha.Device)
49 d.Root = false
50 d.Vendor = "onu_adapter_mock"
51 d.Model = "go-mock"
52 d.SerialNumber = com.GetRandomSerialNumber()
53 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
54 onuA.storeDevice(d)
55 if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000056 logger.Fatalf("deviceUpdate-failed-%s", res)
khenaidooab1f7bd2019-11-14 14:00:27 -050057 }
khenaidooab1f7bd2019-11-14 14:00:27 -050058
khenaidoo442e7c72020-03-10 16:13:48 -040059 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
60 d.OperStatus = voltha.OperStatus_DISCOVERED
khenaidoo93181522020-01-23 12:43:21 -050061
khenaidoo442e7c72020-03-10 16:13:48 -040062 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000063 logger.Fatalf("device-state-update-failed-%s", err)
khenaidoo442e7c72020-03-10 16:13:48 -040064 }
khenaidooab1f7bd2019-11-14 14:00:27 -050065
66 uniPortNo := uint32(2)
67 if device.ProxyAddress != nil {
68 if device.ProxyAddress.ChannelId != 0 {
69 uniPortNo = device.ProxyAddress.ChannelId
70 }
71 }
72
73 uniPort := &voltha.Port{
74 PortNo: uniPortNo,
75 Label: fmt.Sprintf("uni-%d", uniPortNo),
76 Type: voltha.Port_ETHERNET_UNI,
77 OperStatus: voltha.OperStatus_ACTIVE,
78 }
79 var err error
80 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000081 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050082 }
83
84 ponPortNo := uint32(1)
85 if device.ParentPortNo != 0 {
86 ponPortNo = device.ParentPortNo
87 }
88
89 ponPort := &voltha.Port{
90 PortNo: ponPortNo,
91 Label: fmt.Sprintf("pon-%d", ponPortNo),
92 Type: voltha.Port_PON_ONU,
93 OperStatus: voltha.OperStatus_ACTIVE,
94 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -050095 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -050096 }
npujar1d86a522019-11-14 17:11:16 +053097 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000098 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050099 }
100
101 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
102 d.OperStatus = voltha.OperStatus_ACTIVE
103
104 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000105 logger.Fatalf("device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500106 }
107 //Get the latest device data from the Core
108 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000109 logger.Fatalf("getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500110 }
111
khenaidoo8b4abbf2020-04-24 17:04:30 -0400112 onuA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500113 }()
114 return nil
115}
116
npujar1d86a522019-11-14 17:11:16 +0530117// Get_ofp_port_info returns ofp device info
118func (onuA *ONUAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500119 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000120 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500121 }
122 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
123 return &ic.PortCapability{
124 Port: &voltha.LogicalPort{
125 OfpPort: &of.OfpPort{
126 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
127 Config: 0,
128 State: uint32(of.OfpPortState_OFPPS_LIVE),
129 Curr: capability,
130 Advertised: capability,
131 Peer: capability,
132 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
133 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
134 },
135 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530136 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500137 },
138 }, nil
139}
140
npujar1d86a522019-11-14 17:11:16 +0530141// Disable_device disables device
142func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500143 go func() {
144 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000145 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500146 }
147 cloned := proto.Clone(device).(*voltha.Device)
148 // Update the all ports state on that device to disable
149 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400150 // Device may also have been deleted in the Core
Girish Kumarf56a4682020-03-20 20:07:46 +0000151 logger.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400152 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500153 }
154 //Update the device state
155 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
156 cloned.OperStatus = voltha.OperStatus_UNKNOWN
157
158 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000159 logger.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400160 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500161 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400162 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500163 }()
164 return nil
165}
166
npujar1d86a522019-11-14 17:11:16 +0530167// Reenable_device reenables device
168func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500169 go func() {
170 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000171 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500172 }
173
174 cloned := proto.Clone(device).(*voltha.Device)
175 // Update the all ports state on that device to enable
176 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000177 logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500178 }
179
180 //Update the device state
181 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
182 cloned.OperStatus = voltha.OperStatus_ACTIVE
183
184 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000185 logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500186 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400187
188 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500189 }()
190 return nil
191}
khenaidoo67b22152020-03-02 16:01:25 -0500192
Scott Baker432f9be2020-03-26 11:56:30 -0700193// Start_omci_test begins an omci self-test
194func (onuA *ONUAdapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint
195 _ = device
196 return &ic.TestResponse{Result: ic.TestResponse_SUCCESS}, nil
197}
198
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800199func (onuA *ONUAdapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint
200 _ = deviceId
201 _ = device
202 _ = valueflag
203 return nil, errors.New("get-ext-value-not-implemented")
204}