Merge "Bug 762: Verify input in typedef codecs"
[mdsal.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / InterfaceTemplate.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.yangtools.sal.java.api.generator
9
10 import java.util.List
11 import org.opendaylight.yangtools.binding.generator.util.TypeConstants
12 import org.opendaylight.yangtools.sal.binding.model.api.Constant
13 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
14 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
15 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
16 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature\rimport org.opendaylight.yangtools.sal.binding.model.api.AnnotationType
17
18 /**
19  * Template for generating JAVA interfaces. 
20  */
21 class InterfaceTemplate extends BaseTemplate {
22     
23     /**
24      * List of constant instances which are generated as JAVA public static final attributes.
25      */
26     val List<Constant> consts
27     
28     /**
29      * List of method signatures which are generated as method declarations.
30      */
31     val List<MethodSignature> methods
32     
33     /**
34      * List of enumeration which are generated as JAVA enum type.
35      */
36     val List<Enumeration> enums
37     
38     /**
39      * List of generated types which are enclosed inside <code>genType</code>
40      */
41     val List<GeneratedType> enclosedGeneratedTypes
42     
43     /**
44      * Creates the instance of this class which is used for generating the interface file source 
45      * code from <code>genType</code>.
46      * 
47      * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
48      */
49     new(GeneratedType genType) {
50         super(genType)
51         if (genType == null) {
52             throw new IllegalArgumentException("Generated type reference cannot be NULL!")
53         }
54         
55         consts = genType.constantDefinitions
56         methods = genType.methodDefinitions
57         enums = genType.enumerations
58         enclosedGeneratedTypes = genType.enclosedTypes
59     }
60     
61
62     
63     /**
64      * Template method which generate the whole body of the interface.
65      * 
66      * @return string with code for interface body in JAVA format
67      */
68     override body() '''
69         «type.comment.asJavadoc»
70         public interface «type.name»
71             «superInterfaces»
72         {
73         
74             «generateInnerClasses»
75         
76             «generateEnums»
77         
78             «generateConstants»
79         
80             «generateMethods»
81         
82         }
83         
84     '''
85    
86
87     def private generateAnnotations(List<AnnotationType> annotations) '''
88         «IF annotations != null && !annotations.empty»
89             «FOR annotation : annotations»
90                 @«annotation.importedName»
91                 «IF annotation.parameters != null && !annotation.parameters.empty»
92                 (
93                 «FOR param : annotation.parameters SEPARATOR ","»
94                     «param.name»=«param.value»
95                 «ENDFOR»
96                 )
97                 «ENDIF»
98             «ENDFOR»
99         «ENDIF»
100     '''
101
102     /**
103      * Template method which generates the interface name declaration.
104      * 
105      * @return string with the code for the interface declaration in JAVA format
106      */
107     def private superInterfaces() 
108     '''
109     «IF (!type.implements.empty)»
110          extends
111          «FOR type : type.implements SEPARATOR ","»
112              «type.importedName»
113          «ENDFOR»
114      « ENDIF»
115      '''
116
117     /**
118      * Template method which generates inner classes inside this interface.
119      * 
120      * @return string with the source code for inner classes in JAVA format
121      */
122     def private generateInnerClasses() '''
123         «IF !enclosedGeneratedTypes.empty»
124             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
125                 «IF (innerClass instanceof GeneratedTransferObject)»
126                     «IF (innerClass as GeneratedTransferObject).unionType»
127                         «val unionTemplate = new UnionTemplate(innerClass as GeneratedTransferObject)»
128                         «unionTemplate.generateAsInnerClass»
129                         «this.importMap.putAll(unionTemplate.importMap)»
130                     «ELSE»
131                         «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)»
132                         «classTemplate.generateAsInnerClass»
133                         «this.importMap.putAll(classTemplate.importMap)»
134                     «ENDIF»
135
136                 «ENDIF»
137             «ENDFOR»
138         «ENDIF»
139     '''
140
141     /**
142      * Template method which generates JAVA enum type.
143      * 
144      * @return string with inner enum source code in JAVA format
145      */    
146     def private generateEnums() '''
147         «IF !enums.empty»
148             «FOR e : enums SEPARATOR "\n"»
149                 «val enumTemplate = new EnumTemplate(e)»
150                 «enumTemplate.generateAsInnerClass»
151             «ENDFOR»
152         «ENDIF»
153     '''
154     
155     /**
156      * Template method wich generates JAVA constants.
157      * 
158      * @return string with constants in JAVA format 
159      */    
160     def private generateConstants() '''
161         «IF !consts.empty»
162             «FOR c : consts»
163                 «IF c.name != TypeConstants.PATTERN_CONSTANT_NAME»
164                     public static final «c.type.importedName» «c.name» = «c.value»;
165                 «ENDIF»
166             «ENDFOR»
167         «ENDIF»
168     '''
169
170     /**
171      * Template method which generates the declaration of the methods.
172      * 
173      * @return string with the declaration of methods source code in JAVA format 
174      */    
175     def private generateMethods() '''
176         «IF !methods.empty»
177             «FOR m : methods SEPARATOR "\n"»
178                 «m.comment.asJavadoc»
179                 «m.annotations.generateAnnotations»
180                 «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);
181             «ENDFOR»
182         «ENDIF»
183     '''
184
185 }
186