X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2Fplugin%2FJMXGenerator.java;h=18eb601747ca02ab32d826a02c046347f1f79f12;hb=f43b01b81319959b1907e3e04537f5169e7f33d8;hp=1f1776f0a5f86ffa1f229de339a8463ebcb2a9b0;hpb=7d03757a62c1909ee74b42e6acf30d3d7b961bfa;p=controller.git diff --git a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGenerator.java b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGenerator.java index 1f1776f0a5..18eb601747 100644 --- a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGenerator.java +++ b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGenerator.java @@ -9,27 +9,29 @@ package org.opendaylight.controller.config.yangjmxgenerator.plugin; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; +import com.google.common.io.Files; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.Set; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.io.FileUtils; import org.apache.maven.project.MavenProject; import org.opendaylight.controller.config.spi.ModuleFactory; import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry; import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator; import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry; import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper; -import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl; +import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; @@ -64,7 +66,6 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware { private static final Logger LOG = LoggerFactory.getLogger(JMXGenerator.class); private static final Pattern NAMESPACE_MAPPING_PATTERN = Pattern.compile("(.+)" + NAMESPACE_TO_PACKAGE_DIVIDER + "(.+)"); - private PackageTranslator packageTranslator; private final CodeWriter codeWriter; private Map namespaceToPackageMapping; private File resourceBaseDir; @@ -80,52 +81,50 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware { } @Override - public Collection generateSources(final SchemaContext context, - final File outputBaseDir, final Set yangModulesInCurrentMavenModule) { + public Collection generateSources(final SchemaContext context, final File outputBaseDir, + final Set currentModules, final Function> moduleResourcePathResolver) { Preconditions.checkArgument(context != null, "Null context received"); Preconditions.checkArgument(outputBaseDir != null, "Null outputBaseDir received"); Preconditions - .checkArgument(namespaceToPackageMapping != null && !namespaceToPackageMapping.isEmpty(), + .checkArgument(this.namespaceToPackageMapping != null && !this.namespaceToPackageMapping.isEmpty(), "No namespace to package mapping provided in additionalConfiguration"); - packageTranslator = new PackageTranslator(namespaceToPackageMapping); + final PackageTranslator packageTranslator = new PackageTranslator(this.namespaceToPackageMapping); if (!outputBaseDir.exists()) { outputBaseDir.mkdirs(); } - GeneratedFilesTracker generatedFiles = new GeneratedFilesTracker(); + final GeneratedFilesTracker generatedFiles = new GeneratedFilesTracker(); // create SIE structure qNamesToSIEs - Map qNamesToSIEs = new HashMap<>(); + final Map qNamesToSIEs = new HashMap<>(); - Map knownSEITracker = new HashMap<>(); - for (Module module : context.getModules()) { - String packageName = packageTranslator.getPackageName(module); - Map namesToSIEntries = ServiceInterfaceEntry + final Map knownSEITracker = new HashMap<>(); + for (final Module module : context.getModules()) { + final String packageName = packageTranslator.getPackageName(module); + final Map namesToSIEntries = ServiceInterfaceEntry .create(module, packageName, knownSEITracker); - for (Entry sieEntry : namesToSIEntries + for (final Entry sieEntry : namesToSIEntries .entrySet()) { // merge value into qNamesToSIEs - if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) { - qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue()); - } else { + if (qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue()) != null) { throw new IllegalStateException( - "Cannot add two SIE with same qname " + "Cannot add two SIE with same qname " + sieEntry.getValue()); } } - if (yangModulesInCurrentMavenModule.contains(module)) { + if (currentModules.contains(module)) { // write this sie to disk - for (ServiceInterfaceEntry sie : namesToSIEntries.values()) { + for (final ServiceInterfaceEntry sie : namesToSIEntries.values()) { try { - generatedFiles.addFile(codeWriter.writeSie(sie, + generatedFiles.addFile(this.codeWriter.writeSie(sie, outputBaseDir)); - } catch (Exception e) { + } catch (final Exception e) { throw new RuntimeException( "Error occurred during SIE source generate phase", e); @@ -134,26 +133,26 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware { } } - File mainBaseDir = concatFolders(projectBaseDir, "src", "main", "java"); - Preconditions.checkNotNull(resourceBaseDir, + final File mainBaseDir = concatFolders(this.projectBaseDir, "src", "main", "java"); + Preconditions.checkNotNull(this.resourceBaseDir, "resource base dir attribute was null"); - StringBuilder fullyQualifiedNamesOfFactories = new StringBuilder(); + final StringBuilder fullyQualifiedNamesOfFactories = new StringBuilder(); // create MBEs - for (Module module : yangModulesInCurrentMavenModule) { - String packageName = packageTranslator.getPackageName(module); - Map namesToMBEs = ModuleMXBeanEntry + for (final Module module : currentModules) { + final String packageName = packageTranslator.getPackageName(module); + final Map namesToMBEs = ModuleMXBeanEntry .create(module, qNamesToSIEs, context, new TypeProviderWrapper(new TypeProviderImpl(context)), packageName); - for (Entry mbeEntry : namesToMBEs + for (final Entry mbeEntry : namesToMBEs .entrySet()) { - ModuleMXBeanEntry mbe = mbeEntry.getValue(); + final ModuleMXBeanEntry mbe = mbeEntry.getValue(); try { - List files1 = codeWriter.writeMbe(mbe, outputBaseDir, + final List files1 = this.codeWriter.writeMbe(mbe, outputBaseDir, mainBaseDir); generatedFiles.addFile(files1); - } catch (Exception e) { + } catch (final Exception e) { throw new RuntimeException( "Error occurred during MBE source generate phase", e); @@ -165,19 +164,19 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware { } // create ModuleFactory file if needed if (fullyQualifiedNamesOfFactories.length() > 0 - && generateModuleFactoryFile) { - File serviceLoaderFile = JMXGenerator.concatFolders( - resourceBaseDir, "META-INF", "services", + && this.generateModuleFactoryFile) { + final File serviceLoaderFile = JMXGenerator.concatFolders( + this.resourceBaseDir, "META-INF", "services", ModuleFactory.class.getName()); // if this file does not exist, create empty file serviceLoaderFile.getParentFile().mkdirs(); try { serviceLoaderFile.createNewFile(); - FileUtils.write(serviceLoaderFile, - fullyQualifiedNamesOfFactories.toString()); - } catch (IOException e) { - String message = "Cannot write to " + serviceLoaderFile; - LOG.error(message); + Files.asCharSink(serviceLoaderFile, StandardCharsets.UTF_8).write( + fullyQualifiedNamesOfFactories.toString()); + } catch (final IOException e) { + final String message = "Cannot write to " + serviceLoaderFile; + LOG.error(message, e); throw new RuntimeException(message, e); } } @@ -186,12 +185,11 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware { @VisibleForTesting static File concatFolders(final File projectBaseDir, final String... folderNames) { - StringBuilder b = new StringBuilder(); - for (String folder : folderNames) { - b.append(folder); - b.append(File.separator); + File result = projectBaseDir; + for (final String folder: folderNames) { + result = new File(result, folder); } - return new File(projectBaseDir, b.toString()); + return result; } @Override @@ -201,25 +199,18 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware { this.generateModuleFactoryFile = extractModuleFactoryBoolean(additionalCfg); } - private boolean extractModuleFactoryBoolean( - final Map additionalCfg) { - String bool = additionalCfg.get(MODULE_FACTORY_FILE_BOOLEAN); - if (bool == null) { - return true; - } - if ("false".equals(bool)) { - return false; - } - return true; + private static boolean extractModuleFactoryBoolean(final Map additionalCfg) { + final String bool = additionalCfg.get(MODULE_FACTORY_FILE_BOOLEAN); + return !"false".equals(bool); } private static Map extractNamespaceMapping( final Map additionalCfg) { - Map namespaceToPackage = Maps.newHashMap(); - for (String key : additionalCfg.keySet()) { + final Map namespaceToPackage = new HashMap<>(); + for (final String key : additionalCfg.keySet()) { if (key.startsWith(NAMESPACE_TO_PACKAGE_PREFIX)) { - String mapping = additionalCfg.get(key); - NamespaceMapping mappingResolved = extractNamespaceMapping(mapping); + final String mapping = additionalCfg.get(key); + final NamespaceMapping mappingResolved = extractNamespaceMapping(mapping); namespaceToPackage.put(mappingResolved.namespace, mappingResolved.packageName); } @@ -228,7 +219,7 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware { } private static NamespaceMapping extractNamespaceMapping(final String mapping) { - Matcher matcher = NAMESPACE_MAPPING_PATTERN.matcher(mapping); + final Matcher matcher = NAMESPACE_MAPPING_PATTERN.matcher(mapping); Preconditions.checkArgument(matcher.matches(), "Namespace to package mapping:%s is in invalid format, requested format is: %s", mapping, NAMESPACE_MAPPING_PATTERN); @@ -243,41 +234,38 @@ public class JMXGenerator implements BasicCodeGenerator, MavenProjectAware { @Override public void setMavenProject(final MavenProject project) { this.projectBaseDir = project.getBasedir(); - LOG.debug("{}: project base dir: {}", getClass().getCanonicalName(), projectBaseDir); + LOG.debug("{}: project base dir: {}", getClass().getCanonicalName(), this.projectBaseDir); } @VisibleForTesting static class GeneratedFilesTracker { - private final Set files = Sets.newHashSet(); + private final Set files = new HashSet<>(); void addFile(final File file) { - if (files.contains(file)) { - List undeletedFiles = Lists.newArrayList(); - for (File presentFile : files) { - if (presentFile.delete() == false) { + if (this.files.contains(file)) { + final List undeletedFiles = new ArrayList<>(); + for (final File presentFile : this.files) { + if (!presentFile.delete()) { undeletedFiles.add(presentFile); } } - if (undeletedFiles.isEmpty() == false) { - LOG.error( - "Illegal state occurred: Unable to delete already generated files, undeleted files: {}", + if (!undeletedFiles.isEmpty()) { + LOG.error("Illegal state occurred: Unable to delete already generated files, undeleted files: {}", undeletedFiles); } - throw new IllegalStateException( - "Name conflict in generated files, file" + file - + " present twice"); + throw new IllegalStateException("Name conflict in generated files, file" + file + " present twice"); } - files.add(file); + this.files.add(file); } void addFile(final Collection files) { - for (File file : files) { + for (final File file : files) { addFile(file); } } public Set getFiles() { - return files; + return this.files; } } }