Bug 1459-3 #3 - Re-organize mdsal-binding2-generator-impl
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / yang / types / TypeProviderImpl.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.mdsal.binding.javav2.generator.yang.types;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import java.util.Date;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
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;
28
29 @Beta
30 public final class TypeProviderImpl implements TypeProvider {
31
32     /**
33      * Contains the schema data red from YANG files.
34      */
35     private final SchemaContext schemaContext;
36
37     /**
38      * Map<moduleName, Map<moduleDate, Map<typeName, type>>>
39      */
40     private final Map<String, Map<Date, Map<String, Type>>> genTypeDefsContextMap;
41
42     private final Map<Module, Set<Type>> additionalTypes;
43     /**
44      * Creates new instance of class <code>TypeProviderImpl</code>.
45      *
46      * @param schemaContext
47      *            contains the schema data red from YANG files
48      * @throws IllegalArgumentException
49      *             if <code>schemaContext</code> equal null.
50      */
51     public TypeProviderImpl(final SchemaContext schemaContext) {
52         this.schemaContext = schemaContext;
53         this.genTypeDefsContextMap = new HashMap<>();
54         this.additionalTypes = new HashMap<>();
55     }
56
57     @Override
58     public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode) {
59         return null;
60     }
61
62     @Override
63     public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode, Restrictions restrictions) {
64         return null;
65     }
66
67     @Override
68     public String getTypeDefaultConstruction(LeafSchemaNode node) {
69         return null;
70     }
71
72     @Override
73     public String getConstructorPropertyName(SchemaNode node) {
74         return null;
75     }
76
77     @Override
78     public String getParamNameFromType(TypeDefinition<?> type) {
79         return null;
80     }
81
82     /**
83      * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
84      *
85      * @param typeDefinition
86      *            type definition which should be converted to JAVA
87      *            <code>Type</code>
88      * @return JAVA <code>Type</code> which represents
89      *         <code>typeDefinition</code>
90      * @throws IllegalArgumentException
91      *             <ul>
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>
95      *             </ul>
96      */
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!)");
102         }
103         Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
104                 "Type Definitions Local Name cannot be NULL!");
105
106         final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
107         if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
108             final Module module = SchemaContextUtil.findParentModule(schemaContext, parentNode);
109
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());
115                 }
116             }
117         }
118         return null;
119     }
120
121     /**
122      * Gets base type definition for <code>extendTypeDef</code>. The method is
123      * recursively called until non <code>ExtendedType</code> type is found.
124      *
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
130      */
131     private static TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
132         Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
133
134         TypeDefinition<?> ret = extendTypeDef;
135         while (ret.getBaseType() != null) {
136             ret = ret.getBaseType();
137         }
138
139         return ret;
140     }
141
142     public Map<Module, Set<Type>> getAdditionalTypes() {
143         return additionalTypes;
144     }
145
146 }