blob: cd92c0b186177d2d16a30a2c9deea658a6d832f4 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 Barbarie4a2564d2018-07-26 11:02:58 -040016package model
17
18import (
19 "errors"
20 "fmt"
khenaidoob9203542018-09-17 22:56:37 -040021 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040022 "github.com/opencord/voltha-go/db/kvstore"
23 "strconv"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040024 "time"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040025)
26
27//TODO: missing cache stuff
28//TODO: missing retry stuff
29//TODO: missing proper logging
30
31type Backend struct {
32 Client kvstore.Client
33 StoreType string
34 Host string
35 Port int
36 Timeout int
37 PathPrefix string
38}
39
40func 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 Barbarieec0919b2018-09-05 14:14:29 -040053 log.Errorf("failed to create a new kv Client - %s", err.Error())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040054 }
55
56 return b
57}
58
59func (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
69func (b *Backend) makePath(key string) string {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040070 path := fmt.Sprintf("%s/%s", b.PathPrefix, key)
71 log.Debugf("formatting path: %s", path)
72 return path
73}
74func (b *Backend) List(key string) (map[string]*kvstore.KVPair, error) {
75 return b.Client.List(b.makePath(key), b.Timeout)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040076}
77func (b *Backend) Get(key string) (*kvstore.KVPair, error) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040078 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 Barbarie4a2564d2018-07-26 11:02:58 -040083}
84func (b *Backend) Put(key string, value interface{}) error {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040085 log.Debugf("Put key: %s, value: %+v, path: %s", key, string(value.([]byte)), b.makePath(key))
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040086 return b.Client.Put(b.makePath(key), value, b.Timeout)
87}
88func (b *Backend) Delete(key string) error {
89 return b.Client.Delete(b.makePath(key), b.Timeout)
90}