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 | |
Scott Baker | 5129015 | 2019-10-24 14:23:20 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v2/pkg/kafka" |
Scott Baker | c6e54cb | 2019-11-04 09:31:25 -0800 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v2/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 |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 33 | Devices map[string]*voltha.Device |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // UpdateCoreReference mock updatesCoreReference |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 37 | func (mcp *MockCoreProxy) UpdateCoreReference(deviceID string, coreReference string) { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 38 | panic("implement me") |
| 39 | } |
| 40 | |
| 41 | // DeleteCoreReference mock DeleteCoreReference function |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 42 | func (mcp *MockCoreProxy) DeleteCoreReference(deviceID string) { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 43 | panic("implement me") |
| 44 | } |
| 45 | |
| 46 | // GetCoreTopic implements mock GetCoreTopic |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 47 | func (mcp *MockCoreProxy) GetCoreTopic(deviceID string) kafka.Topic { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 48 | panic("implement me") |
| 49 | } |
| 50 | |
| 51 | // GetAdapterTopic implements mock GetAdapterTopic |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 52 | func (mcp *MockCoreProxy) GetAdapterTopic(args ...string) kafka.Topic { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 53 | panic("implement me") |
| 54 | } |
| 55 | |
| 56 | // RegisterAdapter implements mock RegisterAdapter |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 57 | func (mcp *MockCoreProxy) RegisterAdapter(ctx context.Context, adapter *voltha.Adapter, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 58 | deviceTypes *voltha.DeviceTypes) error { |
| 59 | if ctx == nil || adapter == nil || deviceTypes == nil { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 60 | return errors.New("registerAdapter func parameters cannot be nil") |
| 61 | } |
| 62 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // DeviceUpdate implements mock DeviceUpdate |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 66 | func (mcp *MockCoreProxy) DeviceUpdate(ctx context.Context, device *voltha.Device) error { |
| 67 | if device.Id == "" { |
| 68 | return errors.New("no Device") |
| 69 | } |
| 70 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // PortCreated implements mock PortCreated |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 74 | func (mcp *MockCoreProxy) PortCreated(ctx context.Context, deviceID string, port *voltha.Port) error { |
| 75 | if deviceID == "" { |
| 76 | return errors.New("no deviceID") |
| 77 | } |
| 78 | if port.Type > 7 { |
| 79 | return errors.New("invalid porttype") |
| 80 | } |
| 81 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // PortsStateUpdate implements mock PortsStateUpdate |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 85 | func (mcp *MockCoreProxy) PortsStateUpdate(ctx context.Context, deviceID string, operStatus voltha.OperStatus_OperStatus) error { |
| 86 | if deviceID == "" { |
| 87 | return errors.New("no Device") |
| 88 | } |
| 89 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // DeleteAllPorts implements mock DeleteAllPorts |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 93 | func (mcp *MockCoreProxy) DeleteAllPorts(ctx context.Context, deviceID string) error { |
| 94 | if deviceID == "" { |
| 95 | return errors.New("no Device id") |
| 96 | } |
| 97 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // DeviceStateUpdate implements mock DeviceStateUpdate |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 101 | func (mcp *MockCoreProxy) DeviceStateUpdate(ctx context.Context, deviceID string, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 102 | connStatus voltha.ConnectStatus_ConnectStatus, operStatus voltha.OperStatus_OperStatus) error { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 103 | if deviceID == "" { |
| 104 | return errors.New("no Device id") |
| 105 | } |
| 106 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // ChildDeviceDetected implements mock ChildDeviceDetected |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 110 | func (mcp *MockCoreProxy) ChildDeviceDetected(ctx context.Context, parentdeviceID string, parentPortNo int, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 111 | childDeviceType string, channelID int, vendorID string, serialNumber string, onuID int64) (*voltha.Device, error) { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 112 | if parentdeviceID == "" { |
| 113 | return nil, errors.New("no deviceID") |
| 114 | } |
| 115 | return nil, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 116 | } |
| 117 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 118 | // ChildDevicesLost implements mock ChildDevicesLost. |
| 119 | func (mcp *MockCoreProxy) ChildDevicesLost(ctx context.Context, parentdeviceID string) error { |
| 120 | //panic("implement me") |
| 121 | if parentdeviceID == "" { |
| 122 | return errors.New("no device id") |
| 123 | } |
| 124 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // ChildDevicesDetected implements mock ChildDevicesDetecte |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 128 | func (mcp *MockCoreProxy) ChildDevicesDetected(ctx context.Context, parentdeviceID string) error { |
| 129 | if parentdeviceID == "" { |
| 130 | return errors.New("no device id") |
| 131 | } |
| 132 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // GetDevice implements mock GetDevice |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 136 | func (mcp *MockCoreProxy) GetDevice(ctx context.Context, parentdeviceID string, deviceID string) (*voltha.Device, error) { |
| 137 | if parentdeviceID == "" || deviceID == "" { |
| 138 | return &voltha.Device{}, errors.New("no deviceID") |
| 139 | } |
| 140 | for k, v := range mcp.Devices { |
| 141 | if k == "olt" { |
| 142 | return v, nil |
| 143 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 144 | } |
| 145 | return nil, errors.New("device detection failed") |
| 146 | } |
| 147 | |
| 148 | // GetChildDevice implements mock GetChildDevice |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 149 | func (mcp *MockCoreProxy) GetChildDevice(ctx context.Context, parentdeviceID string, kwargs map[string]interface{}) (*voltha.Device, error) { |
| 150 | |
| 151 | if parentdeviceID == "" { |
| 152 | return nil, errors.New("device detection failed") |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 153 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 154 | onuID := kwargs["onu_id"] |
| 155 | var onuDevice *voltha.Device |
| 156 | for _, val := range mcp.Devices { |
| 157 | if val.GetId() == fmt.Sprintf("%v", onuID) { |
| 158 | onuDevice = val |
| 159 | break |
| 160 | } |
| 161 | } |
| 162 | if onuDevice != nil { |
| 163 | return onuDevice, nil |
| 164 | } |
| 165 | //return &voltha.Device{}, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 166 | return nil, errors.New("device detection failed") |
| 167 | } |
| 168 | |
| 169 | // GetChildDevices implements mock GetChildDevices |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 170 | func (mcp *MockCoreProxy) GetChildDevices(ctx context.Context, parentdeviceID string) (*voltha.Devices, error) { |
| 171 | if parentdeviceID == "" { |
| 172 | return nil, errors.New("no deviceID") |
| 173 | } |
| 174 | onuDevices := make([]*voltha.Device, 0) |
| 175 | |
| 176 | for _, val := range mcp.Devices { |
| 177 | if val != nil { |
| 178 | onuDevices = append(onuDevices, val) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | deviceList := &voltha.Devices{Items: onuDevices} |
| 183 | if len(deviceList.Items) > 0 { |
| 184 | return deviceList, nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 185 | } |
| 186 | return nil, errors.New("device detection failed") |
| 187 | } |
| 188 | |
| 189 | // SendPacketIn implements mock SendPacketIn |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 190 | func (mcp *MockCoreProxy) SendPacketIn(ctx context.Context, deviceID string, port uint32, pktPayload []byte) error { |
| 191 | if deviceID == "" { |
| 192 | return errors.New("no Device ID") |
| 193 | } |
| 194 | return nil |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 195 | } |
David Bainbridge | be7cac1 | 2019-10-23 19:53:07 +0000 | [diff] [blame] | 196 | |
| 197 | // DeviceReasonUpdate implements mock SendPacketIn |
| 198 | func (mcp *MockCoreProxy) DeviceReasonUpdate(ctx context.Context, deviceID string, reason string) error { |
| 199 | if deviceID == "" { |
| 200 | return errors.New("no Device ID") |
| 201 | } |
| 202 | return nil |
| 203 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 204 | |
| 205 | // DevicePMConfigUpdate implements mock DevicePMConfigUpdate |
| 206 | func (mcp *MockCoreProxy) DevicePMConfigUpdate(ctx context.Context, pmConfigs *voltha.PmConfigs) error { |
| 207 | return nil |
| 208 | } |