Bug 1459-3 #4 - Re-organize mdsal-binding2-generator-util
[mdsal.git] / binding2 / mdsal-binding2-java-api-generator / src / main / twirl / org / opendaylight / mdsal / binding2 / 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.binding2.java.api.generator.util.TextTemplateUtil.getFormattedRevision
10 @import org.opendaylight.mdsal.binding2.java.api.generator.util.TextTemplateUtil.getSourcePath
11 @import org.opendaylight.mdsal.binding2.java.api.generator.renderers.YangModuleInfoTemplateRenderer.getSortedQName
12 @import org.opendaylight.mdsal.binding2.util.BindingMapping.getClassName
13 @import org.opendaylight.mdsal.binding2.util.BindingMapping.getRootPackageName
14 @import org.opendaylight.mdsal.binding2.util.BindingMapping.MODULE_INFO_CLASS_NAME
15 @import org.opendaylight.yangtools.yang.model.api.Module
16 @import org.opendaylight.yangtools.yang.model.api.SchemaContext
17
18 @(module: Module, ctx: SchemaContext, importedNames: Map[String, String])
19 @if(module != null && ctx != null) {
20 public final class @{MODULE_INFO_CLASS_NAME} implements @{importedNames.get("yangModuleInfo")} {
21
22     private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{MODULE_INFO_CLASS_NAME}();
23
24     private final @{importedNames.get("string")} name = "@{module.getName}";
25     private final @{importedNames.get("string")} namespace = "@{module.getNamespace.toString}";
26     private final @{importedNames.get("string")} revision = "@{getFormattedRevision(module.getRevision)}";
27     private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(module)}";
28     private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
29
30     public static @{importedNames.get("yangModuleInfo")} getInstance() {
31         return INSTANCE;
32     }
33
34     @{classBody(module, MODULE_INFO_CLASS_NAME)}
35 }
36 }
37
38 @generateSubInfo(module: Module) = {
39 @for(submodule <- module.getSubmodules) {
40     private static final class @{getClassName(submodule.getName)}Info implements @{importedNames.get("yangModuleInfo")} {
41
42         private static final @{importedNames.get("yangModuleInfo")} INSTANCE = new @{getClassName(submodule.getName)}Info();
43
44         private final @{importedNames.get("string")} name = "@{submodule.getName}";
45         private final @{importedNames.get("string")} namespace = "@{submodule.getNamespace.toString}";
46         private final @{importedNames.get("string")} revision = "@{getFormattedRevision(submodule.getRevision)}";
47         private final @{importedNames.get("string")} resourcePath = "@{getSourcePath(submodule)}";
48         private final @{importedNames.get("set")}<YangModuleInfo> importedModules;
49
50         public static @{importedNames.get("yangModuleInfo")} getInstance() {
51             return INSTANCE;
52         }
53
54         @{classBody(submodule, getClassName(submodule.getName + "Info"))}
55     }
56 }
57 }
58
59 @classBody(module: Module, className: String) = {
60     private @{className}() {
61         @if(!module.getImports.isEmpty || !module.getSubmodules.isEmpty) {
62             @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> set = new @{importedNames.get("hashSet")}<>();
63         }
64         @if(!module.getImports.isEmpty) {
65             @for(moduleImport <- module.getImports) {
66                 @if(moduleImport.getRevision == null) {
67                     set.add(@{getRootPackageName(getSortedQName(ctx.getModules, moduleImport.getModuleName))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
68                 } else {
69                     set.add(@{getRootPackageName(ctx.findModuleByName(moduleImport.getModuleName, moduleImport.getRevision))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
70                 }
71             }
72         }
73         @if(!module.getSubmodules.isEmpty) {
74             @for(submodule <- module.getSubmodules) {
75                 set.add(@{getClassName(submodule.getName)}Info.getInstance());
76             }
77         }
78         @if(!module.getImports.isEmpty && !module.getSubmodules.isEmpty) {
79             importedModules = @{importedNames.get("collections")}.emptySet();
80         } else {
81             importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
82         }
83
84         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
85         if (stream == null) {
86             throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
87         }
88         try {
89             stream.close();
90         } catch (@{importedNames.get("iOException")} e) {
91         // Resource leak, but there is nothing we can do
92         }
93     }
94
95     @@Override
96     public @{importedNames.get("string")} getName() {
97         return name;
98     }
99
100     @@Override
101     public @{importedNames.get("string")} getRevision() {
102         return revision;
103     }
104
105     @@Override
106     public @{importedNames.get("string")} getNamespace() {
107         return namespace;
108     }
109
110     @@Override
111     public @{importedNames.get("inputStream")} getModuleSourceStream() throws IOException {
112         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
113         if (stream == null) {
114             throw new @{importedNames.get("iOException")}("Resource " + resourcePath + " is missing");
115         }
116         return stream;
117     }
118
119     @@Override
120     public @{importedNames.get("set")}<@{importedNames.get("YangModuleInfo")}> getImportedModules() {
121         return importedModules;
122     }
123
124     @@Override
125     public @{importedNames.get("string")} toString() {
126         @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
127         sb.append("[");
128         sb.append("name = " + name);
129         sb.append(", namespace = " + namespace);
130         sb.append(", revision = " + revision);
131         sb.append(", resourcePath = " + resourcePath);
132         sb.append(", imports = " + importedModules);
133         sb.append("]");
134         return sb.toString();
135     }
136
137     @{generateSubInfo(module)}
138 }