blob: 0eab36e868232382b014a7d272951b301866cd50 [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"
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
91// Reserve mock function implementation for KVClient
Neha Sharma130ac6d2020-04-08 08:46:32 +000092func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
Esin Karaman5351fc52020-02-14 07:45:49 +000093 return nil, errors.New("key didn't find")
94}
95
96// ReleaseReservation mock function implementation for KVClient
97func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
98 return nil
99}
100
101// ReleaseAllReservations mock function implementation for KVClient
102func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
103 return nil
104}
105
106// RenewReservation mock function implementation for KVClient
107func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
108 return nil
109}
110
111// Watch mock function implementation for KVClient
112func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
113 return nil
114}
115
116// AcquireLock mock function implementation for KVClient
Neha Sharma130ac6d2020-04-08 08:46:32 +0000117func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
Esin Karaman5351fc52020-02-14 07:45:49 +0000118 return nil
119}
120
121// ReleaseLock mock function implementation for KVClient
122func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
123 return nil
124}
125
126// IsConnectionUp mock function implementation for KVClient
127func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
128 return true
129}
130
131// CloseWatch mock function implementation for KVClient
Neha Sharma94f16a92020-06-26 04:17:55 +0000132func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
Esin Karaman5351fc52020-02-14 07:45:49 +0000133}
134
135// Close mock function implementation for KVClient
Neha Sharma94f16a92020-06-26 04:17:55 +0000136func (kvclient *MockResKVClient) Close(ctx context.Context) {
Esin Karaman5351fc52020-02-14 07:45:49 +0000137}
138
139func TestExcludeReservedGemPortIdFromThePool(t *testing.T) {
Neha Sharma94f16a92020-06-26 04:17:55 +0000140 ctx := context.Background()
141 PONRMgr, err := NewPONResourceManager(ctx, "gpon", "onu", "olt1",
Neha Sharmadd9af392020-04-28 09:03:57 +0000142 "etcd", "1:1")
Esin Karaman5351fc52020-02-14 07:45:49 +0000143 if err != nil {
144 return
145 }
146 PONRMgr.KVStore = &db.Backend{
Neha Sharma94f16a92020-06-26 04:17:55 +0000147 Client: newMockKvClient(ctx),
Esin Karaman5351fc52020-02-14 07:45:49 +0000148 }
149
150 PONRMgr.KVStoreForConfig = &db.Backend{
Neha Sharma94f16a92020-06-26 04:17:55 +0000151 Client: newMockKvClient(ctx),
Esin Karaman5351fc52020-02-14 07:45:49 +0000152 }
153 // create a pool in the range of [1,16]
154 // and exclude id 5 from this pool
155 StartIndex := uint32(1)
156 EndIndex := uint32(16)
157
Esin Karaman5351fc52020-02-14 07:45:49 +0000158 reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx)
159 if !defined {
160 return
161 }
162
Neha Sharma94f16a92020-06-26 04:17:55 +0000163 FormatResult, err := PONRMgr.FormatResource(ctx, 1, StartIndex, EndIndex, reservedGemPortIds)
Esin Karaman5351fc52020-02-14 07:45:49 +0000164 if err != nil {
165 t.Error("Failed to format resource", err)
166 return
167 }
168
169 // Add resource as json in kv store.
170 err = PONRMgr.KVStore.Put(ctx, GEMPORT_ID_POOL_PATH, FormatResult)
171 if err != nil {
172 t.Error("Error in posting data to kv store", GEMPORT_ID_POOL_PATH)
173 return
174 }
175
176 for i := StartIndex; i <= (EndIndex - uint32(len(reservedGemPortIds))); i++ {
177 // get gem port id pool from the kv store
178 resource, err := PONRMgr.GetResource(context.Background(), GEMPORT_ID_POOL_PATH)
179 if err != nil {
180 t.Error("Failed to get resource from gem port id pool", err)
181 return
182 }
183 // get a gem port id from the pool
Neha Sharma94f16a92020-06-26 04:17:55 +0000184 nextID, err := PONRMgr.GenerateNextID(ctx, resource)
Esin Karaman5351fc52020-02-14 07:45:49 +0000185 if err != nil {
186 t.Error("Failed to get gem port id from the pool", err)
187 return
188 }
189
190 //given gem port id should not equal to the reserved gem port id
191 assert.NotEqual(t, nextID, RESERVED_GEM_PORT_ID)
192 // put updated gem port id pool into the kv store
193 err = PONRMgr.UpdateResource(context.Background(), GEMPORT_ID_POOL_PATH, resource)
194 if err != nil {
195 t.Error("Failed to put updated gem port id pool into the kv store", err)
196 return
197 }
198 }
199
200}