blob: 9c99599fd63c2997428de8060dd8b50158f79713 [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
kdarapu891693b2019-09-16 12:33:49 +053033 Devices map[string]*voltha.Device
kdarapu381c6902019-07-31 18:23:16 +053034}
35
36// UpdateCoreReference mock updatesCoreReference
kdarapu891693b2019-09-16 12:33:49 +053037func (mcp *MockCoreProxy) UpdateCoreReference(deviceID string, coreReference string) {
kdarapu381c6902019-07-31 18:23:16 +053038 panic("implement me")
39}
40
41// DeleteCoreReference mock DeleteCoreReference function
kdarapu891693b2019-09-16 12:33:49 +053042func (mcp *MockCoreProxy) DeleteCoreReference(deviceID string) {
kdarapu381c6902019-07-31 18:23:16 +053043 panic("implement me")
44}
45
46// GetCoreTopic implements mock GetCoreTopic
kdarapu891693b2019-09-16 12:33:49 +053047func (mcp *MockCoreProxy) GetCoreTopic(deviceID string) kafka.Topic {
kdarapu381c6902019-07-31 18:23:16 +053048 panic("implement me")
49}
50
51// GetAdapterTopic implements mock GetAdapterTopic
kdarapu891693b2019-09-16 12:33:49 +053052func (mcp *MockCoreProxy) GetAdapterTopic(args ...string) kafka.Topic {
kdarapu381c6902019-07-31 18:23:16 +053053 panic("implement me")
54}
55
56// RegisterAdapter implements mock RegisterAdapter
kdarapu891693b2019-09-16 12:33:49 +053057func (mcp *MockCoreProxy) RegisterAdapter(ctx context.Context, adapter *voltha.Adapter,
kdarapu381c6902019-07-31 18:23:16 +053058 deviceTypes *voltha.DeviceTypes) error {
59 if ctx == nil || adapter == nil || deviceTypes == nil {
kdarapu381c6902019-07-31 18:23:16 +053060 return errors.New("registerAdapter func parameters cannot be nil")
61 }
62 return nil
kdarapu381c6902019-07-31 18:23:16 +053063}
64
65// DeviceUpdate implements mock DeviceUpdate
kdarapu891693b2019-09-16 12:33:49 +053066func (mcp *MockCoreProxy) DeviceUpdate(ctx context.Context, device *voltha.Device) error {
67 if device.Id == "" {
68 return errors.New("no Device")
69 }
70 return nil
kdarapu381c6902019-07-31 18:23:16 +053071}
72
73// PortCreated implements mock PortCreated
kdarapu891693b2019-09-16 12:33:49 +053074func (mcp *MockCoreProxy) PortCreated(ctx context.Context, deviceID string, port *voltha.Port) error {
75 if deviceID == "" {
76 return errors.New("no deviceID")
77 }
78 if port.Type > 7 {
79 return errors.New("invalid porttype")
80 }
81 return nil
kdarapu381c6902019-07-31 18:23:16 +053082}
83
84// PortsStateUpdate implements mock PortsStateUpdate
Esin Karamanccb714b2019-11-29 15:02:06 +000085func (mcp *MockCoreProxy) PortsStateUpdate(ctx context.Context, deviceID string, operStatus voltha.OperStatus_Types) error {
kdarapu891693b2019-09-16 12:33:49 +053086 if deviceID == "" {
87 return errors.New("no Device")
88 }
89 return nil
kdarapu381c6902019-07-31 18:23:16 +053090}
91
92// DeleteAllPorts implements mock DeleteAllPorts
kdarapu891693b2019-09-16 12:33:49 +053093func (mcp *MockCoreProxy) DeleteAllPorts(ctx context.Context, deviceID string) error {
94 if deviceID == "" {
95 return errors.New("no Device id")
96 }
97 return nil
kdarapu381c6902019-07-31 18:23:16 +053098}
99
100// DeviceStateUpdate implements mock DeviceStateUpdate
kdarapu891693b2019-09-16 12:33:49 +0530101func (mcp *MockCoreProxy) DeviceStateUpdate(ctx context.Context, deviceID string,
Esin Karamanccb714b2019-11-29 15:02:06 +0000102 connStatus voltha.ConnectStatus_Types, operStatus voltha.OperStatus_Types) error {
kdarapu891693b2019-09-16 12:33:49 +0530103 if deviceID == "" {
104 return errors.New("no Device id")
105 }
106 return nil
kdarapu381c6902019-07-31 18:23:16 +0530107}
108
109// ChildDeviceDetected implements mock ChildDeviceDetected
kdarapu891693b2019-09-16 12:33:49 +0530110func (mcp *MockCoreProxy) ChildDeviceDetected(ctx context.Context, parentdeviceID string, parentPortNo int,
kdarapu381c6902019-07-31 18:23:16 +0530111 childDeviceType string, channelID int, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) {
kdarapu891693b2019-09-16 12:33:49 +0530112 if parentdeviceID == "" {
113 return nil, errors.New("no deviceID")
114 }
115 return nil, nil
kdarapu381c6902019-07-31 18:23:16 +0530116}
117
kdarapu891693b2019-09-16 12:33:49 +0530118// ChildDevicesLost implements mock ChildDevicesLost.
119func (mcp *MockCoreProxy) ChildDevicesLost(ctx context.Context, parentdeviceID string) error {
120 //panic("implement me")
121 if parentdeviceID == "" {
122 return errors.New("no device id")
123 }
124 return nil
kdarapu381c6902019-07-31 18:23:16 +0530125}
126
127// ChildDevicesDetected implements mock ChildDevicesDetecte
kdarapu891693b2019-09-16 12:33:49 +0530128func (mcp *MockCoreProxy) ChildDevicesDetected(ctx context.Context, parentdeviceID string) error {
129 if parentdeviceID == "" {
130 return errors.New("no device id")
131 }
132 return nil
kdarapu381c6902019-07-31 18:23:16 +0530133}
134
135// GetDevice implements mock GetDevice
kdarapu891693b2019-09-16 12:33:49 +0530136func (mcp *MockCoreProxy) GetDevice(ctx context.Context, parentdeviceID string, deviceID string) (*voltha.Device, error) {
137 if parentdeviceID == "" || deviceID == "" {
138 return &voltha.Device{}, errors.New("no deviceID")
139 }
140 for k, v := range mcp.Devices {
141 if k == "olt" {
142 return v, nil
143 }
kdarapu381c6902019-07-31 18:23:16 +0530144 }
145 return nil, errors.New("device detection failed")
146}
147
148// GetChildDevice implements mock GetChildDevice
kdarapu891693b2019-09-16 12:33:49 +0530149func (mcp *MockCoreProxy) GetChildDevice(ctx context.Context, parentdeviceID string, kwargs map[string]interface{}) (*voltha.Device, error) {
150
151 if parentdeviceID == "" {
152 return nil, errors.New("device detection failed")
kdarapu381c6902019-07-31 18:23:16 +0530153 }
kdarapu891693b2019-09-16 12:33:49 +0530154 onuID := kwargs["onu_id"]
155 var onuDevice *voltha.Device
156 for _, val := range mcp.Devices {
157 if val.GetId() == fmt.Sprintf("%v", onuID) {
158 onuDevice = val
159 break
160 }
161 }
162 if onuDevice != nil {
163 return onuDevice, nil
164 }
165 //return &voltha.Device{}, nil
kdarapu381c6902019-07-31 18:23:16 +0530166 return nil, errors.New("device detection failed")
167}
168
169// GetChildDevices implements mock GetChildDevices
kdarapu891693b2019-09-16 12:33:49 +0530170func (mcp *MockCoreProxy) GetChildDevices(ctx context.Context, parentdeviceID string) (*voltha.Devices, error) {
171 if parentdeviceID == "" {
172 return nil, errors.New("no deviceID")
173 }
174 onuDevices := make([]*voltha.Device, 0)
175
176 for _, val := range mcp.Devices {
177 if val != nil {
178 onuDevices = append(onuDevices, val)
179 }
180 }
181
182 deviceList := &voltha.Devices{Items: onuDevices}
183 if len(deviceList.Items) > 0 {
184 return deviceList, nil
kdarapu381c6902019-07-31 18:23:16 +0530185 }
186 return nil, errors.New("device detection failed")
187}
188
189// SendPacketIn implements mock SendPacketIn
kdarapu891693b2019-09-16 12:33:49 +0530190func (mcp *MockCoreProxy) SendPacketIn(ctx context.Context, deviceID string, port uint32, pktPayload []byte) error {
191 if deviceID == "" {
192 return errors.New("no Device ID")
193 }
194 return nil
kdarapu381c6902019-07-31 18:23:16 +0530195}
David Bainbridgebe7cac12019-10-23 19:53:07 +0000196
197// DeviceReasonUpdate implements mock SendPacketIn
198func (mcp *MockCoreProxy) DeviceReasonUpdate(ctx context.Context, deviceID string, reason string) error {
199 if deviceID == "" {
200 return errors.New("no Device ID")
201 }
202 return nil
203}
Naga Manjunath7615e552019-10-11 22:35:47 +0530204
205// DevicePMConfigUpdate implements mock DevicePMConfigUpdate
206func (mcp *MockCoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
207 return nil
208}
Chaitrashree G Sded0a832020-01-09 20:21:48 -0500209
210// PortStateUpdate implements mock PortStateUpdate
211func (mcp *MockCoreProxy) PortStateUpdate(ctx context.Context, deviceID string, pType voltha.Port_PortType, portNo uint32,
Esin Karamanccb714b2019-11-29 15:02:06 +0000212 operStatus voltha.OperStatus_Types) error {
kdarapu1afeceb2020-02-12 01:38:09 -0500213 if deviceID == "" {
214 return errors.New("no Device")
215 }
Chaitrashree G Sded0a832020-01-09 20:21:48 -0500216 return nil
217}