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