blob: 361c7a1e5bfb02bcccd48444f86667c95714c2eb [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"
Kent Hagerman2a07b862020-06-19 15:23:07 -040023 "strings"
24
khenaidooab1f7bd2019-11-14 14:00:27 -050025 "github.com/gogo/protobuf/proto"
Maninderdfadc982020-10-28 14:04:33 +053026 "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif"
27 com "github.com/opencord/voltha-lib-go/v4/pkg/adapters/common"
28 "github.com/opencord/voltha-lib-go/v4/pkg/log"
29 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
30 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
31 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050032)
33
npujar1d86a522019-11-14 17:11:16 +053034// ONUAdapter represent ONU adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050035type ONUAdapter struct {
khenaidoo8b4abbf2020-04-24 17:04:30 -040036 *Adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050037}
38
npujar1d86a522019-11-14 17:11:16 +053039// NewONUAdapter creates ONU adapter
Rohan Agrawal31f21802020-06-12 05:38:46 +000040func NewONUAdapter(ctx context.Context, cp adapterif.CoreProxy) *ONUAdapter {
khenaidoo67b22152020-03-02 16:01:25 -050041 return &ONUAdapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040042 Adapter: NewAdapter(cp),
khenaidoo67b22152020-03-02 16:01:25 -050043 }
khenaidooab1f7bd2019-11-14 14:00:27 -050044}
45
npujar1d86a522019-11-14 17:11:16 +053046// Adopt_device creates new handler for added device
Rohan Agrawal31f21802020-06-12 05:38:46 +000047func (onuA *ONUAdapter) Adopt_device(ctx context.Context, 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 {
Rohan Agrawal31f21802020-06-12 05:38:46 +000057 logger.Fatalf(ctx, "deviceUpdate-failed-%s", res)
khenaidooab1f7bd2019-11-14 14:00:27 -050058 }
khenaidooab1f7bd2019-11-14 14:00:27 -050059
khenaidoo442e7c72020-03-10 16:13:48 -040060 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
61 d.OperStatus = voltha.OperStatus_DISCOVERED
khenaidoo93181522020-01-23 12:43:21 -050062
khenaidoo442e7c72020-03-10 16:13:48 -040063 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000064 logger.Fatalf(ctx, "device-state-update-failed-%s", err)
khenaidoo442e7c72020-03-10 16:13:48 -040065 }
khenaidooab1f7bd2019-11-14 14:00:27 -050066
67 uniPortNo := uint32(2)
68 if device.ProxyAddress != nil {
69 if device.ProxyAddress.ChannelId != 0 {
70 uniPortNo = device.ProxyAddress.ChannelId
71 }
72 }
73
khenaidooc6c7bda2020-06-17 17:20:18 -040074 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
khenaidooab1f7bd2019-11-14 14:00:27 -050075 uniPort := &voltha.Port{
76 PortNo: uniPortNo,
77 Label: fmt.Sprintf("uni-%d", uniPortNo),
78 Type: voltha.Port_ETHERNET_UNI,
79 OperStatus: voltha.OperStatus_ACTIVE,
khenaidooc6c7bda2020-06-17 17:20:18 -040080 OfpPort: &of.OfpPort{
81 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
82 Config: 0,
83 State: uint32(of.OfpPortState_OFPPS_LIVE),
84 Curr: capability,
85 Advertised: capability,
86 Peer: capability,
87 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
88 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
89 },
khenaidooab1f7bd2019-11-14 14:00:27 -050090 }
khenaidooc6c7bda2020-06-17 17:20:18 -040091
khenaidooab1f7bd2019-11-14 14:00:27 -050092 var err error
93 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000094 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050095 }
96
97 ponPortNo := uint32(1)
98 if device.ParentPortNo != 0 {
99 ponPortNo = device.ParentPortNo
100 }
101
102 ponPort := &voltha.Port{
103 PortNo: ponPortNo,
104 Label: fmt.Sprintf("pon-%d", ponPortNo),
105 Type: voltha.Port_PON_ONU,
106 OperStatus: voltha.OperStatus_ACTIVE,
107 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -0500108 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -0500109 }
npujar1d86a522019-11-14 17:11:16 +0530110 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000111 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500112 }
113
114 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
115 d.OperStatus = voltha.OperStatus_ACTIVE
116
117 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000118 logger.Fatalf(ctx, "device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500119 }
120 //Get the latest device data from the Core
121 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000122 logger.Fatalf(ctx, "getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500123 }
124
khenaidoo8b4abbf2020-04-24 17:04:30 -0400125 onuA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500126 }()
127 return nil
128}
129
npujar1d86a522019-11-14 17:11:16 +0530130// Disable_device disables device
Rohan Agrawal31f21802020-06-12 05:38:46 +0000131func (onuA *ONUAdapter) Disable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500132 go func() {
133 if d := onuA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000134 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500135 }
136 cloned := proto.Clone(device).(*voltha.Device)
137 // Update the all ports state on that device to disable
Kent Hagerman2a07b862020-06-19 15:23:07 -0400138 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400139 // Device may also have been deleted in the Core
divyadesaicb8b59d2020-08-18 09:55:47 +0000140 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400141 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500142 }
143 //Update the device state
144 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
145 cloned.OperStatus = voltha.OperStatus_UNKNOWN
146
147 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000148 logger.Warnw(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400149 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500150 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400151 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500152 }()
153 return nil
154}
155
npujar1d86a522019-11-14 17:11:16 +0530156// Reenable_device reenables device
Rohan Agrawal31f21802020-06-12 05:38:46 +0000157func (onuA *ONUAdapter) Reenable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500158 go func() {
159 if d := onuA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000160 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500161 }
162
163 cloned := proto.Clone(device).(*voltha.Device)
164 // Update the all ports state on that device to enable
Kent Hagerman2a07b862020-06-19 15:23:07 -0400165 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_ACTIVE); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000166 logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500167 }
168
169 //Update the device state
170 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
171 cloned.OperStatus = voltha.OperStatus_ACTIVE
172
173 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000174 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500175 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400176
177 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500178 }()
179 return nil
180}
khenaidoo67b22152020-03-02 16:01:25 -0500181
Scott Baker432f9be2020-03-26 11:56:30 -0700182// Start_omci_test begins an omci self-test
Rohan Agrawal31f21802020-06-12 05:38:46 +0000183func (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 -0700184 _ = device
185 return &ic.TestResponse{Result: ic.TestResponse_SUCCESS}, nil
186}
187
Rohan Agrawal31f21802020-06-12 05:38:46 +0000188func (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 -0800189 _ = deviceId
190 _ = device
191 _ = valueflag
192 return nil, errors.New("get-ext-value-not-implemented")
193}