blob: 37e959eb80dc45fd6314c6ded5f3d13aabbe9f2c [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001/*
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"
23 "fmt"
24
25 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
26 "github.com/opencord/voltha-protos/v3/go/voltha"
27)
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
33 Devices map[string]*voltha.Device
34 DevicePorts map[string][]*voltha.Port
35}
36
37// UpdateCoreReference mock updatesCoreReference
38func (mcp *MockCoreProxy) UpdateCoreReference(deviceID string, coreReference string) {
39 panic("implement me")
40}
41
42// DeleteCoreReference mock DeleteCoreReference function
43func (mcp *MockCoreProxy) DeleteCoreReference(deviceID string) {
44 panic("implement me")
45}
46
47// GetCoreTopic implements mock GetCoreTopic
48func (mcp *MockCoreProxy) GetCoreTopic(deviceID string) kafka.Topic {
49 panic("implement me")
50}
51
52// GetAdapterTopic implements mock GetAdapterTopic
53func (mcp *MockCoreProxy) GetAdapterTopic(args ...string) kafka.Topic {
54 panic("implement me")
55}
56
57// RegisterAdapter implements mock RegisterAdapter
58func (mcp *MockCoreProxy) RegisterAdapter(ctx context.Context, adapter *voltha.Adapter,
59 deviceTypes *voltha.DeviceTypes) error {
60 if ctx == nil || adapter == nil || deviceTypes == nil {
61 return errors.New("registerAdapter func parameters cannot be nil")
62 }
63 return nil
64}
65
66// DeviceUpdate implements mock DeviceUpdate
67func (mcp *MockCoreProxy) DeviceUpdate(ctx context.Context, device *voltha.Device) error {
68 if device.Id == "" {
69 return errors.New("no Device")
70 }
71 return nil
72}
73
74// PortCreated implements mock PortCreated
75func (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
83}
84
85// PortsStateUpdate implements mock PortsStateUpdate
86func (mcp *MockCoreProxy) PortsStateUpdate(ctx context.Context, deviceID string, portTypeFilter uint32, operStatus voltha.OperStatus_Types) error {
87 if deviceID == "" {
88 return errors.New("no Device")
89 }
90 return nil
91}
92
93// DeleteAllPorts implements mock DeleteAllPorts
94func (mcp *MockCoreProxy) DeleteAllPorts(ctx context.Context, deviceID string) error {
95 if deviceID == "" {
96 return errors.New("no Device id")
97 }
98 return nil
99}
100
101// 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
120// DeviceStateUpdate implements mock DeviceStateUpdate
121func (mcp *MockCoreProxy) DeviceStateUpdate(ctx context.Context, deviceID string,
122 connStatus voltha.ConnectStatus_Types, operStatus voltha.OperStatus_Types) error {
123 if deviceID == "" {
124 return errors.New("no Device id")
125 }
126 return nil
127}
128
129// ChildDeviceDetected implements mock ChildDeviceDetected
130func (mcp *MockCoreProxy) ChildDeviceDetected(ctx context.Context, parentdeviceID string, parentPortNo int,
131 childDeviceType string, channelID int, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) {
132 if parentdeviceID == "" {
133 return nil, errors.New("no deviceID")
134 }
135 return nil, nil
136}
137
138// 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
145}
146
147// ChildDevicesDetected implements mock ChildDevicesDetecte
148func (mcp *MockCoreProxy) ChildDevicesDetected(ctx context.Context, parentdeviceID string) error {
149 if parentdeviceID == "" {
150 return errors.New("no device id")
151 }
152 return nil
153}
154
155// GetDevice implements mock GetDevice
156func (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 }
164 }
165 return nil, errors.New("device detection failed")
166}
167
168// GetChildDevice implements mock GetChildDevice
169func (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")
173 }
174 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
186 return nil, errors.New("device detection failed")
187}
188
189// GetChildDevices implements mock GetChildDevices
190func (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
205 }
206 return nil, errors.New("device detection failed")
207}
208
209// SendPacketIn implements mock SendPacketIn
210func (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
215}
216
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}
224
225// DevicePMConfigUpdate implements mock DevicePMConfigUpdate
226func (mcp *MockCoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error {
227 return nil
228}
229
230// PortStateUpdate implements mock PortStateUpdate
231func (mcp *MockCoreProxy) PortStateUpdate(ctx context.Context, deviceID string, pType voltha.Port_PortType, portNo uint32,
232 operStatus voltha.OperStatus_Types) error {
233 if deviceID == "" {
234 return errors.New("no Device")
235 }
236 return nil
237}