blob: a7cbf2bdb7bf9692fad9f19f2d881e02895a25ff [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 */
khenaidoocfee5f42018-07-19 22:47:38 -040016package kvstore
17
khenaidoob9203542018-09-17 22:56:37 -040018import (
19 "github.com/opencord/voltha-go/common/log"
20)
21
khenaidoocfee5f42018-07-19 22:47:38 -040022const (
23 // Default timeout in seconds when making a kvstore request
24 defaultKVGetTimeout = 5
25 // Maximum channel buffer between publisher/subscriber goroutines
26 maxClientChannelBufferSize = 10
27)
28
29// These constants represent the event types returned by the KV client
30const (
31 PUT = iota
32 DELETE
33 CONNECTIONDOWN
34 UNKNOWN
35)
36
37// KVPair is a common wrapper for key-value pairs returned from the KV store
38type KVPair struct {
39 Key string
40 Value interface{}
41 Session string
42 Lease int64
43}
44
khenaidoob9203542018-09-17 22:56:37 -040045func init() {
46 log.AddPackage(log.JSON, log.WarnLevel, nil)
47}
48
khenaidoocfee5f42018-07-19 22:47:38 -040049// NewKVPair creates a new KVPair object
50func NewKVPair(key string, value interface{}, session string, lease int64) *KVPair {
51 kv := new(KVPair)
52 kv.Key = key
53 kv.Value = value
54 kv.Session = session
55 kv.Lease = lease
56 return kv
57}
58
59// Event is generated by the KV client when a key change is detected
60type Event struct {
61 EventType int
62 Key interface{}
63 Value interface{}
64}
65
66// NewEvent creates a new Event object
67func NewEvent(eventType int, key interface{}, value interface{}) *Event {
68 evnt := new(Event)
69 evnt.EventType = eventType
70 evnt.Key = key
71 evnt.Value = value
72
73 return evnt
74}
75
76// Client represents the set of APIs a KV Client must implement
77type Client interface {
78 List(key string, timeout int) (map[string]*KVPair, error)
79 Get(key string, timeout int) (*KVPair, error)
80 Put(key string, value interface{}, timeout int) error
81 Delete(key string, timeout int) error
82 Reserve(key string, value interface{}, ttl int64) (interface{}, error)
83 ReleaseReservation(key string) error
84 ReleaseAllReservations() error
85 RenewReservation(key string) error
86 Watch(key string) chan *Event
87 CloseWatch(key string, ch chan *Event)
88 Close()
89}