2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 @import org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.getFormattedRevision
10 @import org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.getSourcePath
11 @import org.opendaylight.mdsal.binding2.java.api.generator.renderers.YangModuleInfoTemplateRenderer.getSortedQName
12 @import org.opendaylight.mdsal.binding2.util.BindingMapping.getClassName
13 @import org.opendaylight.mdsal.binding2.util.BindingMapping.getRootPackageName
14 @import org.opendaylight.mdsal.binding2.util.BindingMapping.MODULE_INFO_CLASS_NAME
15 @import org.opendaylight.yangtools.yang.model.api.Module
16 @import org.opendaylight.yangtools.yang.model.api.SchemaContext
18 @(module: Module, ctx: SchemaContext, importedNames: Map[String, String])
19 @if(module != null && ctx != null) {
20 public final class @{MODULE_INFO_CLASS_NAME} implements @{importedNames.get("yangModuleInfo")} {
22 private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}();
24 private final @{importedNames.get("string")} name = "@{module.getName}";
25 private final @{importedNames.get("string")} namespace = "@{module.getNamespace.toString}";
26 private final @{importedNames.get("string")} revision = "@{getFormattedRevision(module.getRevision)}";
27 private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(module)}";
28 private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
30 public static @{importedNames.get("yangModuleInfo")} getInstance() {
34 @{classBody(module, MODULE_INFO_CLASS_NAME)}
38 @generateSubInfo(module: Module) = {
39 @for(submodule <- module.getSubmodules) {
40 private static final class @{getClassName(submodule.getName)}Info implements @{importedNames.get("yangModuleInfo")} {
42 private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{getClassName(submodule.getName)}Info();
44 private final @{importedNames.get("string")} name = "@{submodule.getName}";
45 private final @{importedNames.get("string")} namespace = "@{submodule.getNamespace.toString}";
46 private final @{importedNames.get("string")} revision = "@{getFormattedRevision(submodule.getRevision)}";
47 private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(submodule)}";
48 private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
50 public static @{importedNames.get("yangModuleInfo")} getInstance() {
54 @{classBody(submodule, getClassName(submodule.getName + "Info"))}
59 @classBody(module: Module, className: String) = {
60 private @{className}() {
61 @if(!module.getImports.isEmpty || !module.getSubmodules.isEmpty) {
62 @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> set = new @{importedNames.get("hashSet")}<>();
64 @if(!module.getImports.isEmpty) {
65 @for(moduleImport <- module.getImports) {
66 @if(moduleImport.getRevision == null) {
67 set.add(@{getRootPackageName(getSortedQName(ctx.getModules, moduleImport.getModuleName))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
69 set.add(@{getRootPackageName(ctx.findModuleByName(moduleImport.getModuleName, moduleImport.getRevision))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
73 @if(!module.getSubmodules.isEmpty) {
74 @for(submodule <- module.getSubmodules) {
75 set.add(@{getClassName(submodule.getName)}Info.getInstance());
78 @if(!module.getImports.isEmpty && !module.getSubmodules.isEmpty) {
79 importedModules = @{importedNames.get("collections")}.emptySet();
81 importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
84 @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
86 throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
90 } catch (@{importedNames.get("iOException")} e) {
91 // Resource leak, but there is nothing we can do
96 public @{importedNames.get("string")} getName() {
101 public @{importedNames.get("string")} getRevision() {
106 public @{importedNames.get("string")} getNamespace() {
111 public @{importedNames.get("inputStream")} getModuleSourceStream() throws IOException {
112 @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
113 if (stream == null) {
114 throw new @{importedNames.get("iOException")}("Resource " + resourcePath + " is missing");
120 public @{importedNames.get("set")}<@{importedNames.get("YangModuleInfo")}> getImportedModules() {
121 return importedModules;
125 public @{importedNames.get("string")} toString() {
126 @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
128 sb.append("name = " + name);
129 sb.append(", namespace = " + namespace);
130 sb.append(", revision = " + revision);
131 sb.append(", resourcePath = " + resourcePath);
132 sb.append(", imports = " + importedModules);
134 return sb.toString();
137 @{generateSubInfo(module)}