package org.opendaylight.yangtools.yang.wadl.generator import java.io.BufferedWriter import java.io.File import java.io.OutputStreamWriter import java.util.ArrayList import java.util.HashSet import java.util.List import java.util.Set import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode import org.opendaylight.yangtools.yang.model.api.DataNodeContainer import org.opendaylight.yangtools.yang.model.api.DataSchemaNode import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode import org.opendaylight.yangtools.yang.model.api.ListSchemaNode import org.opendaylight.yangtools.yang.model.api.Module import org.opendaylight.yangtools.yang.model.api.SchemaContext import org.sonatype.plexus.build.incremental.BuildContext import org.sonatype.plexus.build.incremental.DefaultBuildContext class WadlRestconfGenerator { File path static val BuildContext CTX = new DefaultBuildContext(); var SchemaContext context; var List configData; var List operationalData; var Module module; var List pathListParams; val PATH_DELIMETER = "/" new(File targetPath) { if (!targetPath.exists) targetPath.mkdirs path = targetPath } def generate(SchemaContext context, Set modules) { val result = new HashSet; this.context = context for (module : modules) { val dataContainers = module.childNodes.filter[it|it instanceof ContainerSchemaNode || it instanceof ListSchemaNode] if (!dataContainers.empty || !module.rpcs.nullOrEmpty) { configData = new ArrayList operationalData = new ArrayList for (data : dataContainers) { if (data.configuration) { configData.add(data) } else { operationalData.add(data) } } this.module = module val destination = new File(path, '''«module.name».wadl''') val fw = new OutputStreamWriter(CTX.newFileOutputStream(destination)) val bw = new BufferedWriter(fw) bw.append(application); bw.close(); fw.close(); result.add(destination) } } return result } private def application() ''' «grammars» «resources» ''' private def importsAsNamespaces(Module module) ''' «FOR imprt : module.imports» xmlns:«imprt.prefix»="«context.findModuleByName(imprt.moduleName, imprt.revision).namespace»" «ENDFOR» ''' private def grammars() ''' «FOR imprt : module.imports» «ENDFOR» ''' private def resources() ''' «resourceOperational» «resourceConfig» «resourceOperations» ''' private def resourceOperational() ''' «IF !operationalData.nullOrEmpty» «FOR schemaNode : operationalData» «schemaNode.firstResource(false)» «ENDFOR» «ENDIF» ''' private def resourceConfig() ''' «IF !configData.nullOrEmpty» «FOR schemaNode : configData» «schemaNode.firstResource(true)» «ENDFOR» «ENDIF» ''' private def resourceOperations() ''' «IF !module.rpcs.nullOrEmpty» «FOR rpc : module.rpcs» «methodPostRpc(rpc.input != null, rpc.output !== null)» «ENDFOR» «ENDIF» ''' private def String firstResource(DataSchemaNode schemaNode, boolean config) ''' «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» ''' private def String resource(DataSchemaNode schemaNode, boolean config) ''' «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» ''' private def String createPath(DataSchemaNode schemaNode) { pathListParams = new ArrayList var StringBuilder path = new StringBuilder path.append(schemaNode.QName.localName) if (schemaNode instanceof ListSchemaNode) { val listKeys = (schemaNode as ListSchemaNode).keyDefinition for (listKey : listKeys) { pathListParams.add((schemaNode as DataNodeContainer).getDataChildByName(listKey) as LeafSchemaNode) path.append(PATH_DELIMETER + "{" + listKey.localName + "}") } } return path.toString } private def resourceParams() ''' «FOR pathParam : pathListParams» «IF pathParam != null» «val prefix = pathParam.type.QName.prefix» «val type = if (prefix.nullOrEmpty) pathParam.type.QName.localName else prefix + ":" + pathParam.type.QName.localName» «ENDIF» «ENDFOR» ''' private def methodGet(DataSchemaNode schemaNode) ''' ''' private def mehodPut(DataSchemaNode schemaNode) ''' ''' private def mehodPost(DataSchemaNode schemaNode) ''' ''' private def methodPostRpc(boolean input, boolean output) ''' «IF input» «ENDIF» «IF output» «ENDIF» ''' }