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" |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 55 | ) |
| 56 | |
| 57 | // MockKVClient mocks the AdapterProxy interface. |
| 58 | type MockKVClient struct { |
| 59 | } |
| 60 | |
| 61 | // List mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 62 | 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] | 63 | if key != "" { |
| 64 | maps := make(map[string]*kvstore.KVPair) |
| 65 | maps[key] = &kvstore.KVPair{Key: key} |
| 66 | return maps, nil |
| 67 | } |
| 68 | return nil, errors.New("key didn't find") |
| 69 | } |
| 70 | |
| 71 | // Get mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 72 | func (kvclient *MockKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 73 | logger.Debugw("Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key}) |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 74 | if key != "" { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 75 | logger.Debug("Warning Key Not Blank") |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 76 | if strings.Contains(key, "meter_id/{0,62,8}/{upstream}") { |
| 77 | meterConfig := ofp.OfpMeterConfig{ |
| 78 | Flags: 0, |
| 79 | MeterId: 1, |
| 80 | } |
| 81 | str, _ := json.Marshal(meterConfig) |
| 82 | return kvstore.NewKVPair(key, string(str), "mock", 3000, 1), nil |
| 83 | } |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 84 | if strings.Contains(key, MeterConfig) { |
| 85 | var bands []*ofp.OfpMeterBandHeader |
| 86 | bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK, |
| 87 | Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}}) |
| 88 | |
| 89 | bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK, |
| 90 | Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}}) |
| 91 | |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 92 | sep := strings.Split(key, "/")[1] |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 93 | val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32) |
| 94 | if uint32(val) > 1 { |
| 95 | meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands} |
| 96 | str, _ := json.Marshal(meterConfig) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 97 | |
| 98 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 99 | } |
| 100 | |
| 101 | if strings.Contains(key, "meter_id/{1,1,1}/{downstream}") { |
| 102 | |
| 103 | band1 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000} |
| 104 | band2 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000} |
| 105 | bands := []*ofp.OfpMeterBandHeader{band1, band2} |
| 106 | ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands} |
| 107 | str, _ := json.Marshal(ofpMeterConfig) |
| 108 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 109 | } |
| 110 | if uint32(val) == 1 { |
| 111 | return nil, nil |
| 112 | } |
| 113 | return nil, errors.New("invalid meter") |
| 114 | } |
| 115 | if strings.Contains(key, TpIDPathSuffix) { |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 116 | str, _ := json.Marshal(64) |
| 117 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 118 | } |
| 119 | if strings.Contains(key, FlowIDpool) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 120 | logger.Debug("Error Error Error Key:", FlowIDpool) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 121 | data := make(map[string]interface{}) |
| 122 | data["pool"] = "1024" |
| 123 | data["start_idx"] = 1 |
| 124 | data["end_idx"] = 1024 |
| 125 | str, _ := json.Marshal(data) |
| 126 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 127 | } |
| 128 | if strings.Contains(key, FlowIDs) { |
| 129 | data := []uint32{1, 2} |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 130 | logger.Debug("Error Error Error Key:", FlowIDs) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 131 | str, _ := json.Marshal(data) |
| 132 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 133 | } |
| 134 | if strings.Contains(key, FlowIDInfo) { |
| 135 | |
| 136 | data := []resourcemanager.FlowInfo{ |
| 137 | { |
| 138 | Flow: &openolt.Flow{FlowId: 1, OnuId: 1, UniId: 1, GemportId: 1}, |
| 139 | FlowStoreCookie: uint64(48132224281636694), |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 140 | LogicalFlowID: 1, |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 141 | }, |
| 142 | } |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 143 | logger.Debug("Error Error Error Key:", FlowIDs) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 144 | str, _ := json.Marshal(data) |
| 145 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 146 | } |
| 147 | if strings.Contains(key, GemportIDs) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 148 | logger.Debug("Error Error Error Key:", GemportIDs) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 149 | str, _ := json.Marshal(1) |
| 150 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 151 | } |
| 152 | if strings.Contains(key, AllocIDs) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 153 | logger.Debug("Error Error Error Key:", AllocIDs) |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 154 | str, _ := json.Marshal(1) |
| 155 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 156 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 157 | if strings.Contains(key, FlowGroup) || strings.Contains(key, FlowGroupCached) { |
Girish Kumar | 2ad402b | 2020-03-20 19:45:12 +0000 | [diff] [blame] | 158 | logger.Debug("Error Error Error Key:", FlowGroup) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 159 | groupInfo := resourcemanager.GroupInfo{ |
| 160 | GroupID: 2, |
| 161 | OutPorts: []uint32{1}, |
| 162 | } |
| 163 | str, _ := json.Marshal(&groupInfo) |
| 164 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 165 | } |
| 166 | |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 167 | maps := make(map[string]*kvstore.KVPair) |
| 168 | maps[key] = &kvstore.KVPair{Key: key} |
| 169 | return maps[key], nil |
| 170 | } |
| 171 | return nil, errors.New("key didn't find") |
| 172 | } |
| 173 | |
| 174 | // Put mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 175 | func (kvclient *MockKVClient) Put(ctx context.Context, key string, value interface{}) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 176 | if key != "" { |
| 177 | |
| 178 | return nil |
| 179 | } |
| 180 | return errors.New("key didn't find") |
| 181 | } |
| 182 | |
| 183 | // Delete mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 184 | func (kvclient *MockKVClient) Delete(ctx context.Context, key string) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 185 | if key == "" { |
| 186 | return errors.New("key didn't find") |
| 187 | } |
| 188 | return nil |
| 189 | } |
| 190 | |
| 191 | // Reserve mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 192 | func (kvclient *MockKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl int64) (interface{}, error) { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 193 | if key != "" { |
| 194 | maps := make(map[string]*kvstore.KVPair) |
| 195 | maps[key] = &kvstore.KVPair{Key: key} |
| 196 | return maps[key], nil |
| 197 | } |
| 198 | return nil, errors.New("key didn't find") |
| 199 | } |
| 200 | |
| 201 | // ReleaseReservation mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 202 | func (kvclient *MockKVClient) ReleaseReservation(ctx context.Context, key string) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 203 | // return nil |
| 204 | if key == "" { |
| 205 | return errors.New("key didn't find") |
| 206 | } |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | // ReleaseAllReservations mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 211 | func (kvclient *MockKVClient) ReleaseAllReservations(ctx context.Context) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 212 | return nil |
| 213 | } |
| 214 | |
| 215 | // RenewReservation mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 216 | func (kvclient *MockKVClient) RenewReservation(ctx context.Context, key string) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 217 | // return nil |
| 218 | if key == "" { |
| 219 | return errors.New("key didn't find") |
| 220 | } |
| 221 | return nil |
| 222 | } |
| 223 | |
| 224 | // Watch mock function implementation for KVClient |
Scott Baker | e701b86 | 2020-02-20 16:19:16 -0800 | [diff] [blame] | 225 | 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] | 226 | return nil |
| 227 | // if key == "" { |
| 228 | // return nil |
| 229 | // } |
| 230 | // return &kvstore.Event{EventType: 1, Key: key} |
| 231 | } |
| 232 | |
| 233 | // AcquireLock mock function implementation for KVClient |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 234 | func (kvclient *MockKVClient) AcquireLock(ctx context.Context, lockName string, timeout int) error { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 235 | return nil |
| 236 | } |
| 237 | |
| 238 | // ReleaseLock mock function implementation for KVClient |
| 239 | func (kvclient *MockKVClient) ReleaseLock(lockName string) error { |
| 240 | return nil |
| 241 | } |
| 242 | |
| 243 | // IsConnectionUp mock function implementation for KVClient |
Matteo Scandolo | 945e401 | 2019-12-12 14:16:11 -0800 | [diff] [blame] | 244 | func (kvclient *MockKVClient) IsConnectionUp(ctx context.Context) bool { |
| 245 | // timeout in second |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 246 | t, _ := ctx.Deadline() |
| 247 | if t.Second()-time.Now().Second() < 1 { |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 248 | return false |
| 249 | } |
| 250 | return true |
| 251 | } |
| 252 | |
| 253 | // CloseWatch mock function implementation for KVClient |
| 254 | func (kvclient *MockKVClient) CloseWatch(key string, ch chan *kvstore.Event) { |
| 255 | } |
| 256 | |
| 257 | // Close mock function implementation for KVClient |
| 258 | func (kvclient *MockKVClient) Close() { |
| 259 | } |