khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 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 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 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 26 | "github.com/gogo/protobuf/proto" |
| 27 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
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 |
| 70 | func (p *Proxy) List(ctx context.Context, target interface{}) error { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 71 | logger.Debugw("proxy-list", log.Fields{ |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 72 | "path": p.path, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 73 | }) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 74 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 75 | // 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 Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 90 | blobs, err := p.kvStore.List(ctx, p.path) |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 91 | if err != nil { |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 92 | 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] | 93 | } |
| 94 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 95 | logger.Debugw("parsing-data-blobs", log.Fields{ |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 96 | "path": p.path, |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 97 | "size": len(blobs), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 98 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 99 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 100 | 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 Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 114 | // Get will retrieve information from the data model at the proxy's path location, and write it to target |
| 115 | func (p *Proxy) Get(ctx context.Context, id string, target proto.Message) (bool, error) { |
| 116 | completePath := p.path + id |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 117 | |
| 118 | logger.Debugw("proxy-get", log.Fields{ |
| 119 | "path": completePath, |
| 120 | }) |
| 121 | |
| 122 | blob, err := p.kvStore.Get(ctx, completePath) |
| 123 | if err != nil { |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 124 | 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] | 125 | } else if blob == nil { |
| 126 | return false, nil // this blob does not exist |
| 127 | } |
| 128 | |
| 129 | logger.Debugw("parsing-data-blobs", log.Fields{ |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 130 | "path": completePath, |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 131 | }) |
| 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 139 | // Set will add new or update existing entry at the proxy's path location |
| 140 | func (p *Proxy) Set(ctx context.Context, id string, data proto.Message) error { |
| 141 | completePath := p.path + id |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 142 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 143 | logger.Debugw("proxy-add", log.Fields{ |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 144 | "path": completePath, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 145 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 146 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 147 | blob, err := proto.Marshal(data) |
| 148 | if err != nil { |
| 149 | return fmt.Errorf("unable to save to kvStore, error marshalling: %s", err) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 150 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 151 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 152 | 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame^] | 158 | // Remove will delete an entry at the proxy's path location |
| 159 | func (p *Proxy) Remove(ctx context.Context, id string) error { |
| 160 | completePath := p.path + id |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 161 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 162 | logger.Debugw("proxy-remove", log.Fields{ |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 163 | "path": completePath, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 164 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 165 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 166 | if err := p.kvStore.Delete(ctx, completePath); err != nil { |
| 167 | return fmt.Errorf("unable to delete %s in kvStore: %s", completePath, err) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 168 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 169 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 170 | } |