2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.generator.context;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ArrayListMultimap;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.HashBiMap;
14 import com.google.common.collect.HashMultimap;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ListMultimap;
17 import com.google.common.collect.Maps;
18 import com.google.common.collect.Multimap;
19 import com.google.common.collect.Multimaps;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
27 import java.util.stream.Collectors;
28 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
29 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
30 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
31 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
34 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
37 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
42 * This class holds information about generated entities in context of YANG module
45 public final class ModuleContext {
46 private GeneratedTypeBuilder moduleNode;
47 private final Map<SchemaPath,GeneratedTOBuilder> genTOs = new HashMap<>();
48 private final Map<SchemaPath, Type> typedefs = new HashMap<>();
49 private final Map<SchemaPath, GeneratedTypeBuilder> childNodes = new HashMap<>();
50 private final BiMap<String, GeneratedTypeBuilder> dataTypes = HashBiMap.create();
51 private final Map<SchemaPath, GeneratedTypeBuilder> groupings = new HashMap<>();
52 private final Map<SchemaPath, GeneratedTypeBuilder> cases = new HashMap<>();
53 private final Map<QName,GeneratedTOBuilder> identities = new HashMap<>();
54 private final Set<GeneratedTypeBuilder> topLevelNodes = new HashSet<>();
55 private final List<GeneratedTypeBuilder> augmentations = new ArrayList<>();
56 private final BiMap<Type,AugmentationSchema> typeToAugmentation = HashBiMap.create();
57 private final BiMap<SchemaPath,Type> targetToAugmentation = HashBiMap.create();
58 private final Map<Type,Object> typeToSchema = new HashMap<>();
59 private final Multimap<Type, Type> choiceToCases = HashMultimap.create();
60 private final BiMap<Type,ChoiceCaseNode> caseTypeToSchema = HashBiMap.create();
61 private final Map<SchemaPath, Type> innerTypes = new HashMap<>();
62 private final Map<SchemaPath,GeneratedTypeBuilder> keyTypes = new HashMap<>();
63 //map is getting manipulated based on unique YANG module namespace rule
64 private final ListMultimap<String, String> packagesMap = Multimaps.synchronizedListMultimap
65 (ArrayListMultimap.create());
67 public List<Type> getGeneratedTypes() {
68 final List<Type> result = new ArrayList<>();
70 if (this.moduleNode != null) {
71 result.add(this.moduleNode.toInstance());
74 result.addAll(this.genTOs.values().stream().map(GeneratedTOBuilder::toInstance).collect(Collectors.toList()));
75 result.addAll(this.typedefs.values().stream().filter(b -> b != null).collect(Collectors.toList()));
76 result.addAll(this.dataTypes.values().stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
77 result.addAll(this.groupings.values().stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
78 result.addAll(this.cases.values().stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
79 result.addAll(this.identities.values().stream().map(GeneratedTOBuilder::toInstance).collect(Collectors.toList()));
80 result.addAll(this.topLevelNodes.stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
81 result.addAll(this.augmentations.stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
82 result.addAll(this.keyTypes.values().stream().map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
83 return ImmutableList.copyOf(result);
86 public Multimap<Type, Type> getChoiceToCases() {
87 return Multimaps.unmodifiableMultimap(this.choiceToCases);
90 public GeneratedTypeBuilder getModuleNode() {
91 return this.moduleNode;
94 public GeneratedTypeBuilder getChildNode(final SchemaPath p) {
95 return this.childNodes.get(p);
98 public GeneratedTypeBuilder getGrouping(final SchemaPath p) {
99 return this.groupings.get(p);
102 public GeneratedTypeBuilder getCase(final SchemaPath p) {
103 return this.cases.get(p);
106 public void addModuleNode(final GeneratedTypeBuilder moduleNode) {
107 this.moduleNode = moduleNode;
110 public void addGeneratedTOBuilder(final SchemaPath schemaPath, final GeneratedTOBuilder b) {
111 this.genTOs.put(schemaPath, b);
114 public void addChildNodeType(final SchemaNode p, final GeneratedTypeBuilder b) {
115 this.childNodes.put(p.getPath(), b);
116 this.typeToSchema.put(b,p);
117 this.dataTypes.put(b.getFullyQualifiedName(), b);
120 public void addGroupingType(final GroupingDefinition p, final GeneratedTypeBuilder b) {
121 this.groupings.put(p.getPath(), b);
122 this.typeToSchema.put(b, p);
125 public void addTypedefType(final SchemaPath p, final Type t) {
126 this.typedefs.put(p, t);
129 public void addCaseType(final SchemaPath p, final GeneratedTypeBuilder b) {
130 this.cases.put(p, b);
133 public void addIdentityType(final QName name,final GeneratedTOBuilder b) {
134 this.identities.put(name,b);
137 public void addTopLevelNodeType(final GeneratedTypeBuilder b) {
138 this.topLevelNodes.add(b);
141 public void addAugmentType(final GeneratedTypeBuilder b) {
142 this.augmentations.add(b);
145 public Map<SchemaPath, Type> getTypedefs() {
146 return this.typedefs;
149 public Map<SchemaPath, GeneratedTypeBuilder> getChildNodes() {
150 return Collections.unmodifiableMap(this.childNodes);
153 public Map<SchemaPath, GeneratedTypeBuilder> getGroupings() {
154 return Collections.unmodifiableMap(this.groupings);
157 public Map<SchemaPath, GeneratedTypeBuilder> getCases() {
158 return Collections.unmodifiableMap(this.cases);
161 public Map<QName,GeneratedTOBuilder> getIdentities() {
162 return Collections.unmodifiableMap(this.identities);
165 public Set<GeneratedTypeBuilder> getTopLevelNodes() {
166 return Collections.unmodifiableSet(this.topLevelNodes);
169 public List<GeneratedTypeBuilder> getAugmentations() {
170 return Collections.unmodifiableList(this.augmentations);
173 public BiMap<Type, AugmentationSchema> getTypeToAugmentation() {
174 return Maps.unmodifiableBiMap(this.typeToAugmentation);
177 public BiMap<SchemaPath, Type> getTargetToAugmentation() {
178 return Maps.unmodifiableBiMap(this.targetToAugmentation);
181 public void addTypeToAugmentation(final GeneratedTypeBuilder builder, final AugmentationSchema schema) {
182 this.typeToAugmentation.put(builder, schema);
183 this.typeToSchema.put(builder, schema);
186 public void addTargetToAugmentation(final GeneratedTypeBuilder builder, final SchemaPath augmentTarget) {
187 this.targetToAugmentation.put(augmentTarget, builder);
190 public void addChoiceToCaseMapping(final Type choiceType, final Type caseType, final ChoiceCaseNode schema) {
191 this.choiceToCases.put(choiceType, caseType);
192 this.caseTypeToSchema.put(caseType, schema);
193 this.typeToSchema.put(caseType, schema);
196 public BiMap<Type, ChoiceCaseNode> getCaseTypeToSchemas() {
197 return Maps.unmodifiableBiMap(this.caseTypeToSchema);
202 * Returns mapping of type to its schema.
204 * Valid values are only instances of {@link DataSchemaNode} or {@link AugmentationSchema}
206 * @return Mapping from type to corresponding schema
208 public Map<Type, Object> getTypeToSchema() {
209 return Collections.unmodifiableMap(this.typeToSchema);
212 public void addTypeToSchema(final Type type, final TypeDefinition<?> typedef) {
213 this.typeToSchema.put(type, typedef);
217 * Adds mapping between schema path and inner enum.
222 public void addInnerTypedefType(final SchemaPath path, final EnumBuilder enumBuilder) {
223 this.innerTypes.put(path, enumBuilder);
226 public Type getInnerType(final SchemaPath path) {
227 return this.innerTypes.get(path);
231 public void addKeyType(final SchemaPath path, final GeneratedTypeBuilder genType) {
232 this.keyTypes.put(path, genType);
235 public GeneratedTypeBuilder getKeyType(final SchemaPath path) {
236 return this.keyTypes.get(path);
239 public GeneratedTOBuilder getKeyGenTO(final SchemaPath path) {
240 return this.genTOs.get(path);
243 public ListMultimap<String, String> getPackagesMap() {
244 return Multimaps.unmodifiableListMultimap(packagesMap);
247 public void putToPackagesMap(final String packageName, final String actualClassName) {
248 this.packagesMap.put(packageName, actualClassName);
251 public void cleanPackagesMap() {
252 this.packagesMap.clear();