@* * Copyright (c) 2016 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 *@ @import org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.getFormattedRevision @import org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.getSourcePath @import org.opendaylight.mdsal.binding2.java.api.generator.renderers.YangModuleInfoTemplateRenderer.getSortedQName @import org.opendaylight.mdsal.binding2.util.BindingMapping.getClassName @import org.opendaylight.mdsal.binding2.util.BindingMapping.getRootPackageName @import org.opendaylight.mdsal.binding2.util.BindingMapping.MODULE_INFO_CLASS_NAME @import org.opendaylight.yangtools.yang.model.api.Module @import org.opendaylight.yangtools.yang.model.api.SchemaContext @(module: Module, ctx: SchemaContext, importedNames: Map[String, String]) @if(module != null && ctx != null) { public final class @{MODULE_INFO_CLASS_NAME} implements @{importedNames.get("yangModuleInfo")} { private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}(); private final @{importedNames.get("string")} name = "@{module.getName}"; private final @{importedNames.get("string")} namespace = "@{module.getNamespace.toString}"; private final @{importedNames.get("string")} revision = "@{getFormattedRevision(module.getRevision)}"; private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(module)}"; private final @{importedNames.get("set")} importedModules; public static @{importedNames.get("yangModuleInfo")} getInstance() { return INSTANCE; } @{classBody(module, MODULE_INFO_CLASS_NAME)} } } @generateSubInfo(module: Module) = { @for(submodule <- module.getSubmodules) { private static final class @{getClassName(submodule.getName)}Info implements @{importedNames.get("yangModuleInfo")} { private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{getClassName(submodule.getName)}Info(); private final @{importedNames.get("string")} name = "@{submodule.getName}"; private final @{importedNames.get("string")} namespace = "@{submodule.getNamespace.toString}"; private final @{importedNames.get("string")} revision = "@{getFormattedRevision(submodule.getRevision)}"; private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(submodule)}"; private final @{importedNames.get("set")} importedModules; public static @{importedNames.get("yangModuleInfo")} getInstance() { return INSTANCE; } @{classBody(submodule, getClassName(submodule.getName + "Info"))} } } } @classBody(module: Module, className: String) = { private @{className}() { @if(!module.getImports.isEmpty || !module.getSubmodules.isEmpty) { @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> set = new @{importedNames.get("hashSet")}<>(); } @if(!module.getImports.isEmpty) { @for(moduleImport <- module.getImports) { @if(moduleImport.getRevision == null) { set.add(@{getRootPackageName(getSortedQName(ctx.getModules, moduleImport.getModuleName))}.@{MODULE_INFO_CLASS_NAME}.getInstance()); } else { set.add(@{getRootPackageName(ctx.findModuleByName(moduleImport.getModuleName, moduleImport.getRevision))}.@{MODULE_INFO_CLASS_NAME}.getInstance()); } } } @if(!module.getSubmodules.isEmpty) { @for(submodule <- module.getSubmodules) { set.add(@{getClassName(submodule.getName)}Info.getInstance()); } } @if(!module.getImports.isEmpty && !module.getSubmodules.isEmpty) { importedModules = @{importedNames.get("collections")}.emptySet(); } else { importedModules = @{importedNames.get("immutableSet")}.copyOf(set); } @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath); if (stream == null) { throw new IllegalStateException("Resource '" + resourcePath + "' is missing"); } try { stream.close(); } catch (@{importedNames.get("iOException")} e) { // Resource leak, but there is nothing we can do } } @@Override public @{importedNames.get("string")} getName() { return name; } @@Override public @{importedNames.get("string")} getRevision() { return revision; } @@Override public @{importedNames.get("string")} getNamespace() { return namespace; } @@Override public @{importedNames.get("inputStream")} getModuleSourceStream() throws IOException { @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath); if (stream == null) { throw new @{importedNames.get("iOException")}("Resource " + resourcePath + " is missing"); } return stream; } @@Override public @{importedNames.get("set")}<@{importedNames.get("YangModuleInfo")}> getImportedModules() { return importedModules; } @@Override public @{importedNames.get("string")} toString() { @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName()); sb.append("["); sb.append("name = " + name); sb.append(", namespace = " + namespace); sb.append(", revision = " + revision); sb.append(", resourcePath = " + resourcePath); sb.append(", imports = " + importedModules); sb.append("]"); return sb.toString(); } @{generateSubInfo(module)} }