blob: 0020e7d6d7bf2114c2876a2beb29e2ecbc9ef38d [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 Barbarie4a2564d2018-07-26 11:02:58 -040016package model
17
18import (
19 "fmt"
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/protos/go/common"
24 "reflect"
25 "strconv"
26 "sync"
27)
28
29type singleton struct {
30 ChildrenFieldsCache map[interface{}]map[string]*ChildType
31}
32
33var instance *singleton
34var once sync.Once
35
36func GetInstance() *singleton {
37 once.Do(func() {
38 instance = &singleton{}
39 })
40 return instance
41}
42
43type ChildType struct {
44 ClassModule string
45 ClassType reflect.Type
46 IsContainer bool
47 Key string
48 KeyFromStr func(s string) interface{}
49}
50
51func ChildrenFields(cls interface{}) map[string]*ChildType {
52 if cls == nil {
53 return nil
54 }
55 var names map[string]*ChildType
56 var names_exist bool
57
58 if GetInstance().ChildrenFieldsCache == nil {
59 GetInstance().ChildrenFieldsCache = make(map[interface{}]map[string]*ChildType)
60 }
61
62 msgType := reflect.TypeOf(cls)
63
64 if names, names_exist = GetInstance().ChildrenFieldsCache[msgType.String()]; !names_exist {
65 names = make(map[string]*ChildType)
66
67 _, md := desc.ForMessage(cls.(desc.Message))
68
69 // TODO: Do we need to validate MD for nil, panic or exception?
70 for _, field := range md.Field {
71 if options := field.GetOptions(); options != nil {
72 if proto.HasExtension(options, common.E_ChildNode) {
73 isContainer := *field.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED
74 meta, _ := proto.GetExtension(options, common.E_ChildNode)
75 var keyFromStr func(string) interface{}
76
77 if meta.(*common.ChildNode).GetKey() == "" {
78 fmt.Println("Child key is empty ... moving on")
79 } else {
80 parentType := FindOwnerType(reflect.ValueOf(cls), field.GetName(), 0, false)
81 keyType := FindKeyOwner(reflect.New(parentType).Elem().Interface(), meta.(*common.ChildNode).GetKey(), 0)
82
83 switch keyType.(reflect.Type).Name() {
84 case "string":
85 keyFromStr = func(s string) interface{} {
86 return s
87 }
88 case "int32":
89 fallthrough
90 case "int64":
91 fallthrough
92 case "uint32":
93 fallthrough
94 case "uint64":
95 keyFromStr = func(s string) interface{} {
96 i, _ := strconv.Atoi(s)
97 return i
98 }
99 default:
100 fmt.Errorf("Key type not implemented - type: %s\n", keyType.(reflect.Type))
101 }
102
103 ct := ChildType{
104 ClassModule: parentType.String(),
105 ClassType: parentType,
106 IsContainer: isContainer,
107 Key: meta.(*common.ChildNode).GetKey(),
108 KeyFromStr: keyFromStr,
109 }
110
111 names[field.GetName()] = &ct
112
113 }
114 }
115 }
116 }
117
118 GetInstance().ChildrenFieldsCache[msgType.String()] = names
119 }
120
121 return names
122}