khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | f9bffdf | 2022-12-27 07:05:28 -0500 | [diff] [blame^] | 2 | * Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 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. |
| 18 | package mocks |
| 19 | |
| 20 | import ( |
| 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" |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 29 | ca "github.com/opencord/voltha-protos/v5/go/core_adapter" |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 30 | "github.com/opencord/voltha-protos/v5/go/core_service" |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 31 | "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 |
| 36 | func NewMockCoreClient(coreService *MockCoreService) *vgrpc.Client { |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 37 | cc, _ := vgrpc.NewClient("mock-core-endpoint", "mock-server-endpoint", "core_service.CoreService", nil) |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 38 | cc.SetService(coreService) |
| 39 | return cc |
| 40 | } |
| 41 | |
| 42 | // MockCoreService represents a mock core service |
| 43 | type 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 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 50 | // RegisterAdapter implements mock RegisterAdapter |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 51 | func (mcs MockCoreService) RegisterAdapter(ctx context.Context, in *ca.AdapterRegistration, opts ...grpc.CallOption) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 52 | 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 |
| 59 | func (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 |
| 67 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 78 | func (mcs MockCoreService) PortsStateUpdate(ctx context.Context, in *ca.PortStateFilter, opts ...grpc.CallOption) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 79 | if in.DeviceId == "" { |
| 80 | return nil, errors.New("no Device") |
| 81 | } |
| 82 | return &empty.Empty{}, nil |
| 83 | } |
| 84 | |
| 85 | // DeleteAllPorts implements mock DeleteAllPorts |
| 86 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 94 | func (mcs MockCoreService) GetDevicePort(ctx context.Context, in *ca.PortFilter, opts ...grpc.CallOption) (*voltha.Port, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 95 | 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 |
| 104 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 113 | func (mcs MockCoreService) DeviceStateUpdate(ctx context.Context, in *ca.DeviceStateFilter, opts ...grpc.CallOption) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 114 | if in.DeviceId == "" { |
| 115 | return nil, errors.New("no Device id") |
| 116 | } |
| 117 | return &empty.Empty{}, nil |
| 118 | } |
| 119 | |
| 120 | // DevicePMConfigUpdate implements mock DevicePMConfigUpdate |
| 121 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 126 | func (mcs MockCoreService) ChildDeviceDetected(ctx context.Context, in *ca.DeviceDiscovery, opts ...grpc.CallOption) (*voltha.Device, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 127 | 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 |
| 139 | func (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 |
| 147 | func (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 |
| 155 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 168 | func (mcs MockCoreService) GetChildDevice(ctx context.Context, in *ca.ChildDeviceFilter, opts ...grpc.CallOption) (*voltha.Device, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 169 | 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 |
| 187 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 207 | func (mcs MockCoreService) SendPacketIn(ctx context.Context, in *ca.PacketIn, opts ...grpc.CallOption) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 208 | if in.DeviceId == "" { |
| 209 | return nil, errors.New("no Device ID") |
| 210 | } |
| 211 | return &empty.Empty{}, nil |
| 212 | } |
| 213 | |
| 214 | // DeviceReasonUpdate implements mock DeviceReasonUpdate |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 215 | func (mcs MockCoreService) DeviceReasonUpdate(ctx context.Context, in *ca.DeviceReason, opts ...grpc.CallOption) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 216 | if in.DeviceId == "" { |
| 217 | return nil, errors.New("no Device ID") |
| 218 | } |
| 219 | return &empty.Empty{}, nil |
| 220 | } |
| 221 | |
| 222 | // PortStateUpdate implements mock PortStateUpdate |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 223 | func (mcs MockCoreService) PortStateUpdate(ctx context.Context, in *ca.PortState, opts ...grpc.CallOption) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 224 | if in.DeviceId == "" { |
| 225 | return nil, errors.New("no Device") |
| 226 | } |
| 227 | return &empty.Empty{}, nil |
| 228 | } |
| 229 | |
khenaidoo | efff76e | 2021-12-15 16:51:30 -0500 | [diff] [blame] | 230 | // GetHealthStatus implements mock GetHealthStatus |
| 231 | func (mcs MockCoreService) GetHealthStatus(ctx context.Context, opts ...grpc.CallOption) (core_service.CoreService_GetHealthStatusClient, error) { |
| 232 | return nil, nil |
| 233 | } |
| 234 | |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 235 | // Additional API found in the Core - unused? |
| 236 | |
| 237 | // ReconcileChildDevices implements mock ReconcileChildDevices |
| 238 | func (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 |
| 243 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 248 | func (mcs MockCoreService) GetPorts(ctx context.Context, in *ca.PortFilter, opts ...grpc.CallOption) (*voltha.Ports, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 249 | return nil, nil |
| 250 | } |
| 251 | |
| 252 | // ChildrenStateUpdate implements mock ChildrenStateUpdate |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 253 | func (mcs MockCoreService) ChildrenStateUpdate(ctx context.Context, in *ca.DeviceStateFilter, opts ...grpc.CallOption) (*empty.Empty, error) { |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 254 | return &empty.Empty{}, nil |
| 255 | } |
| 256 | |
| 257 | // UpdateImageDownload implements mock UpdateImageDownload |
| 258 | func (mcs MockCoreService) UpdateImageDownload(ctx context.Context, in *voltha.ImageDownload, opts ...grpc.CallOption) (*empty.Empty, error) { |
| 259 | return &empty.Empty{}, nil |
| 260 | } |