blob: 2417f513b9f4d168ca0fb5a3793a78a6fb25a785 [file] [log] [blame]
kdarapu3248f9a2019-10-03 13:54:52 +05301/*
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.
18package mocks
19
20import (
21 "encoding/json"
22 "errors"
23 "strconv"
24 "strings"
25
kdarapub26b4502019-10-05 03:02:33 +053026 "github.com/opencord/voltha-go/common/log"
27 "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
28
kdarapu3248f9a2019-10-03 13:54:52 +053029 "github.com/opencord/voltha-go/db/kvstore"
30 ofp "github.com/opencord/voltha-protos/go/openflow_13"
kdarapub26b4502019-10-05 03:02:33 +053031 openolt "github.com/opencord/voltha-protos/go/openolt"
kdarapu3248f9a2019-10-03 13:54:52 +053032)
33
34const (
35 // MeterConfig meter to extarct meter
36 MeterConfig = "meter_id"
37 // TpIDPathSuffix to extract Techprofile
38 TpIDPathSuffix = "tp_id"
kdarapub26b4502019-10-05 03:02:33 +053039 // 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"
kdarapu3248f9a2019-10-03 13:54:52 +053049)
50
51// MockKVClient mocks the AdapterProxy interface.
52type MockKVClient struct {
53}
54
55// List mock function implementation for KVClient
56func (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
66func (kvclient *MockKVClient) Get(key string, timeout int, lock ...bool) (*kvstore.KVPair, error) {
kdarapub26b4502019-10-05 03:02:33 +053067 log.Debugw("Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
kdarapu3248f9a2019-10-03 13:54:52 +053068 if key != "" {
kdarapub26b4502019-10-05 03:02:33 +053069 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 }
kdarapu3248f9a2019-10-03 13:54:52 +053078 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
kdarapu3248f9a2019-10-03 13:54:52 +053086 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)
kdarapub26b4502019-10-05 03:02:33 +053091
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
kdarapu3248f9a2019-10-03 13:54:52 +0530103 }
104 if uint32(val) == 1 {
105 return nil, nil
106 }
107 return nil, errors.New("invalid meter")
108 }
109 if strings.Contains(key, TpIDPathSuffix) {
kdarapub26b4502019-10-05 03:02:33 +0530110 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)
kdarapu3248f9a2019-10-03 13:54:52 +0530147 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
158func (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
167func (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
175func (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
185func (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
194func (kvclient *MockKVClient) ReleaseAllReservations() error {
195 return nil
196}
197
198// RenewReservation mock function implementation for KVClient
199func (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
208func (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
217func (kvclient *MockKVClient) AcquireLock(lockName string, timeout int) error {
218 return nil
219}
220
221// ReleaseLock mock function implementation for KVClient
222func (kvclient *MockKVClient) ReleaseLock(lockName string) error {
223 return nil
224}
225
226// IsConnectionUp mock function implementation for KVClient
227func (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
235func (kvclient *MockKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
236}
237
238// Close mock function implementation for KVClient
239func (kvclient *MockKVClient) Close() {
240}