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 | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 23 | "github.com/gogo/protobuf/proto" |
| 24 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 26 | "reflect" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 29 | // RequestTimestamp attribute used to store a timestamp in the context object |
| 30 | const RequestTimestamp contextKey = "request-timestamp" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 31 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 32 | type contextKey string |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 33 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 34 | // Proxy holds the information for a specific location with the data model |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 35 | type Proxy struct { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 36 | kvStore *db.Backend |
| 37 | path string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 40 | // NewProxy instantiates a new proxy to a specific location |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 41 | func NewProxy(kvStore *db.Backend, path string) *Proxy { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 42 | if path == "/" { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 43 | path = "" |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 44 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 45 | return &Proxy{ |
| 46 | kvStore: kvStore, |
| 47 | path: path, |
| 48 | } |
| 49 | } |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 50 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 51 | // List will retrieve information from the data model at the specified path location, and write it to the target slice |
| 52 | // target must be a type of the form *[]<proto.Message Type> For example: *[]*voltha.Device |
| 53 | func (p *Proxy) List(ctx context.Context, path string, target interface{}) error { |
| 54 | completePath := p.path + path |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 55 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 56 | logger.Debugw("proxy-list", log.Fields{ |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 57 | "path": completePath, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 58 | }) |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 59 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 60 | // verify type of target is *[]*<type> |
| 61 | pointerType := reflect.TypeOf(target) // *[]*<type> |
| 62 | if pointerType.Kind() != reflect.Ptr { |
| 63 | return errors.New("target is not of type *[]*<type>") |
| 64 | } |
| 65 | sliceType := pointerType.Elem() // []*type |
| 66 | if sliceType.Kind() != reflect.Slice { |
| 67 | return errors.New("target is not of type *[]*<type>") |
| 68 | } |
| 69 | elemType := sliceType.Elem() // *type |
| 70 | if sliceType.Implements(reflect.TypeOf((*proto.Message)(nil)).Elem()) { |
| 71 | return errors.New("target slice does not contain elements of type proto.Message") |
| 72 | } |
| 73 | dataType := elemType.Elem() // type |
| 74 | |
| 75 | blobs, err := p.kvStore.List(ctx, completePath) |
| 76 | if err != nil { |
| 77 | return fmt.Errorf("failed to retrieve %s from kvstore: %s", path, err) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 80 | logger.Debugw("parsing-data-blobs", log.Fields{ |
| 81 | "path": path, |
| 82 | "size": len(blobs), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 83 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 84 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 85 | ret := reflect.MakeSlice(sliceType, len(blobs), len(blobs)) |
| 86 | i := 0 |
| 87 | for _, blob := range blobs { |
| 88 | data := reflect.New(dataType) |
| 89 | if err := proto.Unmarshal(blob.Value.([]byte), data.Interface().(proto.Message)); err != nil { |
| 90 | return fmt.Errorf("failed to unmarshal %s: %s", blob.Key, err) |
| 91 | } |
| 92 | ret.Index(i).Set(data) |
| 93 | i++ |
| 94 | } |
| 95 | reflect.ValueOf(target).Elem().Set(ret) |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | // Get will retrieve information from the data model at the specified path location, and write it to target |
| 100 | func (p *Proxy) Get(ctx context.Context, path string, target proto.Message) (bool, error) { |
| 101 | completePath := p.path + path |
| 102 | |
| 103 | logger.Debugw("proxy-get", log.Fields{ |
| 104 | "path": completePath, |
| 105 | }) |
| 106 | |
| 107 | blob, err := p.kvStore.Get(ctx, completePath) |
| 108 | if err != nil { |
| 109 | return false, fmt.Errorf("failed to retrieve %s from kvstore: %s", path, err) |
| 110 | } else if blob == nil { |
| 111 | return false, nil // this blob does not exist |
| 112 | } |
| 113 | |
| 114 | logger.Debugw("parsing-data-blobs", log.Fields{ |
| 115 | "path": path, |
| 116 | }) |
| 117 | |
| 118 | if err := proto.Unmarshal(blob.Value.([]byte), target); err != nil { |
| 119 | return false, fmt.Errorf("failed to unmarshal %s: %s", blob.Key, err) |
| 120 | } |
| 121 | return true, nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 124 | // Update will modify information in the data model at the specified location with the provided data |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 125 | func (p *Proxy) Update(ctx context.Context, path string, data proto.Message) error { |
| 126 | return p.add(ctx, path, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 129 | // AddWithID will insert new data at specified location. |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 130 | // This method also allows the user to specify the ID. |
| 131 | func (p *Proxy) AddWithID(ctx context.Context, path string, id string, data proto.Message) error { |
| 132 | return p.add(ctx, path+"/"+id, data) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 135 | // add will insert new data at specified location. |
| 136 | func (p *Proxy) add(ctx context.Context, path string, data proto.Message) error { |
| 137 | completePath := p.path + path |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 138 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 139 | logger.Debugw("proxy-add", log.Fields{ |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 140 | "path": completePath, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 141 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 142 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 143 | blob, err := proto.Marshal(data) |
| 144 | if err != nil { |
| 145 | return fmt.Errorf("unable to save to kvStore, error marshalling: %s", err) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 146 | } |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 147 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 148 | if err := p.kvStore.Put(ctx, completePath, blob); err != nil { |
| 149 | return fmt.Errorf("unable to write to kvStore: %s", err) |
| 150 | } |
| 151 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 154 | // Remove will delete an entry at the specified location |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 155 | func (p *Proxy) Remove(ctx context.Context, path string) error { |
| 156 | completePath := p.path + path |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 157 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 158 | logger.Debugw("proxy-remove", log.Fields{ |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 159 | "path": completePath, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 160 | }) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 161 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 162 | if err := p.kvStore.Delete(ctx, completePath); err != nil { |
| 163 | return fmt.Errorf("unable to delete %s in kvStore: %s", completePath, err) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 164 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame^] | 165 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 166 | } |