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 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 26 | "github.com/opencord/voltha-protos/v3/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 | } |
| 135 | return nil, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 136 | } |
| 137 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 138 | // ChildDevicesLost implements mock ChildDevicesLost. |
| 139 | func (mcp *MockCoreProxy) ChildDevicesLost(ctx context.Context, parentdeviceID string) error { |
| 140 | //panic("implement me") |
| 141 | if parentdeviceID == "" { |
| 142 | return errors.New("no device id") |
| 143 | } |
| 144 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // ChildDevicesDetected implements mock ChildDevicesDetecte |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 148 | func (mcp *MockCoreProxy) ChildDevicesDetected(ctx context.Context, parentdeviceID string) error { |
| 149 | if parentdeviceID == "" { |
| 150 | return errors.New("no device id") |
| 151 | } |
| 152 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // GetDevice implements mock GetDevice |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 156 | func (mcp *MockCoreProxy) GetDevice(ctx context.Context, parentdeviceID string, deviceID string) (*voltha.Device, error) { |
| 157 | if parentdeviceID == "" || deviceID == "" { |
| 158 | return &voltha.Device{}, errors.New("no deviceID") |
| 159 | } |
| 160 | for k, v := range mcp.Devices { |
| 161 | if k == "olt" { |
| 162 | return v, nil |
| 163 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 164 | } |
| 165 | return nil, errors.New("device detection failed") |
| 166 | } |
| 167 | |
| 168 | // GetChildDevice implements mock GetChildDevice |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 169 | func (mcp *MockCoreProxy) GetChildDevice(ctx context.Context, parentdeviceID string, kwargs map[string]interface{}) (*voltha.Device, error) { |
| 170 | |
| 171 | if parentdeviceID == "" { |
| 172 | return nil, errors.New("device detection failed") |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 173 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 174 | onuID := kwargs["onu_id"] |
| 175 | var onuDevice *voltha.Device |
| 176 | for _, val := range mcp.Devices { |
| 177 | if val.GetId() == fmt.Sprintf("%v", onuID) { |
| 178 | onuDevice = val |
| 179 | break |
| 180 | } |
| 181 | } |
| 182 | if onuDevice != nil { |
| 183 | return onuDevice, nil |
| 184 | } |
| 185 | //return &voltha.Device{}, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 186 | return nil, errors.New("device detection failed") |
| 187 | } |
| 188 | |
| 189 | // GetChildDevices implements mock GetChildDevices |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 190 | func (mcp *MockCoreProxy) GetChildDevices(ctx context.Context, parentdeviceID string) (*voltha.Devices, error) { |
| 191 | if parentdeviceID == "" { |
| 192 | return nil, errors.New("no deviceID") |
| 193 | } |
| 194 | onuDevices := make([]*voltha.Device, 0) |
| 195 | |
| 196 | for _, val := range mcp.Devices { |
| 197 | if val != nil { |
| 198 | onuDevices = append(onuDevices, val) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | deviceList := &voltha.Devices{Items: onuDevices} |
| 203 | if len(deviceList.Items) > 0 { |
| 204 | return deviceList, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 205 | } |
| 206 | return nil, errors.New("device detection failed") |
| 207 | } |
| 208 | |
| 209 | // SendPacketIn implements mock SendPacketIn |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 210 | func (mcp *MockCoreProxy) SendPacketIn(ctx context.Context, deviceID string, port uint32, pktPayload []byte) error { |
| 211 | if deviceID == "" { |
| 212 | return errors.New("no Device ID") |
| 213 | } |
| 214 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 215 | } |
David Bainbridge | be7cac1 | 2019-10-23 19:53:07 +0000 | [diff] [blame] | 216 | |
| 217 | // DeviceReasonUpdate implements mock SendPacketIn |
| 218 | func (mcp *MockCoreProxy) DeviceReasonUpdate(ctx context.Context, deviceID string, reason string) error { |
| 219 | if deviceID == "" { |
| 220 | return errors.New("no Device ID") |
| 221 | } |
| 222 | return nil |
| 223 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 224 | |
| 225 | // DevicePMConfigUpdate implements mock DevicePMConfigUpdate |
| 226 | func (mcp *MockCoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
| 227 | return nil |
| 228 | } |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 229 | |
| 230 | // PortStateUpdate implements mock PortStateUpdate |
| 231 | 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] | 232 | operStatus voltha.OperStatus_Types) error { |
kdarapu | 1afeceb | 2020-02-12 01:38:09 -0500 | [diff] [blame] | 233 | if deviceID == "" { |
| 234 | return errors.New("no Device") |
| 235 | } |
Chaitrashree G S | ded0a83 | 2020-01-09 20:21:48 -0500 | [diff] [blame] | 236 | return nil |
| 237 | } |