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.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
30 public final class TypeProviderImpl implements TypeProvider {
33 * Contains the schema data red from YANG files.
35 private final SchemaContext schemaContext;
38 * Map<moduleName, Map<moduleDate, Map<typeName, type>>>
40 private final Map<String, Map<Date, Map<String, Type>>> genTypeDefsContextMap;
42 private final Map<Module, Set<Type>> additionalTypes;
44 * Creates new instance of class <code>TypeProviderImpl</code>.
46 * @param schemaContext
47 * contains the schema data red from YANG files
48 * @throws IllegalArgumentException
49 * if <code>schemaContext</code> equal null.
51 public TypeProviderImpl(final SchemaContext schemaContext) {
52 this.schemaContext = schemaContext;
53 this.genTypeDefsContextMap = new HashMap<>();
54 this.additionalTypes = new HashMap<>();
58 public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode) {
63 public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode, Restrictions restrictions) {
68 public String getTypeDefaultConstruction(LeafSchemaNode node) {
73 public String getConstructorPropertyName(SchemaNode node) {
78 public String getParamNameFromType(TypeDefinition<?> type) {
83 * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
85 * @param typeDefinition
86 * type definition which should be converted to JAVA
88 * @return JAVA <code>Type</code> which represents
89 * <code>typeDefinition</code>
90 * @throws IllegalArgumentException
92 * <li>if <code>typeDefinition</code> equal null</li>
93 * <li>if Q name of <code>typeDefinition</code></li>
94 * <li>if name of <code>typeDefinition</code></li>
97 public Type generatedTypeForExtendedDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode) {
98 Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
99 if (typeDefinition.getQName() == null) {
100 throw new IllegalArgumentException(
101 "Type Definition cannot have non specified QName (QName cannot be NULL!)");
103 Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
104 "Type Definitions Local Name cannot be NULL!");
106 final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
107 if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
108 final Module module = SchemaContextUtil.findParentModule(schemaContext, parentNode);
110 if (module != null) {
111 final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(module.getName());
112 final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
113 if (genTOs != null) {
114 return genTOs.get(typeDefinition.getQName().getLocalName());
122 * Gets base type definition for <code>extendTypeDef</code>. The method is
123 * recursively called until non <code>ExtendedType</code> type is found.
125 * @param extendTypeDef
126 * type definition for which is the base type definition sought
127 * @return type definition which is base type for <code>extendTypeDef</code>
128 * @throws IllegalArgumentException
129 * if <code>extendTypeDef</code> equal null
131 private static TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
132 Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
134 TypeDefinition<?> ret = extendTypeDef;
135 while (ret.getBaseType() != null) {
136 ret = ret.getBaseType();
142 public Map<Module, Set<Type>> getAdditionalTypes() {
143 return additionalTypes;