JavaIdentifierNormalizer ThreadSafe/Memory leak fix
[mdsal.git] / binding2 / mdsal-binding2-generator-api / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / context / ModuleContext.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.javav2.generator.context;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ArrayListMultimap;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.HashBiMap;
14 import com.google.common.collect.HashMultimap;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ListMultimap;
17 import com.google.common.collect.Maps;
18 import com.google.common.collect.Multimap;
19 import com.google.common.collect.Multimaps;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
29 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
30 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
31 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
34 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
37 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40
41 /**
42  * This class holds information about generated entities in context of YANG module
43  */
44 @Beta
45 public final class ModuleContext {
46     private GeneratedTypeBuilder moduleNode;
47     private final Map<SchemaPath,GeneratedTOBuilder> genTOs = new HashMap<>();
48     private final Map<SchemaPath, Type> typedefs = new HashMap<>();
49     private final Map<SchemaPath, GeneratedTypeBuilder> childNodes = new HashMap<>();
50     private final BiMap<String, GeneratedTypeBuilder> dataTypes = HashBiMap.create();
51     private final Map<SchemaPath, GeneratedTypeBuilder> groupings = new HashMap<>();
52     private final Map<SchemaPath, GeneratedTypeBuilder> cases = new HashMap<>();
53     private final Map<QName,GeneratedTOBuilder> identities = new HashMap<>();
54     private final Set<GeneratedTypeBuilder> topLevelNodes = new HashSet<>();
55     private final List<GeneratedTypeBuilder> augmentations = new ArrayList<>();
56     private final BiMap<Type,AugmentationSchema> typeToAugmentation = HashBiMap.create();
57     private final BiMap<SchemaPath,Type> targetToAugmentation = HashBiMap.create();
58     private final Map<Type,Object> typeToSchema = new HashMap<>();
59     private final Multimap<Type, Type> choiceToCases = HashMultimap.create();
60     private final BiMap<Type,ChoiceCaseNode> caseTypeToSchema = HashBiMap.create();
61     private final Map<SchemaPath, Type> innerTypes = new HashMap<>();
62     private final Map<SchemaPath,GeneratedTypeBuilder> keyTypes = new HashMap<>();
63     //map is getting manipulated based on unique YANG module namespace rule
64     private final ListMultimap<String, String> packagesMap = Multimaps.synchronizedListMultimap
65             (ArrayListMultimap.create());
66
67     public List<Type> getGeneratedTypes() {
68         final List<Type> result = new ArrayList<>();
69
70         if (this.moduleNode != null) {
71             result.add(this.moduleNode.toInstance());
72         }
73
74         result.addAll(this.genTOs.values().stream().map(GeneratedTOBuilder::toInstance).collect(Collectors.toList()));
75         result.addAll(this.typedefs.values().stream().filter(b -> b != null).collect(Collectors.toList()));
76         result.addAll(this.dataTypes.values().stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
77         result.addAll(this.groupings.values().stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
78         result.addAll(this.cases.values().stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
79         result.addAll(this.identities.values().stream().map(GeneratedTOBuilder::toInstance).collect(Collectors.toList()));
80         result.addAll(this.topLevelNodes.stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
81         result.addAll(this.augmentations.stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
82         result.addAll(this.keyTypes.values().stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
83         return ImmutableList.copyOf(result);
84     }
85
86     public Multimap<Type, Type> getChoiceToCases() {
87         return Multimaps.unmodifiableMultimap(this.choiceToCases);
88     }
89
90     public GeneratedTypeBuilder getModuleNode() {
91         return this.moduleNode;
92     }
93
94     public GeneratedTypeBuilder getChildNode(final SchemaPath p) {
95         return this.childNodes.get(p);
96     }
97
98     public GeneratedTypeBuilder getGrouping(final SchemaPath p) {
99         return this.groupings.get(p);
100     }
101
102     public GeneratedTypeBuilder getCase(final SchemaPath p) {
103         return this.cases.get(p);
104     }
105
106     public void addModuleNode(final GeneratedTypeBuilder moduleNode) {
107         this.moduleNode = moduleNode;
108     }
109
110     public void addGeneratedTOBuilder(final SchemaPath schemaPath, final GeneratedTOBuilder b) {
111         this.genTOs.put(schemaPath, b);
112     }
113
114     public void addChildNodeType(final SchemaNode p, final GeneratedTypeBuilder b) {
115         this.childNodes.put(p.getPath(), b);
116         this.typeToSchema.put(b,p);
117         this.dataTypes.put(b.getFullyQualifiedName(), b);
118     }
119
120     public void addGroupingType(final GroupingDefinition p, final GeneratedTypeBuilder b) {
121         this.groupings.put(p.getPath(), b);
122         this.typeToSchema.put(b, p);
123     }
124
125     public void addTypedefType(final SchemaPath p, final Type t) {
126         this.typedefs.put(p, t);
127     }
128
129     public void addCaseType(final SchemaPath p, final GeneratedTypeBuilder b) {
130         this.cases.put(p, b);
131     }
132
133     public void addIdentityType(final QName name,final GeneratedTOBuilder b) {
134         this.identities.put(name,b);
135     }
136
137     public void addTopLevelNodeType(final GeneratedTypeBuilder b) {
138         this.topLevelNodes.add(b);
139     }
140
141     public void addAugmentType(final GeneratedTypeBuilder b) {
142         this.augmentations.add(b);
143     }
144
145     public Map<SchemaPath, Type> getTypedefs() {
146         return this.typedefs;
147     }
148
149     public Map<SchemaPath, GeneratedTypeBuilder> getChildNodes() {
150         return Collections.unmodifiableMap(this.childNodes);
151     }
152
153     public Map<SchemaPath, GeneratedTypeBuilder> getGroupings() {
154         return Collections.unmodifiableMap(this.groupings);
155     }
156
157     public Map<SchemaPath, GeneratedTypeBuilder> getCases() {
158         return Collections.unmodifiableMap(this.cases);
159     }
160
161     public Map<QName,GeneratedTOBuilder> getIdentities() {
162         return Collections.unmodifiableMap(this.identities);
163     }
164
165     public Set<GeneratedTypeBuilder> getTopLevelNodes() {
166         return Collections.unmodifiableSet(this.topLevelNodes);
167     }
168
169     public List<GeneratedTypeBuilder> getAugmentations() {
170         return Collections.unmodifiableList(this.augmentations);
171     }
172
173     public BiMap<Type, AugmentationSchema> getTypeToAugmentation() {
174         return Maps.unmodifiableBiMap(this.typeToAugmentation);
175     }
176
177     public BiMap<SchemaPath, Type> getTargetToAugmentation() {
178         return Maps.unmodifiableBiMap(this.targetToAugmentation);
179     }
180
181     public void addTypeToAugmentation(final GeneratedTypeBuilder builder, final AugmentationSchema schema) {
182         this.typeToAugmentation.put(builder, schema);
183         this.typeToSchema.put(builder, schema);
184     }
185
186     public void addTargetToAugmentation(final GeneratedTypeBuilder builder, final SchemaPath augmentTarget) {
187         this.targetToAugmentation.put(augmentTarget, builder);
188     }
189
190     public void addChoiceToCaseMapping(final Type choiceType, final Type caseType, final ChoiceCaseNode schema) {
191         this.choiceToCases.put(choiceType, caseType);
192         this.caseTypeToSchema.put(caseType, schema);
193         this.typeToSchema.put(caseType, schema);
194     }
195
196     public BiMap<Type, ChoiceCaseNode> getCaseTypeToSchemas() {
197         return Maps.unmodifiableBiMap(this.caseTypeToSchema);
198     }
199
200     /**
201      *
202      * Returns mapping of type to its schema.
203      *
204      * Valid values are only instances of {@link DataSchemaNode} or {@link AugmentationSchema}
205      *
206      * @return Mapping from type to corresponding schema
207      */
208     public Map<Type, Object> getTypeToSchema() {
209         return Collections.unmodifiableMap(this.typeToSchema);
210     }
211
212     public void addTypeToSchema(final Type type, final TypeDefinition<?> typedef) {
213         this.typeToSchema.put(type, typedef);
214     }
215
216     /**
217      * Adds mapping between schema path and inner enum.
218      *
219      * @param path
220      * @param enumBuilder
221      */
222     public void addInnerTypedefType(final SchemaPath path, final EnumBuilder enumBuilder) {
223         this.innerTypes.put(path, enumBuilder);
224     }
225
226     public Type getInnerType(final SchemaPath path) {
227         return this.innerTypes.get(path);
228     }
229
230
231     public void addKeyType(final SchemaPath path, final GeneratedTypeBuilder genType) {
232         this.keyTypes.put(path, genType);
233     }
234
235     public GeneratedTypeBuilder getKeyType(final SchemaPath path) {
236         return this.keyTypes.get(path);
237     }
238
239     public GeneratedTOBuilder getKeyGenTO(final SchemaPath path) {
240         return this.genTOs.get(path);
241     }
242
243     public ListMultimap<String, String> getPackagesMap() {
244         return Multimaps.unmodifiableListMultimap(packagesMap);
245     }
246
247     public void putToPackagesMap(final String packageName, final String actualClassName) {
248         this.packagesMap.put(packageName, actualClassName);
249     }
250
251     public void cleanPackagesMap() {
252         this.packagesMap.clear();
253     }
254 }