blob: b91107c4127d8e6b1fc4a39ee8275981993b5a8a [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
Scott Bakere6685952020-06-23 04:05:39 +000042func newMockKvClient() *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) {
Scott Bakere6685952020-06-23 04:05:39 +000055 logger.Debugw("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) {
Scott Bakere6685952020-06-23 04:05:39 +000058 logger.Debug("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) {
Scott Bakere6685952020-06-23 04:05:39 +000064 logger.Debug("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
Scott Bakere6685952020-06-23 04:05:39 +0000132func (kvclient *MockResKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
Esin Karaman5351fc52020-02-14 07:45:49 +0000133}
134
135// Close mock function implementation for KVClient
Scott Bakere6685952020-06-23 04:05:39 +0000136func (kvclient *MockResKVClient) Close() {
Esin Karaman5351fc52020-02-14 07:45:49 +0000137}
138
139func TestExcludeReservedGemPortIdFromThePool(t *testing.T) {
Scott Bakere6685952020-06-23 04:05:39 +0000140 PONRMgr, err := NewPONResourceManager("gpon", "onu", "olt1",
Neha Sharmadd9af392020-04-28 09:03:57 +0000141 "etcd", "1:1")
Esin Karaman5351fc52020-02-14 07:45:49 +0000142 if err != nil {
143 return
144 }
145 PONRMgr.KVStore = &db.Backend{
Scott Bakere6685952020-06-23 04:05:39 +0000146 Client: newMockKvClient(),
Esin Karaman5351fc52020-02-14 07:45:49 +0000147 }
148
149 PONRMgr.KVStoreForConfig = &db.Backend{
Scott Bakere6685952020-06-23 04:05:39 +0000150 Client: newMockKvClient(),
Esin Karaman5351fc52020-02-14 07:45:49 +0000151 }
152 // create a pool in the range of [1,16]
153 // and exclude id 5 from this pool
154 StartIndex := uint32(1)
155 EndIndex := uint32(16)
156
Scott Bakere6685952020-06-23 04:05:39 +0000157 ctx := context.Background()
Esin Karaman5351fc52020-02-14 07:45:49 +0000158 reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx)
159 if !defined {
160 return
161 }
162
Scott Bakere6685952020-06-23 04:05:39 +0000163 FormatResult, err := PONRMgr.FormatResource(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
Scott Bakere6685952020-06-23 04:05:39 +0000184 nextID, err := PONRMgr.GenerateNextID(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}