blob: 1255ab750e75b89b37cf0a4cfa784fbbe7f70f64 [file] [log] [blame]
khenaidoo106c61a2021-08-11 18:05:46 -04001/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoo106c61a2021-08-11 18:05:46 -04003
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
Joey Armstrong3f0e2422023-07-05 18:25:41 -040017// Package mocks provides the mocks for openolt-adapter.
khenaidoo106c61a2021-08-11 18:05:46 -040018package mocks
19
20import (
21 "context"
22 "errors"
23 "fmt"
24 "strings"
25
khenaidoo106c61a2021-08-11 18:05:46 -040026 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
27 "github.com/opencord/voltha-protos/v5/go/common"
khenaidoodc2116e2021-10-19 17:33:19 -040028 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidooefff76e2021-12-15 16:51:30 -050029 "github.com/opencord/voltha-protos/v5/go/core_service"
khenaidoo106c61a2021-08-11 18:05:46 -040030 "github.com/opencord/voltha-protos/v5/go/voltha"
31 "google.golang.org/grpc"
bseeniva0b9cbcb2026-02-12 19:11:11 +053032 "google.golang.org/protobuf/types/known/emptypb"
khenaidoo106c61a2021-08-11 18:05:46 -040033)
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
bseeniva0b9cbcb2026-02-12 19:11:11 +053051func (mcs MockCoreService) RegisterAdapter(ctx context.Context, in *ca.AdapterRegistration, opts ...grpc.CallOption) (*emptypb.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 }
bseeniva0b9cbcb2026-02-12 19:11:11 +053055 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -040056}
57
58// DeviceUpdate implements mock DeviceUpdate
bseeniva0b9cbcb2026-02-12 19:11:11 +053059func (mcs MockCoreService) DeviceUpdate(ctx context.Context, in *voltha.Device, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -040060 if in.Id == "" {
61 return nil, errors.New("no Device")
62 }
bseeniva0b9cbcb2026-02-12 19:11:11 +053063 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -040064}
65
66// PortCreated implements mock PortCreated
bseeniva0b9cbcb2026-02-12 19:11:11 +053067func (mcs MockCoreService) PortCreated(ctx context.Context, in *voltha.Port, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -040068 if in.DeviceId == "" {
69 return nil, errors.New("no deviceID")
70 }
71 if in.Type > 7 {
72 return nil, errors.New("invalid porttype")
73 }
bseeniva0b9cbcb2026-02-12 19:11:11 +053074 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -040075}
76
77// PortsStateUpdate implements mock PortsStateUpdate
bseeniva0b9cbcb2026-02-12 19:11:11 +053078func (mcs MockCoreService) PortsStateUpdate(ctx context.Context, in *ca.PortStateFilter, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -040079 if in.DeviceId == "" {
80 return nil, errors.New("no Device")
81 }
bseeniva0b9cbcb2026-02-12 19:11:11 +053082 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -040083}
84
85// DeleteAllPorts implements mock DeleteAllPorts
bseeniva0b9cbcb2026-02-12 19:11:11 +053086func (mcs MockCoreService) DeleteAllPorts(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -040087 if in.Id == "" {
88 return nil, errors.New("no Device id")
89 }
bseeniva0b9cbcb2026-02-12 19:11:11 +053090 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -040091}
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
bseeniva0b9cbcb2026-02-12 19:11:11 +0530113func (mcs MockCoreService) DeviceStateUpdate(ctx context.Context, in *ca.DeviceStateFilter, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400114 if in.DeviceId == "" {
115 return nil, errors.New("no Device id")
116 }
bseeniva0b9cbcb2026-02-12 19:11:11 +0530117 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400118}
119
120// DevicePMConfigUpdate implements mock DevicePMConfigUpdate
bseeniva0b9cbcb2026-02-12 19:11:11 +0530121func (mcs MockCoreService) DevicePMConfigUpdate(ctx context.Context, in *voltha.PmConfigs, opts ...grpc.CallOption) (*emptypb.Empty, error) {
122 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400123}
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
bseeniva0b9cbcb2026-02-12 19:11:11 +0530139func (mcs MockCoreService) ChildDevicesLost(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400140 if in.Id == "" {
141 return nil, errors.New("no device id")
142 }
bseeniva0b9cbcb2026-02-12 19:11:11 +0530143 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400144}
145
146// ChildDevicesDetected implements mock ChildDevicesDetected
bseeniva0b9cbcb2026-02-12 19:11:11 +0530147func (mcs MockCoreService) ChildDevicesDetected(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400148 if in.Id == "" {
149 return nil, errors.New("no device id")
150 }
bseeniva0b9cbcb2026-02-12 19:11:11 +0530151 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400152}
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
Akash Reddy Kankanala3c8b4eb2026-02-16 06:27:35 +0000168func (mcs MockCoreService) GetChildDevice(
169 ctx context.Context,
170 in *ca.ChildDeviceFilter,
171 opts ...grpc.CallOption,
172) (*voltha.Device, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400173 if in.ParentId == "" {
174 return nil, errors.New("device detection failed")
175 }
Akash Reddy Kankanala3c8b4eb2026-02-16 06:27:35 +0000176 if in.OnuId == nil {
177 return nil, errors.New("device detection failed")
178 }
khenaidoo106c61a2021-08-11 18:05:46 -0400179 var onuDevice *voltha.Device
180 for _, val := range mcs.Devices {
Akash Reddy Kankanala3c8b4eb2026-02-16 06:27:35 +0000181 if val.GetId() == fmt.Sprintf("%d", *in.OnuId) {
khenaidoo106c61a2021-08-11 18:05:46 -0400182 onuDevice = val
183 break
184 }
185 }
186 if onuDevice != nil {
187 return onuDevice, nil
188 }
khenaidoo106c61a2021-08-11 18:05:46 -0400189 return nil, errors.New("device detection failed")
190}
191
192// GetChildDevices implements mock GetChildDevices
193func (mcs MockCoreService) GetChildDevices(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*voltha.Devices, error) {
194 if in.Id == "" {
195 return nil, errors.New("no deviceID")
196 }
197 onuDevices := make([]*voltha.Device, 0)
198
199 for _, val := range mcs.Devices {
200 if val != nil && val.ParentId == in.Id {
201 onuDevices = append(onuDevices, val)
202 }
203 }
204
205 deviceList := &voltha.Devices{Items: onuDevices}
206 if len(deviceList.Items) > 0 {
207 return deviceList, nil
208 }
209 return nil, errors.New("device detection failed")
210}
211
212// SendPacketIn implements mock SendPacketIn
bseeniva0b9cbcb2026-02-12 19:11:11 +0530213func (mcs MockCoreService) SendPacketIn(ctx context.Context, in *ca.PacketIn, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400214 if in.DeviceId == "" {
215 return nil, errors.New("no Device ID")
216 }
bseeniva0b9cbcb2026-02-12 19:11:11 +0530217 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400218}
219
220// DeviceReasonUpdate implements mock DeviceReasonUpdate
bseeniva0b9cbcb2026-02-12 19:11:11 +0530221func (mcs MockCoreService) DeviceReasonUpdate(ctx context.Context, in *ca.DeviceReason, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400222 if in.DeviceId == "" {
223 return nil, errors.New("no Device ID")
224 }
bseeniva0b9cbcb2026-02-12 19:11:11 +0530225 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400226}
227
228// PortStateUpdate implements mock PortStateUpdate
bseeniva0b9cbcb2026-02-12 19:11:11 +0530229func (mcs MockCoreService) PortStateUpdate(ctx context.Context, in *ca.PortState, opts ...grpc.CallOption) (*emptypb.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400230 if in.DeviceId == "" {
231 return nil, errors.New("no Device")
232 }
bseeniva0b9cbcb2026-02-12 19:11:11 +0530233 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400234}
235
khenaidooefff76e2021-12-15 16:51:30 -0500236// GetHealthStatus implements mock GetHealthStatus
237func (mcs MockCoreService) GetHealthStatus(ctx context.Context, opts ...grpc.CallOption) (core_service.CoreService_GetHealthStatusClient, error) {
238 return nil, nil
239}
240
khenaidoo106c61a2021-08-11 18:05:46 -0400241// Additional API found in the Core - unused?
242
243// ReconcileChildDevices implements mock ReconcileChildDevices
bseeniva0b9cbcb2026-02-12 19:11:11 +0530244func (mcs MockCoreService) ReconcileChildDevices(ctx context.Context, in *common.ID, opts ...grpc.CallOption) (*emptypb.Empty, error) {
245 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400246}
247
248// GetChildDeviceWithProxyAddress implements mock GetChildDeviceWithProxyAddress
249func (mcs MockCoreService) GetChildDeviceWithProxyAddress(ctx context.Context, in *voltha.Device_ProxyAddress, opts ...grpc.CallOption) (*voltha.Device, error) {
250 return nil, nil
251}
252
253// GetPorts implements mock GetPorts
khenaidoodc2116e2021-10-19 17:33:19 -0400254func (mcs MockCoreService) GetPorts(ctx context.Context, in *ca.PortFilter, opts ...grpc.CallOption) (*voltha.Ports, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400255 return nil, nil
256}
257
258// ChildrenStateUpdate implements mock ChildrenStateUpdate
bseeniva0b9cbcb2026-02-12 19:11:11 +0530259func (mcs MockCoreService) ChildrenStateUpdate(ctx context.Context, in *ca.DeviceStateFilter, opts ...grpc.CallOption) (*emptypb.Empty, error) {
260 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400261}
262
263// UpdateImageDownload implements mock UpdateImageDownload
bseeniva0b9cbcb2026-02-12 19:11:11 +0530264func (mcs MockCoreService) UpdateImageDownload(ctx context.Context, in *voltha.ImageDownload, opts ...grpc.CallOption) (*emptypb.Empty, error) {
265 return &emptypb.Empty{}, nil
khenaidoo106c61a2021-08-11 18:05:46 -0400266}