blob: e45b30bd792619a2de9776cc9ed6d0ebc057a153 [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001/*
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 "context"
22 "encoding/json"
23 "errors"
24 "strconv"
25 "strings"
26 "time"
27
28 "github.com/opencord/voltha-lib-go/v3/pkg/log"
29 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
30
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"
34)
35
36const (
37 // MeterConfig meter to extarct meter
38 MeterConfig = "meter_id"
39 // TpIDPathSuffix to extract Techprofile
40 TpIDPathSuffix = "tp_id"
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"
51 //FlowGroup flow_groups/<flow_group_id>
52 FlowGroup = "flow_groups"
53 //FlowGroupCached flow_groups_cached/<flow_group_id>
54 FlowGroupCached = "flow_groups_cached"
55 //OnuPacketIn to extract gem port from packet-in
56 OnuPacketIn = "onu_packetin"
57)
58
59// MockKVClient mocks the AdapterProxy interface.
60type MockKVClient struct {
61}
62
63// List mock function implementation for KVClient
64func (kvclient *MockKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
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
74func (kvclient *MockKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
75 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
76 if key != "" {
77 logger.Debug(ctx, "Warning Key Not Blank")
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 }
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
94 sep := strings.Split(key, "/")[1]
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)
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
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) {
118 str, _ := json.Marshal(64)
119 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
120 }
121 if strings.Contains(key, FlowIDpool) {
122 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool)
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}
132 logger.Debug(ctx, "Error Error Error Key:", FlowIDs)
133 str, _ := json.Marshal(data)
134 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
135 }
136 if strings.Contains(key, "/{olt}/{0,-1,-1}/flow_id_info/") {
137 //multicast flow
138 data := []resourcemanager.FlowInfo{
139 {
140 Flow: &openolt.Flow{FlowId: 1, OnuId: 0, UniId: 0, GemportId: 4000},
141 FlowStoreCookie: uint64(48132224281636694),
142 LogicalFlowID: 3961977515762683568,
143 },
144 }
145 logger.Debug(ctx, "Error Error Error Key:", FlowIDs)
146 str, _ := json.Marshal(data)
147 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
148 }
149
150 if strings.Contains(key, FlowIDInfo) {
151
152 data := []resourcemanager.FlowInfo{
153 {
154 Flow: &openolt.Flow{FlowId: 1, OnuId: 1, UniId: 1, GemportId: 1},
155 FlowStoreCookie: uint64(48132224281636694),
156 LogicalFlowID: 1,
157 },
158 }
159 logger.Debug(ctx, "Error Error Error Key:", FlowIDs)
160 str, _ := json.Marshal(data)
161 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
162 }
163 if strings.Contains(key, GemportIDs) {
164 logger.Debug(ctx, "Error Error Error Key:", GemportIDs)
165 str, _ := json.Marshal(1)
166 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
167 }
168 if strings.Contains(key, AllocIDs) {
169 logger.Debug(ctx, "Error Error Error Key:", AllocIDs)
170 str, _ := json.Marshal(1)
171 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
172 }
173 if strings.Contains(key, FlowGroup) || strings.Contains(key, FlowGroupCached) {
174 logger.Debug(ctx, "Error Error Error Key:", FlowGroup)
175 groupInfo := resourcemanager.GroupInfo{
176 GroupID: 2,
177 OutPorts: []uint32{1},
178 }
179 str, _ := json.Marshal(&groupInfo)
180 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
181 }
182
183 if strings.Contains(key, OnuPacketIn) {
184 return getPacketInGemPort(key)
185 }
186
187 maps := make(map[string]*kvstore.KVPair)
188 maps[key] = &kvstore.KVPair{Key: key}
189 return maps[key], nil
190 }
191 return nil, errors.New("key didn't find")
192}
193
194//getPacketInGemPort returns the GEM port associated with the given key
195func getPacketInGemPort(key string) (*kvstore.KVPair, error) {
196 //parse interface, onu, uni, vlan, priority values
197 arr := getParamsFromPacketInKey(key)
198
199 if len(arr) < 5 {
200 return nil, errors.New("key didn't find")
201 }
202 if arr[0] == "1" && arr[1] == "1" && arr[2] == "3" && arr[3] == "0" && arr[4] == "0" {
203 str, _ := json.Marshal(3)
204 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
205 }
206 if arr[0] == "2" && arr[1] == "2" && arr[2] == "4" && arr[3] == "549" && arr[4] == "0" {
207 str, _ := json.Marshal(4)
208 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
209 }
210 if arr[0] == "1" && arr[1] == "2" && arr[2] == "2" && arr[3] == "48" && arr[4] == "7" {
211 str, _ := json.Marshal(2)
212 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
213 }
214 return nil, errors.New("key didn't find")
215}
216
217//getParamsFromPacketInKey parse packetIn key that is in the format of "onu_packetin/{1,1,1,1,2}"
218func getParamsFromPacketInKey(key string) []string {
219 //return intfID, onuID, uniID, vlanID, priority
220 firstIndex := strings.Index(key, "{")
221 lastIndex := strings.Index(key, "}")
222 if firstIndex == -1 && lastIndex == -1 {
223 return []string{}
224 }
225 arr := strings.Split(key[firstIndex+1:lastIndex], ",")
226 if len(arr) < 5 {
227 return []string{}
228 }
229 return arr
230}
231
232// Put mock function implementation for KVClient
233func (kvclient *MockKVClient) Put(ctx context.Context, key string, value interface{}) error {
234 if key != "" {
235
236 return nil
237 }
238 return errors.New("key didn't find")
239}
240
241// Delete mock function implementation for KVClient
242func (kvclient *MockKVClient) Delete(ctx context.Context, key string) error {
243 if key == "" {
244 return errors.New("key didn't find")
245 }
246 return nil
247}
248
249// Reserve mock function implementation for KVClient
250func (kvclient *MockKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
251 if key != "" {
252 maps := make(map[string]*kvstore.KVPair)
253 maps[key] = &kvstore.KVPair{Key: key}
254 return maps[key], nil
255 }
256 return nil, errors.New("key didn't find")
257}
258
259// ReleaseReservation mock function implementation for KVClient
260func (kvclient *MockKVClient) ReleaseReservation(ctx context.Context, key string) error {
261 // return nil
262 if key == "" {
263 return errors.New("key didn't find")
264 }
265 return nil
266}
267
268// ReleaseAllReservations mock function implementation for KVClient
269func (kvclient *MockKVClient) ReleaseAllReservations(ctx context.Context) error {
270 return nil
271}
272
273// RenewReservation mock function implementation for KVClient
274func (kvclient *MockKVClient) RenewReservation(ctx context.Context, key string) error {
275 // return nil
276 if key == "" {
277 return errors.New("key didn't find")
278 }
279 return nil
280}
281
282// Watch mock function implementation for KVClient
283func (kvclient *MockKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
284 return nil
285 // if key == "" {
286 // return nil
287 // }
288 // return &kvstore.Event{EventType: 1, Key: key}
289}
290
291// AcquireLock mock function implementation for KVClient
292func (kvclient *MockKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
293 return nil
294}
295
296// ReleaseLock mock function implementation for KVClient
297func (kvclient *MockKVClient) ReleaseLock(lockName string) error {
298 return nil
299}
300
301// IsConnectionUp mock function implementation for KVClient
302func (kvclient *MockKVClient) IsConnectionUp(ctx context.Context) bool {
303 // timeout in second
304 t, _ := ctx.Deadline()
305 return t.Second()-time.Now().Second() >= 1
306}
307
308// CloseWatch mock function implementation for KVClient
309func (kvclient *MockKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
310}
311
312// Close mock function implementation for KVClient
313func (kvclient *MockKVClient) Close(ctx context.Context) {
314}