Rehost BindingMapping in yang.binding.contract.Naming
[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.yangtools.yang.binding.contract.Naming.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME
17 import static org.opendaylight.yangtools.yang.binding.contract.Naming.BUILDER_SUFFIX
18 import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isBitsType
19 import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isIdentityType
20
21 import java.util.Base64;
22 import org.opendaylight.mdsal.binding.model.api.Enumeration
23 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
24 import org.opendaylight.mdsal.binding.model.api.Type
25
26 /**
27  * Template for generating JAVA class.
28  */
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             «val propFieldName = property.fieldName»
70             public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
71                 «IF !parentProperties.empty»
72                     super(«parentProperties.asArguments»);
73                 «ENDIF»
74                 «IF restrictions !== null»
75                     «checkArgument(property, restrictions, actualType, propFieldName)»
76                 «ENDIF»
77                 «FOR other : finalProperties»
78                     «IF property.equals(other)»
79                         this.«propFieldName» = «JU_OBJECTS.importedName».requireNonNull(«propFieldName»);
80                     «ELSE»
81                         this.«other.fieldName» = null;
82                     «ENDIF»
83                 «ENDFOR»
84             }
85         «ENDFOR»
86     '''
87
88     def typeBuilder() {
89         val outerCls = getOuterClassName(type);
90         if (outerCls !== null) {
91             return outerCls + type.name + BUILDER_SUFFIX
92         }
93         return type.name + BUILDER_SUFFIX
94     }
95
96     private def unionConstructorsParentProperties() '''
97         «FOR property : parentProperties SEPARATOR "\n"»
98             public «type.name»(«property.returnType.importedName» «property.fieldName») {
99                 super(«property.fieldName»);
100             }
101         «ENDFOR»
102     '''
103
104     def generateStringValue()
105     '''
106         /**
107          * Return a String representing the value of this union.
108          *
109          * @return String representation of this union's value.
110          */
111         public «STRING.importedName» stringValue() {
112             «FOR property : finalProperties»
113                 «val field = property.fieldName»
114             if («field» != null) {
115                 «val propRet = property.returnType»
116                 «IF STRING_TYPE.equals(propRet)»
117                     ««« type string
118                 return «field»;
119                 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
120                     ««« type instance-identifier
121                 return «field».toString();
122                 «ELSEIF BINARY_TYPE.equals(propRet)»
123                     ««« type binary
124                 return new «STRING.importedName»(«field»);
125                 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
126                     ««« type int* or enumeration*
127                 return «field».toString();
128                 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
129                         && (propRet.name.startsWith("Uint") || "Decimal64".equals(propRet.name))»
130                     ««« type uint*, decimal64
131                 return «field».toCanonicalString();
132                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
133                     ««« union type
134                 return «field».stringValue();
135                 «ELSEIF BOOLEAN_TYPE.equals(propRet.typedefReturnType)»
136                     ««« generated boolean typedef
137                 return «field».isValue().toString();
138                 «ELSEIF BINARY_TYPE.equals(propRet.typedefReturnType)»
139                     ««« generated byte[] typedef
140                 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
141                 «ELSEIF EMPTY_TYPE.equals(propRet) || EMPTY_TYPE.equals(propRet.typedefReturnType)»
142                     ««« generated empty typedef
143                 return "";
144                 «ELSEIF propRet.isBitsType»
145                     ««« generated bits typedef
146                 return «JU_ARRAYS.importedName».toString(«field».values());
147                 «ELSEIF propRet.isIdentityType»
148                     ««« generated identity
149                 return «field».«BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»().toString();
150                 «ELSE»
151                     ««« generated type
152                 return «field».getValue().toString();
153                 «ENDIF»
154             }
155             «ENDFOR»
156             throw new IllegalStateException("No value assigned");
157         }
158     '''
159
160     private static def Type typedefReturnType(Type type) {
161         if (!(type instanceof GeneratedTransferObject)) {
162             return null
163         }
164         val gto = type as GeneratedTransferObject
165         if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
166             return null
167         }
168         val prop = gto.properties.get(0)
169         if (prop.name.equals("value")) {
170             return prop.returnType
171         }
172         return null
173     }
174
175     override protected copyConstructor() '''
176         /**
177          * Creates a copy from Source Object.
178          *
179          * @param source Source object
180          */
181         public «type.name»(«type.name» source) {
182             «IF !parentProperties.empty»
183                 super(source);
184             «ENDIF»
185             «FOR p : properties»
186                 «val fieldName = p.fieldName»
187                 «IF p.returnType.name.endsWith("[]")»
188                 this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
189                 «ELSE»
190                 this.«fieldName» = source.«fieldName»;
191                 «ENDIF»
192             «ENDFOR»
193         }
194     '''
195 }