Prince Pereira | 32d40f2 | 2021-05-27 06:14:29 +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 db holds utils for datastore implementation |
| 18 | package db |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "errors" |
| 23 | "strings" |
| 24 | "sync" |
| 25 | "time" |
| 26 | |
| 27 | "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore" |
| 28 | |
| 29 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 30 | ) |
| 31 | |
| 32 | type mockKVClient struct { |
| 33 | } |
| 34 | |
| 35 | var testKvPairCache *sync.Map |
| 36 | |
| 37 | // MockKVClient function mimics the kvclient |
| 38 | func MockKVClient() { |
| 39 | kvClient = new(mockKVClient) |
| 40 | testKvPairCache = new(sync.Map) |
| 41 | } |
| 42 | |
| 43 | // ClearCache function clears the kvclient cache |
| 44 | func ClearCache() { |
| 45 | testKvPairCache = new(sync.Map) |
| 46 | } |
| 47 | |
| 48 | // List function implemented for KVClient. |
| 49 | func (kvclient *mockKVClient) List(ctx context.Context, prefix string) (map[string]*kvstore.KVPair, error) { |
| 50 | kvPairMap := make(map[string]*kvstore.KVPair) |
| 51 | testKvPairCache.Range(func(key, value interface{}) bool { |
| 52 | if strings.Contains(key.(string), prefix) { |
| 53 | kvPair := new(kvstore.KVPair) |
| 54 | kvPair.Key = key.(string) |
| 55 | kvPair.Value = value.([]byte) |
| 56 | kvPairMap[kvPair.Key] = kvPair |
| 57 | } |
| 58 | return true |
| 59 | }) |
| 60 | |
| 61 | if len(kvPairMap) != 0 { |
| 62 | logger.Debugw(ctx, "List of MockKVClient called", log.Fields{"kvPairMap": kvPairMap}) |
| 63 | return kvPairMap, nil |
| 64 | } |
| 65 | |
| 66 | return nil, errors.New("key didn't find") |
| 67 | } |
| 68 | |
| 69 | // Get mock function implementation for KVClient |
| 70 | func (kvclient *mockKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) { |
| 71 | logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key}) |
| 72 | |
| 73 | if val, ok := testKvPairCache.Load(key); ok { |
| 74 | kvPair := new(kvstore.KVPair) |
| 75 | kvPair.Key = key |
| 76 | kvPair.Value = val |
| 77 | return kvPair, nil |
| 78 | } |
| 79 | |
| 80 | return nil, errors.New("key didn't find") |
| 81 | } |
| 82 | |
| 83 | // Put mock function implementation for KVClient |
| 84 | func (kvclient *mockKVClient) Put(ctx context.Context, key string, value interface{}) error { |
| 85 | if key != "" { |
| 86 | value = []byte(value.(string)) |
| 87 | testKvPairCache.Store(key, value) |
| 88 | return nil |
| 89 | } |
| 90 | return errors.New("key didn't find") |
| 91 | } |
| 92 | |
| 93 | // Delete mock function implementation for KVClient |
| 94 | func (kvclient *mockKVClient) Delete(ctx context.Context, key string) error { |
| 95 | logger.Infow(ctx, "Error Error Error Key:", log.Fields{}) |
| 96 | if key != "" { |
| 97 | testKvPairCache.Delete(key) |
| 98 | return nil |
| 99 | } |
| 100 | return errors.New("key didn't find") |
| 101 | } |
| 102 | |
| 103 | // DeleteWithPrefix mock function implementation for KVClient |
| 104 | func (kvclient *mockKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error { |
| 105 | testKvPairCache.Range(func(key, value interface{}) bool { |
| 106 | if strings.Contains(key.(string), prefix) { |
| 107 | testKvPairCache.Delete(key) |
| 108 | } |
| 109 | return true |
| 110 | }) |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | // Reserve mock function implementation for KVClient |
| 115 | func (kvclient *mockKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) { |
| 116 | return nil, errors.New("key didn't find") |
| 117 | } |
| 118 | |
| 119 | // ReleaseReservation mock function implementation for KVClient |
| 120 | func (kvclient *mockKVClient) ReleaseReservation(ctx context.Context, key string) error { |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | // ReleaseAllReservations mock function implementation for KVClient |
| 125 | func (kvclient *mockKVClient) ReleaseAllReservations(ctx context.Context) error { |
| 126 | return nil |
| 127 | } |
| 128 | |
| 129 | // RenewReservation mock function implementation for KVClient |
| 130 | func (kvclient *mockKVClient) RenewReservation(ctx context.Context, key string) error { |
| 131 | return nil |
| 132 | } |
| 133 | |
| 134 | // Watch mock function implementation for KVClient |
| 135 | func (kvclient *mockKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event { |
| 136 | return nil |
| 137 | } |
| 138 | |
| 139 | // AcquireLock mock function implementation for KVClient |
| 140 | func (kvclient *mockKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error { |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | // ReleaseLock mock function implementation for KVClient |
| 145 | func (kvclient *mockKVClient) ReleaseLock(lockName string) error { |
| 146 | return nil |
| 147 | } |
| 148 | |
| 149 | // IsConnectionUp mock function implementation for KVClient |
| 150 | func (kvclient *mockKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second |
| 151 | return true |
| 152 | } |
| 153 | |
| 154 | // CloseWatch mock function implementation for KVClient |
| 155 | func (kvclient *mockKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) { |
| 156 | } |
| 157 | |
| 158 | // Close mock function implementation for KVClient |
| 159 | func (kvclient *mockKVClient) Close(ctx context.Context) { |
| 160 | } |