blob: b1b004badc0a3d034e814f3ad26a681b9a2be024 [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"
Andrea Campanella025667e2021-01-14 11:50:07 +010023 "github.com/opencord/voltha-protos/v4/go/extension"
Kent Hagerman2a07b862020-06-19 15:23:07 -040024 "strings"
25
khenaidooab1f7bd2019-11-14 14:00:27 -050026 "github.com/gogo/protobuf/proto"
Maninderdfadc982020-10-28 14:04:33 +053027 "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif"
28 com "github.com/opencord/voltha-lib-go/v4/pkg/adapters/common"
29 "github.com/opencord/voltha-lib-go/v4/pkg/log"
30 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
31 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
32 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050033)
34
npujar1d86a522019-11-14 17:11:16 +053035// ONUAdapter represent ONU adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050036type ONUAdapter struct {
khenaidoo8b4abbf2020-04-24 17:04:30 -040037 *Adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050038}
39
npujar1d86a522019-11-14 17:11:16 +053040// NewONUAdapter creates ONU adapter
Rohan Agrawal31f21802020-06-12 05:38:46 +000041func NewONUAdapter(ctx context.Context, cp adapterif.CoreProxy) *ONUAdapter {
khenaidoo67b22152020-03-02 16:01:25 -050042 return &ONUAdapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040043 Adapter: NewAdapter(cp),
khenaidoo67b22152020-03-02 16:01:25 -050044 }
khenaidooab1f7bd2019-11-14 14:00:27 -050045}
46
npujar1d86a522019-11-14 17:11:16 +053047// Adopt_device creates new handler for added device
Rohan Agrawal31f21802020-06-12 05:38:46 +000048func (onuA *ONUAdapter) Adopt_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050049 go func() {
50 d := proto.Clone(device).(*voltha.Device)
51 d.Root = false
52 d.Vendor = "onu_adapter_mock"
53 d.Model = "go-mock"
54 d.SerialNumber = com.GetRandomSerialNumber()
55 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
56 onuA.storeDevice(d)
57 if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000058 logger.Fatalf(ctx, "deviceUpdate-failed-%s", res)
khenaidooab1f7bd2019-11-14 14:00:27 -050059 }
khenaidooab1f7bd2019-11-14 14:00:27 -050060
khenaidoo442e7c72020-03-10 16:13:48 -040061 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
62 d.OperStatus = voltha.OperStatus_DISCOVERED
khenaidoo93181522020-01-23 12:43:21 -050063
khenaidoo442e7c72020-03-10 16:13:48 -040064 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000065 logger.Fatalf(ctx, "device-state-update-failed-%s", err)
khenaidoo442e7c72020-03-10 16:13:48 -040066 }
khenaidooab1f7bd2019-11-14 14:00:27 -050067
68 uniPortNo := uint32(2)
69 if device.ProxyAddress != nil {
70 if device.ProxyAddress.ChannelId != 0 {
71 uniPortNo = device.ProxyAddress.ChannelId
72 }
73 }
74
khenaidooc6c7bda2020-06-17 17:20:18 -040075 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
khenaidooab1f7bd2019-11-14 14:00:27 -050076 uniPort := &voltha.Port{
77 PortNo: uniPortNo,
78 Label: fmt.Sprintf("uni-%d", uniPortNo),
79 Type: voltha.Port_ETHERNET_UNI,
80 OperStatus: voltha.OperStatus_ACTIVE,
khenaidooc6c7bda2020-06-17 17:20:18 -040081 OfpPort: &of.OfpPort{
82 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
83 Config: 0,
84 State: uint32(of.OfpPortState_OFPPS_LIVE),
85 Curr: capability,
86 Advertised: capability,
87 Peer: capability,
88 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
89 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
90 },
khenaidooab1f7bd2019-11-14 14:00:27 -050091 }
khenaidooc6c7bda2020-06-17 17:20:18 -040092
khenaidooab1f7bd2019-11-14 14:00:27 -050093 var err error
94 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000095 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050096 }
97
98 ponPortNo := uint32(1)
99 if device.ParentPortNo != 0 {
100 ponPortNo = device.ParentPortNo
101 }
102
103 ponPort := &voltha.Port{
104 PortNo: ponPortNo,
105 Label: fmt.Sprintf("pon-%d", ponPortNo),
106 Type: voltha.Port_PON_ONU,
107 OperStatus: voltha.OperStatus_ACTIVE,
108 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -0500109 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -0500110 }
npujar1d86a522019-11-14 17:11:16 +0530111 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000112 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500113 }
114
115 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
116 d.OperStatus = voltha.OperStatus_ACTIVE
117
118 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000119 logger.Fatalf(ctx, "device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500120 }
121 //Get the latest device data from the Core
122 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000123 logger.Fatalf(ctx, "getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500124 }
125
khenaidoo8b4abbf2020-04-24 17:04:30 -0400126 onuA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500127 }()
128 return nil
129}
130
Andrea Campanella025667e2021-01-14 11:50:07 +0100131// Single_get_value_request retrieves a single value.
132func (onuA *ONUAdapter) Single_get_value_request(ctx context.Context, // nolint
133 request extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
134 logger.Fatalf(ctx, "Single_get_value_request unimplemented")
135 return nil, nil
136}
137
npujar1d86a522019-11-14 17:11:16 +0530138// Disable_device disables device
Rohan Agrawal31f21802020-06-12 05:38:46 +0000139func (onuA *ONUAdapter) Disable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500140 go func() {
141 if d := onuA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000142 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500143 }
144 cloned := proto.Clone(device).(*voltha.Device)
145 // Update the all ports state on that device to disable
Kent Hagerman2a07b862020-06-19 15:23:07 -0400146 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400147 // Device may also have been deleted in the Core
divyadesaicb8b59d2020-08-18 09:55:47 +0000148 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400149 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500150 }
151 //Update the device state
152 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
153 cloned.OperStatus = voltha.OperStatus_UNKNOWN
154
155 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000156 logger.Warnw(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400157 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500158 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400159 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500160 }()
161 return nil
162}
163
npujar1d86a522019-11-14 17:11:16 +0530164// Reenable_device reenables device
Rohan Agrawal31f21802020-06-12 05:38:46 +0000165func (onuA *ONUAdapter) Reenable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500166 go func() {
167 if d := onuA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000168 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500169 }
170
171 cloned := proto.Clone(device).(*voltha.Device)
172 // Update the all ports state on that device to enable
Kent Hagerman2a07b862020-06-19 15:23:07 -0400173 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_ACTIVE); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000174 logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500175 }
176
177 //Update the device state
178 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
179 cloned.OperStatus = voltha.OperStatus_ACTIVE
180
181 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000182 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500183 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400184
185 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500186 }()
187 return nil
188}
khenaidoo67b22152020-03-02 16:01:25 -0500189
Scott Baker432f9be2020-03-26 11:56:30 -0700190// Start_omci_test begins an omci self-test
Rohan Agrawal31f21802020-06-12 05:38:46 +0000191func (onuA *ONUAdapter) Start_omci_test(ctx context.Context, device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint
Scott Baker432f9be2020-03-26 11:56:30 -0700192 _ = device
193 return &ic.TestResponse{Result: ic.TestResponse_SUCCESS}, nil
194}
195
Rohan Agrawal31f21802020-06-12 05:38:46 +0000196func (onuA *ONUAdapter) Get_ext_value(ctx context.Context, deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800197 _ = deviceId
198 _ = device
199 _ = valueflag
200 return nil, errors.New("get-ext-value-not-implemented")
201}
ssiddiquif076cb82021-04-23 10:47:04 +0530202
203func (onuA *ONUAdapter) Download_onu_image(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) { //nolint
204 logger.Infof(ctx, "Download_onu_image")
205 _ = request
206 return nil, errors.New("download-onu-image-not-implemented")
207}
208
209func (onuA *ONUAdapter) Get_onu_image_status(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
210 _ = in
211 return nil, errors.New("get-onu-image-not-implemented")
212}
213
214func (onuA *ONUAdapter) Abort_onu_image_upgrade(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
215 _ = in
216 return nil, errors.New("abort-onu-image-upgrade-not-implemented")
217}
218
219func (onuA *ONUAdapter) Get_onu_images(ctx context.Context, deviceID string) (*voltha.OnuImages, error) { //nolint
220 _ = deviceID
221 return nil, errors.New("get-onu-images-not-implemented")
222}
223
224func (onuA *ONUAdapter) Activate_onu_image(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
225 _ = in
226 return nil, errors.New("activate-onu-image-not-implemented")
227}
228
229func (onuA *ONUAdapter) Commit_onu_image(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
230 _ = in
231 return nil, errors.New("commit-onu-image-not-implemented")
232}