Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | */ |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 16 | |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 20 | "context" |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 21 | "strings" |
| 22 | "sync" |
| 23 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/db/model" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 26 | "google.golang.org/grpc/codes" |
| 27 | "google.golang.org/grpc/status" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // ModelProxy controls requests made to the data model |
| 31 | type ModelProxy struct { |
| 32 | rootProxy *model.Proxy |
| 33 | basePath string |
| 34 | mutex sync.RWMutex |
| 35 | } |
| 36 | |
| 37 | func newModelProxy(basePath string, rootProxy *model.Proxy) *ModelProxy { |
| 38 | ga := &ModelProxy{} |
| 39 | ga.rootProxy = rootProxy |
| 40 | |
| 41 | if strings.HasPrefix(basePath, "/") { |
| 42 | ga.basePath = basePath |
| 43 | } else { |
| 44 | ga.basePath = "/" + basePath |
| 45 | } |
| 46 | |
| 47 | return ga |
| 48 | } |
| 49 | |
| 50 | // Get retrieves information at the provided path |
| 51 | func (mp *ModelProxy) Get(parts ...string) (interface{}, error) { |
| 52 | mp.mutex.Lock() |
| 53 | defer mp.mutex.Unlock() |
| 54 | |
| 55 | rawPath := []string{mp.basePath} |
| 56 | rawPath = append(rawPath, parts...) |
| 57 | path := strings.Join(rawPath, "/") |
| 58 | |
| 59 | log.Debugw("get-data", log.Fields{"path": path}) |
| 60 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 61 | if data, err := mp.rootProxy.Get(context.Background(), path, 1, false, ""); err != nil { |
| 62 | log.Errorw("failed-to-retrieve-data-from-model-proxy", log.Fields{"error": err}) |
| 63 | return nil, err |
| 64 | } else if data != nil { |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 65 | return data, nil |
| 66 | } |
| 67 | return nil, status.Errorf(codes.NotFound, "data-path: %s", path) |
| 68 | } |