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