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 | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 58 | log.Errorf("failed to create a new kv Client - %s", err.Error()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return b |
| 62 | } |
| 63 | |
| 64 | func (b *Backend) newClient(address string, timeout int) (kvstore.Client, error) { |
| 65 | switch b.StoreType { |
| 66 | case "consul": |
| 67 | return kvstore.NewConsulClient(address, timeout) |
| 68 | case "etcd": |
| 69 | return kvstore.NewEtcdClient(address, timeout) |
| 70 | } |
| 71 | return nil, errors.New("Unsupported KV store") |
| 72 | } |
| 73 | |
| 74 | func (b *Backend) makePath(key string) string { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 75 | path := fmt.Sprintf("%s/%s", b.PathPrefix, key) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 76 | return path |
| 77 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 78 | |
| 79 | // List retrieves one or more items that match the specified key |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 80 | func (b *Backend) List(key string) (map[string]*kvstore.KVPair, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 81 | b.Lock() |
| 82 | defer b.Unlock() |
| 83 | |
| 84 | formattedPath := b.makePath(key) |
| 85 | log.Debugf("List key: %s, path: %s", key, formattedPath) |
| 86 | |
| 87 | return b.Client.List(formattedPath, b.Timeout) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 88 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 89 | |
| 90 | // Get retrieves an item that matches the specified key |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 91 | func (b *Backend) Get(key string) (*kvstore.KVPair, error) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 92 | b.Lock() |
| 93 | defer b.Unlock() |
| 94 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 95 | formattedPath := b.makePath(key) |
| 96 | log.Debugf("Get key: %s, path: %s", key, formattedPath) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 97 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 98 | start := time.Now() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 99 | err, pair := b.Client.Get(formattedPath, b.Timeout) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 100 | stop := time.Now() |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 101 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 102 | GetProfiling().AddToDatabaseRetrieveTime(stop.Sub(start).Seconds()) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 103 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 104 | return err, pair |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 105 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 106 | |
| 107 | // Put stores an item value under the specifed key |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 108 | func (b *Backend) Put(key string, value interface{}) error { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 109 | b.Lock() |
| 110 | defer b.Unlock() |
| 111 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 112 | formattedPath := b.makePath(key) |
| 113 | log.Debugf("Put key: %s, value: %+v, path: %s", key, string(value.([]byte)), formattedPath) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 114 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 115 | return b.Client.Put(formattedPath, value, b.Timeout) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 116 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 117 | |
| 118 | // Delete removes an item under the specified key |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 119 | func (b *Backend) Delete(key string) error { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 120 | b.Lock() |
| 121 | defer b.Unlock() |
| 122 | |
| 123 | formattedPath := b.makePath(key) |
| 124 | log.Debugf("Delete key: %s, path: %s", key, formattedPath) |
| 125 | |
| 126 | return b.Client.Delete(formattedPath, b.Timeout) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 127 | } |