*/
package org.opendaylight.mdsal.binding.java.api.generator
-import static org.opendaylight.yangtools.yang.binding.BindingMapping.getClassName
-import static org.opendaylight.yangtools.yang.binding.BindingMapping.getRootPackageName
-import static org.opendaylight.yangtools.yang.binding.BindingMapping.MODEL_BINDING_PROVIDER_CLASS_NAME
-import static org.opendaylight.yangtools.yang.binding.BindingMapping.MODULE_INFO_CLASS_NAME
+import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getClassName
+import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getRootPackageName
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.MODEL_BINDING_PROVIDER_CLASS_NAME
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.MODULE_INFO_CLASS_NAME
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.MODULE_INFO_QNAMEOF_METHOD_NAME
import com.google.common.base.Preconditions
import com.google.common.collect.ImmutableSet
-import java.io.InputStream
-import java.io.IOException
-import java.text.DateFormat
-import java.text.SimpleDateFormat
-import java.util.Optional
-import java.util.Collections
-import java.util.Date
+import java.util.Comparator
import java.util.HashSet
-import java.util.LinkedHashMap
-import java.util.Map
+import java.util.Optional
import java.util.Set
import java.util.TreeMap
import java.util.function.Function
-import org.opendaylight.mdsal.binding.generator.util.Types
-import org.opendaylight.mdsal.binding.model.api.ParameterizedType
-import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.api.WildcardType
+import org.eclipse.xtend.lib.annotations.Accessors
import org.opendaylight.yangtools.yang.binding.YangModuleInfo
-import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider
+import org.opendaylight.yangtools.yang.common.Revision
import org.opendaylight.yangtools.yang.model.api.Module
import org.opendaylight.yangtools.yang.model.api.SchemaContext
+/**
+ * Template for {@link YangModuleInfo} implementation for a particular module. Aside from fulfilling that contract,
+ * this class provides a static {@code createQName(String)} method, which is used by co-generated code to initialize
+ * QNAME constants.
+ */
class YangModuleInfoTemplate {
+ static val Comparator<Optional<Revision>> REVISION_COMPARATOR =
+ [ Optional<Revision> first, Optional<Revision> second | Revision.compare(first, second) ]
+
+ // These are always imported. Note we need to import even java.lang members, as there can be conflicting definitions
+ // in our package
+ static val CORE_IMPORT_STR = '''
+ import com.google.common.collect.ImmutableSet;
+ import java.lang.Override;
+ import java.lang.String;
+ import org.eclipse.jdt.annotation.NonNull;
+ import org.opendaylight.yangtools.yang.binding.ResourceYangModuleInfo;
+ import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+ import org.opendaylight.yangtools.yang.common.QName;
+ '''
+ static val EXT_IMPORT_STR = '''
+ import com.google.common.collect.ImmutableSet;
+ import java.lang.Override;
+ import java.lang.String;
+ import java.util.HashSet;
+ import java.util.Set;
+ import org.eclipse.jdt.annotation.NonNull;
+ import org.opendaylight.yangtools.yang.binding.ResourceYangModuleInfo;
+ import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+ import org.opendaylight.yangtools.yang.common.QName;
+ '''
val Module module
val SchemaContext ctx
- val Map<String, String> importMap = new LinkedHashMap()
val Function<Module, Optional<String>> moduleFilePathResolver
- @Property
- val String packageName;
+ var importedTypes = CORE_IMPORT_STR
- @Property
- val String modelBindingProviderName;
+ @Accessors
+ val String packageName
+
+ @Accessors
+ val String modelBindingProviderName
new(Module module, SchemaContext ctx, Function<Module, Optional<String>> moduleFilePathResolver) {
- Preconditions.checkArgument(module !== null, "Module must not be null.");
+ Preconditions.checkArgument(module !== null, "Module must not be null.")
this.module = module
this.ctx = ctx
this.moduleFilePathResolver = moduleFilePathResolver
- _packageName = getRootPackageName(module.getQNameModule());
- _modelBindingProviderName = '''«packageName».«MODEL_BINDING_PROVIDER_CLASS_NAME»''';
+ packageName = module.QNameModule.rootPackageName;
+ modelBindingProviderName = '''«packageName».«MODEL_BINDING_PROVIDER_CLASS_NAME»'''
}
def String generate() {
- val body = '''
- public final class «MODULE_INFO_CLASS_NAME» implements «YangModuleInfo.importedName» {
-
- private static final «YangModuleInfo.importedName» INSTANCE = new «MODULE_INFO_CLASS_NAME»();
+ val Set<Module> submodules = new HashSet
+ collectSubmodules(submodules, module)
- private final «String.importedName» name = "«module.name»";
- private final «String.importedName» namespace = "«module.namespace.toString»";
- «val DateFormat df = new SimpleDateFormat("yyyy-MM-dd")»
- private final «String.importedName» revision = "«df.format(module.revision)»";
- private final «String.importedName» resourcePath = "«sourcePath(module)»";
+ val body = '''
+ public final class «MODULE_INFO_CLASS_NAME» extends ResourceYangModuleInfo {
+ «val rev = module.revision»
+ private static final @NonNull QName NAME = QName.create("«module.namespace.toString»", «IF rev.present»"«rev.get.toString»", «ENDIF»"«module.name»").intern();
+ private static final @NonNull YangModuleInfo INSTANCE = new «MODULE_INFO_CLASS_NAME»();
- private final «Set.importedName»<YangModuleInfo> importedModules;
+ private final @NonNull ImmutableSet<YangModuleInfo> importedModules;
- public static «YangModuleInfo.importedName» getInstance() {
+ public static @NonNull YangModuleInfo getInstance() {
return INSTANCE;
}
- «classBody(module, MODULE_INFO_CLASS_NAME)»
+ public static @NonNull QName «MODULE_INFO_QNAMEOF_METHOD_NAME»(final String localName) {
+ return QName.create(NAME, localName).intern();
+ }
+
+ «classBody(module, MODULE_INFO_CLASS_NAME, submodules)»
}
'''
return '''
- package «packageName» ;
- «imports»
+ package «packageName»;
+
+ «importedTypes»
+
«body»
'''.toString
}
- def String generateModelProvider() {
- '''
- package «packageName»;
+ def String generateModelProvider() '''
+ package «packageName»;
- public final class «MODEL_BINDING_PROVIDER_CLASS_NAME» implements «YangModelBindingProvider.name» {
+ import java.lang.Override;
+ import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
+ import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
- public «YangModuleInfo.name» getModuleInfo() {
- return «MODULE_INFO_CLASS_NAME».getInstance();
- }
+ public final class «MODEL_BINDING_PROVIDER_CLASS_NAME» implements YangModelBindingProvider {
+ @Override
+ public YangModuleInfo getModuleInfo() {
+ return «MODULE_INFO_CLASS_NAME».getInstance();
}
- '''
+ }
+ '''
+ private static def void collectSubmodules(Set<Module> dest, Module module) {
+ for (Module submodule : module.submodules) {
+ if (dest.add(submodule)) {
+ collectSubmodules(dest, submodule)
+ }
+ }
}
- private def CharSequence classBody(Module m, String className) '''
+ private def CharSequence classBody(Module m, String className, Set<Module> submodules) '''
private «className»() {
- «IF !m.imports.empty || !m.submodules.empty»
- «Set.importedName»<«YangModuleInfo.importedName»> set = new «HashSet.importedName»<>();
+ «IF !m.imports.empty || !submodules.empty»
+ «extendImports»
+ Set<YangModuleInfo> set = new HashSet<>();
«ENDIF»
«IF !m.imports.empty»
«FOR imp : m.imports»
«val name = imp.moduleName»
«val rev = imp.revision»
- «IF rev === null»
+ «IF !rev.present»
«val Set<Module> modules = ctx.modules»
- «val TreeMap<Date, Module> sorted = new TreeMap()»
+ «val TreeMap<Optional<Revision>, Module> sorted = new TreeMap(REVISION_COMPARATOR)»
«FOR module : modules»
«IF module.name.equals(name)»
«sorted.put(module.revision, module)»
«ENDIF»
«ENDFOR»
- set.add(«getRootPackageName(sorted.lastEntry().value.QNameModule)».«MODULE_INFO_CLASS_NAME».getInstance());
+ set.add(«sorted.lastEntry().value.QNameModule.rootPackageName».«MODULE_INFO_CLASS_NAME».getInstance());
«ELSE»
- set.add(«getRootPackageName((ctx.findModuleByName(name, rev).QNameModule))».«MODULE_INFO_CLASS_NAME».getInstance());
+ set.add(«(ctx.findModule(name, rev).get.QNameModule).rootPackageName».«MODULE_INFO_CLASS_NAME».getInstance());
«ENDIF»
«ENDFOR»
«ENDIF»
- «IF !m.submodules.empty»
- «FOR submodule : m.submodules»
- set.add(«getClassName(submodule.name)»Info.getInstance());
- «ENDFOR»
- «ENDIF»
- «IF m.imports.empty && m.submodules.empty»
- importedModules = «Collections.importedName».emptySet();
+ «FOR submodule : submodules»
+ set.add(«submodule.name.className»Info.getInstance());
+ «ENDFOR»
+ «IF m.imports.empty && submodules.empty»
+ importedModules = ImmutableSet.of();
«ELSE»
- importedModules = «ImmutableSet.importedName».copyOf(set);
+ importedModules = ImmutableSet.copyOf(set);
«ENDIF»
-
- «InputStream.importedName» stream = «MODULE_INFO_CLASS_NAME».class.getResourceAsStream(resourcePath);
- if (stream == null) {
- throw new IllegalStateException("Resource '" + resourcePath + "' is missing");
- }
- try {
- stream.close();
- } catch («IOException.importedName» e) {
- // Resource leak, but there is nothing we can do
- }
}
@Override
- public «String.importedName» getName() {
- return name;
+ public QName getName() {
+ return NAME;
}
@Override
- public «String.importedName» getRevision() {
- return revision;
+ protected String resourceName() {
+ return "«sourcePath(m)»";
}
@Override
- public «String.importedName» getNamespace() {
- return namespace;
- }
-
- @Override
- public «InputStream.importedName» getModuleSourceStream() throws IOException {
- «InputStream.importedName» stream = «MODULE_INFO_CLASS_NAME».class.getResourceAsStream(resourcePath);
- if (stream == null) {
- throw new «IOException.importedName»("Resource " + resourcePath + " is missing");
- }
- return stream;
- }
-
- @Override
- public «Set.importedName»<«YangModuleInfo.importedName»> getImportedModules() {
+ public ImmutableSet<YangModuleInfo> getImportedModules() {
return importedModules;
}
-
- @Override
- public «String.importedName» toString() {
- «StringBuilder.importedName» sb = new «StringBuilder.importedName»(this.getClass().getCanonicalName());
- sb.append("[");
- sb.append("name = " + name);
- sb.append(", namespace = " + namespace);
- sb.append(", revision = " + revision);
- sb.append(", resourcePath = " + resourcePath);
- sb.append(", imports = " + importedModules);
- sb.append("]");
- return sb.toString();
- }
-
- «generateSubInfo(m)»
-
+ «generateSubInfo(submodules)»
'''
+ private def void extendImports() {
+ importedTypes = EXT_IMPORT_STR
+ }
+
private def sourcePath(Module module) {
val opt = moduleFilePathResolver.apply(module)
Preconditions.checkState(opt.isPresent, "Module %s does not have a file path", module)
return opt.get
}
- private def imports() '''
- «IF !importMap.empty»
- «FOR entry : importMap.entrySet»
- «IF entry.value != getRootPackageName(module.QNameModule)»
- import «entry.value».«entry.key»;
- «ENDIF»
- «ENDFOR»
- «ENDIF»
- '''
-
- final protected def importedName(Class<?> cls) {
- val Type intype = Types.typeForClass(cls)
- putTypeIntoImports(intype);
- getExplicitType(intype)
- }
-
- final def void putTypeIntoImports(Type type) {
- val String typeName = type.getName();
- val String typePackageName = type.getPackageName();
- if (typePackageName.startsWith("java.lang") || typePackageName.isEmpty()) {
- return;
- }
- if (!importMap.containsKey(typeName)) {
- importMap.put(typeName, typePackageName);
- }
- if (type instanceof ParameterizedType) {
- val Type[] params = type.getActualTypeArguments()
- if (params !== null) {
- for (Type param : params) {
- putTypeIntoImports(param);
- }
- }
- }
- }
-
- final def String getExplicitType(Type type) {
- val String typePackageName = type.getPackageName();
- val String typeName = type.getName();
- val String importedPackageName = importMap.get(typeName);
- var StringBuilder builder;
- if (typePackageName.equals(importedPackageName)) {
- builder = new StringBuilder(type.getName());
- if (builder.toString().equals("Void")) {
- return "void";
- }
- addActualTypeParameters(builder, type);
- } else {
- if (type.equals(Types.voidType())) {
- return "void";
- }
- builder = new StringBuilder();
- if (!typePackageName.isEmpty()) {
- builder.append(typePackageName + Constants.DOT + type.getName());
- } else {
- builder.append(type.getName());
- }
- addActualTypeParameters(builder, type);
- }
- return builder.toString();
- }
-
- final def StringBuilder addActualTypeParameters(StringBuilder builder, Type type) {
- if (type instanceof ParameterizedType) {
- val Type[] pTypes = type.getActualTypeArguments();
- builder.append('<');
- builder.append(getParameters(pTypes));
- builder.append('>');
- }
- return builder;
- }
-
- final def String getParameters(Type[] pTypes) {
- if (pTypes === null || pTypes.length == 0) {
- return "?";
- }
- val StringBuilder builder = new StringBuilder();
-
- var int i = 0;
- for (pType : pTypes) {
- val Type t = pTypes.get(i)
+ private def generateSubInfo(Set<Module> submodules) '''
+ «FOR submodule : submodules»
+ «val className = submodule.name.className»
- var String separator = ",";
- if (i == (pTypes.length - 1)) {
- separator = "";
- }
+ private static final class «className»Info extends ResourceYangModuleInfo {
+ «val rev = submodule.revision»
+ private final @NonNull QName NAME = QName.create("«submodule.namespace.toString»", «
+ IF rev.present»"«rev.get.toString»", «ENDIF»"«submodule.name»").intern();
+ private static final @NonNull YangModuleInfo INSTANCE = new «className»Info();
- var String wildcardParam = "";
- if (t.equals(Types.voidType())) {
- builder.append("java.lang.Void" + separator);
- } else {
-
- if (t instanceof WildcardType) {
- wildcardParam = "? extends ";
- }
+ private final @NonNull ImmutableSet<YangModuleInfo> importedModules;
- builder.append(wildcardParam + getExplicitType(t) + separator);
- i = i + 1
- }
- }
- return builder.toString();
- }
-
- private def generateSubInfo(Module module) '''
- «FOR submodule : module.submodules»
- private static final class «getClassName(submodule.name)»Info implements «YangModuleInfo.importedName» {
-
- private static final «YangModuleInfo.importedName» INSTANCE = new «getClassName(submodule.name)»Info();
-
- private final «String.importedName» name = "«submodule.name»";
- private final «String.importedName» namespace = "«submodule.namespace.toString»";
- «val DateFormat df = new SimpleDateFormat("yyyy-MM-dd")»
- private final «String.importedName» revision = "«df.format(submodule.revision)»";
- private final «String.importedName» resourcePath = "«sourcePath(submodule)»";
-
- private final «Set.importedName»<YangModuleInfo> importedModules;
-
- public static «YangModuleInfo.importedName» getInstance() {
+ public static @NonNull YangModuleInfo getInstance() {
return INSTANCE;
}
- «classBody(submodule, getClassName(submodule.name + "Info"))»
+ «classBody(submodule, className + "Info", ImmutableSet.of)»
}
«ENDFOR»
'''
-
}