blob: e4d8021e6798bfc87364af2b2d340d0b6fa49ea8 [file] [log] [blame]
Esin Karaman5351fc52020-02-14 07:45:49 +00001/*
2 * Copyright 2020-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
17package ponresourcemanager
18
19import (
20 "context"
21 "encoding/json"
22 "errors"
23 "github.com/opencord/voltha-lib-go/v3/pkg/db"
24 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
25 "github.com/opencord/voltha-lib-go/v3/pkg/log"
26 "github.com/stretchr/testify/assert"
27 "strings"
28 "testing"
29)
30
31const (
32 GEM_POOL_PATH = "gemport_id_pool"
33 RESERVED_GEM_PORT_ID = uint32(5)
34)
35
36func init() {
37 _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
38 if err != nil {
39 panic(err)
40 }
41}
42
43// MockKVClient mocks the AdapterProxy interface.
44type MockResKVClient struct {
45 resourceMap map[string]interface{}
46}
47
48func newMockKvClient() *MockResKVClient {
49 var mockResKVClient MockResKVClient
50 mockResKVClient.resourceMap = make(map[string]interface{})
51 return &mockResKVClient
52}
53
54// List function implemented for KVClient.
55func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
56 return nil, errors.New("key didn't find")
57}
58
59// Get mock function implementation for KVClient
60func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
61 log.Debugw("Get of MockKVClient called", log.Fields{"key": key})
62 if key != "" {
63 if strings.Contains(key, RESERVED_GEMPORT_IDS_PATH) {
64 log.Debug("Getting Key:", RESERVED_GEMPORT_IDS_PATH)
65 reservedGemPorts := []uint32{RESERVED_GEM_PORT_ID}
66 str, _ := json.Marshal(reservedGemPorts)
67 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
68 }
69 if strings.Contains(key, GEM_POOL_PATH) {
70 log.Debug("Getting Key:", GEM_POOL_PATH)
71 resource := kvclient.resourceMap[key]
72 return kvstore.NewKVPair(key, resource, "mock", 3000, 1), nil
73 }
74 maps := make(map[string]*kvstore.KVPair)
75 maps[key] = &kvstore.KVPair{Key: key}
76 return maps[key], nil
77 }
78 return nil, errors.New("key didn't find")
79}
80
81// Put mock function implementation for KVClient
82func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
83 if key != "" {
84 if strings.Contains(key, GEMPORT_ID_POOL_PATH) && value != nil {
85 kvclient.resourceMap[key] = value
86 }
87 return nil
88 }
89 return errors.New("key didn't find")
90}
91
92// Delete mock function implementation for KVClient
93func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
94 return nil
95}
96
97// Reserve mock function implementation for KVClient
98func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl int64) (interface{}, error) {
99 return nil, errors.New("key didn't find")
100}
101
102// ReleaseReservation mock function implementation for KVClient
103func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
104 return nil
105}
106
107// ReleaseAllReservations mock function implementation for KVClient
108func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
109 return nil
110}
111
112// RenewReservation mock function implementation for KVClient
113func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
114 return nil
115}
116
117// Watch mock function implementation for KVClient
118func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
119 return nil
120}
121
122// AcquireLock mock function implementation for KVClient
123func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout int) error {
124 return nil
125}
126
127// ReleaseLock mock function implementation for KVClient
128func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
129 return nil
130}
131
132// IsConnectionUp mock function implementation for KVClient
133func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
134 return true
135}
136
137// CloseWatch mock function implementation for KVClient
138func (kvclient *MockResKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
139}
140
141// Close mock function implementation for KVClient
142func (kvclient *MockResKVClient) Close() {
143}
144
145func TestExcludeReservedGemPortIdFromThePool(t *testing.T) {
146 PONRMgr, err := NewPONResourceManager("gpon", "onu", "olt1",
147 "etcd", "1", 1)
148 if err != nil {
149 return
150 }
151 PONRMgr.KVStore = &db.Backend{
152 Client: newMockKvClient(),
153 }
154
155 PONRMgr.KVStoreForConfig = &db.Backend{
156 Client: newMockKvClient(),
157 }
158 // create a pool in the range of [1,16]
159 // and exclude id 5 from this pool
160 StartIndex := uint32(1)
161 EndIndex := uint32(16)
162
163 ctx := context.Background()
164 reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx)
165 if !defined {
166 return
167 }
168
169 FormatResult, err := PONRMgr.FormatResource(1, StartIndex, EndIndex, reservedGemPortIds)
170 if err != nil {
171 t.Error("Failed to format resource", err)
172 return
173 }
174
175 // Add resource as json in kv store.
176 err = PONRMgr.KVStore.Put(ctx, GEMPORT_ID_POOL_PATH, FormatResult)
177 if err != nil {
178 t.Error("Error in posting data to kv store", GEMPORT_ID_POOL_PATH)
179 return
180 }
181
182 for i := StartIndex; i <= (EndIndex - uint32(len(reservedGemPortIds))); i++ {
183 // get gem port id pool from the kv store
184 resource, err := PONRMgr.GetResource(context.Background(), GEMPORT_ID_POOL_PATH)
185 if err != nil {
186 t.Error("Failed to get resource from gem port id pool", err)
187 return
188 }
189 // get a gem port id from the pool
190 nextID, err := PONRMgr.GenerateNextID(resource)
191 if err != nil {
192 t.Error("Failed to get gem port id from the pool", err)
193 return
194 }
195
196 //given gem port id should not equal to the reserved gem port id
197 assert.NotEqual(t, nextID, RESERVED_GEM_PORT_ID)
198 // put updated gem port id pool into the kv store
199 err = PONRMgr.UpdateResource(context.Background(), GEMPORT_ID_POOL_PATH, resource)
200 if err != nil {
201 t.Error("Failed to put updated gem port id pool into the kv store", err)
202 return
203 }
204 }
205
206}