khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 1 | /* |
| 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. |
| 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" |
| 30 | "github.com/opencord/voltha-protos/v5/go/health" |
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 { |
| 37 | cc, _ := vgrpc.NewClient("mock-endpoint", nil) |
| 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 | |
| 50 | // GetHealthStatus implements mock GetHealthStatus |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 51 | func (mcs MockCoreService) GetHealthStatus(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*health.HealthStatus, error) { |
| 52 | return &health.HealthStatus{State: health.HealthStatus_HEALTHY}, nil |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // RegisterAdapter implements mock RegisterAdapter |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 56 | 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] | 57 | 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 |
| 64 | func (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 |
| 72 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 83 | 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] | 84 | if in.DeviceId == "" { |
| 85 | return nil, errors.New("no Device") |
| 86 | } |
| 87 | return &empty.Empty{}, nil |
| 88 | } |
| 89 | |
| 90 | // DeleteAllPorts implements mock DeleteAllPorts |
| 91 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 99 | 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] | 100 | 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 |
| 109 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 118 | 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] | 119 | if in.DeviceId == "" { |
| 120 | return nil, errors.New("no Device id") |
| 121 | } |
| 122 | return &empty.Empty{}, nil |
| 123 | } |
| 124 | |
| 125 | // DevicePMConfigUpdate implements mock DevicePMConfigUpdate |
| 126 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 131 | 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] | 132 | 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 |
| 144 | func (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 |
| 152 | func (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 |
| 160 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 173 | 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] | 174 | 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 |
| 192 | func (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 |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 212 | 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] | 213 | if in.DeviceId == "" { |
| 214 | return nil, errors.New("no Device ID") |
| 215 | } |
| 216 | return &empty.Empty{}, nil |
| 217 | } |
| 218 | |
| 219 | // DeviceReasonUpdate implements mock DeviceReasonUpdate |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 220 | 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] | 221 | if in.DeviceId == "" { |
| 222 | return nil, errors.New("no Device ID") |
| 223 | } |
| 224 | return &empty.Empty{}, nil |
| 225 | } |
| 226 | |
| 227 | // PortStateUpdate implements mock PortStateUpdate |
khenaidoo | dc2116e | 2021-10-19 17:33:19 -0400 | [diff] [blame] | 228 | 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] | 229 | 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 |
| 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 | } |