3c7444e52791bfffd526a5981ee42d5173cff3eb
[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.util.Types.BOOLEAN;
11 import static org.opendaylight.mdsal.binding.model.util.Types.getOuterClassName;
12
13 import com.google.common.base.Preconditions;
14 import java.beans.ConstructorProperties
15 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
16 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
17 import org.opendaylight.mdsal.binding.model.api.Enumeration
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
19
20 /**
21  * Template for generating JAVA class.
22  */
23 class UnionTemplate extends ClassTemplate {
24
25     /**
26      * Creates instance of this class with concrete <code>genType</code>.
27      *
28      * @param genType generated transfer object which will be transformed to JAVA class source code
29      */
30     new(GeneratedTransferObject genType) {
31         super(genType)
32         if (isBaseEncodingImportRequired) {
33             addImport("BaseEncoding","com.google.common.io")
34         }
35     }
36
37     final private def boolean isBaseEncodingImportRequired() {
38         for (property : finalProperties) {
39             val propRet = property.returnType
40             if (propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).typedef &&
41                 (propRet as GeneratedTransferObject).properties !== null &&
42                 !(propRet as GeneratedTransferObject).properties.empty &&
43                 ((propRet as GeneratedTransferObject).properties.size == 1) &&
44                 (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") &&
45                 "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)) {
46                 return true;
47             }
48         }
49     }
50
51     override constructors() '''
52         «unionConstructorsParentProperties»
53         «unionConstructors»
54         «IF !allProperties.empty»
55             «copyConstructor»
56         «ENDIF»
57         «IF properties.empty && !parentProperties.empty»
58             «parentConstructor»
59         «ENDIF»
60     '''
61
62     private def unionConstructors() '''
63         «FOR property : finalProperties SEPARATOR "\n"»
64             «val propRet = property.returnType»
65             «IF "char[]".equals(propRet.name)»
66                 /**
67                  * Constructor provided only for using in JMX. Don't use it for
68                  * construction new object of this union type.
69                  */
70                 @«ConstructorProperties.importedName»("«property.name»")
71                 public «type.name»(«propRet.importedName» «property.fieldName») {
72                     «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
73                     «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
74                     «FOR other : finalProperties»
75                         «IF other.name.equals("value")»
76                             «IF other.returnType.importedName.contains("[]")»
77                             this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
78                             «ELSE»
79                             this.«other.fieldName» = «other.fieldName»;
80                             «ENDIF»
81                         «ELSE»
82                             this.«other.fieldName» = defInst.«other.fieldName»;
83                         «ENDIF»
84                     «ENDFOR»
85                 }
86             «ELSE»
87                 «val propertyAndTopParentProperties = parentProperties + #[property]»
88                 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
89                     super(«parentProperties.asArguments»);
90                     this.«property.fieldName» = «property.fieldName»;
91                     «FOR other : finalProperties»
92                         «IF property != other && !"value".equals(other.name)»
93                              this.«other.fieldName» = null;
94                         «ENDIF»
95                     «ENDFOR»
96                 }
97             «ENDIF»
98         «ENDFOR»
99     '''
100
101     def typeBuilder() {
102         val outerCls = getOuterClassName(type);
103         if(outerCls !== null) {
104             return outerCls + type.name + "Builder"
105         }
106         return type.name + "Builder"
107     }
108
109     private def unionConstructorsParentProperties() '''
110         «FOR property : parentProperties SEPARATOR "\n"»
111             public «type.name»(«property.returnType.importedName» «property.fieldName») {
112                 super(«property.fieldName»);
113             }
114         «ENDFOR»
115     '''
116
117     override protected getterMethod(GeneratedProperty field) {
118         if (!"value".equals(field.name)) {
119             return super.getterMethod(field)
120         }
121
122         Preconditions.checkArgument("char[]".equals(field.returnType.importedName))
123
124         '''
125             public char[] «field.getterMethodName»() {
126                 if («field.fieldName» == null) {
127                     «FOR property : finalProperties.filter([ p | !"value".equals(p.name)]) SEPARATOR " else"»
128                         if («property.fieldName» != null) {
129                             «val propRet = property.returnType»
130                             «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
131                                 ««« type string
132                                 «field.fieldName» = «property.fieldName».toCharArray();
133                             «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
134                                 ««« type instance-identifier
135                                 «field.fieldName» = «property.fieldName».toString().toCharArray();
136                             «ELSEIF "byte[]".equals(propRet.name)»
137                                 ««« type binary
138                                 «field.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
139                             «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
140                                 || propRet instanceof Enumeration
141                                 || propRet.fullyQualifiedName.startsWith("java.math")»
142                                 ««« type int*, uint, decimal64 or enumeration*
143                                 «field.fieldName» = «property.fieldName».toString().toCharArray();
144                             «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
145                                 ««« union type
146                                 «field.fieldName» = «property.fieldName».getValue();
147                             «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
148                                     && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
149                                     && (propRet as GeneratedTransferObject).properties !== null
150                                     && !(propRet as GeneratedTransferObject).properties.empty
151                                     && ((propRet as GeneratedTransferObject).properties.size == 1)
152                                     && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
153                                     && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
154                                 ««« generated boolean typedef
155                                 «field.fieldName» = «property.fieldName».isValue().toString().toCharArray();
156                             «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
157                                     && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
158                                     && (propRet as GeneratedTransferObject).properties !== null
159                                     && !(propRet as GeneratedTransferObject).properties.empty
160                                     && ((propRet as GeneratedTransferObject).properties.size == 1)
161                                     && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
162                                     && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
163                                 ««« generated byte[] typedef
164                                 «field.fieldName» = BaseEncoding.base64().encode(«property.fieldName».getValue()).toCharArray();
165                             «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
166                                     && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
167                                     && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
168                                 ««« generated bits typedef
169                                 «field.fieldName» = java.util.Arrays.toString(«property.fieldName».getValue()).toCharArray();
170                             «ELSE»
171                                 ««« generated type
172                                 «field.fieldName» = «property.fieldName».getValue().toString().toCharArray();
173                             «ENDIF»
174                         }
175                     «ENDFOR»
176                 }
177                 return «field.fieldName» == null ? null : «field.fieldName».clone();
178             }
179         '''
180     }
181
182     override def isReadOnly(GeneratedProperty field) {
183         return !"value".equals(field.name) && super.isReadOnly(field)
184     }
185
186     override protected copyConstructor() '''
187         /**
188          * Creates a copy from Source Object.
189          *
190          * @param source Source object
191          */
192         public «type.name»(«type.name» source) {
193             «IF !parentProperties.empty»
194                 super(source);
195             «ENDIF»
196             «IF !properties.empty»
197                 «FOR p : properties»
198                     «IF !"value".equals(p.name) && p.returnType.importedName.contains("[]")»
199                     this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
200                     «ELSE»
201                     this.«p.fieldName» = source.«p.fieldName»;
202                     «ENDIF»
203                 «ENDFOR»
204             «ENDIF»
205         }
206     '''
207
208 }