blob: 1bc908e6c946b396b00af48b60f6a5f63383e945 [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"
Girish Gowdra89c985b2020-10-14 15:02:09 -070023 "github.com/opencord/voltha-lib-go/v4/pkg/db"
24 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
25 "github.com/opencord/voltha-lib-go/v4/pkg/log"
Esin Karaman5351fc52020-02-14 07:45:49 +000026 "github.com/stretchr/testify/assert"
27 "strings"
28 "testing"
Neha Sharma130ac6d2020-04-08 08:46:32 +000029 "time"
Esin Karaman5351fc52020-02-14 07:45:49 +000030)
31
32const (
33 GEM_POOL_PATH = "gemport_id_pool"
34 RESERVED_GEM_PORT_ID = uint32(5)
35)
36
Esin Karaman5351fc52020-02-14 07:45:49 +000037// MockKVClient mocks the AdapterProxy interface.
38type MockResKVClient struct {
39 resourceMap map[string]interface{}
40}
41
Neha Sharma94f16a92020-06-26 04:17:55 +000042func newMockKvClient(ctx context.Context) *MockResKVClient {
Esin Karaman5351fc52020-02-14 07:45:49 +000043 var mockResKVClient MockResKVClient
44 mockResKVClient.resourceMap = make(map[string]interface{})
45 return &mockResKVClient
46}
47
48// List function implemented for KVClient.
49func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
50 return nil, errors.New("key didn't find")
51}
52
53// Get mock function implementation for KVClient
54func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma94f16a92020-06-26 04:17:55 +000055 logger.Debugw(ctx, "Get of MockKVClient called", log.Fields{"key": key})
Esin Karaman5351fc52020-02-14 07:45:49 +000056 if key != "" {
57 if strings.Contains(key, RESERVED_GEMPORT_IDS_PATH) {
Neha Sharma94f16a92020-06-26 04:17:55 +000058 logger.Debug(ctx, "Getting Key:", RESERVED_GEMPORT_IDS_PATH)
Esin Karaman5351fc52020-02-14 07:45:49 +000059 reservedGemPorts := []uint32{RESERVED_GEM_PORT_ID}
60 str, _ := json.Marshal(reservedGemPorts)
61 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
62 }
63 if strings.Contains(key, GEM_POOL_PATH) {
Neha Sharma94f16a92020-06-26 04:17:55 +000064 logger.Debug(ctx, "Getting Key:", GEM_POOL_PATH)
Esin Karaman5351fc52020-02-14 07:45:49 +000065 resource := kvclient.resourceMap[key]
66 return kvstore.NewKVPair(key, resource, "mock", 3000, 1), nil
67 }
68 maps := make(map[string]*kvstore.KVPair)
69 maps[key] = &kvstore.KVPair{Key: key}
70 return maps[key], nil
71 }
72 return nil, errors.New("key didn't find")
73}
74
75// Put mock function implementation for KVClient
76func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
77 if key != "" {
78 if strings.Contains(key, GEMPORT_ID_POOL_PATH) && value != nil {
79 kvclient.resourceMap[key] = value
80 }
81 return nil
82 }
83 return errors.New("key didn't find")
84}
85
86// Delete mock function implementation for KVClient
87func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
88 return nil
89}
90
Serkant Uluderya198de902020-11-16 20:29:17 +030091func (c *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefixKey string) error {
92 return nil
93}
94
Esin Karaman5351fc52020-02-14 07:45:49 +000095// Reserve mock function implementation for KVClient
Neha Sharma130ac6d2020-04-08 08:46:32 +000096func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
Esin Karaman5351fc52020-02-14 07:45:49 +000097 return nil, errors.New("key didn't find")
98}
99
100// ReleaseReservation mock function implementation for KVClient
101func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
102 return nil
103}
104
105// ReleaseAllReservations mock function implementation for KVClient
106func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
107 return nil
108}
109
110// RenewReservation mock function implementation for KVClient
111func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
112 return nil
113}
114
115// Watch mock function implementation for KVClient
116func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
117 return nil
118}
119
120// AcquireLock mock function implementation for KVClient
Neha Sharma130ac6d2020-04-08 08:46:32 +0000121func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
Esin Karaman5351fc52020-02-14 07:45:49 +0000122 return nil
123}
124
125// ReleaseLock mock function implementation for KVClient
126func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
127 return nil
128}
129
130// IsConnectionUp mock function implementation for KVClient
131func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
132 return true
133}
134
135// CloseWatch mock function implementation for KVClient
Neha Sharma94f16a92020-06-26 04:17:55 +0000136func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
Esin Karaman5351fc52020-02-14 07:45:49 +0000137}
138
139// Close mock function implementation for KVClient
Neha Sharma94f16a92020-06-26 04:17:55 +0000140func (kvclient *MockResKVClient) Close(ctx context.Context) {
Esin Karaman5351fc52020-02-14 07:45:49 +0000141}
142
143func TestExcludeReservedGemPortIdFromThePool(t *testing.T) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000144 ctx := context.Background()
145 PONRMgr, err := NewPONResourceManager(ctx, "gpon", "onu", "olt1",
Matteo Scandolo29ff79c2020-11-06 13:03:17 -0800146 "etcd", "1:1", "service/voltha")
Esin Karaman5351fc52020-02-14 07:45:49 +0000147 if err != nil {
148 return
149 }
150 PONRMgr.KVStore = &db.Backend{
Neha Sharma94f16a92020-06-26 04:17:55 +0000151 Client: newMockKvClient(ctx),
Esin Karaman5351fc52020-02-14 07:45:49 +0000152 }
153
154 PONRMgr.KVStoreForConfig = &db.Backend{
Neha Sharma94f16a92020-06-26 04:17:55 +0000155 Client: newMockKvClient(ctx),
Esin Karaman5351fc52020-02-14 07:45:49 +0000156 }
157 // create a pool in the range of [1,16]
158 // and exclude id 5 from this pool
159 StartIndex := uint32(1)
160 EndIndex := uint32(16)
161
Esin Karaman5351fc52020-02-14 07:45:49 +0000162 reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx)
163 if !defined {
164 return
165 }
166
Neha Sharma94f16a92020-06-26 04:17:55 +0000167 FormatResult, err := PONRMgr.FormatResource(ctx, 1, StartIndex, EndIndex, reservedGemPortIds)
Esin Karaman5351fc52020-02-14 07:45:49 +0000168 if err != nil {
169 t.Error("Failed to format resource", err)
170 return
171 }
172
173 // Add resource as json in kv store.
174 err = PONRMgr.KVStore.Put(ctx, GEMPORT_ID_POOL_PATH, FormatResult)
175 if err != nil {
176 t.Error("Error in posting data to kv store", GEMPORT_ID_POOL_PATH)
177 return
178 }
179
180 for i := StartIndex; i <= (EndIndex - uint32(len(reservedGemPortIds))); i++ {
181 // get gem port id pool from the kv store
182 resource, err := PONRMgr.GetResource(context.Background(), GEMPORT_ID_POOL_PATH)
183 if err != nil {
184 t.Error("Failed to get resource from gem port id pool", err)
185 return
186 }
187 // get a gem port id from the pool
Neha Sharma94f16a92020-06-26 04:17:55 +0000188 nextID, err := PONRMgr.GenerateNextID(ctx, resource)
Esin Karaman5351fc52020-02-14 07:45:49 +0000189 if err != nil {
190 t.Error("Failed to get gem port id from the pool", err)
191 return
192 }
193
194 //given gem port id should not equal to the reserved gem port id
195 assert.NotEqual(t, nextID, RESERVED_GEM_PORT_ID)
196 // put updated gem port id pool into the kv store
197 err = PONRMgr.UpdateResource(context.Background(), GEMPORT_ID_POOL_PATH, resource)
198 if err != nil {
199 t.Error("Failed to put updated gem port id pool into the kv store", err)
200 return
201 }
202 }
203
204}