Changed generation of POST operation block 76/4176/2
authorMartin Sunal <msunal@cisco.com>
Mon, 13 Jan 2014 11:34:22 +0000 (12:34 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 16 Jan 2014 17:51:34 +0000 (17:51 +0000)
- POST block was changed according to changes in RESTCONF https://git.opendaylight.org/gerrit/#/c/4081/
- added block for DELETE operation
- refactoring

Signed-off-by: Martin Sunal <msunal@cisco.com>
code-generator/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/wadl/generator/WadlRestconfGenerator.xtend

index 586b000c5673e433831ef0fd006369fe4745be36..aeb42423c803d7eb3047beb21666debe3e6b86e6 100644 (file)
@@ -37,7 +37,7 @@ class WadlRestconfGenerator {
         val result = new HashSet;
                this.context = context
                for (module : modules) {
-                       val dataContainers = module.childNodes.filter[it|it instanceof ContainerSchemaNode || it instanceof ListSchemaNode]
+                       val dataContainers = module.childNodes.filter[it|it.listOrContainer]
                        if (!dataContainers.empty || !module.rpcs.nullOrEmpty) {
                                configData = new ArrayList
                                operationalData = new ArrayList
@@ -109,6 +109,9 @@ class WadlRestconfGenerator {
        private def resourceConfig() '''
                «IF !configData.nullOrEmpty»
                        <resource path="config">
+                               «FOR schemaNode : configData»
+                                       «schemaNode.mehodPost»
+                               «ENDFOR»
                                «FOR schemaNode : configData»
                                        «schemaNode.firstResource(true)»
                                «ENDFOR»
@@ -130,45 +133,13 @@ class WadlRestconfGenerator {
        
        private def String firstResource(DataSchemaNode schemaNode, boolean config) '''
                <resource path="«module.name»:«schemaNode.createPath»">
-                       «IF !pathListParams.nullOrEmpty»
-                               «resourceParams»
-                       «ENDIF»
-                       «schemaNode.methodGet»
-                       «IF config»
-                               «schemaNode.mehodPut»
-                               «schemaNode.mehodPost»
-                       «ENDIF»
-                       
-                       «IF schemaNode instanceof DataNodeContainer»
-                               «val children = (schemaNode as DataNodeContainer).childNodes.filter[it|it instanceof ContainerSchemaNode || it instanceof ListSchemaNode]»
-                               «IF !children.empty»
-                                       «FOR child : children»
-                                               «child.resource(config)»
-                                       «ENDFOR»
-                               «ENDIF»
-                       «ENDIF»
+                       «resourceBody(schemaNode, config)»
                </resource>
        '''
                
        private def String resource(DataSchemaNode schemaNode, boolean config) '''
                <resource path="«schemaNode.createPath»">
-                       «IF !pathListParams.nullOrEmpty»
-                               «resourceParams»
-                       «ENDIF»
-                       «schemaNode.methodGet»
-                       «IF config»
-                               «schemaNode.mehodPut»
-                               «schemaNode.mehodPost»
-                       «ENDIF»
-                       
-                       «IF schemaNode instanceof DataNodeContainer»
-                               «val children = (schemaNode as DataNodeContainer).childNodes.filter[it|it instanceof ContainerSchemaNode || it instanceof ListSchemaNode]»
-                               «IF !children.empty»
-                                       «FOR child : children»
-                                               «child.resource(config)»
-                                       «ENDFOR»
-                               «ENDIF»
-                       «ENDIF»
+                       «resourceBody(schemaNode, config)»
                </resource>
        '''
        
@@ -186,6 +157,24 @@ class WadlRestconfGenerator {
                return path.toString
        }
        
+       private def String resourceBody(DataSchemaNode schemaNode, boolean config) '''
+               «IF !pathListParams.nullOrEmpty»
+                       «resourceParams»
+               «ENDIF»
+               «schemaNode.methodGet»
+               «val children = (schemaNode as DataNodeContainer).childNodes.filter[it|it.listOrContainer]»
+               «IF config»
+                       «schemaNode.methodDelete»
+                       «schemaNode.mehodPut»
+                       «FOR child : children»
+                               «child.mehodPost»
+                       «ENDFOR»
+               «ENDIF»
+               «FOR child : children»
+                       «child.resource(config)»
+               «ENDFOR»
+       '''
+       
        private def resourceParams() '''
                «FOR pathParam : pathListParams»
                    «IF pathParam != null»
@@ -199,11 +188,7 @@ class WadlRestconfGenerator {
        private def methodGet(DataSchemaNode schemaNode) '''
                <method name="GET">
                        <response>
-                               <representation mediaType="application/xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="text/xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/json" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/yang.data+xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/yang.data+json" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
+                               «representation(schemaNode.QName.prefix, schemaNode.QName.localName)»
                        </response>
                </method>
        '''
@@ -211,11 +196,7 @@ class WadlRestconfGenerator {
        private def mehodPut(DataSchemaNode schemaNode) '''
                <method name="PUT">
                        <request>
-                               <representation mediaType="application/xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="text/xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/json" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/yang.data+xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/yang.data+json" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
+                               «representation(schemaNode.QName.prefix, schemaNode.QName.localName)»
                        </request>
                </method>
        '''
@@ -223,11 +204,7 @@ class WadlRestconfGenerator {
        private def mehodPost(DataSchemaNode schemaNode) '''
                <method name="POST">
                        <request>
-                               <representation mediaType="application/xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="text/xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/json" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/yang.data+xml" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
-                               <representation mediaType="application/yang.data+json" element="«schemaNode.QName.prefix»:«schemaNode.QName.localName»"/>
+                               «representation(schemaNode.QName.prefix, schemaNode.QName.localName)»
                        </request>
                </method>
        '''
@@ -236,23 +213,32 @@ class WadlRestconfGenerator {
                <method name="POST">
                        «IF input»
                        <request>
-                               <representation mediaType="application/xml" element="input"/>
-                               <representation mediaType="text/xml" element="input"/>
-                               <representation mediaType="application/json" element="input"/>
-                               <representation mediaType="application/yang.data+xml" element="input"/>
-                               <representation mediaType="application/yang.data+json" element="input"/>
+                               «representation(null, "input")»
                        </request>
                        «ENDIF»
                        «IF output»
                        <response>
-                               <representation mediaType="application/xml" element="output"/>
-                               <representation mediaType="text/xml" element="output"/>
-                               <representation mediaType="application/json" element="output"/>
-                               <representation mediaType="application/yang.data+xml" element="output"/>
-                               <representation mediaType="application/yang.data+json" element="output"/>
+                               «representation(null, "output")»
                        </response>
                        «ENDIF»
                </method>
        '''
 
+       private def methodDelete(DataSchemaNode schemaNode) '''
+               <method name="DELETE" />
+       '''
+
+       private def representation(String prefix, String name) '''
+               «val elementData = if (prefix.nullOrEmpty) name else prefix + ":" + name»
+               <representation mediaType="application/xml" element="«elementData»"/>
+               <representation mediaType="text/xml" element="«elementData»"/>
+               <representation mediaType="application/json" element="«elementData»"/>
+               <representation mediaType="application/yang.data+xml" element="«elementData»"/>
+               <representation mediaType="application/yang.data+json" element="«elementData»"/>
+       '''
+       
+       private def boolean isListOrContainer(DataSchemaNode schemaNode) {
+               return (schemaNode instanceof ListSchemaNode || schemaNode instanceof ContainerSchemaNode)
+       }
+
 }