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