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.generator.util.JavaIdentifierNormalizer.normalizeFullPackageName
12 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getFormattedRevision
13 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getSourcePath
14 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.renderers.YangModuleInfoTemplateRenderer.getSortedQName
15 @import org.opendaylight.mdsal.binding.javav2.util.BindingMapping.getRootPackageName
16 @import org.opendaylight.mdsal.binding.javav2.util.BindingMapping.MODULE_INFO_CLASS_NAME
17 @import org.opendaylight.yangtools.yang.model.api.Module
18 @import org.opendaylight.yangtools.yang.model.api.SchemaContext
20 @(module: Module, ctx: SchemaContext, importedNames: Map[String, String])
21 @if(module != null && ctx != null) {
22 public final class @{MODULE_INFO_CLASS_NAME} implements @{importedNames.get("yangModuleInfo")} {
24 private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}();
26 private final @{importedNames.get("string")} name = "@{module.getName}";
27 private final @{importedNames.get("string")} namespace = "@{module.getNamespace.toString}";
28 private final @{importedNames.get("string")} semanticVersion = "@{module.getSemanticVersion}";
29 private final @{importedNames.get("string")} revision = "@{getFormattedRevision(module.getRevision)}";
30 private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(module)}";
31 private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
33 public static @{importedNames.get("yangModuleInfo")} getInstance() {
37 @{classBody(module, MODULE_INFO_CLASS_NAME)}
41 @generateSubInfo(module: Module) = {
42 @for(submodule <- module.getSubmodules) {
43 private static final class @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info implements @{importedNames.get("yangModuleInfo")} {
45 private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info();
47 private final @{importedNames.get("string")} name = "@{submodule.getName}";
48 private final @{importedNames.get("string")} semanticVersion = "@{submodule.getSemanticVersion}";
49 private final @{importedNames.get("string")} namespace = "@{submodule.getNamespace.toString}";
50 private final @{importedNames.get("string")} revision = "@{getFormattedRevision(submodule.getRevision)}";
51 private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(submodule)}";
52 private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
54 public static @{importedNames.get("yangModuleInfo")} getInstance() {
58 @{classBody(submodule, normalizeSpecificIdentifier(submodule.getName + "Info", CLASS))}
63 @classBody(module: Module, className: String) = {
64 private @{className}() {
65 @if(!module.getImports.isEmpty || !module.getSubmodules.isEmpty) {
66 @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> set = new @{importedNames.get("hashSet")}<>();
68 @if(!module.getImports.isEmpty) {
69 @for(moduleImport <- module.getImports) {
70 @if(moduleImport.getRevision == null) {
71 set.add(@{normalizeFullPackageName(getRootPackageName(getSortedQName(ctx.getModules, moduleImport
72 .getModuleName)))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
74 set.add(@{normalizeFullPackageName(getRootPackageName(ctx.findModuleByName(moduleImport
75 .getModuleName, moduleImport.getRevision)))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
79 @if(!module.getSubmodules.isEmpty) {
80 @for(submodule <- module.getSubmodules) {
81 set.add(@{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info.getInstance());
84 @if(module.getImports.isEmpty && module.getSubmodules.isEmpty) {
85 importedModules = @{importedNames.get("collections")}.emptySet();
87 importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
90 @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
92 throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
96 } catch (@{importedNames.get("iOException")} e) {
97 // Resource leak, but there is nothing we can do
102 public @{importedNames.get("string")} getName() {
107 public @{importedNames.get("string")} getRevision() {
112 public @{importedNames.get("string")} getNamespace() {
117 public @{importedNames.get("optional")}<@{importedNames.get("semVer")}> getSemanticVersion() {
118 return Optional.of(@{importedNames.get("semVer")}.valueOf(semanticVersion));
122 public @{importedNames.get("schemaSourceRepresentation")} getModuleSourceRepresentation() {
128 public @{importedNames.get("inputStream")} getModuleSourceStream() {
129 @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
130 if (stream == null) {
131 throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
137 public @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> getImportedModules() {
138 return importedModules;
142 public @{importedNames.get("string")} toString() {
143 @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
145 sb.append("name = " + name);
146 sb.append(", namespace = " + namespace);
147 sb.append(", semanticVersion = " + semanticVersion);
148 sb.append(", revision = " + revision);
149 sb.append(", resourcePath = " + resourcePath);
150 sb.append(", imports = " + importedModules);
152 return sb.toString();
155 @{generateSubInfo(module)}