Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package ponresourcemanager |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
| 22 | "errors" |
Girish Gowdra | 89c985b | 2020-10-14 15:02:09 -0700 | [diff] [blame] | 23 | "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 Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 26 | "github.com/stretchr/testify/assert" |
| 27 | "strings" |
| 28 | "testing" |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 29 | "time" |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | const ( |
| 33 | GEM_POOL_PATH = "gemport_id_pool" |
| 34 | RESERVED_GEM_PORT_ID = uint32(5) |
| 35 | ) |
| 36 | |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 37 | // MockKVClient mocks the AdapterProxy interface. |
| 38 | type MockResKVClient struct { |
| 39 | resourceMap map[string]interface{} |
| 40 | } |
| 41 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 42 | func newMockKvClient(ctx context.Context) *MockResKVClient { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 43 | var mockResKVClient MockResKVClient |
| 44 | mockResKVClient.resourceMap = make(map[string]interface{}) |
| 45 | return &mockResKVClient |
| 46 | } |
| 47 | |
| 48 | // List function implemented for KVClient. |
| 49 | func (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 |
| 54 | func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 55 | logger.Debugw(ctx, "Get of MockKVClient called", log.Fields{"key": key}) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 56 | if key != "" { |
| 57 | if strings.Contains(key, RESERVED_GEMPORT_IDS_PATH) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 58 | logger.Debug(ctx, "Getting Key:", RESERVED_GEMPORT_IDS_PATH) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 59 | 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 Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 64 | logger.Debug(ctx, "Getting Key:", GEM_POOL_PATH) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 65 | 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 |
| 76 | func (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 |
| 87 | func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error { |
| 88 | return nil |
| 89 | } |
| 90 | |
Serkant Uluderya | 198de90 | 2020-11-16 20:29:17 +0300 | [diff] [blame] | 91 | func (c *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefixKey string) error { |
| 92 | return nil |
| 93 | } |
| 94 | |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 95 | // Reserve mock function implementation for KVClient |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 96 | func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 97 | return nil, errors.New("key didn't find") |
| 98 | } |
| 99 | |
| 100 | // ReleaseReservation mock function implementation for KVClient |
| 101 | func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error { |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | // ReleaseAllReservations mock function implementation for KVClient |
| 106 | func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error { |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | // RenewReservation mock function implementation for KVClient |
| 111 | func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error { |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | // Watch mock function implementation for KVClient |
| 116 | func (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 Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 121 | func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 122 | return nil |
| 123 | } |
| 124 | |
| 125 | // ReleaseLock mock function implementation for KVClient |
| 126 | func (kvclient *MockResKVClient) ReleaseLock(lockName string) error { |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | // IsConnectionUp mock function implementation for KVClient |
| 131 | func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second |
| 132 | return true |
| 133 | } |
| 134 | |
| 135 | // CloseWatch mock function implementation for KVClient |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 136 | func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Close mock function implementation for KVClient |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 140 | func (kvclient *MockResKVClient) Close(ctx context.Context) { |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | func TestExcludeReservedGemPortIdFromThePool(t *testing.T) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 144 | ctx := context.Background() |
| 145 | PONRMgr, err := NewPONResourceManager(ctx, "gpon", "onu", "olt1", |
Matteo Scandolo | 29ff79c | 2020-11-06 13:03:17 -0800 | [diff] [blame] | 146 | "etcd", "1:1", "service/voltha") |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 147 | if err != nil { |
| 148 | return |
| 149 | } |
| 150 | PONRMgr.KVStore = &db.Backend{ |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 151 | Client: newMockKvClient(ctx), |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | PONRMgr.KVStoreForConfig = &db.Backend{ |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 155 | Client: newMockKvClient(ctx), |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 156 | } |
| 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 Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 162 | reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx) |
| 163 | if !defined { |
| 164 | return |
| 165 | } |
| 166 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 167 | FormatResult, err := PONRMgr.FormatResource(ctx, 1, StartIndex, EndIndex, reservedGemPortIds) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 168 | 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 Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 188 | nextID, err := PONRMgr.GenerateNextID(ctx, resource) |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 189 | 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 | } |