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