blob: 37e959eb80dc45fd6314c6ded5f3d13aabbe9f2c [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
Esin Karamanccb714b2019-11-29 15:02:06 +000025 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
26 "github.com/opencord/voltha-protos/v3/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 }
135 return nil, nil
kdarapu381c6902019-07-31 18:23:16 +0530136}
137
kdarapu891693b2019-09-16 12:33:49 +0530138// ChildDevicesLost implements mock ChildDevicesLost.
139func (mcp *MockCoreProxy) ChildDevicesLost(ctx context.Context, parentdeviceID string) error {
140 //panic("implement me")
141 if parentdeviceID == "" {
142 return errors.New("no device id")
143 }
144 return nil
kdarapu381c6902019-07-31 18:23:16 +0530145}
146
147// ChildDevicesDetected implements mock ChildDevicesDetecte
kdarapu891693b2019-09-16 12:33:49 +0530148func (mcp *MockCoreProxy) ChildDevicesDetected(ctx context.Context, parentdeviceID string) error {
149 if parentdeviceID == "" {
150 return errors.New("no device id")
151 }
152 return nil
kdarapu381c6902019-07-31 18:23:16 +0530153}
154
155// GetDevice implements mock GetDevice
kdarapu891693b2019-09-16 12:33:49 +0530156func (mcp *MockCoreProxy) GetDevice(ctx context.Context, parentdeviceID string, deviceID string) (*voltha.Device, error) {
157 if parentdeviceID == "" || deviceID == "" {
158 return &voltha.Device{}, errors.New("no deviceID")
159 }
160 for k, v := range mcp.Devices {
161 if k == "olt" {
162 return v, nil
163 }
kdarapu381c6902019-07-31 18:23:16 +0530164 }
165 return nil, errors.New("device detection failed")
166}
167
168// GetChildDevice implements mock GetChildDevice
kdarapu891693b2019-09-16 12:33:49 +0530169func (mcp *MockCoreProxy) GetChildDevice(ctx context.Context, parentdeviceID string, kwargs map[string]interface{}) (*voltha.Device, error) {
170
171 if parentdeviceID == "" {
172 return nil, errors.New("device detection failed")
kdarapu381c6902019-07-31 18:23:16 +0530173 }
kdarapu891693b2019-09-16 12:33:49 +0530174 onuID := kwargs["onu_id"]
175 var onuDevice *voltha.Device
176 for _, val := range mcp.Devices {
177 if val.GetId() == fmt.Sprintf("%v", onuID) {
178 onuDevice = val
179 break
180 }
181 }
182 if onuDevice != nil {
183 return onuDevice, nil
184 }
185 //return &voltha.Device{}, nil
kdarapu381c6902019-07-31 18:23:16 +0530186 return nil, errors.New("device detection failed")
187}
188
189// GetChildDevices implements mock GetChildDevices
kdarapu891693b2019-09-16 12:33:49 +0530190func (mcp *MockCoreProxy) GetChildDevices(ctx context.Context, parentdeviceID string) (*voltha.Devices, error) {
191 if parentdeviceID == "" {
192 return nil, errors.New("no deviceID")
193 }
194 onuDevices := make([]*voltha.Device, 0)
195
196 for _, val := range mcp.Devices {
197 if val != nil {
198 onuDevices = append(onuDevices, val)
199 }
200 }
201
202 deviceList := &voltha.Devices{Items: onuDevices}
203 if len(deviceList.Items) > 0 {
204 return deviceList, nil
kdarapu381c6902019-07-31 18:23:16 +0530205 }
206 return nil, errors.New("device detection failed")
207}
208
209// SendPacketIn implements mock SendPacketIn
kdarapu891693b2019-09-16 12:33:49 +0530210func (mcp *MockCoreProxy) SendPacketIn(ctx context.Context, deviceID string, port uint32, pktPayload []byte) error {
211 if deviceID == "" {
212 return errors.New("no Device ID")
213 }
214 return nil
kdarapu381c6902019-07-31 18:23:16 +0530215}
David Bainbridgebe7cac12019-10-23 19:53:07 +0000216
217// DeviceReasonUpdate implements mock SendPacketIn
218func (mcp *MockCoreProxy) DeviceReasonUpdate(ctx context.Context, deviceID string, reason string) error {
219 if deviceID == "" {
220 return errors.New("no Device ID")
221 }
222 return nil
223}
Naga Manjunath7615e552019-10-11 22:35:47 +0530224
225// DevicePMConfigUpdate implements mock DevicePMConfigUpdate
226func (mcp *MockCoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
227 return nil
228}
Chaitrashree G Sded0a832020-01-09 20:21:48 -0500229
230// PortStateUpdate implements mock PortStateUpdate
231func (mcp *MockCoreProxy) PortStateUpdate(ctx context.Context, deviceID string, pType voltha.Port_PortType, portNo uint32,
Esin Karamanccb714b2019-11-29 15:02:06 +0000232 operStatus voltha.OperStatus_Types) error {
kdarapu1afeceb2020-02-12 01:38:09 -0500233 if deviceID == "" {
234 return errors.New("no Device")
235 }
Chaitrashree G Sded0a832020-01-09 20:21:48 -0500236 return nil
237}