9d730b642d80859c604c9adc2eaf74220a88a017
[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 com.google.common.collect.Streams;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Set;
29 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
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.AugmentationSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
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,GeneratedTypeBuilder> identities = new HashMap<>();
54     private final Set<GeneratedTypeBuilder> topLevelNodes = new HashSet<>();
55     private final List<GeneratedTypeBuilder> augmentations = new ArrayList<>();
56     private final Multimap<Type,AugmentationSchemaNode> typeToAugmentations = HashMultimap.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, CaseSchemaNode> 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         Streams.concat(
75             this.genTOs.values().stream().map(GeneratedTOBuilder::toInstance),
76             this.typedefs.values().stream().filter(Objects::nonNull),
77             this.dataTypes.values().stream().map(GeneratedTypeBuilder::toInstance),
78             this.groupings.values().stream().map(GeneratedTypeBuilder::toInstance),
79             this.cases.values().stream().map(GeneratedTypeBuilder::toInstance),
80             this.identities.values().stream().map(GeneratedTypeBuilder::toInstance),
81             this.topLevelNodes.stream().map(GeneratedTypeBuilder::toInstance),
82             this.augmentations.stream().map(GeneratedTypeBuilder::toInstance),
83             this.keyTypes.values().stream().map(GeneratedTypeBuilder::toInstance))
84         .forEach(result::add);
85         return ImmutableList.copyOf(result);
86     }
87
88     public Multimap<Type, Type> getChoiceToCases() {
89         return Multimaps.unmodifiableMultimap(this.choiceToCases);
90     }
91
92     public GeneratedTypeBuilder getModuleNode() {
93         return this.moduleNode;
94     }
95
96     public GeneratedTypeBuilder getChildNode(final SchemaPath p) {
97         return this.childNodes.get(p);
98     }
99
100     public GeneratedTypeBuilder getGrouping(final SchemaPath p) {
101         return this.groupings.get(p);
102     }
103
104     public GeneratedTypeBuilder getCase(final SchemaPath p) {
105         return this.cases.get(p);
106     }
107
108     public void addModuleNode(final GeneratedTypeBuilder moduleNode) {
109         this.moduleNode = moduleNode;
110     }
111
112     public void addGeneratedTOBuilder(final SchemaPath schemaPath, final GeneratedTOBuilder b) {
113         this.genTOs.put(schemaPath, b);
114     }
115
116     public void addChildNodeType(final SchemaNode p, final GeneratedTypeBuilder b) {
117         this.childNodes.put(p.getPath(), b);
118         this.typeToSchema.put(b,p);
119         this.dataTypes.put(b.getFullyQualifiedName(), b);
120     }
121
122     public void addGroupingType(final GroupingDefinition p, final GeneratedTypeBuilder b) {
123         this.groupings.put(p.getPath(), b);
124         this.typeToSchema.put(b, p);
125     }
126
127     public void addTypedefType(final SchemaPath p, final Type t) {
128         this.typedefs.put(p, t);
129     }
130
131     public void addCaseType(final SchemaPath p, final GeneratedTypeBuilder b) {
132         this.cases.put(p, b);
133     }
134
135     public void addIdentityType(final QName name,final GeneratedTypeBuilder b) {
136         this.identities.put(name,b);
137     }
138
139     public void addTopLevelNodeType(final GeneratedTypeBuilder b) {
140         this.topLevelNodes.add(b);
141     }
142
143     public void addAugmentType(final GeneratedTypeBuilder b) {
144         this.augmentations.add(b);
145     }
146
147     public Map<SchemaPath, Type> getTypedefs() {
148         return this.typedefs;
149     }
150
151     public Map<SchemaPath, GeneratedTypeBuilder> getChildNodes() {
152         return Collections.unmodifiableMap(this.childNodes);
153     }
154
155     public Map<SchemaPath, GeneratedTypeBuilder> getGroupings() {
156         return Collections.unmodifiableMap(this.groupings);
157     }
158
159     public Map<SchemaPath, GeneratedTypeBuilder> getCases() {
160         return Collections.unmodifiableMap(this.cases);
161     }
162
163     public Map<QName,GeneratedTypeBuilder> getIdentities() {
164         return Collections.unmodifiableMap(this.identities);
165     }
166
167     public Set<GeneratedTypeBuilder> getTopLevelNodes() {
168         return Collections.unmodifiableSet(this.topLevelNodes);
169     }
170
171     public List<GeneratedTypeBuilder> getAugmentations() {
172         return Collections.unmodifiableList(this.augmentations);
173     }
174
175     public Multimap<Type, AugmentationSchemaNode> getTypeToAugmentations() {
176         return Multimaps.unmodifiableMultimap(this.typeToAugmentations);
177     }
178
179     public BiMap<SchemaPath, Type> getTargetToAugmentation() {
180         return Maps.unmodifiableBiMap(this.targetToAugmentation);
181     }
182
183     public void addTypeToAugmentations(final GeneratedTypeBuilder builder,
184             final List<AugmentationSchemaNode> schemaList) {
185         schemaList.forEach(augmentNode -> this.typeToAugmentations.put(builder, augmentNode));
186     }
187
188     public void addTargetToAugmentation(final GeneratedTypeBuilder builder, final SchemaPath augmentTarget) {
189         this.targetToAugmentation.put(augmentTarget, builder);
190     }
191
192     public void addChoiceToCaseMapping(final Type choiceType, final Type caseType, final CaseSchemaNode schema) {
193         this.choiceToCases.put(choiceType, caseType);
194         this.caseTypeToSchema.put(caseType, schema);
195         this.typeToSchema.put(caseType, schema);
196     }
197
198     public BiMap<Type, CaseSchemaNode> getCaseTypeToSchemas() {
199         return Maps.unmodifiableBiMap(this.caseTypeToSchema);
200     }
201
202     /**
203      *
204      * Returns mapping of type to its schema.
205      *
206      * Valid values are only instances of {@link DataSchemaNode} or {@link AugmentationSchemaNode}
207      *
208      * @return Mapping from type to corresponding schema
209      */
210     public Map<Type, Object> getTypeToSchema() {
211         return Collections.unmodifiableMap(this.typeToSchema);
212     }
213
214     public void addTypeToSchema(final Type type, final TypeDefinition<?> typedef) {
215         this.typeToSchema.put(type, typedef);
216     }
217
218     /**
219      * Adds mapping between schema path and inner enum, inner union, inner bits.
220      *
221      * @param path
222      * @param builder
223      */
224     public void addInnerTypedefType(final SchemaPath path, final Type builder) {
225         this.innerTypes.put(path, builder);
226     }
227
228     public Type getInnerType(final SchemaPath path) {
229         return this.innerTypes.get(path);
230     }
231
232
233     public void addKeyType(final SchemaPath path, final GeneratedTypeBuilder genType) {
234         this.keyTypes.put(path, genType);
235     }
236
237     public GeneratedTypeBuilder getKeyType(final SchemaPath path) {
238         return this.keyTypes.get(path);
239     }
240
241     public GeneratedTOBuilder getKeyGenTO(final SchemaPath path) {
242         return this.genTOs.get(path);
243     }
244
245     public ListMultimap<String, String> getPackagesMap() {
246         return Multimaps.unmodifiableListMultimap(packagesMap);
247     }
248
249     public void putToPackagesMap(final String packageName, final String actualClassName) {
250         this.packagesMap.put(packageName, actualClassName);
251     }
252
253     public void cleanPackagesMap() {
254         this.packagesMap.clear();
255     }
256 }