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" |
| 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 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 | |
| 42 | func newMockKvClient() *MockResKVClient { |
| 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) { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 55 | logger.Debugw("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) { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 58 | logger.Debug("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) { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 64 | logger.Debug("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 | |
| 91 | // Reserve mock function implementation for KVClient |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 92 | 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] | 93 | return nil, errors.New("key didn't find") |
| 94 | } |
| 95 | |
| 96 | // ReleaseReservation mock function implementation for KVClient |
| 97 | func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error { |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | // ReleaseAllReservations mock function implementation for KVClient |
| 102 | func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error { |
| 103 | return nil |
| 104 | } |
| 105 | |
| 106 | // RenewReservation mock function implementation for KVClient |
| 107 | func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error { |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | // Watch mock function implementation for KVClient |
| 112 | func (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 Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 117 | 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] | 118 | return nil |
| 119 | } |
| 120 | |
| 121 | // ReleaseLock mock function implementation for KVClient |
| 122 | func (kvclient *MockResKVClient) ReleaseLock(lockName string) error { |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | // IsConnectionUp mock function implementation for KVClient |
| 127 | func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second |
| 128 | return true |
| 129 | } |
| 130 | |
| 131 | // CloseWatch mock function implementation for KVClient |
| 132 | func (kvclient *MockResKVClient) CloseWatch(key string, ch chan *kvstore.Event) { |
| 133 | } |
| 134 | |
| 135 | // Close mock function implementation for KVClient |
| 136 | func (kvclient *MockResKVClient) Close() { |
| 137 | } |
| 138 | |
| 139 | func TestExcludeReservedGemPortIdFromThePool(t *testing.T) { |
| 140 | PONRMgr, err := NewPONResourceManager("gpon", "onu", "olt1", |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 141 | "etcd", "1:1") |
Esin Karaman | 5351fc5 | 2020-02-14 07:45:49 +0000 | [diff] [blame] | 142 | if err != nil { |
| 143 | return |
| 144 | } |
| 145 | PONRMgr.KVStore = &db.Backend{ |
| 146 | Client: newMockKvClient(), |
| 147 | } |
| 148 | |
| 149 | PONRMgr.KVStoreForConfig = &db.Backend{ |
| 150 | Client: newMockKvClient(), |
| 151 | } |
| 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 | |
| 157 | ctx := context.Background() |
| 158 | reservedGemPortIds, defined := PONRMgr.getReservedGemPortIdsFromKVStore(ctx) |
| 159 | if !defined { |
| 160 | return |
| 161 | } |
| 162 | |
| 163 | FormatResult, err := PONRMgr.FormatResource(1, StartIndex, EndIndex, reservedGemPortIds) |
| 164 | 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 |
| 184 | nextID, err := PONRMgr.GenerateNextID(resource) |
| 185 | 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 | } |