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