Fix sonar complains 55/57055/9
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Mon, 15 May 2017 11:55:23 +0000 (13:55 +0200)
committerRobert Varga <nite@hq.sk>
Sun, 11 Jun 2017 07:49:54 +0000 (07:49 +0000)
- Remove unused imports
- Remove unused exception
- Extract try block
- dont use generic Exception
- Reduce the number of conditional operators

Change-Id: I0d51ef2aa6540d1d841f637308431ce768b1636b
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
bgp/config-loader-impl/src/main/java/org/opendaylight/protocol/bgp/config/loader/impl/ConfigLoaderImpl.java
bgp/path-selection-mode/src/main/java/org/opendaylight/protocol/bgp/mode/impl/add/AddPathAbstractRouteEntry.java
pcep/topology-provider/src/main/java/org/opendaylight/bgpcep/pcep/topology/provider/PCEPTopologyProvider.java
pcep/topology-provider/src/main/java/org/opendaylight/bgpcep/pcep/topology/provider/config/PCEPTopologyProviderBean.java

index ddfd80e0aba38a349ed72e1930430d4ee2dd81b2..34f565057144a21cc917cf151e9b2a0c9c8dd9c3 100644 (file)
@@ -11,7 +11,9 @@ package org.opendaylight.protocol.bgp.config.loader.impl;
 import com.google.common.base.Preconditions;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.URISyntaxException;
 import java.nio.file.WatchEvent;
 import java.nio.file.WatchKey;
 import java.nio.file.WatchService;
@@ -19,7 +21,9 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.regex.Pattern;
 import javax.annotation.concurrent.GuardedBy;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
 import org.opendaylight.protocol.bgp.config.loader.spi.ConfigFileProcessor;
@@ -35,6 +39,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.xml.sax.SAXException;
+
 public final class ConfigLoaderImpl implements ConfigLoader, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(ConfigLoaderImpl.class);
     private static final String INTERRUPTED = "InterruptedException";
@@ -53,29 +59,7 @@ public final class ConfigLoaderImpl implements ConfigLoader, AutoCloseable {
         this.bindingSerializer = Preconditions.checkNotNull(bindingSerializer);
         this.path = Preconditions.checkNotNull(path);
         Preconditions.checkNotNull(watchService);
-        this.watcherThread = new Thread(() -> {
-            try {
-                while (!Thread.currentThread().isInterrupted()) {
-                    try {
-                        final WatchKey key = watchService.take();
-                        if (key != null) {
-                            for (final WatchEvent event : key.pollEvents()) {
-                                handleEvent(event.context().toString());
-                            }
-                            final boolean reset = key.reset();
-                            if (!reset) {
-                                LOG.warn("Could not reset the watch key.");
-                                return;
-                            }
-                        }
-                    } catch (final InterruptedException e) {
-                        LOG.warn(INTERRUPTED, e);
-                    }
-                }
-            } catch (final Exception e) {
-                LOG.warn(INTERRUPTED, e);
-            }
-        });
+        this.watcherThread = new Thread(new ConfigLoaderImplRunnable(watchService));
         this.watcherThread.start();
         LOG.info("Config Loader service initiated");
     }
@@ -92,7 +76,8 @@ public final class ConfigLoaderImpl implements ConfigLoader, AutoCloseable {
         config.loadConfiguration(dto);
     }
 
