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