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