blob: da6f688c7b4ae2777d0443c8cdf3cdb4900fd4c7 [file] [log] [blame]
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001/*
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 */
16
17package model
18
19import (
20 desc "github.com/golang/protobuf/descriptor"
21 "github.com/golang/protobuf/proto"
22 "github.com/golang/protobuf/protoc-gen-go/descriptor"
23 "github.com/opencord/voltha-go/common/log"
24 "github.com/opencord/voltha-protos/go/common"
25 "reflect"
26 "strconv"
27 "sync"
28)
29
30type singletonChildTypeCache struct {
31 Cache map[interface{}]map[string]*ChildType
32}
33
34var instanceChildTypeCache *singletonChildTypeCache
35var onceChildTypeCache sync.Once
36
37func getChildTypeCache() *singletonChildTypeCache {
38 onceChildTypeCache.Do(func() {
39 instanceChildTypeCache = &singletonChildTypeCache{}
40 })
41 return instanceChildTypeCache
42}
43
44// ChildType structure contains construct details of an object
45type ChildType struct {
46 ClassModule string
47 ClassType reflect.Type
48 IsContainer bool
49 Key string
50 KeyFromStr func(s string) interface{}
51}
52
53// ChildrenFields retrieves list of child objects associated to a given interface
54func ChildrenFields(cls interface{}) map[string]*ChildType {
55 if cls == nil {
56 return nil
57 }
58 var names map[string]*ChildType
59 var namesExist bool
60
61 if getChildTypeCache().Cache == nil {
62 getChildTypeCache().Cache = make(map[interface{}]map[string]*ChildType)
63 }
64
65 msgType := reflect.TypeOf(cls)
66 inst := getChildTypeCache()
67
68 if names, namesExist = inst.Cache[msgType.String()]; !namesExist {
69 names = make(map[string]*ChildType)
70
71 _, md := desc.ForMessage(cls.(desc.Message))
72
73 // TODO: Do we need to validate MD for nil, panic or exception?
74 for _, field := range md.Field {
75 if options := field.GetOptions(); options != nil {
76 if proto.HasExtension(options, common.E_ChildNode) {
77 isContainer := *field.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED
78 meta, _ := proto.GetExtension(options, common.E_ChildNode)
79
80 var keyFromStr func(string) interface{}
81 var ct ChildType
82
83 parentType := FindOwnerType(reflect.ValueOf(cls), field.GetName(), 0, false)
84 if meta.(*common.ChildNode).GetKey() != "" {
85 keyType := FindKeyOwner(reflect.New(parentType).Elem().Interface(), meta.(*common.ChildNode).GetKey(), 0)
86
87 switch keyType.(reflect.Type).Name() {
88 case "string":
89 keyFromStr = func(s string) interface{} {
90 return s
91 }
92 case "int32":
93 keyFromStr = func(s string) interface{} {
94 i, _ := strconv.Atoi(s)
95 return int32(i)
96 }
97 case "int64":
98 keyFromStr = func(s string) interface{} {
99 i, _ := strconv.Atoi(s)
100 return int64(i)
101 }
102 case "uint32":
103 keyFromStr = func(s string) interface{} {
104 i, _ := strconv.Atoi(s)
105 return uint32(i)
106 }
107 case "uint64":
108 keyFromStr = func(s string) interface{} {
109 i, _ := strconv.Atoi(s)
110 return uint64(i)
111 }
112 default:
113 log.Errorf("Key type not implemented - type: %s\n", keyType.(reflect.Type))
114 }
115 }
116
117 ct = ChildType{
118 ClassModule: parentType.String(),
119 ClassType: parentType,
120 IsContainer: isContainer,
121 Key: meta.(*common.ChildNode).GetKey(),
122 KeyFromStr: keyFromStr,
123 }
124
125 names[field.GetName()] = &ct
126 }
127 }
128 }
129
130 getChildTypeCache().Cache[msgType.String()] = names
131 } else {
132 log.Debugf("Cache entry for %s: %+v", msgType.String(), inst.Cache[msgType.String()])
133 }
134
135 return names
136}