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 | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 20 | desc "github.com/golang/protobuf/descriptor" |
| 21 | "github.com/golang/protobuf/proto" |
| 22 | "github.com/golang/protobuf/protoc-gen-go/descriptor" |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/common/log" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/go/common" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | "reflect" |
| 26 | "strconv" |
| 27 | "sync" |
| 28 | ) |
| 29 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 30 | type singletonChildTypeCache struct { |
| 31 | Cache map[interface{}]map[string]*ChildType |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 34 | var instanceChildTypeCache *singletonChildTypeCache |
| 35 | var onceChildTypeCache sync.Once |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 36 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 37 | func getChildTypeCache() *singletonChildTypeCache { |
| 38 | onceChildTypeCache.Do(func() { |
| 39 | instanceChildTypeCache = &singletonChildTypeCache{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 40 | }) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 41 | return instanceChildTypeCache |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 44 | // ChildType structure contains construct details of an object |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 45 | type ChildType struct { |
| 46 | ClassModule string |
| 47 | ClassType reflect.Type |
| 48 | IsContainer bool |
| 49 | Key string |
| 50 | KeyFromStr func(s string) interface{} |
| 51 | } |
| 52 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 53 | // ChildrenFields retrieves list of child objects associated to a given interface |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 54 | func ChildrenFields(cls interface{}) map[string]*ChildType { |
| 55 | if cls == nil { |
| 56 | return nil |
| 57 | } |
| 58 | var names map[string]*ChildType |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 59 | var namesExist bool |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 60 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 61 | if getChildTypeCache().Cache == nil { |
| 62 | getChildTypeCache().Cache = make(map[interface{}]map[string]*ChildType) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | msgType := reflect.TypeOf(cls) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 66 | inst := getChildTypeCache() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 67 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 68 | if names, namesExist = inst.Cache[msgType.String()]; !namesExist { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 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) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 79 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 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() != "" { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 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": |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 93 | keyFromStr = func(s string) interface{} { |
| 94 | i, _ := strconv.Atoi(s) |
| 95 | return int32(i) |
| 96 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 97 | case "int64": |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 98 | keyFromStr = func(s string) interface{} { |
| 99 | i, _ := strconv.Atoi(s) |
| 100 | return int64(i) |
| 101 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 102 | case "uint32": |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 103 | keyFromStr = func(s string) interface{} { |
| 104 | i, _ := strconv.Atoi(s) |
| 105 | return uint32(i) |
| 106 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 107 | case "uint64": |
| 108 | keyFromStr = func(s string) interface{} { |
| 109 | i, _ := strconv.Atoi(s) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 110 | return uint64(i) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 111 | } |
| 112 | default: |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 113 | log.Errorf("Key type not implemented - type: %s\n", keyType.(reflect.Type)) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 114 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 115 | } |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 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 |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 130 | getChildTypeCache().Cache[msgType.String()] = names |
| 131 | } else { |
| 132 | log.Debugf("Cache entry for %s: %+v", msgType.String(), inst.Cache[msgType.String()]) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return names |
| 136 | } |