blob: 494c0efca9188373d84fab09cf07af15baec66eb [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -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 */
Stephane Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
19import (
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040020 desc "github.com/golang/protobuf/descriptor"
21 "github.com/golang/protobuf/proto"
22 "github.com/golang/protobuf/protoc-gen-go/descriptor"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080023 "github.com/opencord/voltha-protos/v3/go/common"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040024 "reflect"
25 "strconv"
26 "sync"
27)
28
Stephane Barbarieef6650d2019-07-18 12:15:09 -040029type childTypesSingleton struct {
30 mutex sync.RWMutex
Kent Hagermana71a54d2020-03-09 12:30:23 -040031 cache map[reflect.Type]map[string]*ChildType
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040032}
33
Kent Hagermana71a54d2020-03-09 12:30:23 -040034var childTypes = &childTypesSingleton{cache: make(map[reflect.Type]map[string]*ChildType)}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040035
Kent Hagermana71a54d2020-03-09 12:30:23 -040036func (s *childTypesSingleton) GetCacheEntry(key reflect.Type) (map[string]*ChildType, bool) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040037 s.mutex.RLock()
38 defer s.mutex.RUnlock()
Kent Hagermana71a54d2020-03-09 12:30:23 -040039 childTypeMap, exists := s.cache[key]
Stephane Barbarieef6650d2019-07-18 12:15:09 -040040 return childTypeMap, exists
41}
42
Kent Hagermana71a54d2020-03-09 12:30:23 -040043func (s *childTypesSingleton) SetCacheEntry(key reflect.Type, value map[string]*ChildType) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -040044 s.mutex.Lock()
45 defer s.mutex.Unlock()
Kent Hagermana71a54d2020-03-09 12:30:23 -040046 s.cache[key] = value
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040047}
48
Stephane Barbariedc5022d2018-11-19 15:21:44 -050049// ChildType structure contains construct details of an object
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040050type ChildType struct {
51 ClassModule string
52 ClassType reflect.Type
53 IsContainer bool
54 Key string
55 KeyFromStr func(s string) interface{}
56}
57
Kent Hagermana71a54d2020-03-09 12:30:23 -040058// ChildrenFields retrieves list of child objects associated to a given type
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040059func ChildrenFields(cls interface{}) map[string]*ChildType {
60 if cls == nil {
61 return nil
62 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040063
64 msgType := reflect.TypeOf(cls)
65
Kent Hagermana71a54d2020-03-09 12:30:23 -040066 if fields, have := childTypes.GetCacheEntry(msgType); have {
67 return fields
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040068 }
69
Kent Hagermana71a54d2020-03-09 12:30:23 -040070 fields := make(map[string]*ChildType)
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:
Girish Kumarf56a4682020-03-20 20:07:46 +0000113 logger.Errorf("Key type not implemented - type: %s\n", keyType.(reflect.Type))
Kent Hagermana71a54d2020-03-09 12:30:23 -0400114 }
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 fields[field.GetName()] = &ct
126 }
127 }
128 }
129
130 // If called multiple times in quick succession w/ the same message types, it is possible for different cache entries to be returned.
131 // This should not be an issue, as the cache is merely for optimization purposes.
132 childTypes.SetCacheEntry(msgType, fields)
133 return fields
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400134}