Map identities to proper objects
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / UnionTemplate.xtend
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.mdsal.binding.java.api.generator
9
10 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BINARY_TYPE
11 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BOOLEAN_TYPE
12 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.EMPTY_TYPE
13 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.STRING_TYPE
14 import static org.opendaylight.mdsal.binding.model.ri.Types.STRING
15 import static org.opendaylight.mdsal.binding.model.ri.Types.getOuterClassName
16 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME
17 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BUILDER_SUFFIX
18 import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isBitsType
19 import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isIdentityType
20
21 import java.util.Base64;
22 import org.gaul.modernizer_maven_annotations.SuppressModernizer
23 import org.opendaylight.mdsal.binding.model.api.Enumeration
24 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
25 import org.opendaylight.mdsal.binding.model.api.Type
26
27 /**
28  * Template for generating JAVA class.
29  */
30 @SuppressModernizer
31 class UnionTemplate extends ClassTemplate {
32     /**
33      * Creates instance of this class with concrete <code>genType</code>.
34      *
35      * @param genType generated transfer object which will be transformed to JAVA class source code
36      */
37     new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
38         super(javaType, genType)
39     }
40
41     /**
42      * Creates instance of this class with concrete <code>genType</code>.
43      *
44      * @param genType generated transfer object which will be transformed to JAVA class source code
45      */
46     new(GeneratedTransferObject genType) {
47         super(genType)
48     }
49
50     override constructors() '''
51         «unionConstructorsParentProperties»
52         «unionConstructors»
53         «IF !allProperties.empty»
54             «copyConstructor»
55         «ENDIF»
56         «IF properties.empty && !parentProperties.empty»
57             «parentConstructor»
58         «ENDIF»
59
60         «generateStringValue»
61     '''
62
63     private def unionConstructors() '''
64         «FOR property : finalProperties SEPARATOR "\n"»
65             «val actualType = property.returnType»
66             «val restrictions = restrictionsForSetter(actualType)»
67             «IF restrictions !== null»
68                 «generateCheckers(property, restrictions, actualType)»
69             «ENDIF»
70             «val propertyAndTopParentProperties = parentProperties + #[property]»
71             public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
72                 super(«parentProperties.asArguments»);
73                 «IF restrictions !== null»
74                     «checkArgument(property, restrictions, actualType, property.fieldName)»
75                 «ENDIF»
76                 this.«property.fieldName» = «property.fieldName»;
77                 «FOR other : finalProperties»
78                     «IF property != other»
79                          this.«other.fieldName» = null;
80                     «ENDIF»
81                 «ENDFOR»
82             }
83         «ENDFOR»
84     '''
85
86     def typeBuilder() {
87         val outerCls = getOuterClassName(type);
88         if (outerCls !== null) {
89             return outerCls + type.name + BUILDER_SUFFIX
90         }
91         return type.name + BUILDER_SUFFIX
92     }
93
94     private def unionConstructorsParentProperties() '''
95         «FOR property : parentProperties SEPARATOR "\n"»
96             public «type.name»(«property.returnType.importedName» «property.fieldName») {
97                 super(«property.fieldName»);
98             }
99         «ENDFOR»
100     '''
101
102     def generateStringValue()
103     '''
104         /**
105          * Return a String representing the value of this union.
106          *
107          * @return String representation of this union's value.
108          */
109         public «STRING.importedName» stringValue() {
110             «FOR property : finalProperties»
111                 «val field = property.fieldName»
112             if («field» != null) {
113                 «val propRet = property.returnType»
114                 «IF STRING_TYPE.equals(propRet)»
115                     ««« type string
116                 return «field»;
117                 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
118                     ««« type instance-identifier
119                 return «field».toString();
120                 «ELSEIF BINARY_TYPE.equals(propRet)»
121                     ««« type binary
122                 return new «STRING.importedName»(«field»);
123                 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
124                     ««« type int* or enumeration*
125                 return «field».toString();
126                 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
127                         && (propRet.name.startsWith("Uint") || "Decimal64".equals(propRet.name))»
128                     ««« type uint*, decimal64
129                 return «field».toCanonicalString();
130                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
131                     ««« union type
132                 return «field».stringValue();
133                 «ELSEIF BOOLEAN_TYPE.equals(propRet.typedefReturnType)»
134                     ««« generated boolean typedef
135                 return «field».isValue().toString();
136                 «ELSEIF BINARY_TYPE.equals(propRet.typedefReturnType)»
137                     ««« generated byte[] typedef
138                 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
139                 «ELSEIF EMPTY_TYPE.equals(propRet) || EMPTY_TYPE.equals(propRet.typedefReturnType)»
140                     ««« generated empty typedef
141                 return "";
142                 «ELSEIF propRet.isBitsType»
143                     ««« generated bits typedef
144                 return «JU_ARRAYS.importedName».toString(«field».getValue());
145                 «ELSEIF propRet.isIdentityType»
146                     ««« generated identity
147                 return «field».«BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»().toString();
148                 «ELSE»
149                     ««« generated type
150                 return «field».getValue().toString();
151                 «ENDIF»
152             }
153             «ENDFOR»
154             throw new IllegalStateException("No value assigned");
155         }
156     '''
157
158     private static def Type typedefReturnType(Type type) {
159         if (!(type instanceof GeneratedTransferObject)) {
160             return null
161         }
162         val gto = type as GeneratedTransferObject
163         if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
164             return null
165         }
166         val prop = gto.properties.get(0)
167         if (prop.name.equals("value")) {
168             return prop.returnType
169         }
170         return null
171     }
172
173     override protected copyConstructor() '''
174         /**
175          * Creates a copy from Source Object.
176          *
177          * @param source Source object
178          */
179         public «type.name»(«type.name» source) {
180             «IF !parentProperties.empty»
181                 super(source);
182             «ENDIF»
183             «FOR p : properties»
184                 «val fieldName = p.fieldName»
185                 «IF p.returnType.name.endsWith("[]")»
186                 this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
187                 «ELSE»
188                 this.«fieldName» = source.«fieldName»;
189                 «ENDIF»
190             «ENDFOR»
191         }
192     '''
193 }