4e752537bae3aafbccaec601a807416a106d1d5c
[mdsal.git] / binding2 / mdsal-binding2-java-api-generator / src / main / twirl / org / opendaylight / mdsal / binding / javav2 / java / api / generator / yangModuleInfoTemplate.scala.txt
1 @*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  *@
8
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.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
18
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")} {
22
23     private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}();
24
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")} semanticVersion = "@{module.getSemanticVersion}";
28     private final @{importedNames.get("string")} revision = "@{module.getQNameModule.getFormattedRevision}";
29     private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(module)}";
30     private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
31
32     public static @{importedNames.get("yangModuleInfo")} getInstance() {
33         return INSTANCE;
34     }
35
36     @{classBody(module, MODULE_INFO_CLASS_NAME)}
37 }
38 }
39
40 @generateSubInfo(module: Module) = {
41 @for(submodule <- module.getSubmodules) {
42     private static final class @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info implements @{importedNames.get("yangModuleInfo")} {
43
44         private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info();
45
46         private final @{importedNames.get("string")} name = "@{submodule.getName}";
47         private final @{importedNames.get("string")} semanticVersion = "@{submodule.getSemanticVersion}";
48         private final @{importedNames.get("string")} namespace = "@{submodule.getNamespace.toString}";
49         private final @{importedNames.get("string")} revision = "@{submodule.getQNameModule.getFormattedRevision}";
50         private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(submodule)}";
51         private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
52
53         public static @{importedNames.get("yangModuleInfo")} getInstance() {
54             return INSTANCE;
55         }
56
57         @{classBody(submodule, normalizeSpecificIdentifier(submodule.getName, CLASS) + "Info")}
58     }
59 }
60 }
61
62 @classBody(module: Module, className: String) = {
63     private @{className}() {
64         @if(!module.getImports.isEmpty || !module.getSubmodules.isEmpty) {
65             @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> set = new @{importedNames.get("hashSet")}<>();
66         }
67         @if(!module.getImports.isEmpty) {
68             @for(moduleImport <- module.getImports) {
69                 @if(moduleImport.getRevision == null) {
70                     set.add(@{normalizeFullPackageName(getRootPackageName(getSortedQName(ctx.getModules, moduleImport
71                     .getModuleName)))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
72                 } else {
73                     set.add(@{normalizeFullPackageName(getRootPackageName(ctx.findModuleByName(moduleImport
74                     .getModuleName, moduleImport.getRevision)))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
75                 }
76             }
77         }
78         @if(!module.getSubmodules.isEmpty) {
79             @for(submodule <- module.getSubmodules) {
80                 set.add(@{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info.getInstance());
81             }
82         }
83         @if(module.getImports.isEmpty && module.getSubmodules.isEmpty) {
84             importedModules = @{importedNames.get("collections")}.emptySet();
85         } else {
86             importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
87         }
88
89         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
90         if (stream == null) {
91             throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
92         }
93         try {
94             stream.close();
95         } catch (@{importedNames.get("iOException")} e) {
96         // Resource leak, but there is nothing we can do
97         }
98     }
99
100     @@Override
101     public @{importedNames.get("string")} getName() {
102         return name;
103     }
104
105     @@Override
106     public @{importedNames.get("string")} getRevision() {
107         return revision;
108     }
109
110     @@Override
111     public @{importedNames.get("string")} getNamespace() {
112         return namespace;
113     }
114
115     @@Override
116     public @{importedNames.get("optional")}<@{importedNames.get("semVer")}> getSemanticVersion() {
117         return Optional.of(@{importedNames.get("semVer")}.valueOf(semanticVersion));
118     }
119
120     @@Override
121     public @{importedNames.get("schemaSourceRepresentation")} getModuleSourceRepresentation() {
122         //TODO implement
123         return null;
124     }
125
126     @@Override
127     public @{importedNames.get("inputStream")} getModuleSourceStream() {
128         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
129         if (stream == null) {
130             throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
131         }
132         return stream;
133     }
134
135     @@Override
136     public @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> getImportedModules() {
137         return importedModules;
138     }
139
140     @@Override
141     public @{importedNames.get("string")} toString() {
142         @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
143         sb.append("[");
144         sb.append("name = " + name);
145         sb.append(", namespace = " + namespace);
146         sb.append(", semanticVersion = " + semanticVersion);
147         sb.append(", revision = " + revision);
148         sb.append(", resourcePath = " + resourcePath);
149         sb.append(", imports = " + importedModules);
150         sb.append("]");
151         return sb.toString();
152     }
153
154     @{generateSubInfo(module)}
155 }