2 * Copyright (c) 2017 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
9 package org.opendaylight.mdsal.binding.javav2.generator.yang.types;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import java.util.Date;
14 import java.util.HashMap;
17 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
18 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
19 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
31 public final class TypeProviderImpl implements TypeProvider {
34 * Contains the schema data red from YANG files.
36 private final SchemaContext schemaContext;
39 * Map<moduleName, Map<moduleDate, Map<typeName, type>>>
41 private final Map<String, Map<Date, Map<String, Type>>> genTypeDefsContextMap;
45 * Map which maps schema paths to JAVA <code>Type</code>.
47 private final Map<SchemaPath, Type> referencedTypes;
50 * Map for additional types e.g unions
52 private final Map<Module, Set<Type>> additionalTypes;
55 * Creates new instance of class <code>TypeProviderImpl</code>.
57 * @param schemaContext
58 * contains the schema data red from YANG files
59 * @throws IllegalArgumentException
60 * if <code>schemaContext</code> equal null.
62 public TypeProviderImpl(final SchemaContext schemaContext) {
63 this.schemaContext = schemaContext;
64 this.genTypeDefsContextMap = new HashMap<>();
65 this.referencedTypes = new HashMap<>();
66 this.additionalTypes = new HashMap<>();
70 public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode) {
75 public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode, Restrictions restrictions) {
80 public String getTypeDefaultConstruction(LeafSchemaNode node) {
85 public String getConstructorPropertyName(SchemaNode node) {
90 public String getParamNameFromType(TypeDefinition<?> type) {
95 * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
97 * @param typeDefinition
98 * type definition which should be converted to JAVA
100 * @return JAVA <code>Type</code> which represents
101 * <code>typeDefinition</code>
102 * @throws IllegalArgumentException
104 * <li>if <code>typeDefinition</code> equal null</li>
105 * <li>if Q name of <code>typeDefinition</code></li>
106 * <li>if name of <code>typeDefinition</code></li>
109 public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode) {
110 Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
111 if (typeDefinition.getQName() == null) {
112 throw new IllegalArgumentException(
113 "Type Definition cannot have non specified QName (QName cannot be NULL!)");
115 Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
116 "Type Definitions Local Name cannot be NULL!");
118 final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
119 if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
120 final Module module = SchemaContextUtil.findParentModule(schemaContext, parentNode);
122 if (module != null) {
123 final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(module.getName());
124 final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
125 if (genTOs != null) {
126 return genTOs.get(typeDefinition.getQName().getLocalName());
134 * Gets base type definition for <code>extendTypeDef</code>. The method is
135 * recursively called until non <code>ExtendedType</code> type is found.
137 * @param extendTypeDef
138 * type definition for which is the base type definition sought
139 * @return type definition which is base type for <code>extendTypeDef</code>
140 * @throws IllegalArgumentException
141 * if <code>extendTypeDef</code> equal null
143 private static TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
144 Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
146 TypeDefinition<?> ret = extendTypeDef;
147 while (ret.getBaseType() != null) {
148 ret = ret.getBaseType();
154 public Map<Module, Set<Type>> getAdditionalTypes() {
155 return additionalTypes;