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