khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/db/kvstore" |
| 24 | "strconv" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 25 | "sync" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 26 | "time" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | //TODO: missing cache stuff |
| 30 | //TODO: missing retry stuff |
| 31 | //TODO: missing proper logging |
| 32 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 33 | // Backend structure holds details for accessing the kv store |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 34 | type Backend struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 35 | sync.RWMutex |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 36 | Client kvstore.Client |
| 37 | StoreType string |
| 38 | Host string |
| 39 | Port int |
| 40 | Timeout int |
| 41 | PathPrefix string |
| 42 | } |
| 43 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 44 | // NewBackend creates a new instance of a Backend structure |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 45 | func NewBackend(storeType string, host string, port int, timeout int, pathPrefix string) *Backend { |
| 46 | var err error |
| 47 | |
| 48 | b := &Backend{ |
| 49 | StoreType: storeType, |
| 50 | Host: host, |
| 51 | Port: port, |
| 52 | Timeout: timeout, |
| 53 | PathPrefix: pathPrefix, |
| 54 | } |
| 55 | |
| 56 | address := host + ":" + strconv.Itoa(port) |
| 57 | if b.Client, err = b.newClient(address, timeout); err != nil { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 58 | log.Errorw("failed-to-create-kv-client", |
| 59 | log.Fields{ |
| 60 | "type": storeType, "host": host, "port": port, |
| 61 | "timeout": timeout, "prefix": pathPrefix, |
| 62 | "error": err.Error(), |
| 63 | }) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | return b |
| 67 | } |
| 68 | |
| 69 | func (b *Backend) newClient(address string, timeout int) (kvstore.Client, error) { |
| 70 | switch b.StoreType { |
| 71 | case "consul": |
| 72 | return kvstore.NewConsulClient(address, timeout) |
| 73 | case "etcd": |
| 74 | return kvstore.NewEtcdClient(address, timeout) |
| 75 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 76 | return nil, errors.New("unsupported-kv-store") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func (b *Backend) makePath(key string) string { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 80 | path := fmt.Sprintf("%s/%s", b.PathPrefix, key) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 81 | return path |
| 82 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 83 | |
| 84 | // List retrieves one or more items that match the specified key |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 85 | func (b *Backend) List(key string, lock ...bool) (map[string]*kvstore.KVPair, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 86 | b.Lock() |
| 87 | defer b.Unlock() |
| 88 | |
| 89 | formattedPath := b.makePath(key) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 90 | log.Debugw("listing-key", log.Fields{"key": key, "path": formattedPath, "lock": lock}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 91 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 92 | return b.Client.List(formattedPath, b.Timeout, lock...) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 93 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 94 | |
| 95 | // Get retrieves an item that matches the specified key |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 96 | func (b *Backend) Get(key string, lock ...bool) (*kvstore.KVPair, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 97 | b.Lock() |
| 98 | defer b.Unlock() |
| 99 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 100 | formattedPath := b.makePath(key) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 101 | log.Debugw("getting-key", log.Fields{"key": key, "path": formattedPath, "lock": lock}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 102 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 103 | start := time.Now() |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 104 | err, pair := b.Client.Get(formattedPath, b.Timeout, lock...) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 105 | stop := time.Now() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 106 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 107 | GetProfiling().AddToDatabaseRetrieveTime(stop.Sub(start).Seconds()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 108 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 109 | return err, pair |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 110 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 111 | |
| 112 | // Put stores an item value under the specifed key |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 113 | func (b *Backend) Put(key string, value interface{}, lock ...bool) error { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 114 | b.Lock() |
| 115 | defer b.Unlock() |
| 116 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 117 | formattedPath := b.makePath(key) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 118 | log.Debugw("putting-key", log.Fields{"key": key, "value": string(value.([]byte)), "path": formattedPath, "lock": lock}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 119 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 120 | return b.Client.Put(formattedPath, value, b.Timeout, lock...) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 121 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 122 | |
| 123 | // Delete removes an item under the specified key |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 124 | func (b *Backend) Delete(key string, lock ...bool) error { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 125 | b.Lock() |
| 126 | defer b.Unlock() |
| 127 | |
| 128 | formattedPath := b.makePath(key) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 129 | log.Debugw("deleting-key", log.Fields{"key": key, "path": formattedPath, "lock": lock}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 130 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 131 | return b.Client.Delete(formattedPath, b.Timeout, lock...) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 132 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 133 | |
| 134 | // CreateWatch starts watching events for the specified key |
| 135 | func (b *Backend) CreateWatch(key string) chan *kvstore.Event { |
| 136 | b.Lock() |
| 137 | defer b.Unlock() |
| 138 | |
| 139 | formattedPath := b.makePath(key) |
| 140 | log.Debugw("creating-key-watch", log.Fields{"key": key, "path": formattedPath}) |
| 141 | |
| 142 | return b.Client.Watch(formattedPath) |
| 143 | } |
| 144 | |
| 145 | // DeleteWatch stops watching events for the specified key |
| 146 | func (b *Backend) DeleteWatch(key string, ch chan *kvstore.Event) { |
| 147 | b.Lock() |
| 148 | defer b.Unlock() |
| 149 | |
| 150 | formattedPath := b.makePath(key) |
| 151 | log.Debugw("deleting-key-watch", log.Fields{"key": key, "path": formattedPath}) |
| 152 | |
| 153 | b.Client.CloseWatch(formattedPath, ch) |
| 154 | } |