khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 19 | desc "github.com/golang/protobuf/descriptor" |
| 20 | "github.com/golang/protobuf/proto" |
| 21 | "github.com/golang/protobuf/protoc-gen-go/descriptor" |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/protos/common" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 24 | "reflect" |
| 25 | "strconv" |
| 26 | "sync" |
| 27 | ) |
| 28 | |
| 29 | type singleton struct { |
| 30 | ChildrenFieldsCache map[interface{}]map[string]*ChildType |
| 31 | } |
| 32 | |
| 33 | var instance *singleton |
| 34 | var once sync.Once |
| 35 | |
| 36 | func GetInstance() *singleton { |
| 37 | once.Do(func() { |
| 38 | instance = &singleton{} |
| 39 | }) |
| 40 | return instance |
| 41 | } |
| 42 | |
| 43 | type ChildType struct { |
| 44 | ClassModule string |
| 45 | ClassType reflect.Type |
| 46 | IsContainer bool |
| 47 | Key string |
| 48 | KeyFromStr func(s string) interface{} |
| 49 | } |
| 50 | |
| 51 | func 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 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 64 | inst := GetInstance() |
| 65 | |
| 66 | if names, names_exist = inst.ChildrenFieldsCache[msgType.String()]; !names_exist { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 67 | names = make(map[string]*ChildType) |
| 68 | |
| 69 | _, md := desc.ForMessage(cls.(desc.Message)) |
| 70 | |
| 71 | // TODO: Do we need to validate MD for nil, panic or exception? |
| 72 | for _, field := range md.Field { |
| 73 | if options := field.GetOptions(); options != nil { |
| 74 | if proto.HasExtension(options, common.E_ChildNode) { |
| 75 | isContainer := *field.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED |
| 76 | meta, _ := proto.GetExtension(options, common.E_ChildNode) |
| 77 | var keyFromStr func(string) interface{} |
| 78 | |
| 79 | if meta.(*common.ChildNode).GetKey() == "" { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 80 | //log.Debugf("Child key is empty ... moving on") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 81 | } else { |
| 82 | parentType := FindOwnerType(reflect.ValueOf(cls), field.GetName(), 0, false) |
| 83 | keyType := FindKeyOwner(reflect.New(parentType).Elem().Interface(), meta.(*common.ChildNode).GetKey(), 0) |
| 84 | |
| 85 | switch keyType.(reflect.Type).Name() { |
| 86 | case "string": |
| 87 | keyFromStr = func(s string) interface{} { |
| 88 | return s |
| 89 | } |
| 90 | case "int32": |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 91 | keyFromStr = func(s string) interface{} { |
| 92 | i, _ := strconv.Atoi(s) |
| 93 | return int32(i) |
| 94 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 95 | case "int64": |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 96 | keyFromStr = func(s string) interface{} { |
| 97 | i, _ := strconv.Atoi(s) |
| 98 | return int64(i) |
| 99 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 100 | case "uint32": |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 101 | keyFromStr = func(s string) interface{} { |
| 102 | i, _ := strconv.Atoi(s) |
| 103 | return uint32(i) |
| 104 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 105 | case "uint64": |
| 106 | keyFromStr = func(s string) interface{} { |
| 107 | i, _ := strconv.Atoi(s) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 108 | return uint64(i) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 109 | } |
| 110 | default: |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 111 | log.Errorf("Key type not implemented - type: %s\n", keyType.(reflect.Type)) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | ct := ChildType{ |
| 115 | ClassModule: parentType.String(), |
| 116 | ClassType: parentType, |
| 117 | IsContainer: isContainer, |
| 118 | Key: meta.(*common.ChildNode).GetKey(), |
| 119 | KeyFromStr: keyFromStr, |
| 120 | } |
| 121 | |
| 122 | names[field.GetName()] = &ct |
| 123 | |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | GetInstance().ChildrenFieldsCache[msgType.String()] = names |
| 130 | } |
| 131 | |
| 132 | return names |
| 133 | } |