blob: 37755403abad589b87b10556acd7481979fb2777 [file] [log] [blame]
Stephane Barbariea75791c2019-01-24 10:58:06 -05001/*
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 */
npujar03b018e2019-11-13 15:29:36 +053016
Stephane Barbariea75791c2019-01-24 10:58:06 -050017package core
18
19import (
Stephane Barbarieef6650d2019-07-18 12:15:09 -040020 "context"
npujar03b018e2019-11-13 15:29:36 +053021 "strings"
22 "sync"
23
sbarbari17d7e222019-11-05 10:02:29 -050024 "github.com/opencord/voltha-go/db/model"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Stephane Barbariea75791c2019-01-24 10:58:06 -050026 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
Stephane Barbariea75791c2019-01-24 10:58:06 -050028)
29
30// ModelProxy controls requests made to the data model
31type ModelProxy struct {
32 rootProxy *model.Proxy
33 basePath string
34 mutex sync.RWMutex
35}
36
37func 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
51func (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 Se5a44012019-11-07 20:32:24 +053061 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 Barbariea75791c2019-01-24 10:58:06 -050065 return data, nil
66 }
67 return nil, status.Errorf(codes.NotFound, "data-path: %s", path)
68}