2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl;
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;
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.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 public final class ModuleContext {
35 private GeneratedTypeBuilder moduleNode;
36 private final List<GeneratedTOBuilder> genTOs = new ArrayList<>();
37 private final Map<SchemaPath, Type> typedefs = new HashMap<>();
38 private final Map<SchemaPath, GeneratedTypeBuilder> childNodes = new HashMap<>();
39 private final Map<SchemaPath, GeneratedTypeBuilder> groupings = new HashMap<>();
40 private final Map<SchemaPath, GeneratedTypeBuilder> cases = new HashMap<>();
41 private final Map<QName,GeneratedTOBuilder> identities = new HashMap<>();
42 private final Set<GeneratedTypeBuilder> topLevelNodes = new HashSet<>();
43 private final List<GeneratedTypeBuilder> augmentations = new ArrayList<>();
44 private final BiMap<Type, AugmentationSchema> typeToAugmentation = HashBiMap.create();
46 private final Map<Type,Object> typeToSchema = new HashMap<>();
49 private final Multimap<Type, Type> choiceToCases = HashMultimap.create();
50 private final BiMap<Type, ChoiceCaseNode> caseTypeToSchema = HashBiMap.create();
52 private final Map<SchemaPath, Type> innerTypes = new HashMap<>();
54 List<Type> getGeneratedTypes() {
55 List<Type> result = new ArrayList<>();
57 if (moduleNode != null) {
58 result.add(moduleNode.toInstance());
61 for (GeneratedTOBuilder b : genTOs) {
62 result.add(b.toInstance());
64 for (Type b : typedefs.values()) {
69 for (GeneratedTypeBuilder b : childNodes.values()) {
70 result.add(b.toInstance());
72 for (GeneratedTypeBuilder b : groupings.values()) {
73 result.add(b.toInstance());
75 for (GeneratedTypeBuilder b : cases.values()) {
76 result.add(b.toInstance());
78 for (GeneratedTOBuilder b : identities.values()) {
79 result.add(b.toInstance());
81 for (GeneratedTypeBuilder b : topLevelNodes) {
82 result.add(b.toInstance());
84 for (GeneratedTypeBuilder b : augmentations) {
85 result.add(b.toInstance());
90 public Multimap<Type, Type> getChoiceToCases() {
91 return Multimaps.unmodifiableMultimap(choiceToCases);
94 public GeneratedTypeBuilder getModuleNode() {
98 public GeneratedTypeBuilder getChildNode(final SchemaPath p) {
99 return childNodes.get(p);
102 public GeneratedTypeBuilder getGrouping(final SchemaPath p) {
103 return groupings.get(p);
106 public GeneratedTypeBuilder getCase(final SchemaPath p) {
110 public void addModuleNode(final GeneratedTypeBuilder moduleNode) {
111 this.moduleNode = moduleNode;
114 public void addGeneratedTOBuilder(final GeneratedTOBuilder b) {
118 public void addChildNodeType(final SchemaNode p, final GeneratedTypeBuilder b) {
119 childNodes.put(p.getPath(), b);
120 typeToSchema.put(b,p);
123 public void addGroupingType(final SchemaPath p, final GeneratedTypeBuilder b) {
127 public void addTypedefType(final SchemaPath p, final Type t) {
131 public void addCaseType(final SchemaPath p, final GeneratedTypeBuilder b) {
135 public void addIdentityType(final QName name,final GeneratedTOBuilder b) {
136 identities.put(name,b);
139 public void addTopLevelNodeType(final GeneratedTypeBuilder b) {
140 topLevelNodes.add(b);
143 public void addAugmentType(final GeneratedTypeBuilder b) {
144 augmentations.add(b);
147 public Map<SchemaPath, Type> getTypedefs() {
151 public Map<SchemaPath, GeneratedTypeBuilder> getChildNodes() {
152 return Collections.unmodifiableMap(childNodes);
155 public Map<SchemaPath, GeneratedTypeBuilder> getGroupings() {
156 return Collections.unmodifiableMap(groupings);
159 public Map<SchemaPath, GeneratedTypeBuilder> getCases() {
160 return Collections.unmodifiableMap(cases);
163 public Map<QName,GeneratedTOBuilder> getIdentities() {
164 return Collections.unmodifiableMap(identities);
167 public Set<GeneratedTypeBuilder> getTopLevelNodes() {
168 return Collections.unmodifiableSet(topLevelNodes);
171 public List<GeneratedTypeBuilder> getAugmentations() {
172 return Collections.unmodifiableList(augmentations);
175 public BiMap<Type, AugmentationSchema> getTypeToAugmentation() {
176 return Maps.unmodifiableBiMap(typeToAugmentation);
179 public void addTypeToAugmentation(final GeneratedTypeBuilder builder, final AugmentationSchema schema) {
180 typeToAugmentation.put(builder, schema);
181 typeToSchema.put(builder, schema);
184 public void addChoiceToCaseMapping(final Type choiceType, final Type caseType, final ChoiceCaseNode schema) {
185 choiceToCases.put(choiceType, caseType);
186 caseTypeToSchema.put(caseType, schema);
187 typeToSchema.put(caseType, schema);
190 public BiMap<Type, ChoiceCaseNode> getCaseTypeToSchemas() {
191 return Maps.unmodifiableBiMap(caseTypeToSchema);
196 * Returns mapping of type to its schema.
198 * Valid values are only instances of {@link DataSchemaNode} or {@link AugmentationSchema}
200 * @return Mapping from type to corresponding schema
202 public Map<Type, Object> getTypeToSchema() {
203 return Collections.unmodifiableMap(typeToSchema);
206 protected void addTypeToSchema(Type type, TypeDefinition<?> typedef) {
207 typeToSchema.put(type, typedef);
211 * Adds mapping between schema path and an inner type.
216 void addInnerTypedefType(final SchemaPath path, final Type type) {
217 innerTypes.put(path, type);
220 public Type getInnerType(SchemaPath path) {
221 return innerTypes.get(path);