-    private NormalizedNode<?, ?> parseDefaultConfigFile(final ConfigFileProcessor config, final String filename) throws Exception {
+    private NormalizedNode<?, ?> parseDefaultConfigFile(final ConfigFileProcessor config, final String filename)
+        throws IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
         final NormalizedNodeResult result = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
 
@@ -139,13 +124,51 @@ public final class ConfigLoaderImpl implements ConfigLoader, AutoCloseable {
         return this.bindingSerializer;
     }
 
-    private synchronized void handleEvent(final String filename) {
-        this.configServices.entrySet().stream().filter(entry -> Pattern.matches(entry.getKey(), filename)).
-            forEach(entry -> handleConfigFile(entry.getValue(), filename));
-    }
 
     @Override
     public void close() throws Exception {
         this.watcherThread.interrupt();
     }
+
+    private class ConfigLoaderImplRunnable implements Runnable {
+        private final WatchService watchService;
+
+        ConfigLoaderImplRunnable(final WatchService watchService) {
+            this.watchService = watchService;
+        }
+
+        @Override
+        public void run() {
+            try {
+                while (!Thread.currentThread().isInterrupted()) {
+                    handleChanges(this.watchService);
+                }
+            } catch (final Exception e) {
+                LOG.warn(INTERRUPTED, e);
+            }
+        }
+
+        private synchronized void handleChanges(final WatchService watchService) {
+            try {
+                final WatchKey key = watchService.take();
+                if (key != null) {
+                    for (final WatchEvent event : key.pollEvents()) {
+                        handleEvent(event.context().toString());
+                    }
+                    final boolean reset = key.reset();
+                    if (!reset) {
+                        LOG.warn("Could not reset the watch key.");
+                    }
+                }
+            } catch (final InterruptedException e) {
+                LOG.warn(INTERRUPTED, e);
+            }
+        }
+
+        private synchronized void handleEvent(final String filename) {
+            ConfigLoaderImpl.this.configServices.entrySet().stream()
+                .filter(entry -> Pattern.matches(entry.getKey(), filename)).
+                forEach(entry -> handleConfigFile(entry.getValue(), filename));
+        }
+    }
 }
index c422037b7755859d53b0362b2b68ac0204554403..b0b75c1660c4e987fa049bf8a711159b97951e18 100644 (file)
@@ -267,10 +267,14 @@ public abstract class AddPathAbstractRouteEntry extends AbstractRouteEntry {
     }
 
     private boolean isNonAddPathBestPathTheSame(final List<AddPathBestPath> newBestPathList) {
-        return !(this.bestPath == null || newBestPathList == null || this.bestPath.isEmpty() || newBestPathList.isEmpty()) &&
+        return !(isEmptyOrNull(this.bestPath) || isEmptyOrNull(newBestPathList)) &&
             this.bestPath.get(0).equals(newBestPathList.get(0));
     }
 
+    private boolean isEmptyOrNull(final List<AddPathBestPath> pathList) {
+        return pathList == null || pathList.isEmpty();
+    }
+
     private void filterRemovedPaths(final List<AddPathBestPath> newBestPathList) {
         if(this.bestPath == null) {
             return;
index 71741df9f21ae5ff305795c207a6f60ab90143e3..8a2045bb697182dcb9aed11f8ada87aaa5999640 100755 (executable)
@@ -52,7 +52,7 @@ public final class PCEPTopologyProvider extends DefaultTopologyReference {
     private Channel channel;
 
     public static PCEPTopologyProvider create(final PCEPTopologyProviderDependenciesProvider dependenciesProvider,
-        final PCEPTopologyConfigDependencies configDependencies) throws Exception {
+        final PCEPTopologyConfigDependencies configDependencies) {
         final List<PCEPCapability> capabilities = dependenciesProvider.getPCEPDispatcher()
             .getPCEPSessionNegotiatorFactory().getPCEPSessionProposalFactory().getCapabilities();
         boolean statefulCapability = false;
index b9caa8aca2f545799107cd8166e2184141b75a93..dfd698bb67f46e1df80831b0fe4728554763b295 100644 (file)
@@ -115,7 +115,7 @@ public final class PCEPTopologyProviderBean implements PCEPTopologyProviderDepen
         @GuardedBy("this")
         private boolean serviceInstantiated;
 
-        PCEPTopologyProviderBeanCSS(final PCEPTopologyConfigDependencies configDependencies) throws Exception {
+        PCEPTopologyProviderBeanCSS(final PCEPTopologyConfigDependencies configDependencies) {
                 this.sgi = configDependencies.getSchedulerDependency().getIdentifier();
                 this.pcepTopoProvider = PCEPTopologyProvider.create(PCEPTopologyProviderBean.this, configDependencies);