Adjust to yangtools-2.0.0 changes
[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 = @if(module.getRevision.isPresent) { "@{module.getRevision.get.toString}" } else { null };
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 = @if(module.getRevision.isPresent) { "@{module.getRevision.get.toString}" } else { null };
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                 set.add(@{normalizeFullPackageName(getRootPackageName(ctx.findModule(moduleImport
73                 .getModuleName, moduleImport.getRevision).get))}.@{MODULE_INFO_CLASS_NAME}.getInstance());
74             }
75         }
76         @if(!module.getSubmodules.isEmpty) {
77             @for(submodule <- module.getSubmodules) {
78                 set.add(@{normalizeSpecificIdentifier(submodule.getName, CLASS)}Info.getInstance());
79             }
80         }
81         @if(module.getImports.isEmpty && module.getSubmodules.isEmpty) {
82             importedModules = @{importedNames.get("collections")}.emptySet();
83         } else {
84             importedModules = @{importedNames.get("immutableSet")}.copyOf(set);
85         }
86
87         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
88         if (stream == null) {
89             throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
90         }
91         try {
92             stream.close();
93         } catch (@{importedNames.get("iOException")} e) {
94         // Resource leak, but there is nothing we can do
95         }
96     }
97
98     @@Override
99     public @{importedNames.get("string")} getName() {
100         return name;
101     }
102
103     @@Override
104     public @{importedNames.get("string")} getRevision() {
105         return revision;
106     }
107
108     @@Override
109     public @{importedNames.get("string")} getNamespace() {
110         return namespace;
111     }
112
113     @@Override
114     public @{importedNames.get("optional")}<@{importedNames.get("semVer")}> getSemanticVersion() {
115         return Optional.of(@{importedNames.get("semVer")}.valueOf(semanticVersion));
116     }
117
118     @@Override
119     public @{importedNames.get("schemaSourceRepresentation")} getModuleSourceRepresentation() {
120         //TODO implement
121         return null;
122     }
123
124     @@Override
125     public @{importedNames.get("inputStream")} getModuleSourceStream() {
126         @{importedNames.get("inputStream")} stream = @{MODULE_INFO_CLASS_NAME}.class.getResourceAsStream(resourcePath);
127         if (stream == null) {
128             throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
129         }
130         return stream;
131     }
132
133     @@Override
134     public @{importedNames.get("set")}<@{importedNames.get("yangModuleInfo")}> getImportedModules() {
135         return importedModules;
136     }
137
138     @@Override
139     public @{importedNames.get("string")} toString() {
140         @{importedNames.get("stringBuilder")} sb = new @{importedNames.get("stringBuilder")}(this.getClass().getCanonicalName());
141         sb.append("[");
142         sb.append("name = " + name);
143         sb.append(", namespace = " + namespace);
144         sb.append(", semanticVersion = " + semanticVersion);
145         sb.append(", revision = " + revision);
146         sb.append(", resourcePath = " + resourcePath);
147         sb.append(", imports = " + importedModules);
148         sb.append("]");
149         return sb.toString();
150     }
151
152     @{generateSubInfo(module)}
153 }