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.binding.javav2.generator.util.JavaIdentifier.CLASS
10 @import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer.normalizeSpecificIdentifier
11 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getFormattedRevision
12 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getSourcePath
13 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.renderers.YangModuleInfoTemplateRenderer.getSortedQName
14 @import org.opendaylight.mdsal.binding.javav2.util.BindingMapping.getRootPackageName
15 @import org.opendaylight.mdsal.binding.javav2.util.BindingMapping.MODULE_INFO_CLASS_NAME
16 @import org.opendaylight.yangtools.yang.model.api.Module
17 @import org.opendaylight.yangtools.yang.model.api.SchemaContext
19 @(module: Module, ctx: SchemaContext, importedNames: Map[String, String])
20 @if(module != null && ctx != null) {
21 public final class @{MODULE_INFO_CLASS_NAME} implements @{importedNames.get("yangModuleInfo")} {
23 private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}();
25 private final @{importedNames.get("string")} name = "@{module.getName}";
26 private final @{importedNames.get("string")} namespace = "@{module.getNamespace.toString}";
27 private final @{importedNames.get("string")} revision = "@{getFormattedRevision(module.getRevision)}";
28 private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(module)}";
29 private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
31 public static @{importedNames.get("yangModuleInfo")} getInstance() {
35 @{classBody(module, MODULE_INFO_CLASS_NAME)}
39 @generateSubInfo(module: Module) = {
40 @for(submodule <- module.getSubmodules) {
41 private static final class @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info implements @{importedNames.get("yangModuleInfo")} {
43 private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info();
45 private final @{importedNames.get("string")} name = "@{submodule.getName}";
46 private final @{importedNames.get("string")} namespace = "@{submodule.getNamespace.toString}";
47 private final @{importedNames.get("string")} revision = "@{getFormattedRevision(submodule.getRevision)}";
48 private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(submodule)}";
49 private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
51 public static @{importedNames.get("yangModuleInfo")} getInstance() {
55 @{classBody(submodule, normalizeSpecificIdentifier(submodule.getName + "Info", CLASS))}
60 @classBody(module: Module, className: String) = {
61 private @{className}() {
62 @if(!module.getImports.isEmpty || !module.getSubmodules.isEmpty) {
63 @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> set = new @{importedNames.get("hashSet")}<>();
65 @if(!module.getImports.isEmpty) {
66 @for(moduleImport <- module.getImports) {
67 @if(moduleImport.getRevision == null) {
68 set.add(@{getRootPackageName(getSortedQName(ctx.getModules, moduleImport.getModuleName))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
70 set.add(@{getRootPackageName(ctx.findModuleByName(moduleImport.getModuleName, moduleImport.getRevision))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
74 @if(!module.getSubmodules.isEmpty) {
75 @for(submodule <- module.getSubmodules) {
76 set.add(@{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info.getInstance());
79 @if(!module.getImports.isEmpty && !module.getSubmodules.isEmpty) {
80 importedModules = @{importedNames.get("collections")}.emptySet();
82 importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
85 @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
87 throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
91 } catch (@{importedNames.get("iOException")} e) {
92 // Resource leak, but there is nothing we can do
97 public @{importedNames.get("string")} getName() {
102 public @{importedNames.get("string")} getRevision() {
107 public @{importedNames.get("string")} getNamespace() {
112 public @{importedNames.get("inputStream")} getModuleSourceStream() throws IOException {
113 @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
114 if (stream == null) {
115 throw new @{importedNames.get("iOException")}("Resource " + resourcePath + " is missing");
121 public @{importedNames.get("set")}<@{importedNames.get("YangModuleInfo")}> getImportedModules() {
122 return importedModules;
126 public @{importedNames.get("string")} toString() {
127 @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
129 sb.append("name = " + name);
130 sb.append(", namespace = " + namespace);
131 sb.append(", revision = " + revision);
132 sb.append(", resourcePath = " + resourcePath);
133 sb.append(", imports = " + importedModules);
135 return sb.toString();
138 @{generateSubInfo(module)}