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 ( |
| 21 | "encoding/json" |
| 22 | "errors" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/common/log" |
| 27 | "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager" |
| 28 | |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 29 | "github.com/opencord/voltha-go/db/kvstore" |
| 30 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 31 | openolt "github.com/opencord/voltha-protos/go/openolt" |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | const ( |
| 35 | // MeterConfig meter to extarct meter |
| 36 | MeterConfig = "meter_id" |
| 37 | // TpIDPathSuffix to extract Techprofile |
| 38 | TpIDPathSuffix = "tp_id" |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 39 | // FlowIDpool to extract Flow ids |
| 40 | FlowIDpool = "flow_id_pool" |
| 41 | // FlowIDs to extract flow_ids |
| 42 | FlowIDs = "flow_ids" |
| 43 | // FlowIDInfo to extract flowId info |
| 44 | FlowIDInfo = "flow_id_info" |
| 45 | // GemportIDs to gemport_ids |
| 46 | GemportIDs = "gemport_ids" |
| 47 | // AllocIDs to extract alloc_ids |
| 48 | AllocIDs = "alloc_ids" |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 49 | ) |
| 50 | |
| 51 | // MockKVClient mocks the AdapterProxy interface. |
| 52 | type MockKVClient struct { |
| 53 | } |
| 54 | |
| 55 | // List mock function implementation for KVClient |
| 56 | func (kvclient *MockKVClient) List(key string, timeout int, lock ...bool) (map[string]*kvstore.KVPair, error) { |
| 57 | if key != "" { |
| 58 | maps := make(map[string]*kvstore.KVPair) |
| 59 | maps[key] = &kvstore.KVPair{Key: key} |
| 60 | return maps, nil |
| 61 | } |
| 62 | return nil, errors.New("key didn't find") |
| 63 | } |
| 64 | |
| 65 | // Get mock function implementation for KVClient |
| 66 | func (kvclient *MockKVClient) Get(key string, timeout int, lock ...bool) (*kvstore.KVPair, error) { |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 67 | log.Debugw("Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key}) |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 68 | if key != "" { |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 69 | log.Debug("Warning Key Not Blank") |
| 70 | if strings.Contains(key, "meter_id/{0,62,8}/{upstream}") { |
| 71 | meterConfig := ofp.OfpMeterConfig{ |
| 72 | Flags: 0, |
| 73 | MeterId: 1, |
| 74 | } |
| 75 | str, _ := json.Marshal(meterConfig) |
| 76 | return kvstore.NewKVPair(key, string(str), "mock", 3000, 1), nil |
| 77 | } |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 78 | if strings.Contains(key, MeterConfig) { |
| 79 | var bands []*ofp.OfpMeterBandHeader |
| 80 | bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK, |
| 81 | Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}}) |
| 82 | |
| 83 | bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK, |
| 84 | Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}}) |
| 85 | |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 86 | sep := strings.Split(key, "/")[2] |
| 87 | val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32) |
| 88 | if uint32(val) > 1 { |
| 89 | meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands} |
| 90 | str, _ := json.Marshal(meterConfig) |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 91 | |
| 92 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 93 | } |
| 94 | |
| 95 | if strings.Contains(key, "meter_id/{1,1,1}/{downstream}") { |
| 96 | |
| 97 | band1 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000} |
| 98 | band2 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000} |
| 99 | bands := []*ofp.OfpMeterBandHeader{band1, band2} |
| 100 | ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands} |
| 101 | str, _ := json.Marshal(ofpMeterConfig) |
| 102 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 103 | } |
| 104 | if uint32(val) == 1 { |
| 105 | return nil, nil |
| 106 | } |
| 107 | return nil, errors.New("invalid meter") |
| 108 | } |
| 109 | if strings.Contains(key, TpIDPathSuffix) { |
kdarapu | b26b450 | 2019-10-05 03:02:33 +0530 | [diff] [blame] | 110 | str, _ := json.Marshal(64) |
| 111 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 112 | } |
| 113 | if strings.Contains(key, FlowIDpool) { |
| 114 | log.Debug("Error Error Error Key:", FlowIDpool) |
| 115 | data := make(map[string]interface{}) |
| 116 | data["pool"] = "1024" |
| 117 | data["start_idx"] = 1 |
| 118 | data["end_idx"] = 1024 |
| 119 | str, _ := json.Marshal(data) |
| 120 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 121 | } |
| 122 | if strings.Contains(key, FlowIDs) { |
| 123 | data := []uint32{1, 2} |
| 124 | log.Debug("Error Error Error Key:", FlowIDs) |
| 125 | str, _ := json.Marshal(data) |
| 126 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 127 | } |
| 128 | if strings.Contains(key, FlowIDInfo) { |
| 129 | |
| 130 | data := []resourcemanager.FlowInfo{ |
| 131 | { |
| 132 | Flow: &openolt.Flow{FlowId: 1, OnuId: 1, UniId: 1, GemportId: 1}, |
| 133 | FlowStoreCookie: uint64(48132224281636694), |
| 134 | }, |
| 135 | } |
| 136 | log.Debug("Error Error Error Key:", FlowIDs) |
| 137 | str, _ := json.Marshal(data) |
| 138 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 139 | } |
| 140 | if strings.Contains(key, GemportIDs) { |
| 141 | log.Debug("Error Error Error Key:", GemportIDs) |
| 142 | str, _ := json.Marshal(1) |
| 143 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 144 | } |
| 145 | if strings.Contains(key, AllocIDs) { |
| 146 | log.Debug("Error Error Error Key:", AllocIDs) |
kdarapu | 3248f9a | 2019-10-03 13:54:52 +0530 | [diff] [blame] | 147 | str, _ := json.Marshal(1) |
| 148 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 149 | } |
| 150 | maps := make(map[string]*kvstore.KVPair) |
| 151 | maps[key] = &kvstore.KVPair{Key: key} |
| 152 | return maps[key], nil |
| 153 | } |
| 154 | return nil, errors.New("key didn't find") |
| 155 | } |
| 156 | |
| 157 | // Put mock function implementation for KVClient |
| 158 | func (kvclient *MockKVClient) Put(key string, value interface{}, timeout int, lock ...bool) error { |
| 159 | if key != "" { |
| 160 | |
| 161 | return nil |
| 162 | } |
| 163 | return errors.New("key didn't find") |
| 164 | } |
| 165 | |
| 166 | // Delete mock function implementation for KVClient |
| 167 | func (kvclient *MockKVClient) Delete(key string, timeout int, lock ...bool) error { |
| 168 | if key == "" { |
| 169 | return errors.New("key didn't find") |
| 170 | } |
| 171 | return nil |
| 172 | } |
| 173 | |
| 174 | // Reserve mock function implementation for KVClient |
| 175 | func (kvclient *MockKVClient) Reserve(key string, value interface{}, ttl int64) (interface{}, error) { |
| 176 | if key != "" { |
| 177 | maps := make(map[string]*kvstore.KVPair) |
| 178 | maps[key] = &kvstore.KVPair{Key: key} |
| 179 | return maps[key], nil |
| 180 | } |
| 181 | return nil, errors.New("key didn't find") |
| 182 | } |
| 183 | |
| 184 | // ReleaseReservation mock function implementation for KVClient |
| 185 | func (kvclient *MockKVClient) ReleaseReservation(key string) error { |
| 186 | // return nil |
| 187 | if key == "" { |
| 188 | return errors.New("key didn't find") |
| 189 | } |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | // ReleaseAllReservations mock function implementation for KVClient |
| 194 | func (kvclient *MockKVClient) ReleaseAllReservations() error { |
| 195 | return nil |
| 196 | } |
| 197 | |
| 198 | // RenewReservation mock function implementation for KVClient |
| 199 | func (kvclient *MockKVClient) RenewReservation(key string) error { |
| 200 | // return nil |
| 201 | if key == "" { |
| 202 | return errors.New("key didn't find") |
| 203 | } |
| 204 | return nil |
| 205 | } |
| 206 | |
| 207 | // Watch mock function implementation for KVClient |
| 208 | func (kvclient *MockKVClient) Watch(key string) chan *kvstore.Event { |
| 209 | return nil |
| 210 | // if key == "" { |
| 211 | // return nil |
| 212 | // } |
| 213 | // return &kvstore.Event{EventType: 1, Key: key} |
| 214 | } |
| 215 | |
| 216 | // AcquireLock mock function implementation for KVClient |
| 217 | func (kvclient *MockKVClient) AcquireLock(lockName string, timeout int) error { |
| 218 | return nil |
| 219 | } |
| 220 | |
| 221 | // ReleaseLock mock function implementation for KVClient |
| 222 | func (kvclient *MockKVClient) ReleaseLock(lockName string) error { |
| 223 | return nil |
| 224 | } |
| 225 | |
| 226 | // IsConnectionUp mock function implementation for KVClient |
| 227 | func (kvclient *MockKVClient) IsConnectionUp(timeout int) bool { // timeout in second |
| 228 | if timeout < 1 { |
| 229 | return false |
| 230 | } |
| 231 | return true |
| 232 | } |
| 233 | |
| 234 | // CloseWatch mock function implementation for KVClient |
| 235 | func (kvclient *MockKVClient) CloseWatch(key string, ch chan *kvstore.Event) { |
| 236 | } |
| 237 | |
| 238 | // Close mock function implementation for KVClient |
| 239 | func (kvclient *MockKVClient) Close() { |
| 240 | } |