kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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" |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 23 | "fmt" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 24 | |
Girish Gowdra | 4c3d460 | 2021-07-22 16:33:37 -0700 | [diff] [blame^] | 25 | "github.com/opencord/voltha-lib-go/v6/pkg/kafka" |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v4/go/voltha" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | // MockCoreProxy mocks the CoreProxy interface |
| 30 | type MockCoreProxy struct { |
| 31 | // Values to be used in test can reside inside this structure |
| 32 | // TODO store relevant info in this, use this info for negative and positive tests |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 33 | Devices map[string]*voltha.Device |
| 34 | DevicePorts map[string][]*voltha.Port |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // UpdateCoreReference mock updatesCoreReference |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 38 | func (mcp *MockCoreProxy) UpdateCoreReference(deviceID string, coreReference string) { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 39 | panic("implement me") |
| 40 | } |
| 41 | |
| 42 | // DeleteCoreReference mock DeleteCoreReference function |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 43 | func (mcp *MockCoreProxy) DeleteCoreReference(deviceID string) { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 44 | panic("implement me") |
| 45 | } |
| 46 | |
| 47 | // GetCoreTopic implements mock GetCoreTopic |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 48 | func (mcp *MockCoreProxy) GetCoreTopic(deviceID string) kafka.Topic { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 49 | panic("implement me") |
| 50 | } |
| 51 | |
| 52 | // GetAdapterTopic implements mock GetAdapterTopic |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 53 | func (mcp *MockCoreProxy) GetAdapterTopic(args ...string) kafka.Topic { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 54 | panic("implement me") |
| 55 | } |
| 56 | |
| 57 | // RegisterAdapter implements mock RegisterAdapter |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 58 | func (mcp *MockCoreProxy) RegisterAdapter(ctx context.Context, adapter *voltha.Adapter, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 59 | deviceTypes *voltha.DeviceTypes) error { |
| 60 | if ctx == nil || adapter == nil || deviceTypes == nil { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 61 | return errors.New("registerAdapter func parameters cannot be nil") |
| 62 | } |
| 63 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // DeviceUpdate implements mock DeviceUpdate |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 67 | func (mcp *MockCoreProxy) DeviceUpdate(ctx context.Context, device *voltha.Device) error { |
| 68 | if device.Id == "" { |
| 69 | return errors.New("no Device") |
| 70 | } |
| 71 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // PortCreated implements mock PortCreated |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 75 | func (mcp *MockCoreProxy) PortCreated(ctx context.Context, deviceID string, port *voltha.Port) error { |
| 76 | if deviceID == "" { |
| 77 | return errors.New("no deviceID") |
| 78 | } |
| 79 | if port.Type > 7 { |
| 80 | return errors.New("invalid porttype") |
| 81 | } |
| 82 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // PortsStateUpdate implements mock PortsStateUpdate |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 86 | func (mcp *MockCoreProxy) PortsStateUpdate(ctx context.Context, deviceID string, portTypeFilter uint32, operStatus voltha.OperStatus_Types) error { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 87 | if deviceID == "" { |
| 88 | return errors.New("no Device") |
| 89 | } |
| 90 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // DeleteAllPorts implements mock DeleteAllPorts |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 94 | func (mcp *MockCoreProxy) DeleteAllPorts(ctx context.Context, deviceID string) error { |
| 95 | if deviceID == "" { |
| 96 | return errors.New("no Device id") |
| 97 | } |
| 98 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 99 | } |
| 100 | |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 101 | // GetDevicePort implements mock GetDevicePort |
| 102 | func (mcp *MockCoreProxy) GetDevicePort(ctx context.Context, deviceID string, portID uint32) (*voltha.Port, error) { |
| 103 | for _, port := range mcp.DevicePorts[deviceID] { |
| 104 | if port.PortNo == portID { |
| 105 | return port, nil |
| 106 | } |
| 107 | } |
| 108 | return nil, errors.New("device/port not found") |
| 109 | } |
| 110 | |
| 111 | // ListDevicePorts implements mock ListDevicePorts |
| 112 | func (mcp *MockCoreProxy) ListDevicePorts(ctx context.Context, deviceID string) ([]*voltha.Port, error) { |
| 113 | ports, have := mcp.DevicePorts[deviceID] |
| 114 | if !have { |
| 115 | return nil, errors.New("device id not found") |
| 116 | } |
| 117 | return ports, nil |
| 118 | } |
| 119 | |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 120 | // DeviceStateUpdate implements mock DeviceStateUpdate |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 121 | func (mcp *MockCoreProxy) DeviceStateUpdate(ctx context.Context, deviceID string, |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 122 | connStatus voltha.ConnectStatus_Types, operStatus voltha.OperStatus_Types) error { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 123 | if deviceID == "" { |
| 124 | return errors.New("no Device id") |
| 125 | } |
| 126 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // ChildDeviceDetected implements mock ChildDeviceDetected |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 130 | func (mcp *MockCoreProxy) ChildDeviceDetected(ctx context.Context, parentdeviceID string, parentPortNo int, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 131 | childDeviceType string, channelID int, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 132 | if parentdeviceID == "" { |
| 133 | return nil, errors.New("no deviceID") |
| 134 | } |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 135 | for k, v := range mcp.Devices { |
| 136 | if k == "olt" { |
| 137 | return v, nil |
| 138 | } |
| 139 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 140 | return nil, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 141 | } |
| 142 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 143 | // ChildDevicesLost implements mock ChildDevicesLost. |
| 144 | func (mcp *MockCoreProxy) ChildDevicesLost(ctx context.Context, parentdeviceID string) error { |
| 145 | //panic("implement me") |
| 146 | if parentdeviceID == "" { |
| 147 | return errors.New("no device id") |
| 148 | } |
| 149 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // ChildDevicesDetected implements mock ChildDevicesDetecte |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 153 | func (mcp *MockCoreProxy) ChildDevicesDetected(ctx context.Context, parentdeviceID string) error { |
| 154 | if parentdeviceID == "" { |
| 155 | return errors.New("no device id") |
| 156 | } |
| 157 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // GetDevice implements mock GetDevice |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 161 | func (mcp *MockCoreProxy) GetDevice(ctx context.Context, parentdeviceID string, deviceID string) (*voltha.Device, error) { |
| 162 | if parentdeviceID == "" || deviceID == "" { |
| 163 | return &voltha.Device{}, errors.New("no deviceID") |
| 164 | } |
| 165 | for k, v := range mcp.Devices { |
| 166 | if k == "olt" { |
| 167 | return v, nil |
| 168 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 169 | } |
| 170 | return nil, errors.New("device detection failed") |
| 171 | } |
| 172 | |
| 173 | // GetChildDevice implements mock GetChildDevice |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 174 | func (mcp *MockCoreProxy) GetChildDevice(ctx context.Context, parentdeviceID string, kwargs map[string]interface{}) (*voltha.Device, error) { |
| 175 | |
| 176 | if parentdeviceID == "" { |
| 177 | return nil, errors.New("device detection failed") |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 178 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 179 | onuID := kwargs["onu_id"] |
| 180 | var onuDevice *voltha.Device |
| 181 | for _, val := range mcp.Devices { |
| 182 | if val.GetId() == fmt.Sprintf("%v", onuID) { |
| 183 | onuDevice = val |
| 184 | break |
| 185 | } |
| 186 | } |
| 187 | if onuDevice != nil { |
| 188 | return onuDevice, nil |
| 189 | } |
| 190 | //return &voltha.Device{}, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 191 | return nil, errors.New("device detection failed") |
| 192 | } |
| 193 | |
| 194 | // GetChildDevices implements mock GetChildDevices |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 195 | func (mcp *MockCoreProxy) GetChildDevices(ctx context.Context, parentdeviceID string) (*voltha.Devices, error) { |
| 196 | if parentdeviceID == "" { |
| 197 | return nil, errors.New("no deviceID") |
| 198 | } |
| 199 | onuDevices := make([]*voltha.Device, 0) |
| 200 | |
| 201 | for _, val := range mcp.Devices { |
| 202 | if val != nil { |
| 203 | onuDevices = append(onuDevices, val) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | deviceList := &voltha.Devices{Items: onuDevices} |
| 208 | if len(deviceList.Items) > 0 { |
| 209 | return deviceList, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 210 | } |
| 211 | return nil, errors.New("device detection failed") |
| 212 | } |
| 213 | |
| 214 | // SendPacketIn implements mock SendPacketIn |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 215 | func (mcp *MockCoreProxy) SendPacketIn(ctx context.Context, deviceID string, port uint32, pktPayload []byte) error { |
| 216 | if deviceID == "" { |
| 217 | return errors.New("no Device ID") |
| 218 | } |
| 219 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 220 | } |
David Bainbridge | be7cac1 | 2019-10-23 19:53:07 +0000 | [diff] [blame] | 221 | |
| 222 | // DeviceReasonUpdate implements mock SendPacketIn |
| 223 | func (mcp *MockCoreProxy) DeviceReasonUpdate(ctx context.Context, deviceID string, reason string) error { |
| 224 | if deviceID == "" { |
| 225 | return errors.New("no Device ID") |
| 226 | } |
| 227 | return nil |
| 228 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 229 | |
| 230 | // DevicePMConfigUpdate implements mock DevicePMConfigUpdate |
| 231 | func (mcp *MockCoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
| 232 | return nil |
| 233 | } |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 234 | |
| 235 | // PortStateUpdate implements mock PortStateUpdate |
| 236 | func (mcp *MockCoreProxy) PortStateUpdate(ctx context.Context, deviceID string, pType voltha.Port_PortType, portNo uint32, |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 237 | operStatus voltha.OperStatus_Types) error { |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 238 | if deviceID == "" { |
| 239 | return errors.New("no Device") |
| 240 | } |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 241 | return nil |
| 242 | } |