blob: 7d12cac31956a1ff103f280330aab523c4b6c762 [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
Esin Karamanccb714b2019-11-29 15:02:06 +000026 "github.com/opencord/voltha-lib-go/v3/pkg/log"
kdarapub26b4502019-10-05 03:02:33 +053027 "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
28
Esin Karamanccb714b2019-11-29 15:02:06 +000029 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
30 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
31 openolt "github.com/opencord/voltha-protos/v3/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"
Esin Karamanccb714b2019-11-29 15:02:06 +000049 //FlowGroup flow_groups/<flow_group_id>
50 FlowGroup = "flow_groups"
51 //FlowGroupCached flow_groups_cached/<flow_group_id>
52 FlowGroupCached = "flow_groups_cached"
kdarapu3248f9a2019-10-03 13:54:52 +053053)
54
55// MockKVClient mocks the AdapterProxy interface.
56type MockKVClient struct {
57}
58
59// List mock function implementation for KVClient
sbarbaria8910ba2019-11-05 10:12:23 -050060func (kvclient *MockKVClient) List(key string, timeout int) (map[string]*kvstore.KVPair, error) {
kdarapu3248f9a2019-10-03 13:54:52 +053061 if key != "" {
62 maps := make(map[string]*kvstore.KVPair)
63 maps[key] = &kvstore.KVPair{Key: key}
64 return maps, nil
65 }
66 return nil, errors.New("key didn't find")
67}
68
69// Get mock function implementation for KVClient
sbarbaria8910ba2019-11-05 10:12:23 -050070func (kvclient *MockKVClient) Get(key string, timeout int) (*kvstore.KVPair, error) {
kdarapub26b4502019-10-05 03:02:33 +053071 log.Debugw("Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
kdarapu3248f9a2019-10-03 13:54:52 +053072 if key != "" {
kdarapub26b4502019-10-05 03:02:33 +053073 log.Debug("Warning Key Not Blank")
74 if strings.Contains(key, "meter_id/{0,62,8}/{upstream}") {
75 meterConfig := ofp.OfpMeterConfig{
76 Flags: 0,
77 MeterId: 1,
78 }
79 str, _ := json.Marshal(meterConfig)
80 return kvstore.NewKVPair(key, string(str), "mock", 3000, 1), nil
81 }
kdarapu3248f9a2019-10-03 13:54:52 +053082 if strings.Contains(key, MeterConfig) {
83 var bands []*ofp.OfpMeterBandHeader
84 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
85 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
86
87 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
88 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
89
Gamze Abakafee36392019-10-03 11:17:24 +000090 sep := strings.Split(key, "/")[1]
kdarapu3248f9a2019-10-03 13:54:52 +053091 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
92 if uint32(val) > 1 {
93 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
94 str, _ := json.Marshal(meterConfig)
kdarapub26b4502019-10-05 03:02:33 +053095
96 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
97 }
98
99 if strings.Contains(key, "meter_id/{1,1,1}/{downstream}") {
100
101 band1 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 1000, BurstSize: 5000}
102 band2 := &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DROP, Rate: 2000, BurstSize: 5000}
103 bands := []*ofp.OfpMeterBandHeader{band1, band2}
104 ofpMeterConfig := &ofp.OfpMeterConfig{Flags: 1, MeterId: 1, Bands: bands}
105 str, _ := json.Marshal(ofpMeterConfig)
106 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
kdarapu3248f9a2019-10-03 13:54:52 +0530107 }
108 if uint32(val) == 1 {
109 return nil, nil
110 }
111 return nil, errors.New("invalid meter")
112 }
113 if strings.Contains(key, TpIDPathSuffix) {
kdarapub26b4502019-10-05 03:02:33 +0530114 str, _ := json.Marshal(64)
115 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
116 }
117 if strings.Contains(key, FlowIDpool) {
118 log.Debug("Error Error Error Key:", FlowIDpool)
119 data := make(map[string]interface{})
120 data["pool"] = "1024"
121 data["start_idx"] = 1
122 data["end_idx"] = 1024
123 str, _ := json.Marshal(data)
124 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
125 }
126 if strings.Contains(key, FlowIDs) {
127 data := []uint32{1, 2}
128 log.Debug("Error Error Error Key:", FlowIDs)
129 str, _ := json.Marshal(data)
130 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
131 }
132 if strings.Contains(key, FlowIDInfo) {
133
134 data := []resourcemanager.FlowInfo{
135 {
136 Flow: &openolt.Flow{FlowId: 1, OnuId: 1, UniId: 1, GemportId: 1},
137 FlowStoreCookie: uint64(48132224281636694),
Esin Karamanccb714b2019-11-29 15:02:06 +0000138 LogicalFlowID: 1,
kdarapub26b4502019-10-05 03:02:33 +0530139 },
140 }
141 log.Debug("Error Error Error Key:", FlowIDs)
142 str, _ := json.Marshal(data)
143 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
144 }
145 if strings.Contains(key, GemportIDs) {
146 log.Debug("Error Error Error Key:", GemportIDs)
147 str, _ := json.Marshal(1)
148 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
149 }
150 if strings.Contains(key, AllocIDs) {
151 log.Debug("Error Error Error Key:", AllocIDs)
kdarapu3248f9a2019-10-03 13:54:52 +0530152 str, _ := json.Marshal(1)
153 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
154 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000155 if strings.Contains(key, FlowGroup) || strings.Contains(key, FlowGroupCached) {
156 log.Debug("Error Error Error Key:", FlowGroup)
157 groupInfo := resourcemanager.GroupInfo{
158 GroupID: 2,
159 OutPorts: []uint32{1},
160 }
161 str, _ := json.Marshal(&groupInfo)
162 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
163 }
164
kdarapu3248f9a2019-10-03 13:54:52 +0530165 maps := make(map[string]*kvstore.KVPair)
166 maps[key] = &kvstore.KVPair{Key: key}
167 return maps[key], nil
168 }
169 return nil, errors.New("key didn't find")
170}
171
172// Put mock function implementation for KVClient
sbarbaria8910ba2019-11-05 10:12:23 -0500173func (kvclient *MockKVClient) Put(key string, value interface{}, timeout int) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530174 if key != "" {
175
176 return nil
177 }
178 return errors.New("key didn't find")
179}
180
181// Delete mock function implementation for KVClient
sbarbaria8910ba2019-11-05 10:12:23 -0500182func (kvclient *MockKVClient) Delete(key string, timeout int) error {
kdarapu3248f9a2019-10-03 13:54:52 +0530183 if key == "" {
184 return errors.New("key didn't find")
185 }
186 return nil
187}
188
189// Reserve mock function implementation for KVClient
190func (kvclient *MockKVClient) Reserve(key string, value interface{}, ttl int64) (interface{}, error) {
191 if key != "" {
192 maps := make(map[string]*kvstore.KVPair)
193 maps[key] = &kvstore.KVPair{Key: key}
194 return maps[key], nil
195 }
196 return nil, errors.New("key didn't find")
197}
198
199// ReleaseReservation mock function implementation for KVClient
200func (kvclient *MockKVClient) ReleaseReservation(key string) error {
201 // return nil
202 if key == "" {
203 return errors.New("key didn't find")
204 }
205 return nil
206}
207
208// ReleaseAllReservations mock function implementation for KVClient
209func (kvclient *MockKVClient) ReleaseAllReservations() error {
210 return nil
211}
212
213// RenewReservation mock function implementation for KVClient
214func (kvclient *MockKVClient) RenewReservation(key string) error {
215 // return nil
216 if key == "" {
217 return errors.New("key didn't find")
218 }
219 return nil
220}
221
222// Watch mock function implementation for KVClient
223func (kvclient *MockKVClient) Watch(key string) chan *kvstore.Event {
224 return nil
225 // if key == "" {
226 // return nil
227 // }
228 // return &kvstore.Event{EventType: 1, Key: key}
229}
230
231// AcquireLock mock function implementation for KVClient
232func (kvclient *MockKVClient) AcquireLock(lockName string, timeout int) error {
233 return nil
234}
235
236// ReleaseLock mock function implementation for KVClient
237func (kvclient *MockKVClient) ReleaseLock(lockName string) error {
238 return nil
239}
240
241// IsConnectionUp mock function implementation for KVClient
242func (kvclient *MockKVClient) IsConnectionUp(timeout int) bool { // timeout in second
243 if timeout < 1 {
244 return false
245 }
246 return true
247}
248
249// CloseWatch mock function implementation for KVClient
250func (kvclient *MockKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
251}
252
253// Close mock function implementation for KVClient
254func (kvclient *MockKVClient) Close() {
255}