blob: 1c237832e16ce60f13a487dd292e3423e580fe1a [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 Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
19import (
Stephane Barbarieef6650d2019-07-18 12:15:09 -040020 "context"
Stephane Barbarie694e2b92018-09-07 12:17:36 -040021 "errors"
khenaidoob9203542018-09-17 22:56:37 -040022 "fmt"
Kent Hagermanf5a67352020-04-30 15:15:26 -040023 "reflect"
24 "strings"
25
Kent Hagerman4f355f52020-03-30 16:01:33 -040026 "github.com/gogo/protobuf/proto"
khenaidood948f772021-08-11 17:49:24 -040027 "github.com/opencord/voltha-lib-go/v7/pkg/db"
28 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040029)
30
Kent Hagerman4f355f52020-03-30 16:01:33 -040031// RequestTimestamp attribute used to store a timestamp in the context object
32const RequestTimestamp contextKey = "request-timestamp"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040033
Kent Hagerman4f355f52020-03-30 16:01:33 -040034type contextKey string
Stephane Barbarieec0919b2018-09-05 14:14:29 -040035
Kent Hagermanf5a67352020-04-30 15:15:26 -040036// Path holds the information for a specific location within the data model
37type Path struct {
Kent Hagerman4f355f52020-03-30 16:01:33 -040038 kvStore *db.Backend
39 path string
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040040}
41
Kent Hagermanf5a67352020-04-30 15:15:26 -040042// NewDBPath returns a path to the default db location
43func NewDBPath(kvStore *db.Backend) *Path {
44 return &Path{kvStore: kvStore}
45}
46
47// SubPath returns a path which points to a more specific db location
48func (p *Path) SubPath(path string) *Path {
49 path = strings.TrimRight(strings.TrimLeft(path, "/"), "/")
50 return &Path{
51 kvStore: p.kvStore,
52 path: p.path + path + "/",
Kent Hagerman4f355f52020-03-30 16:01:33 -040053 }
54}
Stephane Barbarieaa467942019-02-06 14:09:44 -050055
Kent Hagermanf5a67352020-04-30 15:15:26 -040056// Proxy contains all the information needed to reference a specific resource within the kv
57type Proxy Path
Stephane Barbarieef6650d2019-07-18 12:15:09 -040058
Kent Hagermanf5a67352020-04-30 15:15:26 -040059// Proxy returns a new proxy which references the specified resource
60func (p *Path) Proxy(resource string) *Proxy {
61 resource = strings.TrimRight(strings.TrimLeft(resource, "/"), "/")
62 return &Proxy{
63 kvStore: p.kvStore,
64 path: p.path + resource + "/",
65 }
66}
67
68// List will retrieve information from the data model at the proxy's path location, and write it to the target slice
69// target must be a type of the form *[]<proto.Message Type> For example: *[]*voltha.Device
70func (p *Proxy) List(ctx context.Context, target interface{}) error {
Rohan Agrawal31f21802020-06-12 05:38:46 +000071 logger.Debugw(ctx, "proxy-list", log.Fields{
Kent Hagermanf5a67352020-04-30 15:15:26 -040072 "path": p.path,
Stephane Barbarie7512fc82019-05-07 12:25:46 -040073 })
Stephane Barbarieaa467942019-02-06 14:09:44 -050074
Kent Hagerman4f355f52020-03-30 16:01:33 -040075 // verify type of target is *[]*<type>
76 pointerType := reflect.TypeOf(target) // *[]*<type>
77 if pointerType.Kind() != reflect.Ptr {
78 return errors.New("target is not of type *[]*<type>")
79 }
80 sliceType := pointerType.Elem() // []*type
81 if sliceType.Kind() != reflect.Slice {
82 return errors.New("target is not of type *[]*<type>")
83 }
84 elemType := sliceType.Elem() // *type
85 if sliceType.Implements(reflect.TypeOf((*proto.Message)(nil)).Elem()) {
86 return errors.New("target slice does not contain elements of type proto.Message")
87 }
88 dataType := elemType.Elem() // type
89
Kent Hagermanf5a67352020-04-30 15:15:26 -040090 blobs, err := p.kvStore.List(ctx, p.path)
Kent Hagerman4f355f52020-03-30 16:01:33 -040091 if err != nil {
Kent Hagermanf5a67352020-04-30 15:15:26 -040092 return fmt.Errorf("failed to retrieve %s from kvstore: %s", p.path, err)
Stephane Barbariea188d942018-10-16 16:43:04 -040093 }
94
Rohan Agrawal31f21802020-06-12 05:38:46 +000095 logger.Debugw(ctx, "parsing-data-blobs", log.Fields{
Kent Hagermanf5a67352020-04-30 15:15:26 -040096 "path": p.path,
Kent Hagerman4f355f52020-03-30 16:01:33 -040097 "size": len(blobs),
Stephane Barbarie7512fc82019-05-07 12:25:46 -040098 })
Stephane Barbariea188d942018-10-16 16:43:04 -040099
Kent Hagerman4f355f52020-03-30 16:01:33 -0400100 ret := reflect.MakeSlice(sliceType, len(blobs), len(blobs))
101 i := 0
102 for _, blob := range blobs {
103 data := reflect.New(dataType)
104 if err := proto.Unmarshal(blob.Value.([]byte), data.Interface().(proto.Message)); err != nil {
105 return fmt.Errorf("failed to unmarshal %s: %s", blob.Key, err)
106 }
107 ret.Index(i).Set(data)
108 i++
109 }
110 reflect.ValueOf(target).Elem().Set(ret)
111 return nil
112}
113
Kent Hagermanf5a67352020-04-30 15:15:26 -0400114// Get will retrieve information from the data model at the proxy's path location, and write it to target
115func (p *Proxy) Get(ctx context.Context, id string, target proto.Message) (bool, error) {
116 completePath := p.path + id
Kent Hagerman4f355f52020-03-30 16:01:33 -0400117
Rohan Agrawal31f21802020-06-12 05:38:46 +0000118 logger.Debugw(ctx, "proxy-get", log.Fields{
Kent Hagerman4f355f52020-03-30 16:01:33 -0400119 "path": completePath,
120 })
121
122 blob, err := p.kvStore.Get(ctx, completePath)
123 if err != nil {
Kent Hagermanf5a67352020-04-30 15:15:26 -0400124 return false, fmt.Errorf("failed to retrieve %s from kvstore: %s", completePath, err)
Kent Hagerman4f355f52020-03-30 16:01:33 -0400125 } else if blob == nil {
126 return false, nil // this blob does not exist
127 }
128
Rohan Agrawal31f21802020-06-12 05:38:46 +0000129 logger.Debugw(ctx, "parsing-data-blobs", log.Fields{
Kent Hagermanf5a67352020-04-30 15:15:26 -0400130 "path": completePath,
Kent Hagerman4f355f52020-03-30 16:01:33 -0400131 })
132
133 if err := proto.Unmarshal(blob.Value.([]byte), target); err != nil {
134 return false, fmt.Errorf("failed to unmarshal %s: %s", blob.Key, err)
135 }
136 return true, nil
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400137}
138
Kent Hagermanf5a67352020-04-30 15:15:26 -0400139// Set will add new or update existing entry at the proxy's path location
140func (p *Proxy) Set(ctx context.Context, id string, data proto.Message) error {
141 completePath := p.path + id
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400142
Rohan Agrawal31f21802020-06-12 05:38:46 +0000143 logger.Debugw(ctx, "proxy-add", log.Fields{
Kent Hagerman4f355f52020-03-30 16:01:33 -0400144 "path": completePath,
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400145 })
Stephane Barbariea188d942018-10-16 16:43:04 -0400146
Kent Hagerman4f355f52020-03-30 16:01:33 -0400147 blob, err := proto.Marshal(data)
148 if err != nil {
149 return fmt.Errorf("unable to save to kvStore, error marshalling: %s", err)
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400150 }
Stephane Barbariec53a2752019-03-08 17:50:10 -0500151
Kent Hagerman4f355f52020-03-30 16:01:33 -0400152 if err := p.kvStore.Put(ctx, completePath, blob); err != nil {
153 return fmt.Errorf("unable to write to kvStore: %s", err)
154 }
155 return nil
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400156}
157
Kent Hagermanf5a67352020-04-30 15:15:26 -0400158// Remove will delete an entry at the proxy's path location
159func (p *Proxy) Remove(ctx context.Context, id string) error {
160 completePath := p.path + id
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400161
Rohan Agrawal31f21802020-06-12 05:38:46 +0000162 logger.Debugw(ctx, "proxy-remove", log.Fields{
Kent Hagerman4f355f52020-03-30 16:01:33 -0400163 "path": completePath,
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400164 })
Stephane Barbariea188d942018-10-16 16:43:04 -0400165
Kent Hagerman4f355f52020-03-30 16:01:33 -0400166 if err := p.kvStore.Delete(ctx, completePath); err != nil {
167 return fmt.Errorf("unable to delete %s in kvStore: %s", completePath, err)
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400168 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400169 return nil
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400170}