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