blob: 473e57906cea100491dd56ee81ab2a2cdae5a830 [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 */
16package core
17
18import (
Stephane Barbarieef6650d2019-07-18 12:15:09 -040019 "context"
Stephane Barbariea75791c2019-01-24 10:58:06 -050020 "github.com/opencord/voltha-go/common/log"
21 "github.com/opencord/voltha-go/db/model"
22 "google.golang.org/grpc/codes"
23 "google.golang.org/grpc/status"
24 "strings"
25 "sync"
26)
27
28// ModelProxy controls requests made to the data model
29type ModelProxy struct {
30 rootProxy *model.Proxy
31 basePath string
32 mutex sync.RWMutex
33}
34
35func newModelProxy(basePath string, rootProxy *model.Proxy) *ModelProxy {
36 ga := &ModelProxy{}
37 ga.rootProxy = rootProxy
38
39 if strings.HasPrefix(basePath, "/") {
40 ga.basePath = basePath
41 } else {
42 ga.basePath = "/" + basePath
43 }
44
45 return ga
46}
47
48// Get retrieves information at the provided path
49func (mp *ModelProxy) Get(parts ...string) (interface{}, error) {
50 mp.mutex.Lock()
51 defer mp.mutex.Unlock()
52
53 rawPath := []string{mp.basePath}
54 rawPath = append(rawPath, parts...)
55 path := strings.Join(rawPath, "/")
56
57 log.Debugw("get-data", log.Fields{"path": path})
58
Stephane Barbarieef6650d2019-07-18 12:15:09 -040059 if data := mp.rootProxy.Get(context.Background(), path, 1, false, ""); data != nil {
Stephane Barbariea75791c2019-01-24 10:58:06 -050060 return data, nil
61 }
62 return nil, status.Errorf(codes.NotFound, "data-path: %s", path)
63}