| khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| Joey Armstrong | 7a9af44 | 2024-01-03 19:26:36 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
| khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 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 | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
| Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
| Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 20 | "context" |
| Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 21 | "errors" |
| khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | "fmt" |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 23 | "reflect" |
| 24 | "strings" |
| 25 | |
| khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v7/pkg/db" |
| 27 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| bseeniva | b18867a | 2026-02-12 19:15:25 +0530 | [diff] [blame^] | 28 | "google.golang.org/protobuf/proto" |
| Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 29 | ) |
| 30 | |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 31 | // RequestTimestamp attribute used to store a timestamp in the context object |
| 32 | const RequestTimestamp contextKey = "request-timestamp" |
| Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 33 | |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 34 | type contextKey string |
| Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 35 | |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 36 | // Path holds the information for a specific location within the data model |
| 37 | type Path struct { |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 38 | kvStore *db.Backend |
| 39 | path string |
| Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 42 | // NewDBPath returns a path to the default db location |
| 43 | func 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 |
| 48 | func (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 Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 53 | } |
| 54 | } |
| Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 55 | |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 56 | // Proxy contains all the information needed to reference a specific resource within the kv |
| 57 | type Proxy Path |
| Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 58 | |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 59 | // Proxy returns a new proxy which references the specified resource |
| 60 | func (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 |
| akashreddyk | c8a6401 | 2026-02-06 17:05:44 +0530 | [diff] [blame] | 70 | func (p *Proxy) KeyExists(ctx context.Context, id string) (bool, error) { |
| 71 | completePath := p.path + id |
| 72 | |
| 73 | logger.Debugw(ctx, "proxy-key-exists", log.Fields{ |
| 74 | "path": completePath, |
| 75 | }) |
| 76 | |
| 77 | keyExists, err := p.kvStore.KeyExists(ctx, completePath) |
| 78 | if err != nil { |
| 79 | return false, fmt.Errorf("failed to retrieve %s from kvstore: %s", p.path, err) |
| 80 | } |
| 81 | return keyExists, nil |
| 82 | } |
| 83 | |
| 84 | // List will retrieve information from the data model at the proxy's path location, and write it to the target slice |
| 85 | // target must be a type of the form *[]<proto.Message Type> For example: *[]*voltha.Device |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 86 | func (p *Proxy) List(ctx context.Context, target interface{}) error { |
| Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 87 | logger.Debugw(ctx, "proxy-list", log.Fields{ |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 88 | "path": p.path, |
| Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 89 | }) |
| Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 90 | |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 91 | // verify type of target is *[]*<type> |
| 92 | pointerType := reflect.TypeOf(target) // *[]*<type> |
| 93 | if pointerType.Kind() != reflect.Ptr { |
| 94 | return errors.New("target is not of type *[]*<type>") |
| 95 | } |
| 96 | sliceType := pointerType.Elem() // []*type |
| 97 | if sliceType.Kind() != reflect.Slice { |
| 98 | return errors.New("target is not of type *[]*<type>") |
| 99 | } |
| 100 | elemType := sliceType.Elem() // *type |
| 101 | if sliceType.Implements(reflect.TypeOf((*proto.Message)(nil)).Elem()) { |
| 102 | return errors.New("target slice does not contain elements of type proto.Message") |
| 103 | } |
| 104 | dataType := elemType.Elem() // type |
| 105 | |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 106 | blobs, err := p.kvStore.List(ctx, p.path) |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 107 | if err != nil { |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 108 | return fmt.Errorf("failed to retrieve %s from kvstore: %s", p.path, err) |
| Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 111 | logger.Debugw(ctx, "parsing-data-blobs", log.Fields{ |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 112 | "path": p.path, |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 113 | "size": len(blobs), |
| Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 114 | }) |
| Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 115 | |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 116 | ret := reflect.MakeSlice(sliceType, len(blobs), len(blobs)) |
| 117 | i := 0 |
| 118 | for _, blob := range blobs { |
| 119 | data := reflect.New(dataType) |
| 120 | if err := proto.Unmarshal(blob.Value.([]byte), data.Interface().(proto.Message)); err != nil { |
| 121 | return fmt.Errorf("failed to unmarshal %s: %s", blob.Key, err) |
| 122 | } |
| 123 | ret.Index(i).Set(data) |
| 124 | i++ |
| 125 | } |
| 126 | reflect.ValueOf(target).Elem().Set(ret) |
| 127 | return nil |
| 128 | } |
| 129 | |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 130 | // Get will retrieve information from the data model at the proxy's path location, and write it to target |
| 131 | func (p *Proxy) Get(ctx context.Context, id string, target proto.Message) (bool, error) { |
| 132 | completePath := p.path + id |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 133 | |
| Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 134 | logger.Debugw(ctx, "proxy-get", log.Fields{ |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 135 | "path": completePath, |
| 136 | }) |
| 137 | |
| 138 | blob, err := p.kvStore.Get(ctx, completePath) |
| 139 | if err != nil { |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 140 | return false, fmt.Errorf("failed to retrieve %s from kvstore: %s", completePath, err) |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 141 | } else if blob == nil { |
| 142 | return false, nil // this blob does not exist |
| 143 | } |
| 144 | |
| Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 145 | logger.Debugw(ctx, "parsing-data-blobs", log.Fields{ |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 146 | "path": completePath, |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 147 | }) |
| 148 | |
| 149 | if err := proto.Unmarshal(blob.Value.([]byte), target); err != nil { |
| 150 | return false, fmt.Errorf("failed to unmarshal %s: %s", blob.Key, err) |
| 151 | } |
| 152 | return true, nil |
| Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 155 | // Set will add new or update existing entry at the proxy's path location |
| 156 | func (p *Proxy) Set(ctx context.Context, id string, data proto.Message) error { |
| 157 | completePath := p.path + id |
| Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 158 | |
| Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 159 | logger.Debugw(ctx, "proxy-add", log.Fields{ |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 160 | "path": completePath, |
| Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 161 | }) |
| Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 162 | |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 163 | blob, err := proto.Marshal(data) |
| 164 | if err != nil { |
| Akash Reddy Kankanala | 929cc00 | 2025-04-08 15:05:21 +0530 | [diff] [blame] | 165 | return fmt.Errorf("unable to save to kvStore, error marshaling: %s", err) |
| Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 166 | } |
| Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 167 | |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 168 | if err := p.kvStore.Put(ctx, completePath, blob); err != nil { |
| 169 | return fmt.Errorf("unable to write to kvStore: %s", err) |
| 170 | } |
| 171 | return nil |
| Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 174 | // Remove will delete an entry at the proxy's path location |
| 175 | func (p *Proxy) Remove(ctx context.Context, id string) error { |
| 176 | completePath := p.path + id |
| Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 177 | |
| Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 178 | logger.Debugw(ctx, "proxy-remove", log.Fields{ |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 179 | "path": completePath, |
| Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 180 | }) |
| Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 181 | |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 182 | if err := p.kvStore.Delete(ctx, completePath); err != nil { |
| 183 | return fmt.Errorf("unable to delete %s in kvStore: %s", completePath, err) |
| Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 184 | } |
| Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 185 | return nil |
| Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 186 | } |