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 java.util.function.Function;
10 @import java.util.Optional
11 @import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier.CLASS
12 @import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer.normalizeSpecificIdentifier
13 @import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer.normalizeFullPackageName
14 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getSourcePath
15 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.renderers.YangModuleInfoTemplateRenderer.getSortedQName
16 @import org.opendaylight.mdsal.binding.javav2.util.BindingMapping.getRootPackageName
17 @import org.opendaylight.mdsal.binding.javav2.util.BindingMapping.MODULE_INFO_CLASS_NAME
18 @import org.opendaylight.yangtools.yang.model.api.Module
19 @import org.opendaylight.yangtools.yang.model.api.SchemaContext
21 @(module: Module, ctx: SchemaContext, importedNames: Map[String, String],
22 moduleFilePathResolver: Function[Module, Optional[String]])
23 @if(module != null && ctx != null) {
24 public final class @{MODULE_INFO_CLASS_NAME} implements @{importedNames.get("yangModuleInfo")} {
26 private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}();
28 private final @{importedNames.get("string")} name = "@{module.getName}";
29 private final @{importedNames.get("string")} namespace = "@{module.getNamespace.toString}";
30 private final @{importedNames.get("string")} semanticVersion = "@{module.getSemanticVersion}";
31 private final @{importedNames.get("string")} revision = "@{module.getQNameModule.getFormattedRevision}";
32 private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(module, moduleFilePathResolver)}";
33 private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
35 public static @{importedNames.get("yangModuleInfo")} getInstance() {
39 @{classBody(module, MODULE_INFO_CLASS_NAME)}
43 @generateSubInfo(module: Module) = {
44 @for(submodule <- module.getSubmodules) {
45 private static final class @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info implements @{importedNames.get("yangModuleInfo")} {
47 private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info();
49 private final @{importedNames.get("string")} name = "@{submodule.getName}";
50 private final @{importedNames.get("string")} semanticVersion = "@{submodule.getSemanticVersion}";
51 private final @{importedNames.get("string")} namespace = "@{submodule.getNamespace.toString}";
52 private final @{importedNames.get("string")} revision = "@{submodule.getQNameModule.getFormattedRevision}";
53 private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(submodule, moduleFilePathResolver)}";
54 private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
56 public static @{importedNames.get("yangModuleInfo")} getInstance() {
60 @{classBody(submodule, normalizeSpecificIdentifier(submodule.getName, CLASS) + "Info")}
65 @classBody(module: Module, className: String) = {
66 private @{className}() {
67 @if(!module.getImports.isEmpty || !module.getSubmodules.isEmpty) {
68 @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> set = new @{importedNames.get("hashSet")}<>();
70 @if(!module.getImports.isEmpty) {
71 @for(moduleImport <- module.getImports) {
72 @if(moduleImport.getRevision == null) {
73 set.add(@{normalizeFullPackageName(getRootPackageName(getSortedQName(ctx.getModules, moduleImport
74 .getModuleName)))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
76 set.add(@{normalizeFullPackageName(getRootPackageName(ctx.findModuleByName(moduleImport
77 .getModuleName, moduleImport.getRevision)))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
81 @if(!module.getSubmodules.isEmpty) {
82 @for(submodule <- module.getSubmodules) {
83 set.add(@{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info.getInstance());
86 @if(module.getImports.isEmpty && module.getSubmodules.isEmpty) {
87 importedModules = @{importedNames.get("collections")}.emptySet();
89 importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
92 @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
94 throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
98 } catch (@{importedNames.get("iOException")} e) {
99 // Resource leak, but there is nothing we can do
104 public @{importedNames.get("string")} getName() {
109 public @{importedNames.get("string")} getRevision() {
114 public @{importedNames.get("string")} getNamespace() {
119 public @{importedNames.get("optional")}<@{importedNames.get("semVer")}> getSemanticVersion() {
120 return Optional.of(@{importedNames.get("semVer")}.valueOf(semanticVersion));
124 public @{importedNames.get("schemaSourceRepresentation")} getModuleSourceRepresentation() {
130 public @{importedNames.get("inputStream")} getModuleSourceStream() {
131 @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
132 if (stream == null) {
133 throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
139 public @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> getImportedModules() {
140 return importedModules;
144 public @{importedNames.get("string")} toString() {
145 @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
147 sb.append("name = " + name);
148 sb.append(", namespace = " + namespace);
149 sb.append(", semanticVersion = " + semanticVersion);
150 sb.append(", revision = " + revision);
151 sb.append(", resourcePath = " + resourcePath);
152 sb.append(", imports = " + importedModules);
154 return sb.toString();
157 @{generateSubInfo(module)}