blob: 02c885ef51c0e05d9dbebf2dd12be25b7404a91e [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
khenaidooc6c7bda2020-06-17 17:20:18 -040073 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
khenaidooab1f7bd2019-11-14 14:00:27 -050074 uniPort := &voltha.Port{
75 PortNo: uniPortNo,
76 Label: fmt.Sprintf("uni-%d", uniPortNo),
77 Type: voltha.Port_ETHERNET_UNI,
78 OperStatus: voltha.OperStatus_ACTIVE,
khenaidooc6c7bda2020-06-17 17:20:18 -040079 OfpPort: &of.OfpPort{
80 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
81 Config: 0,
82 State: uint32(of.OfpPortState_OFPPS_LIVE),
83 Curr: capability,
84 Advertised: capability,
85 Peer: capability,
86 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
87 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
88 },
khenaidooab1f7bd2019-11-14 14:00:27 -050089 }
khenaidooc6c7bda2020-06-17 17:20:18 -040090
khenaidooab1f7bd2019-11-14 14:00:27 -050091 var err error
92 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000093 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050094 }
95
96 ponPortNo := uint32(1)
97 if device.ParentPortNo != 0 {
98 ponPortNo = device.ParentPortNo
99 }
100
101 ponPort := &voltha.Port{
102 PortNo: ponPortNo,
103 Label: fmt.Sprintf("pon-%d", ponPortNo),
104 Type: voltha.Port_PON_ONU,
105 OperStatus: voltha.OperStatus_ACTIVE,
106 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -0500107 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -0500108 }
npujar1d86a522019-11-14 17:11:16 +0530109 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000110 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500111 }
112
113 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
114 d.OperStatus = voltha.OperStatus_ACTIVE
115
116 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000117 logger.Fatalf("device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500118 }
119 //Get the latest device data from the Core
120 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000121 logger.Fatalf("getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500122 }
123
khenaidoo8b4abbf2020-04-24 17:04:30 -0400124 onuA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500125 }()
126 return nil
127}
128
npujar1d86a522019-11-14 17:11:16 +0530129// Disable_device disables device
130func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500131 go func() {
132 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000133 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500134 }
135 cloned := proto.Clone(device).(*voltha.Device)
136 // Update the all ports state on that device to disable
137 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400138 // Device may also have been deleted in the Core
Girish Kumarf56a4682020-03-20 20:07:46 +0000139 logger.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400140 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500141 }
142 //Update the device state
143 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
144 cloned.OperStatus = voltha.OperStatus_UNKNOWN
145
146 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000147 logger.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400148 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500149 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400150 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500151 }()
152 return nil
153}
154
npujar1d86a522019-11-14 17:11:16 +0530155// Reenable_device reenables device
156func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500157 go func() {
158 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000159 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500160 }
161
162 cloned := proto.Clone(device).(*voltha.Device)
163 // Update the all ports state on that device to enable
164 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000165 logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500166 }
167
168 //Update the device state
169 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
170 cloned.OperStatus = voltha.OperStatus_ACTIVE
171
172 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000173 logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500174 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400175
176 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500177 }()
178 return nil
179}
khenaidoo67b22152020-03-02 16:01:25 -0500180
Scott Baker432f9be2020-03-26 11:56:30 -0700181// Start_omci_test begins an omci self-test
182func (onuA *ONUAdapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint
183 _ = device
184 return &ic.TestResponse{Result: ic.TestResponse_SUCCESS}, nil
185}
186
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800187func (onuA *ONUAdapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint
188 _ = deviceId
189 _ = device
190 _ = valueflag
191 return nil, errors.New("get-ext-value-not-implemented")
192}