blob: a034f8752182bb0ac16238cc62ad8d193d3eaae1 [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 (
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
Girish Gowdraa09aeab2020-09-14 16:30:52 -070028 "github.com/opencord/voltha-lib-go/v4/pkg/log"
Scott Bakerdbd960e2020-02-28 08:57:51 -080029 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
kdarapub26b4502019-10-05 03:02:33 +053030
Girish Gowdraa09aeab2020-09-14 16:30:52 -070031 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
32 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
33 openolt "github.com/opencord/voltha-protos/v4/go/openolt"
kdarapu3248f9a2019-10-03 13:54:52 +053034)
35
36const (
37 // MeterConfig meter to extarct meter
38 MeterConfig = "meter_id"
39 // TpIDPathSuffix to extract Techprofile
40 TpIDPathSuffix = "tp_id"
kdarapub26b4502019-10-05 03:02:33 +053041 // 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"
Esin Karamanccb714b2019-11-29 15:02:06 +000051 //FlowGroup flow_groups/<flow_group_id>
52 FlowGroup = "flow_groups"
53 //FlowGroupCached flow_groups_cached/<flow_group_id>
54 FlowGroupCached = "flow_groups_cached"
Esin Karaman7fb80c22020-07-16 14:23:33 +000055 //OnuPacketIn to extract gem port from packet-in
56 OnuPacketIn = "onu_packetin"
kdarapu3248f9a2019-10-03 13:54:52 +053057)
58
59// MockKVClient mocks the AdapterProxy interface.
60type MockKVClient struct {
61}
62
63// List mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +053064func (kvclient *MockKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
kdarapu3248f9a2019-10-03 13:54:52 +053065 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
npujarec5762e2020-01-01 14:08:48 +053074func (kvclient *MockKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Girish Gowdraa09aeab2020-09-14 16:30:52 -070075 logger.Debugw(ctx, "Get of MockKVClient called", log.Fields{"key": key})
kdarapu3248f9a2019-10-03 13:54:52 +053076 if key != "" {
kdarapub26b4502019-10-05 03:02:33 +053077 if strings.Contains(key, "meter_id/{0,62,8}/{upstream}") {
78 meterConfig := ofp.OfpMeterConfig{
79 Flags: 0,
80 MeterId: 1,
81 }
82 str, _ := json.Marshal(meterConfig)
83 return kvstore.NewKVPair(key, string(str), "mock", 3000, 1), nil
84 }
kdarapu3248f9a2019-10-03 13:54:52 +053085 if strings.Contains(key, MeterConfig) {
86 var bands []*ofp.OfpMeterBandHeader
87 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
88 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
89
90 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
91 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
92
Gamze Abakafee36392019-10-03 11:17:24 +000093 sep := strings.Split(key, "/")[1]
kdarapu3248f9a2019-10-03 13:54:52 +053094 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
95 if uint32(val) > 1 {
96 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
97 str, _ := json.Marshal(meterConfig)
kdarapub26b4502019-10-05 03:02:33 +053098
99 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
100 }
101
102 if strings.Contains(key, "meter_id/{1,1,1}/{downstream}") {
103
104 band1 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000}
105 band2 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000}
106 bands := []*ofp.OfpMeterBandHeader{band1, band2}
107 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands}
108 str, _ := json.Marshal(ofpMeterConfig)
109 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
kdarapu3248f9a2019-10-03 13:54:52 +0530110 }
111 if uint32(val) == 1 {
112 return nil, nil
113 }
114 return nil, errors.New("invalid meter")
115 }
116 if strings.Contains(key, TpIDPathSuffix) {
kdarapub26b4502019-10-05 03:02:33 +0530117 str, _ := json.Marshal(64)
118 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
119 }
120 if strings.Contains(key, FlowIDpool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000121 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool)
kdarapub26b4502019-10-05 03:02:33 +0530122 data := make(map[string]interface{})
123 data["pool"] = "1024"
124 data["start_idx"] = 1
125 data["end_idx"] = 1024
126 str, _ := json.Marshal(data)
127 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
128 }
129 if strings.Contains(key, FlowIDs) {
130 data := []uint32{1, 2}
Neha Sharma96b7bf22020-06-15 10:37:32 +0000131 logger.Debug(ctx, "Error Error Error Key:", FlowIDs)
kdarapub26b4502019-10-05 03:02:33 +0530132 str, _ := json.Marshal(data)
133 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
134 }
Esin Karamand519bbf2020-07-01 11:16:03 +0000135 if strings.Contains(key, "/{olt}/{0,-1,-1}/flow_id_info/") {
136 //multicast flow
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700137 data := resourcemanager.FlowInfo{
138 Flow: &openolt.Flow{FlowId: 1, OnuId: 0, UniId: 0, GemportId: 4000},
Esin Karamand519bbf2020-07-01 11:16:03 +0000139 }
140 logger.Debug(ctx, "Error Error Error Key:", FlowIDs)
141 str, _ := json.Marshal(data)
142 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
143 }
144
kdarapub26b4502019-10-05 03:02:33 +0530145 if strings.Contains(key, FlowIDInfo) {
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700146 data := resourcemanager.FlowInfo{
147 Flow: &openolt.Flow{FlowId: 1, OnuId: 1, UniId: 1, GemportId: 1},
kdarapub26b4502019-10-05 03:02:33 +0530148 }
Neha Sharma96b7bf22020-06-15 10:37:32 +0000149 logger.Debug(ctx, "Error Error Error Key:", FlowIDs)
kdarapub26b4502019-10-05 03:02:33 +0530150 str, _ := json.Marshal(data)
151 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
152 }
153 if strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000154 logger.Debug(ctx, "Error Error Error Key:", GemportIDs)
kdarapub26b4502019-10-05 03:02:33 +0530155 str, _ := json.Marshal(1)
156 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)
kdarapu3248f9a2019-10-03 13:54:52 +0530160 str, _ := json.Marshal(1)
161 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)
Esin Karamanccb714b2019-11-29 15:02:06 +0000165 groupInfo := resourcemanager.GroupInfo{
166 GroupID: 2,
167 OutPorts: []uint32{1},
168 }
169 str, _ := json.Marshal(&groupInfo)
170 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
171 }
172
Esin Karaman7fb80c22020-07-16 14:23:33 +0000173 if strings.Contains(key, OnuPacketIn) {
174 return getPacketInGemPort(key)
175 }
176
kdarapu3248f9a2019-10-03 13:54:52 +0530177 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
Esin Karaman7fb80c22020-07-16 14:23:33 +0000184//getPacketInGemPort returns the GEM port associated with the given key
185func getPacketInGemPort(key string) (*kvstore.KVPair, error) {
186 //parse interface, onu, uni, vlan, priority values
187 arr := getParamsFromPacketInKey(key)
188
189 if len(arr) < 5 {
190 return nil, errors.New("key didn't find")
191 }
192 if arr[0] == "1" && arr[1] == "1" && arr[2] == "3" && arr[3] == "0" && arr[4] == "0" {
193 str, _ := json.Marshal(3)
194 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
195 }
196 if arr[0] == "2" && arr[1] == "2" && arr[2] == "4" && arr[3] == "549" && arr[4] == "0" {
197 str, _ := json.Marshal(4)
198 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
199 }
200 if arr[0] == "1" && arr[1] == "2" && arr[2] == "2" && arr[3] == "48" && arr[4] == "7" {
201 str, _ := json.Marshal(2)
202 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
203 }
204 return nil, errors.New("key didn't find")
205}
206
207//getParamsFromPacketInKey parse packetIn key that is in the format of "onu_packetin/{1,1,1,1,2}"
208func getParamsFromPacketInKey(key string) []string {
209 //return intfID, onuID, uniID, vlanID, priority
210 firstIndex := strings.Index(key, "{")
211 lastIndex := strings.Index(key, "}")
212 if firstIndex == -1 && lastIndex == -1 {
213 return []string{}
214 }
215 arr := strings.Split(key[firstIndex+1:lastIndex], ",")
216 if len(arr) < 5 {
217 return []string{}
218 }
219 return arr
220}
221
kdarapu3248f9a2019-10-03 13:54:52 +0530222// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530223func (kvclient *MockKVClient) Put(ctx context.Context, key string, value interface{}) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530224 if key != "" {
225
226 return nil
227 }
228 return errors.New("key didn't find")
229}
230
231// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530232func (kvclient *MockKVClient) Delete(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530233 if key == "" {
234 return errors.New("key didn't find")
235 }
236 return nil
237}
238
239// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000240func (kvclient *MockKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
kdarapu3248f9a2019-10-03 13:54:52 +0530241 if key != "" {
242 maps := make(map[string]*kvstore.KVPair)
243 maps[key] = &kvstore.KVPair{Key: key}
244 return maps[key], nil
245 }
246 return nil, errors.New("key didn't find")
247}
248
249// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530250func (kvclient *MockKVClient) ReleaseReservation(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530251 // return nil
252 if key == "" {
253 return errors.New("key didn't find")
254 }
255 return nil
256}
257
258// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530259func (kvclient *MockKVClient) ReleaseAllReservations(ctx context.Context) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530260 return nil
261}
262
263// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530264func (kvclient *MockKVClient) RenewReservation(ctx context.Context, key string) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530265 // return nil
266 if key == "" {
267 return errors.New("key didn't find")
268 }
269 return nil
270}
271
272// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800273func (kvclient *MockKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
kdarapu3248f9a2019-10-03 13:54:52 +0530274 return nil
275 // if key == "" {
276 // return nil
277 // }
278 // return &kvstore.Event{EventType: 1, Key: key}
279}
280
281// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000282func (kvclient *MockKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530283 return nil
284}
285
286// ReleaseLock mock function implementation for KVClient
287func (kvclient *MockKVClient) ReleaseLock(lockName string) error {
288 return nil
289}
290
291// IsConnectionUp mock function implementation for KVClient
Matteo Scandolo945e4012019-12-12 14:16:11 -0800292func (kvclient *MockKVClient) IsConnectionUp(ctx context.Context) bool {
293 // timeout in second
npujarec5762e2020-01-01 14:08:48 +0530294 t, _ := ctx.Deadline()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400295 return t.Second()-time.Now().Second() >= 1
kdarapu3248f9a2019-10-03 13:54:52 +0530296}
297
298// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000299func (kvclient *MockKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
kdarapu3248f9a2019-10-03 13:54:52 +0530300}
301
302// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000303func (kvclient *MockKVClient) Close(ctx context.Context) {
kdarapu3248f9a2019-10-03 13:54:52 +0530304}