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