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 | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "errors" |
| 20 | "fmt" |
| 21 | "github.com/opencord/voltha-go/db/kvstore" |
| 22 | "strconv" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 23 | "time" |
| 24 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | //TODO: missing cache stuff |
| 28 | //TODO: missing retry stuff |
| 29 | //TODO: missing proper logging |
| 30 | |
| 31 | type Backend struct { |
| 32 | Client kvstore.Client |
| 33 | StoreType string |
| 34 | Host string |
| 35 | Port int |
| 36 | Timeout int |
| 37 | PathPrefix string |
| 38 | } |
| 39 | |
| 40 | func NewBackend(storeType string, host string, port int, timeout int, pathPrefix string) *Backend { |
| 41 | var err error |
| 42 | |
| 43 | b := &Backend{ |
| 44 | StoreType: storeType, |
| 45 | Host: host, |
| 46 | Port: port, |
| 47 | Timeout: timeout, |
| 48 | PathPrefix: pathPrefix, |
| 49 | } |
| 50 | |
| 51 | address := host + ":" + strconv.Itoa(port) |
| 52 | if b.Client, err = b.newClient(address, timeout); err != nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 53 | log.Errorf("failed to create a new kv Client - %s", err.Error()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return b |
| 57 | } |
| 58 | |
| 59 | func (b *Backend) newClient(address string, timeout int) (kvstore.Client, error) { |
| 60 | switch b.StoreType { |
| 61 | case "consul": |
| 62 | return kvstore.NewConsulClient(address, timeout) |
| 63 | case "etcd": |
| 64 | return kvstore.NewEtcdClient(address, timeout) |
| 65 | } |
| 66 | return nil, errors.New("Unsupported KV store") |
| 67 | } |
| 68 | |
| 69 | func (b *Backend) makePath(key string) string { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 70 | path := fmt.Sprintf("%s/%s", b.PathPrefix, key) |
| 71 | log.Debugf("formatting path: %s", path) |
| 72 | return path |
| 73 | } |
| 74 | func (b *Backend) List(key string) (map[string]*kvstore.KVPair, error) { |
| 75 | return b.Client.List(b.makePath(key), b.Timeout) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 76 | } |
| 77 | func (b *Backend) Get(key string) (*kvstore.KVPair, error) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 78 | start := time.Now() |
| 79 | err, pair := b.Client.Get(b.makePath(key), b.Timeout) |
| 80 | stop := time.Now() |
| 81 | GetProfiling().AddToDatabaseRetrieveTime(stop.Sub(start).Seconds()) |
| 82 | return err, pair |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 83 | } |
| 84 | func (b *Backend) Put(key string, value interface{}) error { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 85 | log.Debugf("Put key: %s, value: %+v, path: %s", key, string(value.([]byte)), b.makePath(key)) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 86 | return b.Client.Put(b.makePath(key), value, b.Timeout) |
| 87 | } |
| 88 | func (b *Backend) Delete(key string) error { |
| 89 | return b.Client.Delete(b.makePath(key), b.Timeout) |
| 90 | } |