go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/client/kvdb.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"strings"
     7  
     8  	"go.ligato.io/cn-infra/v2/datasync"
     9  	"go.ligato.io/cn-infra/v2/db/keyval"
    10  	"go.ligato.io/cn-infra/v2/db/keyval/kvproto"
    11  	"go.ligato.io/cn-infra/v2/logging"
    12  	"go.ligato.io/cn-infra/v2/servicelabel"
    13  )
    14  
    15  // KVDBClient provides client access to the KVDB server.
    16  type KVDBClient struct {
    17  	keyval.CoreBrokerWatcher
    18  	serviceLabel string
    19  }
    20  
    21  func NewKVDBClient(kvdb keyval.CoreBrokerWatcher, serviceLabel string) *KVDBClient {
    22  	return &KVDBClient{
    23  		CoreBrokerWatcher: kvdb,
    24  		serviceLabel:      serviceLabel,
    25  	}
    26  }
    27  
    28  // ProtoBroker returns ProtoWrapper with JSON serializer for KVDB connection.
    29  func (k *KVDBClient) ProtoBroker() keyval.ProtoBroker {
    30  	return kvproto.NewProtoWrapper(k.CoreBrokerWatcher, &keyval.SerializerJSON{})
    31  }
    32  
    33  func (k *KVDBClient) Put(key string, data []byte, opts ...datasync.PutOption) (err error) {
    34  	key, err = k.CompleteFullKey(key)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	logging.Debugf("KVDBClient.Put: %s", key)
    39  
    40  	return k.CoreBrokerWatcher.Put(key, data, opts...)
    41  }
    42  
    43  func (k *KVDBClient) GetValue(key string) (data []byte, found bool, revision int64, err error) {
    44  	key, err = k.CompleteFullKey(key)
    45  	if err != nil {
    46  		return nil, false, 0, err
    47  	}
    48  	logging.Debugf("KVDBClient.GetValue: %s", key)
    49  
    50  	return k.CoreBrokerWatcher.GetValue(key)
    51  }
    52  
    53  func (k *KVDBClient) ListValues(prefix string) (keyval.BytesKeyValIterator, error) {
    54  	prefix = ensureAllAgentsPrefix(prefix)
    55  	logging.Debugf("KVDBClient.ListValues: %s", prefix)
    56  
    57  	return k.CoreBrokerWatcher.ListValues(prefix)
    58  }
    59  
    60  func (k *KVDBClient) ListKeys(prefix string) (keyval.BytesKeyIterator, error) {
    61  	prefix = ensureAllAgentsPrefix(prefix)
    62  	logging.Debugf("KVDBClient.ListKeys: %s", prefix)
    63  
    64  	return k.CoreBrokerWatcher.ListKeys(prefix)
    65  }
    66  
    67  func (k *KVDBClient) Delete(key string, opts ...datasync.DelOption) (existed bool, err error) {
    68  	key, err = k.CompleteFullKey(key)
    69  	if err != nil {
    70  		return false, err
    71  	}
    72  	logging.Debugf("KVDBClient.Delete: %s", key)
    73  
    74  	return k.CoreBrokerWatcher.Delete(key, opts...)
    75  }
    76  
    77  func (k *KVDBClient) CompleteFullKey(key string) (string, error) {
    78  	if strings.HasPrefix(key, servicelabel.GetAllAgentsPrefix()) {
    79  		return key, nil
    80  	}
    81  	if k.serviceLabel == "" {
    82  		return "", fmt.Errorf("service label is not defined, cannot get complete key")
    83  	}
    84  	key = path.Join(servicelabel.GetAllAgentsPrefix(), k.serviceLabel, key)
    85  	return key, nil
    86  }
    87  
    88  func ensureAllAgentsPrefix(key string) string {
    89  	if strings.HasPrefix(key, servicelabel.GetAllAgentsPrefix()) {
    90  		return key
    91  	}
    92  	return path.Join(servicelabel.GetAllAgentsPrefix(), key)
    93  }