blob: be8ab57a0d09fb77a83abb3ea4bc94606afc4b21 [file] [log] [blame]
kdarapu3248f9a2019-10-03 13:54:52 +05301/*
Joey Armstronga6af1522023-01-17 16:06:16 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
kdarapu3248f9a2019-10-03 13:54:52 +05303
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 (
npujarec5762e2020-01-01 14:08:48 +053021 "context"
kdarapu3248f9a2019-10-03 13:54:52 +053022 "encoding/json"
23 "errors"
24 "strconv"
25 "strings"
npujarec5762e2020-01-01 14:08:48 +053026 "time"
kdarapu3248f9a2019-10-03 13:54:52 +053027
khenaidoo106c61a2021-08-11 18:05:46 -040028 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
29 "github.com/opencord/voltha-lib-go/v7/pkg/log"
30 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
kdarapu3248f9a2019-10-03 13:54:52 +053031)
32
33const (
34 // MeterConfig meter to extarct meter
35 MeterConfig = "meter_id"
36 // TpIDPathSuffix to extract Techprofile
37 TpIDPathSuffix = "tp_id"
kdarapub26b4502019-10-05 03:02:33 +053038 // FlowIDpool to extract Flow ids
39 FlowIDpool = "flow_id_pool"
40 // FlowIDs to extract flow_ids
41 FlowIDs = "flow_ids"
42 // FlowIDInfo to extract flowId info
43 FlowIDInfo = "flow_id_info"
44 // GemportIDs to gemport_ids
45 GemportIDs = "gemport_ids"
46 // AllocIDs to extract alloc_ids
47 AllocIDs = "alloc_ids"
Esin Karamanccb714b2019-11-29 15:02:06 +000048 //FlowGroup flow_groups/<flow_group_id>
49 FlowGroup = "flow_groups"
50 //FlowGroupCached flow_groups_cached/<flow_group_id>
51 FlowGroupCached = "flow_groups_cached"
Esin Karaman7fb80c22020-07-16 14:23:33 +000052 //OnuPacketIn to extract gem port from packet-in
53 OnuPacketIn = "onu_packetin"
Girish Gowdrafb3d6102020-10-16 16:32:36 -070054 // OnuGemInfoPath has path on the kvstore to store OnuGemInfo info per PON interface
55 OnuGemInfoPath = "onu_gem_info"
kdarapu3248f9a2019-10-03 13:54:52 +053056)
57
58// MockKVClient mocks the AdapterProxy interface.
59type MockKVClient struct {
60}
61
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070062// OnuGemInfo holds onu information along with gem port list and uni port list
63type OnuGemInfo struct {
64 OnuID uint32
65 SerialNumber string
66 IntfID uint32
67 GemPorts []uint32
68 UniPorts []uint32
69}
70
71// GroupInfo holds group information
72type GroupInfo struct {
73 GroupID uint32
74 OutPorts []uint32
75}
76
kdarapu3248f9a2019-10-03 13:54:52 +053077// List mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +053078func (kvclient *MockKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
kdarapu3248f9a2019-10-03 13:54:52 +053079 if key != "" {
80 maps := make(map[string]*kvstore.KVPair)
81 maps[key] = &kvstore.KVPair{Key: key}
82 return maps, nil
83 }
84 return nil, errors.New("key didn't find")
85}
86
87// Get mock function implementation for KVClient
Girish Gowdrafb3d6102020-10-16 16:32:36 -070088// nolint: gocyclo
npujarec5762e2020-01-01 14:08:48 +053089func (kvclient *MockKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070090 logger.Debugw(ctx, "Get of MockKVClient called", log.Fields{"key": key})
kdarapu3248f9a2019-10-03 13:54:52 +053091 if key != "" {
kdarapub26b4502019-10-05 03:02:33 +053092 if strings.Contains(key, "meter_id/{0,62,8}/{upstream}") {
93 meterConfig := ofp.OfpMeterConfig{
94 Flags: 0,
95 MeterId: 1,
96 }
97 str, _ := json.Marshal(meterConfig)
98 return kvstore.NewKVPair(key, string(str), "mock", 3000, 1), nil
99 }
kdarapu3248f9a2019-10-03 13:54:52 +0530100 if strings.Contains(key, MeterConfig) {
101 var bands []*ofp.OfpMeterBandHeader
102 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
103 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
104
105 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
106 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
107
Gamze Abakafee36392019-10-03 11:17:24 +0000108 sep := strings.Split(key, "/")[1]
kdarapu3248f9a2019-10-03 13:54:52 +0530109 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
110 if uint32(val) > 1 {
111 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
112 str, _ := json.Marshal(meterConfig)
kdarapub26b4502019-10-05 03:02:33 +0530113
114 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
115 }
116
117 if strings.Contains(key, "meter_id/{1,1,1}/{downstream}") {
118
119 band1 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000}
120 band2 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000}
121 bands := []*ofp.OfpMeterBandHeader{band1, band2}
122 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands}
123 str, _ := json.Marshal(ofpMeterConfig)
124 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
kdarapu3248f9a2019-10-03 13:54:52 +0530125 }
126 if uint32(val) == 1 {
127 return nil, nil
128 }
129 return nil, errors.New("invalid meter")
130 }
131 if strings.Contains(key, TpIDPathSuffix) {
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700132 data := []uint32{64}
133 str, _ := json.Marshal(data)
kdarapub26b4502019-10-05 03:02:33 +0530134 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
135 }
136 if strings.Contains(key, FlowIDpool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000137 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool)
kdarapub26b4502019-10-05 03:02:33 +0530138 data := make(map[string]interface{})
139 data["pool"] = "1024"
140 data["start_idx"] = 1
141 data["end_idx"] = 1024
142 str, _ := json.Marshal(data)
143 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
144 }
145 if strings.Contains(key, FlowIDs) {
146 data := []uint32{1, 2}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000147 logger.Debug(ctx, "Error Error Error Key:", FlowIDs)
kdarapub26b4502019-10-05 03:02:33 +0530148 str, _ := json.Marshal(data)
149 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
150 }
Esin Karamand519bbf2020-07-01 11:16:03 +0000151
kdarapub26b4502019-10-05 03:02:33 +0530152 if strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000153 logger.Debug(ctx, "Error Error Error Key:", GemportIDs)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700154 data := []uint32{1}
155 str, _ := json.Marshal(data)
kdarapub26b4502019-10-05 03:02:33 +0530156 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
157 }
158 if strings.Contains(key, AllocIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000159 logger.Debug(ctx, "Error Error Error Key:", AllocIDs)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700160 data := []uint32{1}
161 str, _ := json.Marshal(data)
kdarapu3248f9a2019-10-03 13:54:52 +0530162 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
163 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000164 if strings.Contains(key, FlowGroup) || strings.Contains(key, FlowGroupCached) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000165 logger.Debug(ctx, "Error Error Error Key:", FlowGroup)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700166 groupInfo := GroupInfo{
Esin Karamanccb714b2019-11-29 15:02:06 +0000167 GroupID: 2,
168 OutPorts: []uint32{1},
169 }
170 str, _ := json.Marshal(&groupInfo)
171 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
172 }
Esin Karaman7fb80c22020-07-16 14:23:33 +0000173 if strings.Contains(key, OnuPacketIn) {
174 return getPacketInGemPort(key)
175 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700176
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700177 if strings.Contains(key, OnuGemInfoPath) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700178 var data []OnuGemInfo
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700179 str, _ := json.Marshal(data)
180 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
181 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700182
Esin Karamandf392e12020-12-16 13:33:09 +0000183 //Interface, GEM port path
184 if strings.Contains(key, "0,255") {
185 //return onuID, uniID associated with the given interface and GEM port
186 data := []uint32{1, 0}
187 str, _ := json.Marshal(data)
188 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
189 }
190 //Interface, GEM port path
191 if strings.Contains(key, "0,257") {
192 //return onuID, uniID associated with the given interface and GEM port
193 data := []uint32{1, 0}
194 str, _ := json.Marshal(data)
195 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
196 }
Esin Karaman7fb80c22020-07-16 14:23:33 +0000197
kdarapu3248f9a2019-10-03 13:54:52 +0530198 maps := make(map[string]*kvstore.KVPair)
199 maps[key] = &kvstore.KVPair{Key: key}
200 return maps[key], nil
201 }
202 return nil, errors.New("key didn't find")
203}
204
Esin Karaman7fb80c22020-07-16 14:23:33 +0000205//getPacketInGemPort returns the GEM port associated with the given key
206func getPacketInGemPort(key string) (*kvstore.KVPair, error) {
207 //parse interface, onu, uni, vlan, priority values
208 arr := getParamsFromPacketInKey(key)
209
210 if len(arr) < 5 {
211 return nil, errors.New("key didn't find")
212 }
213 if arr[0] == "1" && arr[1] == "1" && arr[2] == "3" && arr[3] == "0" && arr[4] == "0" {
214 str, _ := json.Marshal(3)
215 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
216 }
217 if arr[0] == "2" && arr[1] == "2" && arr[2] == "4" && arr[3] == "549" && arr[4] == "0" {
218 str, _ := json.Marshal(4)
219 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
220 }
221 if arr[0] == "1" && arr[1] == "2" && arr[2] == "2" && arr[3] == "48" && arr[4] == "7" {
222 str, _ := json.Marshal(2)
223 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
224 }
225 return nil, errors.New("key didn't find")
226}
227
228//getParamsFromPacketInKey parse packetIn key that is in the format of "onu_packetin/{1,1,1,1,2}"
229func getParamsFromPacketInKey(key string) []string {
230 //return intfID, onuID, uniID, vlanID, priority
231 firstIndex := strings.Index(key, "{")
232 lastIndex := strings.Index(key, "}")
233 if firstIndex == -1 && lastIndex == -1 {
234 return []string{}
235 }
236 arr := strings.Split(key[firstIndex+1:lastIndex], ",")
237 if len(arr) < 5 {
238 return []string{}
239 }
240 return arr
241}
242
kdarapu3248f9a2019-10-03 13:54:52 +0530243// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530244func (kvclient *MockKVClient) Put(ctx context.Context, key string, value interface{}) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530245 if key != "" {
kdarapu3248f9a2019-10-03 13:54:52 +0530246 return nil
247 }
248 return errors.New("key didn't find")
249}
250
251// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530252func (kvclient *MockKVClient) Delete(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530253 if key == "" {
254 return errors.New("key didn't find")
255 }
256 return nil
257}
258
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300259// DeleteWithPrefix mock function implementation for KVClient
260func (kvclient *MockKVClient) DeleteWithPrefix(ctx context.Context, prefixKey string) error {
261 if prefixKey == "" {
262 return errors.New("key didn't find")
263 }
264 return nil
265}
266
kdarapu3248f9a2019-10-03 13:54:52 +0530267// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000268func (kvclient *MockKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
kdarapu3248f9a2019-10-03 13:54:52 +0530269 if key != "" {
270 maps := make(map[string]*kvstore.KVPair)
271 maps[key] = &kvstore.KVPair{Key: key}
272 return maps[key], nil
273 }
274 return nil, errors.New("key didn't find")
275}
276
277// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530278func (kvclient *MockKVClient) ReleaseReservation(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530279 // return nil
280 if key == "" {
281 return errors.New("key didn't find")
282 }
283 return nil
284}
285
286// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530287func (kvclient *MockKVClient) ReleaseAllReservations(ctx context.Context) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530288 return nil
289}
290
291// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530292func (kvclient *MockKVClient) RenewReservation(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530293 // return nil
294 if key == "" {
295 return errors.New("key didn't find")
296 }
297 return nil
298}
299
300// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800301func (kvclient *MockKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
kdarapu3248f9a2019-10-03 13:54:52 +0530302 return nil
303 // if key == "" {
304 // return nil
305 // }
306 // return &kvstore.Event{EventType: 1, Key: key}
307}
308
309// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000310func (kvclient *MockKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530311 return nil
312}
313
314// ReleaseLock mock function implementation for KVClient
315func (kvclient *MockKVClient) ReleaseLock(lockName string) error {
316 return nil
317}
318
319// IsConnectionUp mock function implementation for KVClient
Matteo Scandolo945e4012019-12-12 14:16:11 -0800320func (kvclient *MockKVClient) IsConnectionUp(ctx context.Context) bool {
321 // timeout in second
npujarec5762e2020-01-01 14:08:48 +0530322 t, _ := ctx.Deadline()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400323 return t.Second()-time.Now().Second() >= 1
kdarapu3248f9a2019-10-03 13:54:52 +0530324}
325
326// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000327func (kvclient *MockKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
kdarapu3248f9a2019-10-03 13:54:52 +0530328}
329
330// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000331func (kvclient *MockKVClient) Close(ctx context.Context) {
kdarapu3248f9a2019-10-03 13:54:52 +0530332}