blob: e11a7561b60a17d52c156021227b23cd0ef0e7cb [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
26 "github.com/opencord/voltha-go/db/kvstore"
27 ofp "github.com/opencord/voltha-protos/go/openflow_13"
28)
29
30const (
31 // MeterConfig meter to extarct meter
32 MeterConfig = "meter_id"
33 // TpIDPathSuffix to extract Techprofile
34 TpIDPathSuffix = "tp_id"
35)
36
37// MockKVClient mocks the AdapterProxy interface.
38type MockKVClient struct {
39}
40
41// List mock function implementation for KVClient
42func (kvclient *MockKVClient) List(key string, timeout int, lock ...bool) (map[string]*kvstore.KVPair, error) {
43 if key != "" {
44 maps := make(map[string]*kvstore.KVPair)
45 maps[key] = &kvstore.KVPair{Key: key}
46 return maps, nil
47 }
48 return nil, errors.New("key didn't find")
49}
50
51// Get mock function implementation for KVClient
52func (kvclient *MockKVClient) Get(key string, timeout int, lock ...bool) (*kvstore.KVPair, error) {
53 if key != "" {
54
55 if strings.Contains(key, MeterConfig) {
56 var bands []*ofp.OfpMeterBandHeader
57 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
58 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
59
60 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
61 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
62
63 // bands = append(bands, &ofp.OfpMeterBandHeader{})
64 // Data: &ofp.OfpMeterBandHeader_Drop{Drop: &ofp.OfpMeterBandDrop{}}
65 sep := strings.Split(key, "/")[2]
66 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
67 if uint32(val) > 1 {
68 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
69 str, _ := json.Marshal(meterConfig)
70 //json.marshall()
71 return kvstore.NewKVPair(key, string(str), "mock", 3000, 1), nil
72 }
73 if uint32(val) == 1 {
74 return nil, nil
75 }
76 return nil, errors.New("invalid meter")
77 }
78 if strings.Contains(key, TpIDPathSuffix) {
79 str, _ := json.Marshal(1)
80 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
81 }
82 maps := make(map[string]*kvstore.KVPair)
83 maps[key] = &kvstore.KVPair{Key: key}
84 return maps[key], nil
85 }
86 return nil, errors.New("key didn't find")
87}
88
89// Put mock function implementation for KVClient
90func (kvclient *MockKVClient) Put(key string, value interface{}, timeout int, lock ...bool) error {
91 if key != "" {
92
93 return nil
94 }
95 return errors.New("key didn't find")
96}
97
98// Delete mock function implementation for KVClient
99func (kvclient *MockKVClient) Delete(key string, timeout int, lock ...bool) error {
100 if key == "" {
101 return errors.New("key didn't find")
102 }
103 return nil
104}
105
106// Reserve mock function implementation for KVClient
107func (kvclient *MockKVClient) Reserve(key string, value interface{}, ttl int64) (interface{}, error) {
108 if key != "" {
109 maps := make(map[string]*kvstore.KVPair)
110 maps[key] = &kvstore.KVPair{Key: key}
111 return maps[key], nil
112 }
113 return nil, errors.New("key didn't find")
114}
115
116// ReleaseReservation mock function implementation for KVClient
117func (kvclient *MockKVClient) ReleaseReservation(key string) error {
118 // return nil
119 if key == "" {
120 return errors.New("key didn't find")
121 }
122 return nil
123}
124
125// ReleaseAllReservations mock function implementation for KVClient
126func (kvclient *MockKVClient) ReleaseAllReservations() error {
127 return nil
128}
129
130// RenewReservation mock function implementation for KVClient
131func (kvclient *MockKVClient) RenewReservation(key string) error {
132 // return nil
133 if key == "" {
134 return errors.New("key didn't find")
135 }
136 return nil
137}
138
139// Watch mock function implementation for KVClient
140func (kvclient *MockKVClient) Watch(key string) chan *kvstore.Event {
141 return nil
142 // if key == "" {
143 // return nil
144 // }
145 // return &kvstore.Event{EventType: 1, Key: key}
146}
147
148// AcquireLock mock function implementation for KVClient
149func (kvclient *MockKVClient) AcquireLock(lockName string, timeout int) error {
150 return nil
151}
152
153// ReleaseLock mock function implementation for KVClient
154func (kvclient *MockKVClient) ReleaseLock(lockName string) error {
155 return nil
156}
157
158// IsConnectionUp mock function implementation for KVClient
159func (kvclient *MockKVClient) IsConnectionUp(timeout int) bool { // timeout in second
160 if timeout < 1 {
161 return false
162 }
163 return true
164}
165
166// CloseWatch mock function implementation for KVClient
167func (kvclient *MockKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
168}
169
170// Close mock function implementation for KVClient
171func (kvclient *MockKVClient) Close() {
172}