kdarapu | 3248f9a | 2019-10-03 13:54:52 +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 ( |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 21 | "context" |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 22 | "encoding/json" |
| 23 | "errors" |
| 24 | "strconv" |
| 25 | "strings" |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 26 | "time" |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 27 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 29 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager" |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 30 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 31 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 32 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 33 | openolt "github.com/opencord/voltha-protos/v3/go/openolt" |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | const ( |
| 37 | // MeterConfig meter to extarct meter |
| 38 | MeterConfig = "meter_id" |
| 39 | // TpIDPathSuffix to extract Techprofile |
| 40 | TpIDPathSuffix = "tp_id" |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 41 | // FlowIDpool to extract Flow ids |
| 42 | FlowIDpool = "flow_id_pool" |
| 43 | // FlowIDs to extract flow_ids |
| 44 | FlowIDs = "flow_ids" |
| 45 | // FlowIDInfo to extract flowId info |
| 46 | FlowIDInfo = "flow_id_info" |
| 47 | // GemportIDs to gemport_ids |
| 48 | GemportIDs = "gemport_ids" |
| 49 | // AllocIDs to extract alloc_ids |
| 50 | AllocIDs = "alloc_ids" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 51 | //FlowGroup flow_groups/<flow_group_id> |
| 52 | FlowGroup = "flow_groups" |
| 53 | //FlowGroupCached flow_groups_cached/<flow_group_id> |
| 54 | FlowGroupCached = "flow_groups_cached" |
Esin Karaman | 04166c8 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 55 | //OnuPacketIn to extract gem port from packet-in |
| 56 | OnuPacketIn = "onu_packetin" |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 57 | ) |
| 58 | |
| 59 | // MockKVClient mocks the AdapterProxy interface. |
| 60 | type MockKVClient struct { |
| 61 | } |
| 62 | |
| 63 | // List mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 64 | func (kvclient *MockKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 65 | if key != "" { |
| 66 | maps := make(map[string]*kvstore.KVPair) |
| 67 | maps[key] = &kvstore.KVPair{Key: key} |
| 68 | return maps, nil |
| 69 | } |
| 70 | return nil, errors.New("key didn't find") |
| 71 | } |
| 72 | |
| 73 | // Get mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 74 | func (kvclient *MockKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 75 | logger.Debugw("Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key}) |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 76 | if key != "" { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 77 | logger.Debug("Warning Key Not Blank") |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 78 | if strings.Contains(key, "meter_id/{0,62,8}/{upstream}") { |
| 79 | meterConfig := ofp.OfpMeterConfig{ |
| 80 | Flags: 0, |
| 81 | MeterId: 1, |
| 82 | } |
| 83 | str, _ := json.Marshal(meterConfig) |
| 84 | return kvstore.NewKVPair(key, string(str), "mock", 3000, 1), nil |
| 85 | } |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 86 | if strings.Contains(key, MeterConfig) { |
| 87 | var bands []*ofp.OfpMeterBandHeader |
| 88 | bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK, |
| 89 | Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}}) |
| 90 | |
| 91 | bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK, |
| 92 | Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}}) |
| 93 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 94 | sep := strings.Split(key, "/")[1] |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 95 | val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32) |
| 96 | if uint32(val) > 1 { |
| 97 | meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands} |
| 98 | str, _ := json.Marshal(meterConfig) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 99 | |
| 100 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 101 | } |
| 102 | |
| 103 | if strings.Contains(key, "meter_id/{1,1,1}/{downstream}") { |
| 104 | |
| 105 | band1 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000} |
| 106 | band2 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000} |
| 107 | bands := []*ofp.OfpMeterBandHeader{band1, band2} |
| 108 | ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands} |
| 109 | str, _ := json.Marshal(ofpMeterConfig) |
| 110 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 111 | } |
| 112 | if uint32(val) == 1 { |
| 113 | return nil, nil |
| 114 | } |
| 115 | return nil, errors.New("invalid meter") |
| 116 | } |
| 117 | if strings.Contains(key, TpIDPathSuffix) { |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 118 | str, _ := json.Marshal(64) |
| 119 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 120 | } |
| 121 | if strings.Contains(key, FlowIDpool) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 122 | logger.Debug("Error Error Error Key:", FlowIDpool) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 123 | data := make(map[string]interface{}) |
| 124 | data["pool"] = "1024" |
| 125 | data["start_idx"] = 1 |
| 126 | data["end_idx"] = 1024 |
| 127 | str, _ := json.Marshal(data) |
| 128 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 129 | } |
| 130 | if strings.Contains(key, FlowIDs) { |
| 131 | data := []uint32{1, 2} |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 132 | logger.Debug("Error Error Error Key:", FlowIDs) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 133 | str, _ := json.Marshal(data) |
| 134 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 135 | } |
| 136 | if strings.Contains(key, FlowIDInfo) { |
| 137 | |
| 138 | data := []resourcemanager.FlowInfo{ |
| 139 | { |
| 140 | Flow: &openolt.Flow{FlowId: 1, OnuId: 1, UniId: 1, GemportId: 1}, |
| 141 | FlowStoreCookie: uint64(48132224281636694), |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 142 | LogicalFlowID: 1, |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 143 | }, |
| 144 | } |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 145 | logger.Debug("Error Error Error Key:", FlowIDs) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 146 | str, _ := json.Marshal(data) |
| 147 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 148 | } |
| 149 | if strings.Contains(key, GemportIDs) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 150 | logger.Debug("Error Error Error Key:", GemportIDs) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 151 | str, _ := json.Marshal(1) |
| 152 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 153 | } |
| 154 | if strings.Contains(key, AllocIDs) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 155 | logger.Debug("Error Error Error Key:", AllocIDs) |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 156 | str, _ := json.Marshal(1) |
| 157 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 158 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 159 | if strings.Contains(key, FlowGroup) || strings.Contains(key, FlowGroupCached) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 160 | logger.Debug("Error Error Error Key:", FlowGroup) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 161 | groupInfo := resourcemanager.GroupInfo{ |
| 162 | GroupID: 2, |
| 163 | OutPorts: []uint32{1}, |
| 164 | } |
| 165 | str, _ := json.Marshal(&groupInfo) |
| 166 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 167 | } |
| 168 | |
Esin Karaman | 04166c8 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 169 | if strings.Contains(key, OnuPacketIn) { |
| 170 | return getPacketInGemPort(key) |
| 171 | } |
| 172 | |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 173 | maps := make(map[string]*kvstore.KVPair) |
| 174 | maps[key] = &kvstore.KVPair{Key: key} |
| 175 | return maps[key], nil |
| 176 | } |
| 177 | return nil, errors.New("key didn't find") |
| 178 | } |
| 179 | |
Esin Karaman | 04166c8 | 2020-07-16 14:23:33 +0000 | [diff] [blame] | 180 | //getPacketInGemPort returns the GEM port associated with the given key |
| 181 | func getPacketInGemPort(key string) (*kvstore.KVPair, error) { |
| 182 | //parse interface, onu, uni, vlan, priority values |
| 183 | arr := getParamsFromPacketInKey(key) |
| 184 | |
| 185 | if len(arr) < 5 { |
| 186 | return nil, errors.New("key didn't find") |
| 187 | } |
| 188 | if arr[0] == "1" && arr[1] == "1" && arr[2] == "3" && arr[3] == "0" && arr[4] == "0" { |
| 189 | str, _ := json.Marshal(3) |
| 190 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 191 | } |
| 192 | if arr[0] == "2" && arr[1] == "2" && arr[2] == "4" && arr[3] == "549" && arr[4] == "0" { |
| 193 | str, _ := json.Marshal(4) |
| 194 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 195 | } |
| 196 | if arr[0] == "1" && arr[1] == "2" && arr[2] == "2" && arr[3] == "48" && arr[4] == "7" { |
| 197 | str, _ := json.Marshal(2) |
| 198 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 199 | } |
| 200 | return nil, errors.New("key didn't find") |
| 201 | } |
| 202 | |
| 203 | //getParamsFromPacketInKey parse packetIn key that is in the format of "onu_packetin/{1,1,1,1,2}" |
| 204 | func getParamsFromPacketInKey(key string) []string { |
| 205 | //return intfID, onuID, uniID, vlanID, priority |
| 206 | firstIndex := strings.Index(key, "{") |
| 207 | lastIndex := strings.Index(key, "}") |
| 208 | if firstIndex == -1 && lastIndex == -1 { |
| 209 | return []string{} |
| 210 | } |
| 211 | arr := strings.Split(key[firstIndex+1:lastIndex], ",") |
| 212 | if len(arr) < 5 { |
| 213 | return []string{} |
| 214 | } |
| 215 | return arr |
| 216 | } |
| 217 | |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 218 | // Put mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 219 | func (kvclient *MockKVClient) Put(ctx context.Context, key string, value interface{}) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 220 | if key != "" { |
| 221 | |
| 222 | return nil |
| 223 | } |
| 224 | return errors.New("key didn't find") |
| 225 | } |
| 226 | |
| 227 | // Delete mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 228 | func (kvclient *MockKVClient) Delete(ctx context.Context, key string) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 229 | if key == "" { |
| 230 | return errors.New("key didn't find") |
| 231 | } |
| 232 | return nil |
| 233 | } |
| 234 | |
| 235 | // Reserve mock function implementation for KVClient |
Neha Sharma | cc65696 | 2020-04-14 14:26:11 +0000 | [diff] [blame] | 236 | func (kvclient *MockKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 237 | if key != "" { |
| 238 | maps := make(map[string]*kvstore.KVPair) |
| 239 | maps[key] = &kvstore.KVPair{Key: key} |
| 240 | return maps[key], nil |
| 241 | } |
| 242 | return nil, errors.New("key didn't find") |
| 243 | } |
| 244 | |
| 245 | // ReleaseReservation mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 246 | func (kvclient *MockKVClient) ReleaseReservation(ctx context.Context, key string) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 247 | // return nil |
| 248 | if key == "" { |
| 249 | return errors.New("key didn't find") |
| 250 | } |
| 251 | return nil |
| 252 | } |
| 253 | |
| 254 | // ReleaseAllReservations mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 255 | func (kvclient *MockKVClient) ReleaseAllReservations(ctx context.Context) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 256 | return nil |
| 257 | } |
| 258 | |
| 259 | // RenewReservation mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 260 | func (kvclient *MockKVClient) RenewReservation(ctx context.Context, key string) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 261 | // return nil |
| 262 | if key == "" { |
| 263 | return errors.New("key didn't find") |
| 264 | } |
| 265 | return nil |
| 266 | } |
| 267 | |
| 268 | // Watch mock function implementation for KVClient |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 269 | func (kvclient *MockKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 270 | return nil |
| 271 | // if key == "" { |
| 272 | // return nil |
| 273 | // } |
| 274 | // return &kvstore.Event{EventType: 1, Key: key} |
| 275 | } |
| 276 | |
| 277 | // AcquireLock mock function implementation for KVClient |
Neha Sharma | cc65696 | 2020-04-14 14:26:11 +0000 | [diff] [blame] | 278 | func (kvclient *MockKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 279 | return nil |
| 280 | } |
| 281 | |
| 282 | // ReleaseLock mock function implementation for KVClient |
| 283 | func (kvclient *MockKVClient) ReleaseLock(lockName string) error { |
| 284 | return nil |
| 285 | } |
| 286 | |
| 287 | // IsConnectionUp mock function implementation for KVClient |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 288 | func (kvclient *MockKVClient) IsConnectionUp(ctx context.Context) bool { |
| 289 | // timeout in second |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 290 | t, _ := ctx.Deadline() |
| 291 | if t.Second()-time.Now().Second() < 1 { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 292 | return false |
| 293 | } |
| 294 | return true |
| 295 | } |
| 296 | |
| 297 | // CloseWatch mock function implementation for KVClient |
| 298 | func (kvclient *MockKVClient) CloseWatch(key string, ch chan *kvstore.Event) { |
| 299 | } |
| 300 | |
| 301 | // Close mock function implementation for KVClient |
| 302 | func (kvclient *MockKVClient) Close() { |
| 303 | } |