blob: 39aa35de8f487e05f612a8dec2039f25376a9d6f [file] [log] [blame]
kdarapu3248f9a2019-10-03 13:54:52 +05301/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2018-2024 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
Joey Armstrong3f0e2422023-07-05 18:25:41 -040017// Package mocks provides the mocks for openolt-adapter.
kdarapu3248f9a2019-10-03 13:54:52 +053018package 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"
Akash Kankanala041a2122024-10-16 15:49:22 +053048 // FlowGroup flow_groups/<flow_group_id>
Esin Karamanccb714b2019-11-29 15:02:06 +000049 FlowGroup = "flow_groups"
Akash Kankanala041a2122024-10-16 15:49:22 +053050 // FlowGroupCached flow_groups_cached/<flow_group_id>
Esin Karamanccb714b2019-11-29 15:02:06 +000051 FlowGroupCached = "flow_groups_cached"
Akash Kankanala041a2122024-10-16 15:49:22 +053052 // OnuPacketIn to extract gem port from packet-in
Esin Karaman7fb80c22020-07-16 14:23:33 +000053 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 {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070064 SerialNumber string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070065 GemPorts []uint32
66 UniPorts []uint32
Akash Kankanala041a2122024-10-16 15:49:22 +053067 OnuID uint32
68 IntfID uint32
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070069}
70
71// GroupInfo holds group information
72type GroupInfo struct {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070073 OutPorts []uint32
Akash Kankanala041a2122024-10-16 15:49:22 +053074 GroupID uint32
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070075}
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}") {
kdarapub26b4502019-10-05 03:02:33 +0530118 band1 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000}
119 band2 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000}
120 bands := []*ofp.OfpMeterBandHeader{band1, band2}
121 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands}
122 str, _ := json.Marshal(ofpMeterConfig)
123 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
kdarapu3248f9a2019-10-03 13:54:52 +0530124 }
125 if uint32(val) == 1 {
126 return nil, nil
127 }
128 return nil, errors.New("invalid meter")
129 }
130 if strings.Contains(key, TpIDPathSuffix) {
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700131 data := []uint32{64}
132 str, _ := json.Marshal(data)
kdarapub26b4502019-10-05 03:02:33 +0530133 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
134 }
135 if strings.Contains(key, FlowIDpool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000136 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool)
kdarapub26b4502019-10-05 03:02:33 +0530137 data := make(map[string]interface{})
138 data["pool"] = "1024"
139 data["start_idx"] = 1
140 data["end_idx"] = 1024
141 str, _ := json.Marshal(data)
142 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
143 }
144 if strings.Contains(key, FlowIDs) {
145 data := []uint32{1, 2}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000146 logger.Debug(ctx, "Error Error Error Key:", FlowIDs)
kdarapub26b4502019-10-05 03:02:33 +0530147 str, _ := json.Marshal(data)
148 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
149 }
Esin Karamand519bbf2020-07-01 11:16:03 +0000150
kdarapub26b4502019-10-05 03:02:33 +0530151 if strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000152 logger.Debug(ctx, "Error Error Error Key:", GemportIDs)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700153 data := []uint32{1}
154 str, _ := json.Marshal(data)
kdarapub26b4502019-10-05 03:02:33 +0530155 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
156 }
157 if strings.Contains(key, AllocIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000158 logger.Debug(ctx, "Error Error Error Key:", AllocIDs)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700159 data := []uint32{1}
160 str, _ := json.Marshal(data)
kdarapu3248f9a2019-10-03 13:54:52 +0530161 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
162 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000163 if strings.Contains(key, FlowGroup) || strings.Contains(key, FlowGroupCached) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000164 logger.Debug(ctx, "Error Error Error Key:", FlowGroup)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700165 groupInfo := GroupInfo{
Esin Karamanccb714b2019-11-29 15:02:06 +0000166 GroupID: 2,
167 OutPorts: []uint32{1},
168 }
169 str, _ := json.Marshal(&groupInfo)
170 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
171 }
Esin Karaman7fb80c22020-07-16 14:23:33 +0000172 if strings.Contains(key, OnuPacketIn) {
173 return getPacketInGemPort(key)
174 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700175
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700176 if strings.Contains(key, OnuGemInfoPath) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700177 var data []OnuGemInfo
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700178 str, _ := json.Marshal(data)
179 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
180 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700181
Akash Kankanala041a2122024-10-16 15:49:22 +0530182 // Interface, GEM port path
Esin Karamandf392e12020-12-16 13:33:09 +0000183 if strings.Contains(key, "0,255") {
Akash Kankanala041a2122024-10-16 15:49:22 +0530184 // return onuID, uniID associated with the given interface and GEM port
Esin Karamandf392e12020-12-16 13:33:09 +0000185 data := []uint32{1, 0}
186 str, _ := json.Marshal(data)
187 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
188 }
Akash Kankanala041a2122024-10-16 15:49:22 +0530189 // Interface, GEM port path
Esin Karamandf392e12020-12-16 13:33:09 +0000190 if strings.Contains(key, "0,257") {
Akash Kankanala041a2122024-10-16 15:49:22 +0530191 // return onuID, uniID associated with the given interface and GEM port
Esin Karamandf392e12020-12-16 13:33:09 +0000192 data := []uint32{1, 0}
193 str, _ := json.Marshal(data)
194 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
195 }
Esin Karaman7fb80c22020-07-16 14:23:33 +0000196
kdarapu3248f9a2019-10-03 13:54:52 +0530197 maps := make(map[string]*kvstore.KVPair)
198 maps[key] = &kvstore.KVPair{Key: key}
199 return maps[key], nil
200 }
201 return nil, errors.New("key didn't find")
202}
203
pnalmas04ede3b2025-01-16 18:05:27 +0530204// GetWithPrefix mock function implementation for KVClient
205func (kvclient *MockKVClient) GetWithPrefix(ctx context.Context, prefix string) (map[string]*kvstore.KVPair, error) {
206 result := make(map[string]*kvstore.KVPair)
207
208 // Get all the key-value pairs from the KV store
209 kvPairs, err := kvclient.List(ctx, "")
210 if err != nil {
211 return nil, err
212 }
213
214 // Iterate through all the keys in the KV store
215 for key, kvPair := range kvPairs {
216 // Check if the key has the specified prefix
217 if strings.HasPrefix(key, prefix) {
218 // Add the KVPair to the result map
219 result[key] = kvPair
220 }
221 }
222
223 return result, nil
224}
225
226// GetWithPrefixKeysOnly mock function implementation for KVClient
227func (kvclient *MockKVClient) GetWithPrefixKeysOnly(ctx context.Context, prefix string) ([]string, error) {
228 keys := make([]string, 0)
229
230 // Get all the key-value pairs from the KV store
231 kvPairs, err := kvclient.List(ctx, "")
232 if err != nil {
233 return nil, err
234 }
235
236 // Iterate through all the keys in the KV store
237 for key := range kvPairs {
238 // Check if the key has the specified prefix
239 if strings.HasPrefix(key, prefix) {
240 // Add the key to the result slice
241 keys = append(keys, key)
242 }
243 }
244
245 return keys, nil
246}
247
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400248// getPacketInGemPort returns the GEM port associated with the given key
Esin Karaman7fb80c22020-07-16 14:23:33 +0000249func getPacketInGemPort(key string) (*kvstore.KVPair, error) {
Akash Kankanala041a2122024-10-16 15:49:22 +0530250 // parse interface, onu, uni, vlan, priority values
Esin Karaman7fb80c22020-07-16 14:23:33 +0000251 arr := getParamsFromPacketInKey(key)
252
253 if len(arr) < 5 {
254 return nil, errors.New("key didn't find")
255 }
256 if arr[0] == "1" && arr[1] == "1" && arr[2] == "3" && arr[3] == "0" && arr[4] == "0" {
257 str, _ := json.Marshal(3)
258 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
259 }
260 if arr[0] == "2" && arr[1] == "2" && arr[2] == "4" && arr[3] == "549" && arr[4] == "0" {
261 str, _ := json.Marshal(4)
262 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
263 }
264 if arr[0] == "1" && arr[1] == "2" && arr[2] == "2" && arr[3] == "48" && arr[4] == "7" {
265 str, _ := json.Marshal(2)
266 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
267 }
268 return nil, errors.New("key didn't find")
269}
270
Joey Armstrong3f0e2422023-07-05 18:25:41 -0400271// getParamsFromPacketInKey parse packetIn key that is in the format of "onu_packetin/{1,1,1,1,2}"
Esin Karaman7fb80c22020-07-16 14:23:33 +0000272func getParamsFromPacketInKey(key string) []string {
Akash Kankanala041a2122024-10-16 15:49:22 +0530273 // return intfID, onuID, uniID, vlanID, priority
Esin Karaman7fb80c22020-07-16 14:23:33 +0000274 firstIndex := strings.Index(key, "{")
275 lastIndex := strings.Index(key, "}")
276 if firstIndex == -1 && lastIndex == -1 {
277 return []string{}
278 }
279 arr := strings.Split(key[firstIndex+1:lastIndex], ",")
280 if len(arr) < 5 {
281 return []string{}
282 }
283 return arr
284}
285
kdarapu3248f9a2019-10-03 13:54:52 +0530286// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530287func (kvclient *MockKVClient) Put(ctx context.Context, key string, value interface{}) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530288 if key != "" {
kdarapu3248f9a2019-10-03 13:54:52 +0530289 return nil
290 }
291 return errors.New("key didn't find")
292}
293
294// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530295func (kvclient *MockKVClient) Delete(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530296 if key == "" {
297 return errors.New("key didn't find")
298 }
299 return nil
300}
301
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300302// DeleteWithPrefix mock function implementation for KVClient
303func (kvclient *MockKVClient) DeleteWithPrefix(ctx context.Context, prefixKey string) error {
304 if prefixKey == "" {
305 return errors.New("key didn't find")
306 }
307 return nil
308}
309
kdarapu3248f9a2019-10-03 13:54:52 +0530310// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000311func (kvclient *MockKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
kdarapu3248f9a2019-10-03 13:54:52 +0530312 if key != "" {
313 maps := make(map[string]*kvstore.KVPair)
314 maps[key] = &kvstore.KVPair{Key: key}
315 return maps[key], nil
316 }
317 return nil, errors.New("key didn't find")
318}
319
320// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530321func (kvclient *MockKVClient) ReleaseReservation(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530322 // return nil
323 if key == "" {
324 return errors.New("key didn't find")
325 }
326 return nil
327}
328
329// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530330func (kvclient *MockKVClient) ReleaseAllReservations(ctx context.Context) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530331 return nil
332}
333
334// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530335func (kvclient *MockKVClient) RenewReservation(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530336 // return nil
337 if key == "" {
338 return errors.New("key didn't find")
339 }
340 return nil
341}
342
343// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800344func (kvclient *MockKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
kdarapu3248f9a2019-10-03 13:54:52 +0530345 return nil
346 // if key == "" {
347 // return nil
348 // }
349 // return &kvstore.Event{EventType: 1, Key: key}
350}
351
352// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000353func (kvclient *MockKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530354 return nil
355}
356
357// ReleaseLock mock function implementation for KVClient
358func (kvclient *MockKVClient) ReleaseLock(lockName string) error {
359 return nil
360}
361
362// IsConnectionUp mock function implementation for KVClient
Matteo Scandolo945e4012019-12-12 14:16:11 -0800363func (kvclient *MockKVClient) IsConnectionUp(ctx context.Context) bool {
364 // timeout in second
npujarec5762e2020-01-01 14:08:48 +0530365 t, _ := ctx.Deadline()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400366 return t.Second()-time.Now().Second() >= 1
kdarapu3248f9a2019-10-03 13:54:52 +0530367}
368
369// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000370func (kvclient *MockKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
kdarapu3248f9a2019-10-03 13:54:52 +0530371}
372
373// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000374func (kvclient *MockKVClient) Close(ctx context.Context) {
kdarapu3248f9a2019-10-03 13:54:52 +0530375}