/* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.wadl.generator import java.io.BufferedWriter import java.io.File import java.io.OutputStreamWriter import java.net.URI 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.listOrContainer] 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.mehodPost» «ENDFOR» «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) ''' «resourceBody(schemaNode, config)» ''' private def String resource(DataSchemaNode schemaNode, boolean config) ''' «resourceBody(schemaNode, config)» ''' 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) path.append('{') path.append(listKey.localName) path.append('}') } } 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» «val type = pathParam.type.QName.localName» «ENDIF» «ENDFOR» ''' private def methodGet(DataSchemaNode schemaNode) ''' «representation(schemaNode.QName.namespace, schemaNode.QName.localName)» ''' private def mehodPut(DataSchemaNode schemaNode) ''' «representation(schemaNode.QName.namespace, schemaNode.QName.localName)» ''' private def mehodPost(DataSchemaNode schemaNode) ''' «representation(schemaNode.QName.namespace, schemaNode.QName.localName)» ''' private def methodPostRpc(boolean input, boolean output) ''' «IF input» «representation(null, "input")» «ENDIF» «IF output» «representation(null, "output")» «ENDIF» ''' private def methodDelete(DataSchemaNode schemaNode) ''' ''' private def representation(URI prefix, String name) ''' «val elementData = name» ''' private def boolean isListOrContainer(DataSchemaNode schemaNode) { return (schemaNode instanceof ListSchemaNode || schemaNode instanceof ContainerSchemaNode) } }