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