Switch to java.util.Base64
[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 java.util.Arrays
14 import java.util.Base64;
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(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
30         super(javaType, genType)
31     }
32
33     /**
34      * Creates instance of this class with concrete <code>genType</code>.
35      *
36      * @param genType generated transfer object which will be transformed to JAVA class source code
37      */
38     new(GeneratedTransferObject genType) {
39         super(genType)
40     }
41
42     override constructors() '''
43         «unionConstructorsParentProperties»
44         «unionConstructors»
45         «IF !allProperties.empty»
46             «copyConstructor»
47         «ENDIF»
48         «IF properties.empty && !parentProperties.empty»
49             «parentConstructor»
50         «ENDIF»
51
52         «generateStringValue»
53     '''
54
55     private def unionConstructors() '''
56         «FOR property : finalProperties SEPARATOR "\n"»
57             «val actualType = property.returnType»
58             «val restrictions = restrictionsForSetter(actualType)»
59             «IF restrictions !== null»
60                 «generateCheckers(property, restrictions, actualType)»
61             «ENDIF»
62             «val propertyAndTopParentProperties = parentProperties + #[property]»
63             public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
64                 super(«parentProperties.asArguments»);
65                 «IF restrictions !== null»
66                     «checkArgument(property, restrictions, actualType, property.fieldName.toString)»
67                 «ENDIF»
68                 this.«property.fieldName» = «property.fieldName»;
69                 «FOR other : finalProperties»
70                     «IF property != other»
71                          this.«other.fieldName» = null;
72                     «ENDIF»
73                 «ENDFOR»
74             }
75         «ENDFOR»
76     '''
77
78     def typeBuilder() {
79         val outerCls = getOuterClassName(type);
80         if(outerCls !== null) {
81             return outerCls + type.name + "Builder"
82         }
83         return type.name + "Builder"
84     }
85
86     private def unionConstructorsParentProperties() '''
87         «FOR property : parentProperties SEPARATOR "\n"»
88             public «type.name»(«property.returnType.importedName» «property.fieldName») {
89                 super(«property.fieldName»);
90             }
91         «ENDFOR»
92     '''
93
94     def generateStringValue()
95     '''
96         /**
97          * Return a String representing the value of this union.
98          *
99          * @return String representation of this union's value.
100          */
101         public «String.importedName» stringValue() {
102             «FOR property : finalProperties»
103                 «val field = property.fieldName»
104             if («field» != null) {
105                 «val propRet = property.returnType»
106                 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
107                     ««« type string
108                 return «field»;
109                 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
110                     ««« type instance-identifier
111                 return «field».toString();
112                 «ELSEIF "byte[]".equals(propRet.name)»
113                     ««« type binary
114                 return new «String.importedName»(«field»);
115                 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
116                         || propRet.fullyQualifiedName.startsWith("java.math")»
117                     ««« type int*, uint, decimal64 or enumeration*
118                 return «field».toString();
119                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
120                     ««« union type
121                 return «field».stringValue();
122                 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
123                         && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
124                         && (propRet as GeneratedTransferObject).properties !== null
125                         && !(propRet as GeneratedTransferObject).properties.empty
126                         && ((propRet as GeneratedTransferObject).properties.size == 1)
127                         && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
128                         && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType) // And the property value is of type boolean»
129                     ««« generated boolean typedef
130                 return «field».isValue().toString();
131                 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
132                         && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
133                         && (propRet as GeneratedTransferObject).properties !== null
134                         && !(propRet as GeneratedTransferObject).properties.empty
135                         && ((propRet as GeneratedTransferObject).properties.size == 1)
136                         && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
137                         && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
138                     ««« generated byte[] typedef
139                 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
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 «Arrays.importedName».toString(«field».getValue());
145                 «ELSE»
146                     ««« generated type
147                 return «field».getValue().toString();
148                 «ENDIF»
149             }
150             «ENDFOR»
151
152             throw new IllegalStateException("No value assinged");
153         }
154     '''
155
156     override protected copyConstructor() '''
157         /**
158          * Creates a copy from Source Object.
159          *
160          * @param source Source object
161          */
162         public «type.name»(«type.name» source) {
163             «IF !parentProperties.empty»
164                 super(source);
165             «ENDIF»
166             «FOR p : properties»
167                 «IF p.returnType.importedName.contains("[]")»
168                 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
169                 «ELSE»
170                 this.«p.fieldName» = source.«p.fieldName»;
171                 «ENDIF»
172             «ENDFOR»
173         }
174     '''
175
176 }