blob: 1825f5eefa15d05e47d6dc0d4a9d381887c05007 [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"
khenaidooefff76e2021-12-15 16:51:30 -050030 "github.com/opencord/voltha-protos/v5/go/core_service"
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 {
khenaidooefff76e2021-12-15 16:51:30 -050037 cc, _ := vgrpc.NewClient("mock-core-endpoint", "mock-server-endpoint", "core_service.CoreService", nil)
khenaidoo106c61a2021-08-11 18:05:46 -040038 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
khenaidoo106c61a2021-08-11 18:05:46 -040050// RegisterAdapter implements mock RegisterAdapter
khenaidoodc2116e2021-10-19 17:33:19 -040051func (mcs MockCoreService) RegisterAdapter(ctx context.Context, in *ca.AdapterRegistration, opts ...grpc.CallOption) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -040052 if ctx == nil || in.Adapter == nil || in.DTypes == nil {
53 return nil, errors.New("registerAdapter func parameters cannot be nil")
54 }
55 return &empty.Empty{}, nil
56}
57
58// DeviceUpdate implements mock DeviceUpdate
59func (mcs MockCoreService) DeviceUpdate(ctx context.Context, in *voltha.Device, opts ...grpc.CallOption) (*empty.Empty, error) {
60 if in.Id == "" {
61 return nil, errors.New("no Device")
62 }
63 return &empty.Empty{}, nil
64}
65
66// PortCreated implements mock PortCreated
67func (mcs MockCoreService) PortCreated(ctx context.Context, in *voltha.Port, opts ...grpc.CallOption) (*empty.Empty, error) {
68 if in.DeviceId == "" {
69 return nil, errors.New("no deviceID")
70 }
71 if in.Type > 7 {
72 return nil, errors.New("invalid porttype")
73 }
74 return &empty.Empty{}, nil
75}
76
77// PortsStateUpdate implements mock PortsStateUpdate
khenaidoodc2116e2021-10-19 17:33:19 -040078func (mcs MockCoreService) PortsStateUpdate(ctx context.Context, in *ca.PortStateFilter, opts ...grpc.CallOption) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -040079 if in.DeviceId == "" {
80 return nil, errors.New("no Device")
81 }
82 return &empty.Empty{}, nil
83}
84
85// DeleteAllPorts implements mock DeleteAllPorts
86func (mcs MockCoreService) DeleteAllPorts(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*empty.Empty, error) {
87 if in.Id == "" {
88 return nil, errors.New("no Device id")
89 }
90 return &empty.Empty{}, nil
91}
92
93// GetDevicePort implements mock GetDevicePort
khenaidoodc2116e2021-10-19 17:33:19 -040094func (mcs MockCoreService) GetDevicePort(ctx context.Context, in *ca.PortFilter, opts ...grpc.CallOption) (*voltha.Port, error) {
khenaidoo106c61a2021-08-11 18:05:46 -040095 for _, port := range mcs.DevicePorts[in.DeviceId] {
96 if port.PortNo == in.Port {
97 return port, nil
98 }
99 }
100 return nil, errors.New("device/port not found")
101}
102
103// ListDevicePorts implements mock ListDevicePorts
104func (mcs MockCoreService) ListDevicePorts(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*voltha.Ports, error) {
105 ports, have := mcs.DevicePorts[in.Id]
106 if !have {
107 return nil, errors.New("device id not found")
108 }
109 return &voltha.Ports{Items: ports}, nil
110}
111
112// DeviceStateUpdate implements mock DeviceStateUpdate
khenaidoodc2116e2021-10-19 17:33:19 -0400113func (mcs MockCoreService) DeviceStateUpdate(ctx context.Context, in *ca.DeviceStateFilter, opts ...grpc.CallOption) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400114 if in.DeviceId == "" {
115 return nil, errors.New("no Device id")
116 }
117 return &empty.Empty{}, nil
118}
119
120// DevicePMConfigUpdate implements mock DevicePMConfigUpdate
121func (mcs MockCoreService) DevicePMConfigUpdate(ctx context.Context, in *voltha.PmConfigs, opts ...grpc.CallOption) (*empty.Empty, error) {
122 return &empty.Empty{}, nil
123}
124
125// ChildDeviceDetected implements mock ChildDeviceDetected
khenaidoodc2116e2021-10-19 17:33:19 -0400126func (mcs MockCoreService) ChildDeviceDetected(ctx context.Context, in *ca.DeviceDiscovery, opts ...grpc.CallOption) (*voltha.Device, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400127 if in.ParentId == "" {
128 return nil, errors.New("no deviceID")
129 }
130 for k, v := range mcs.Devices {
131 if strings.Contains(k, "onu") {
132 return v, nil
133 }
134 }
135 return nil, errors.New("no deviceID")
136}
137
138// ChildDevicesLost implements mock ChildDevicesLost
139func (mcs MockCoreService) ChildDevicesLost(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*empty.Empty, error) {
140 if in.Id == "" {
141 return nil, errors.New("no device id")
142 }
143 return &empty.Empty{}, nil
144}
145
146// ChildDevicesDetected implements mock ChildDevicesDetected
147func (mcs MockCoreService) ChildDevicesDetected(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*empty.Empty, error) {
148 if in.Id == "" {
149 return nil, errors.New("no device id")
150 }
151 return &empty.Empty{}, nil
152}
153
154// GetDevice implements mock GetDevice
155func (mcs MockCoreService) GetDevice(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*voltha.Device, error) {
156 if in.Id == "" {
157 return &voltha.Device{}, errors.New("no deviceID")
158 }
159 for k, v := range mcs.Devices {
160 if k == "olt" {
161 return v, nil
162 }
163 }
164 return nil, errors.New("device detection failed")
165}
166
167// GetChildDevice implements mock GetChildDevice
khenaidoodc2116e2021-10-19 17:33:19 -0400168func (mcs MockCoreService) GetChildDevice(ctx context.Context, in *ca.ChildDeviceFilter, opts ...grpc.CallOption) (*voltha.Device, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400169 if in.ParentId == "" {
170 return nil, errors.New("device detection failed")
171 }
172 var onuDevice *voltha.Device
173 for _, val := range mcs.Devices {
174 if val.GetId() == fmt.Sprintf("%d", in.OnuId) {
175 onuDevice = val
176 break
177 }
178 }
179 if onuDevice != nil {
180 return onuDevice, nil
181 }
182 //return &voltha.Device{}, nil
183 return nil, errors.New("device detection failed")
184}
185
186// GetChildDevices implements mock GetChildDevices
187func (mcs MockCoreService) GetChildDevices(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*voltha.Devices, error) {
188 if in.Id == "" {
189 return nil, errors.New("no deviceID")
190 }
191 onuDevices := make([]*voltha.Device, 0)
192
193 for _, val := range mcs.Devices {
194 if val != nil && val.ParentId == in.Id {
195 onuDevices = append(onuDevices, val)
196 }
197 }
198
199 deviceList := &voltha.Devices{Items: onuDevices}
200 if len(deviceList.Items) > 0 {
201 return deviceList, nil
202 }
203 return nil, errors.New("device detection failed")
204}
205
206// SendPacketIn implements mock SendPacketIn
khenaidoodc2116e2021-10-19 17:33:19 -0400207func (mcs MockCoreService) SendPacketIn(ctx context.Context, in *ca.PacketIn, opts ...grpc.CallOption) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400208 if in.DeviceId == "" {
209 return nil, errors.New("no Device ID")
210 }
211 return &empty.Empty{}, nil
212}
213
214// DeviceReasonUpdate implements mock DeviceReasonUpdate
khenaidoodc2116e2021-10-19 17:33:19 -0400215func (mcs MockCoreService) DeviceReasonUpdate(ctx context.Context, in *ca.DeviceReason, opts ...grpc.CallOption) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400216 if in.DeviceId == "" {
217 return nil, errors.New("no Device ID")
218 }
219 return &empty.Empty{}, nil
220}
221
222// PortStateUpdate implements mock PortStateUpdate
khenaidoodc2116e2021-10-19 17:33:19 -0400223func (mcs MockCoreService) PortStateUpdate(ctx context.Context, in *ca.PortState, opts ...grpc.CallOption) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400224 if in.DeviceId == "" {
225 return nil, errors.New("no Device")
226 }
227 return &empty.Empty{}, nil
228}
229
khenaidooefff76e2021-12-15 16:51:30 -0500230// GetHealthStatus implements mock GetHealthStatus
231func (mcs MockCoreService) GetHealthStatus(ctx context.Context, opts ...grpc.CallOption) (core_service.CoreService_GetHealthStatusClient, error) {
232 return nil, nil
233}
234
khenaidoo106c61a2021-08-11 18:05:46 -0400235// 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}