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