blob: bc0e0a2a24a581723b65b33a032baa1c91c0d2e4 [file] [log] [blame]
khenaidoo106c61a2021-08-11 18:05:46 -04001/*
2 * Copyright 2021-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 "strings"
25
26 "github.com/golang/protobuf/ptypes/empty"
27 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
28 "github.com/opencord/voltha-protos/v5/go/common"
29 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
30 "github.com/opencord/voltha-protos/v5/go/voltha"
31 "google.golang.org/grpc"
32)
33
34// NewMockCoreClient creates a new mock core client for a given core service
35func NewMockCoreClient(coreService *MockCoreService) *vgrpc.Client {
36 cc, _ := vgrpc.NewClient("mock-endpoint", nil)
37 cc.SetService(coreService)
38 return cc
39}
40
41// MockCoreService represents a mock core service
42type MockCoreService struct {
43 // Values to be used in test can reside inside this structure
44 // TODO store relevant info in this, use this info for negative and positive tests
45 Devices map[string]*voltha.Device
46 DevicePorts map[string][]*voltha.Port
47}
48
49// GetHealthStatus implements mock GetHealthStatus
50func (mcs MockCoreService) GetHealthStatus(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*voltha.HealthStatus, error) {
51 return &voltha.HealthStatus{State: voltha.HealthStatus_HEALTHY}, nil
52}
53
54// RegisterAdapter implements mock RegisterAdapter
55func (mcs MockCoreService) RegisterAdapter(ctx context.Context, in *ic.AdapterRegistration, opts ...grpc.CallOption) (*empty.Empty, error) {
56 if ctx == nil || in.Adapter == nil || in.DTypes == nil {
57 return nil, errors.New("registerAdapter func parameters cannot be nil")
58 }
59 return &empty.Empty{}, nil
60}
61
62// DeviceUpdate implements mock DeviceUpdate
63func (mcs MockCoreService) DeviceUpdate(ctx context.Context, in *voltha.Device, opts ...grpc.CallOption) (*empty.Empty, error) {
64 if in.Id == "" {
65 return nil, errors.New("no Device")
66 }
67 return &empty.Empty{}, nil
68}
69
70// PortCreated implements mock PortCreated
71func (mcs MockCoreService) PortCreated(ctx context.Context, in *voltha.Port, opts ...grpc.CallOption) (*empty.Empty, error) {
72 if in.DeviceId == "" {
73 return nil, errors.New("no deviceID")
74 }
75 if in.Type > 7 {
76 return nil, errors.New("invalid porttype")
77 }
78 return &empty.Empty{}, nil
79}
80
81// PortsStateUpdate implements mock PortsStateUpdate
82func (mcs MockCoreService) PortsStateUpdate(ctx context.Context, in *ic.PortStateFilter, opts ...grpc.CallOption) (*empty.Empty, error) {
83 if in.DeviceId == "" {
84 return nil, errors.New("no Device")
85 }
86 return &empty.Empty{}, nil
87}
88
89// DeleteAllPorts implements mock DeleteAllPorts
90func (mcs MockCoreService) DeleteAllPorts(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*empty.Empty, error) {
91 if in.Id == "" {
92 return nil, errors.New("no Device id")
93 }
94 return &empty.Empty{}, nil
95}
96
97// GetDevicePort implements mock GetDevicePort
98func (mcs MockCoreService) GetDevicePort(ctx context.Context, in *ic.PortFilter, opts ...grpc.CallOption) (*voltha.Port, error) {
99 for _, port := range mcs.DevicePorts[in.DeviceId] {
100 if port.PortNo == in.Port {
101 return port, nil
102 }
103 }
104 return nil, errors.New("device/port not found")
105}
106
107// ListDevicePorts implements mock ListDevicePorts
108func (mcs MockCoreService) ListDevicePorts(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*voltha.Ports, error) {
109 ports, have := mcs.DevicePorts[in.Id]
110 if !have {
111 return nil, errors.New("device id not found")
112 }
113 return &voltha.Ports{Items: ports}, nil
114}
115
116// DeviceStateUpdate implements mock DeviceStateUpdate
117func (mcs MockCoreService) DeviceStateUpdate(ctx context.Context, in *ic.DeviceStateFilter, opts ...grpc.CallOption) (*empty.Empty, error) {
118 if in.DeviceId == "" {
119 return nil, errors.New("no Device id")
120 }
121 return &empty.Empty{}, nil
122}
123
124// DevicePMConfigUpdate implements mock DevicePMConfigUpdate
125func (mcs MockCoreService) DevicePMConfigUpdate(ctx context.Context, in *voltha.PmConfigs, opts ...grpc.CallOption) (*empty.Empty, error) {
126 return &empty.Empty{}, nil
127}
128
129// ChildDeviceDetected implements mock ChildDeviceDetected
130func (mcs MockCoreService) ChildDeviceDetected(ctx context.Context, in *ic.DeviceDiscovery, opts ...grpc.CallOption) (*voltha.Device, error) {
131 if in.ParentId == "" {
132 return nil, errors.New("no deviceID")
133 }
134 for k, v := range mcs.Devices {
135 if strings.Contains(k, "onu") {
136 return v, nil
137 }
138 }
139 return nil, errors.New("no deviceID")
140}
141
142// ChildDevicesLost implements mock ChildDevicesLost
143func (mcs MockCoreService) ChildDevicesLost(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*empty.Empty, error) {
144 if in.Id == "" {
145 return nil, errors.New("no device id")
146 }
147 return &empty.Empty{}, nil
148}
149
150// ChildDevicesDetected implements mock ChildDevicesDetected
151func (mcs MockCoreService) ChildDevicesDetected(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*empty.Empty, error) {
152 if in.Id == "" {
153 return nil, errors.New("no device id")
154 }
155 return &empty.Empty{}, nil
156}
157
158// GetDevice implements mock GetDevice
159func (mcs MockCoreService) GetDevice(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*voltha.Device, error) {
160 if in.Id == "" {
161 return &voltha.Device{}, errors.New("no deviceID")
162 }
163 for k, v := range mcs.Devices {
164 if k == "olt" {
165 return v, nil
166 }
167 }
168 return nil, errors.New("device detection failed")
169}
170
171// GetChildDevice implements mock GetChildDevice
172func (mcs MockCoreService) GetChildDevice(ctx context.Context, in *ic.ChildDeviceFilter, opts ...grpc.CallOption) (*voltha.Device, error) {
173 if in.ParentId == "" {
174 return nil, errors.New("device detection failed")
175 }
176 var onuDevice *voltha.Device
177 for _, val := range mcs.Devices {
178 if val.GetId() == fmt.Sprintf("%d", in.OnuId) {
179 onuDevice = val
180 break
181 }
182 }
183 if onuDevice != nil {
184 return onuDevice, nil
185 }
186 //return &voltha.Device{}, nil
187 return nil, errors.New("device detection failed")
188}
189
190// GetChildDevices implements mock GetChildDevices
191func (mcs MockCoreService) GetChildDevices(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*voltha.Devices, error) {
192 if in.Id == "" {
193 return nil, errors.New("no deviceID")
194 }
195 onuDevices := make([]*voltha.Device, 0)
196
197 for _, val := range mcs.Devices {
198 if val != nil && val.ParentId == in.Id {
199 onuDevices = append(onuDevices, val)
200 }
201 }
202
203 deviceList := &voltha.Devices{Items: onuDevices}
204 if len(deviceList.Items) > 0 {
205 return deviceList, nil
206 }
207 return nil, errors.New("device detection failed")
208}
209
210// SendPacketIn implements mock SendPacketIn
211func (mcs MockCoreService) SendPacketIn(ctx context.Context, in *ic.PacketIn, opts ...grpc.CallOption) (*empty.Empty, error) {
212 if in.DeviceId == "" {
213 return nil, errors.New("no Device ID")
214 }
215 return &empty.Empty{}, nil
216}
217
218// DeviceReasonUpdate implements mock DeviceReasonUpdate
219func (mcs MockCoreService) DeviceReasonUpdate(ctx context.Context, in *ic.DeviceReason, opts ...grpc.CallOption) (*empty.Empty, error) {
220 if in.DeviceId == "" {
221 return nil, errors.New("no Device ID")
222 }
223 return &empty.Empty{}, nil
224}
225
226// PortStateUpdate implements mock PortStateUpdate
227func (mcs MockCoreService) PortStateUpdate(ctx context.Context, in *ic.PortState, opts ...grpc.CallOption) (*empty.Empty, error) {
228 if in.DeviceId == "" {
229 return nil, errors.New("no Device")
230 }
231 return &empty.Empty{}, nil
232}
233
234// Additional API found in the Core - unused?
235
236// ReconcileChildDevices implements mock ReconcileChildDevices
237func (mcs MockCoreService) ReconcileChildDevices(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*empty.Empty, error) {
238 return &empty.Empty{}, nil
239}
240
241// GetChildDeviceWithProxyAddress implements mock GetChildDeviceWithProxyAddress
242func (mcs MockCoreService) GetChildDeviceWithProxyAddress(ctx context.Context, in *voltha.Device_ProxyAddress, opts ...grpc.CallOption) (*voltha.Device, error) {
243 return nil, nil
244}
245
246// GetPorts implements mock GetPorts
247func (mcs MockCoreService) GetPorts(ctx context.Context, in *ic.PortFilter, opts ...grpc.CallOption) (*voltha.Ports, error) {
248 return nil, nil
249}
250
251// ChildrenStateUpdate implements mock ChildrenStateUpdate
252func (mcs MockCoreService) ChildrenStateUpdate(ctx context.Context, in *ic.DeviceStateFilter, opts ...grpc.CallOption) (*empty.Empty, error) {
253 return &empty.Empty{}, nil
254}
255
256// UpdateImageDownload implements mock UpdateImageDownload
257func (mcs MockCoreService) UpdateImageDownload(ctx context.Context, in *voltha.ImageDownload, opts ...grpc.CallOption) (*empty.Empty, error) {
258 return &empty.Empty{}, nil
259}