e2a7fedacac33c1ff323b257bd06cfeaaf2d6d54
[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 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
20
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")} {
25
26     private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}();
27
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;
34
35     public static @{importedNames.get("yangModuleInfo")} getInstance() {
36         return INSTANCE;
37     }
38
39     @{classBody(module, MODULE_INFO_CLASS_NAME)}
40 }
41 }
42
43 @generateSubInfo(module: Module) = {
44 @for(submodule <- module.getSubmodules) {
45     private static final class @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info implements @{importedNames.get("yangModuleInfo")} {
46
47         private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info();
48
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;
55
56         public static @{importedNames.get("yangModuleInfo")} getInstance() {
57             return INSTANCE;
58         }
59
60         @{classBody(submodule, normalizeSpecificIdentifier(submodule.getName, CLASS) + "Info")}
61     }
62 }
63 }
64
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")}<>();
69         }
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());
75                 } else {
76                     set.add(@{normalizeFullPackageName(getRootPackageName(ctx.findModuleByName(moduleImport
77                     .getModuleName, moduleImport.getRevision)))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
78                 }
79             }
80         }
81         @if(!module.getSubmodules.isEmpty) {
82             @for(submodule <- module.getSubmodules) {
83                 set.add(@{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info.getInstance());
84             }
85         }
86         @if(module.getImports.isEmpty && module.getSubmodules.isEmpty) {
87             importedModules = @{importedNames.get("collections")}.emptySet();
88         } else {
89             importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
90         }
91
92         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
93         if (stream == null) {
94             throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
95         }
96         try {
97             stream.close();
98         } catch (@{importedNames.get("iOException")} e) {
99         // Resource leak, but there is nothing we can do
100         }
101     }
102
103     @@Override
104     public @{importedNames.get("string")} getName() {
105         return name;
106     }
107
108     @@Override
109     public @{importedNames.get("string")} getRevision() {
110         return revision;
111     }
112
113     @@Override
114     public @{importedNames.get("string")} getNamespace() {
115         return namespace;
116     }
117
118     @@Override
119     public @{importedNames.get("optional")}<@{importedNames.get("semVer")}> getSemanticVersion() {
120         return Optional.of(@{importedNames.get("semVer")}.valueOf(semanticVersion));
121     }
122
123     @@Override
124     public @{importedNames.get("schemaSourceRepresentation")} getModuleSourceRepresentation() {
125         //TODO implement
126         return null;
127     }
128
129     @@Override
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");
134         }
135         return stream;
136     }
137
138     @@Override
139     public @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> getImportedModules() {
140         return importedModules;
141     }
142
143     @@Override
144     public @{importedNames.get("string")} toString() {
145         @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
146         sb.append("[");
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);
153         sb.append("]");
154         return sb.toString();
155     }
156
157     @{generateSubInfo(module)}
158 }