blob: f572f098c24de9e5bbd972cf3d92ff5f6ff5aa98 [file] [log] [blame]
kdarapu381c6902019-07-31 18:23:16 +05301/*
2 * Copyright 2018-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 */
16
17//Package mocks provides the mocks for openolt-adapter.
18package mocks
19
20import (
21 "context"
22 "errors"
kdarapu891693b2019-09-16 12:33:49 +053023 "fmt"
kdarapu381c6902019-07-31 18:23:16 +053024
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070025 "github.com/opencord/voltha-lib-go/v5/pkg/kafka"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070026 "github.com/opencord/voltha-protos/v4/go/voltha"
kdarapu381c6902019-07-31 18:23:16 +053027)
28
29// MockCoreProxy mocks the CoreProxy interface
30type MockCoreProxy struct {
31 // Values to be used in test can reside inside this structure
32 // TODO store relevant info in this, use this info for negative and positive tests
Kent Hagermanf1db18b2020-07-08 13:38:15 -040033 Devices map[string]*voltha.Device
34 DevicePorts map[string][]*voltha.Port
kdarapu381c6902019-07-31 18:23:16 +053035}
36
37// UpdateCoreReference mock updatesCoreReference
kdarapu891693b2019-09-16 12:33:49 +053038func (mcp *MockCoreProxy) UpdateCoreReference(deviceID string, coreReference string) {
kdarapu381c6902019-07-31 18:23:16 +053039 panic("implement me")
40}
41
42// DeleteCoreReference mock DeleteCoreReference function
kdarapu891693b2019-09-16 12:33:49 +053043func (mcp *MockCoreProxy) DeleteCoreReference(deviceID string) {
kdarapu381c6902019-07-31 18:23:16 +053044 panic("implement me")
45}
46
47// GetCoreTopic implements mock GetCoreTopic
kdarapu891693b2019-09-16 12:33:49 +053048func (mcp *MockCoreProxy) GetCoreTopic(deviceID string) kafka.Topic {
kdarapu381c6902019-07-31 18:23:16 +053049 panic("implement me")
50}
51
52// GetAdapterTopic implements mock GetAdapterTopic
kdarapu891693b2019-09-16 12:33:49 +053053func (mcp *MockCoreProxy) GetAdapterTopic(args ...string) kafka.Topic {
kdarapu381c6902019-07-31 18:23:16 +053054 panic("implement me")
55}
56
57// RegisterAdapter implements mock RegisterAdapter
kdarapu891693b2019-09-16 12:33:49 +053058func (mcp *MockCoreProxy) RegisterAdapter(ctx context.Context, adapter *voltha.Adapter,
kdarapu381c6902019-07-31 18:23:16 +053059 deviceTypes *voltha.DeviceTypes) error {
60 if ctx == nil || adapter == nil || deviceTypes == nil {
kdarapu381c6902019-07-31 18:23:16 +053061 return errors.New("registerAdapter func parameters cannot be nil")
62 }
63 return nil
kdarapu381c6902019-07-31 18:23:16 +053064}
65
66// DeviceUpdate implements mock DeviceUpdate
kdarapu891693b2019-09-16 12:33:49 +053067func (mcp *MockCoreProxy) DeviceUpdate(ctx context.Context, device *voltha.Device) error {
68 if device.Id == "" {
69 return errors.New("no Device")
70 }
71 return nil
kdarapu381c6902019-07-31 18:23:16 +053072}
73
74// PortCreated implements mock PortCreated
kdarapu891693b2019-09-16 12:33:49 +053075func (mcp *MockCoreProxy) PortCreated(ctx context.Context, deviceID string, port *voltha.Port) error {
76 if deviceID == "" {
77 return errors.New("no deviceID")
78 }
79 if port.Type > 7 {
80 return errors.New("invalid porttype")
81 }
82 return nil
kdarapu381c6902019-07-31 18:23:16 +053083}
84
85// PortsStateUpdate implements mock PortsStateUpdate
Kent Hagermanf1db18b2020-07-08 13:38:15 -040086func (mcp *MockCoreProxy) PortsStateUpdate(ctx context.Context, deviceID string, portTypeFilter uint32, operStatus voltha.OperStatus_Types) error {
kdarapu891693b2019-09-16 12:33:49 +053087 if deviceID == "" {
88 return errors.New("no Device")
89 }
90 return nil
kdarapu381c6902019-07-31 18:23:16 +053091}
92
93// DeleteAllPorts implements mock DeleteAllPorts
kdarapu891693b2019-09-16 12:33:49 +053094func (mcp *MockCoreProxy) DeleteAllPorts(ctx context.Context, deviceID string) error {
95 if deviceID == "" {
96 return errors.New("no Device id")
97 }
98 return nil
kdarapu381c6902019-07-31 18:23:16 +053099}
100
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400101// GetDevicePort implements mock GetDevicePort
102func (mcp *MockCoreProxy) GetDevicePort(ctx context.Context, deviceID string, portID uint32) (*voltha.Port, error) {
103 for _, port := range mcp.DevicePorts[deviceID] {
104 if port.PortNo == portID {
105 return port, nil
106 }
107 }
108 return nil, errors.New("device/port not found")
109}
110
111// ListDevicePorts implements mock ListDevicePorts
112func (mcp *MockCoreProxy) ListDevicePorts(ctx context.Context, deviceID string) ([]*voltha.Port, error) {
113 ports, have := mcp.DevicePorts[deviceID]
114 if !have {
115 return nil, errors.New("device id not found")
116 }
117 return ports, nil
118}
119
kdarapu381c6902019-07-31 18:23:16 +0530120// DeviceStateUpdate implements mock DeviceStateUpdate
kdarapu891693b2019-09-16 12:33:49 +0530121func (mcp *MockCoreProxy) DeviceStateUpdate(ctx context.Context, deviceID string,
Esin Karamanccb714b2019-11-29 15:02:06 +0000122 connStatus voltha.ConnectStatus_Types, operStatus voltha.OperStatus_Types) error {
kdarapu891693b2019-09-16 12:33:49 +0530123 if deviceID == "" {
124 return errors.New("no Device id")
125 }
126 return nil
kdarapu381c6902019-07-31 18:23:16 +0530127}
128
129// ChildDeviceDetected implements mock ChildDeviceDetected
kdarapu891693b2019-09-16 12:33:49 +0530130func (mcp *MockCoreProxy) ChildDeviceDetected(ctx context.Context, parentdeviceID string, parentPortNo int,
kdarapu381c6902019-07-31 18:23:16 +0530131 childDeviceType string, channelID int, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) {
kdarapu891693b2019-09-16 12:33:49 +0530132 if parentdeviceID == "" {
133 return nil, errors.New("no deviceID")
134 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700135 for k, v := range mcp.Devices {
136 if k == "olt" {
137 return v, nil
138 }
139 }
kdarapu891693b2019-09-16 12:33:49 +0530140 return nil, nil
kdarapu381c6902019-07-31 18:23:16 +0530141}
142
kdarapu891693b2019-09-16 12:33:49 +0530143// ChildDevicesLost implements mock ChildDevicesLost.
144func (mcp *MockCoreProxy) ChildDevicesLost(ctx context.Context, parentdeviceID string) error {
145 //panic("implement me")
146 if parentdeviceID == "" {
147 return errors.New("no device id")
148 }
149 return nil
kdarapu381c6902019-07-31 18:23:16 +0530150}
151
152// ChildDevicesDetected implements mock ChildDevicesDetecte
kdarapu891693b2019-09-16 12:33:49 +0530153func (mcp *MockCoreProxy) ChildDevicesDetected(ctx context.Context, parentdeviceID string) error {
154 if parentdeviceID == "" {
155 return errors.New("no device id")
156 }
157 return nil
kdarapu381c6902019-07-31 18:23:16 +0530158}
159
160// GetDevice implements mock GetDevice
kdarapu891693b2019-09-16 12:33:49 +0530161func (mcp *MockCoreProxy) GetDevice(ctx context.Context, parentdeviceID string, deviceID string) (*voltha.Device, error) {
162 if parentdeviceID == "" || deviceID == "" {
163 return &voltha.Device{}, errors.New("no deviceID")
164 }
165 for k, v := range mcp.Devices {
166 if k == "olt" {
167 return v, nil
168 }
kdarapu381c6902019-07-31 18:23:16 +0530169 }
170 return nil, errors.New("device detection failed")
171}
172
173// GetChildDevice implements mock GetChildDevice
kdarapu891693b2019-09-16 12:33:49 +0530174func (mcp *MockCoreProxy) GetChildDevice(ctx context.Context, parentdeviceID string, kwargs map[string]interface{}) (*voltha.Device, error) {
175
176 if parentdeviceID == "" {
177 return nil, errors.New("device detection failed")
kdarapu381c6902019-07-31 18:23:16 +0530178 }
kdarapu891693b2019-09-16 12:33:49 +0530179 onuID := kwargs["onu_id"]
180 var onuDevice *voltha.Device
181 for _, val := range mcp.Devices {
182 if val.GetId() == fmt.Sprintf("%v", onuID) {
183 onuDevice = val
184 break
185 }
186 }
187 if onuDevice != nil {
188 return onuDevice, nil
189 }
190 //return &voltha.Device{}, nil
kdarapu381c6902019-07-31 18:23:16 +0530191 return nil, errors.New("device detection failed")
192}
193
194// GetChildDevices implements mock GetChildDevices
kdarapu891693b2019-09-16 12:33:49 +0530195func (mcp *MockCoreProxy) GetChildDevices(ctx context.Context, parentdeviceID string) (*voltha.Devices, error) {
196 if parentdeviceID == "" {
197 return nil, errors.New("no deviceID")
198 }
199 onuDevices := make([]*voltha.Device, 0)
200
201 for _, val := range mcp.Devices {
202 if val != nil {
203 onuDevices = append(onuDevices, val)
204 }
205 }
206
207 deviceList := &voltha.Devices{Items: onuDevices}
208 if len(deviceList.Items) > 0 {
209 return deviceList, nil
kdarapu381c6902019-07-31 18:23:16 +0530210 }
211 return nil, errors.New("device detection failed")
212}
213
214// SendPacketIn implements mock SendPacketIn
kdarapu891693b2019-09-16 12:33:49 +0530215func (mcp *MockCoreProxy) SendPacketIn(ctx context.Context, deviceID string, port uint32, pktPayload []byte) error {
216 if deviceID == "" {
217 return errors.New("no Device ID")
218 }
219 return nil
kdarapu381c6902019-07-31 18:23:16 +0530220}
David Bainbridgebe7cac12019-10-23 19:53:07 +0000221
222// DeviceReasonUpdate implements mock SendPacketIn
223func (mcp *MockCoreProxy) DeviceReasonUpdate(ctx context.Context, deviceID string, reason string) error {
224 if deviceID == "" {
225 return errors.New("no Device ID")
226 }
227 return nil
228}
Naga Manjunath7615e552019-10-11 22:35:47 +0530229
230// DevicePMConfigUpdate implements mock DevicePMConfigUpdate
231func (mcp *MockCoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
232 return nil
233}
Chaitrashree G Sded0a832020-01-09 20:21:48 -0500234
235// PortStateUpdate implements mock PortStateUpdate
236func (mcp *MockCoreProxy) PortStateUpdate(ctx context.Context, deviceID string, pType voltha.Port_PortType, portNo uint32,
Esin Karamanccb714b2019-11-29 15:02:06 +0000237 operStatus voltha.OperStatus_Types) error {
kdarapu1afeceb2020-02-12 01:38:09 -0500238 if deviceID == "" {
239 return errors.New("no Device")
240 }
Chaitrashree G Sded0a832020-01-09 20:21:48 -0500241 return nil
242}