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