Bug 1411 #1 BindingGeneratorImpl decomposition - Container schema nodes
[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.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;
29
30 @Beta
31 public final class TypeProviderImpl implements TypeProvider {
32
33     /**
34      * Contains the schema data red from YANG files.
35      */
36     private final SchemaContext schemaContext;
37
38     /**
39      * Map<moduleName, Map<moduleDate, Map<typeName, type>>>
40      */
41     private final Map<String, Map<Date, Map<String, Type>>> genTypeDefsContextMap;
42
43
44     /**
45      * Map which maps schema paths to JAVA <code>Type</code>.
46      */
47     private final Map<SchemaPath, Type> referencedTypes;
48
49     /**
50      * Map for additional types e.g unions
51      */
52     private final Map<Module, Set<Type>> additionalTypes;
53
54     /**
55      * Creates new instance of class <code>TypeProviderImpl</code>.
56      *
57      * @param schemaContext
58      *            contains the schema data red from YANG files
59      * @throws IllegalArgumentException
60      *             if <code>schemaContext</code> equal null.
61      */
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<>();
67     }
68
69     @Override
70     public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode) {
71         return null;
72     }
73
74     @Override
75     public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode, Restrictions restrictions) {
76         return null;
77     }
78
79     @Override
80     public String getTypeDefaultConstruction(LeafSchemaNode node) {
81         return null;
82     }
83
84     @Override
85     public String getConstructorPropertyName(SchemaNode node) {
86         return null;
87     }
88
89     @Override
90     public String getParamNameFromType(TypeDefinition<?> type) {
91         return null;
92     }
93
94     /**
95      * Converts <code>typeDefinition</code> to concrete JAVA <code>Type</code>.
96      *
97      * @param typeDefinition
98      *            type definition which should be converted to JAVA
99      *            <code>Type</code>
100      * @return JAVA <code>Type</code> which represents
101      *         <code>typeDefinition</code>
102      * @throws IllegalArgumentException
103      *             <ul>
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>
107      *             </ul>
108      */
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!)");
114         }
115         Preconditions.checkArgument(typeDefinition.getQName().getLocalName() != null,
116                 "Type Definitions Local Name cannot be NULL!");
117
118         final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
119         if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
120             final Module module = SchemaContextUtil.findParentModule(schemaContext, parentNode);
121
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());
127                 }
128             }
129         }
130         return null;
131     }
132
133     /**
134      * Gets base type definition for <code>extendTypeDef</code>. The method is
135      * recursively called until non <code>ExtendedType</code> type is found.
136      *
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
142      */
143     private static TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
144         Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
145
146         TypeDefinition<?> ret = extendTypeDef;
147         while (ret.getBaseType() != null) {
148             ret = ret.getBaseType();
149         }
150
151         return ret;
152     }
153
154     public Map<Module, Set<Type>> getAdditionalTypes() {
155         return additionalTypes;
156     }
157
158 }