Remove deprecated CheckedFuture from callhome-provider module 25/64425/1
authorDavid Suarez <david.suarez.fuentes@gmail.com>
Tue, 17 Oct 2017 22:02:53 +0000 (00:02 +0200)
committerDavid Suarez <david.suarez.fuentes@gmail.com>
Tue, 17 Oct 2017 22:02:53 +0000 (00:02 +0200)
Some other changes regarding checkstyle, sonar and annotations.

Change-Id: I361e2fc1928620ac1ed178f77beb397d5110a761
Signed-off-by: David Suarez <david.suarez.fuentes@gmail.com>
netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/CallHomeAuthProviderImpl.java
netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/CallhomeStatusReporter.java
netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/Configuration.java
netconf/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/IetfZeroTouchCallHomeServerProvider.java

index 83869ad286e7f5f23e0305e00eedc4ace990218b..9e91562cf570f5454a01f23e0fa796ad280903c7 100644 (file)
@@ -73,7 +73,7 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
 
     @Nonnull
     @Override
-    public CallHomeAuthorization provideAuth(SocketAddress remoteAddress, PublicKey serverKey) {
+    public CallHomeAuthorization provideAuth(@Nonnull SocketAddress remoteAddress, @Nonnull PublicKey serverKey) {
         Device deviceSpecific = deviceConfig.get(serverKey);
         String sessionName;
         Credentials deviceCred;
@@ -131,10 +131,10 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
 
         private final AuthorizedKeysDecoder keyDecoder = new AuthorizedKeysDecoder();
 
-        private ConcurrentMap<PublicKey, Device> byPublicKey = new ConcurrentHashMap<PublicKey, Device>();
+        private final ConcurrentMap<PublicKey, Device> byPublicKey = new ConcurrentHashMap<>();
 
         @Override
-        public void onDataTreeChanged(Collection<DataTreeModification<Device>> mods) {
+        public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Device>> mods) {
             for (DataTreeModification<Device> dataTreeModification : mods) {
                 DataObjectModification<Device> rootNode = dataTreeModification.getRootNode();
                 process(rootNode);
@@ -192,10 +192,10 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
 
     private class DeviceOp implements DataTreeChangeListener<Device> {
 
-        private ConcurrentMap<String, Device> byPublicKey = new ConcurrentHashMap<>();
+        private final ConcurrentMap<String, Device> byPublicKey = new ConcurrentHashMap<>();
 
         @Override
-        public void onDataTreeChanged(Collection<DataTreeModification<Device>> mods) {
+        public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Device>> mods) {
             for (DataTreeModification<Device> dataTreeModification : mods) {
                 DataObjectModification<Device> rootNode = dataTreeModification.getRootNode();
                 process(rootNode);
@@ -248,7 +248,7 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
         private volatile Global current = null;
 
         @Override
-        public void onDataTreeChanged(Collection<DataTreeModification<Global>> mods) {
+        public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Global>> mods) {
             for (DataTreeModification<Global> dataTreeModification : mods) {
                 current = dataTreeModification.getRootNode().getDataAfter();
             }
index a9da3f3d7cc5025b7449fd3fa33476d97f36e5c3..6f547f885cf8d5485852f1e1ba8ab50621d82a7f 100644 (file)
@@ -9,7 +9,7 @@
 package org.opendaylight.netconf.callhome.mount;
 
 import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
 import java.security.NoSuchAlgorithmException;
 import java.security.NoSuchProviderException;
@@ -18,6 +18,7 @@ import java.security.spec.InvalidKeySpecException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.concurrent.ExecutionException;
 import javax.annotation.Nonnull;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
@@ -28,7 +29,6 @@ import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.netconf.callhome.protocol.AuthorizedKeysDecoder;
 import org.opendaylight.netconf.callhome.protocol.StatusRecorder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.callhome.device.status.rev170112.Device1;
@@ -70,7 +70,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
     }
 
     @Override
-    public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
+    public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
         for (DataTreeModification<Node> change: changes) {
             final DataObjectModification<Node> rootNode = change.getRootNode();
             final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
@@ -215,12 +215,10 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
         ReadOnlyTransaction opTx = dataBroker.newReadOnlyTransaction();
 
         InstanceIdentifier<Device> deviceIID = buildDeviceInstanceIdentifier(nodeId);
-        CheckedFuture<Optional<Device>, ReadFailedException> devFuture =
-                opTx.read(LogicalDatastoreType.OPERATIONAL, deviceIID);
-
+        ListenableFuture<Optional<Device>> devFuture = opTx.read(LogicalDatastoreType.OPERATIONAL, deviceIID);
         try {
-            return devFuture.checkedGet();
-        } catch (ReadFailedException e) {
+            return devFuture.get();
+        } catch (InterruptedException | ExecutionException e) {
             return Optional.absent();
         }
     }
@@ -274,20 +272,14 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
 
     private AllowedDevices getDevices() {
         ReadOnlyTransaction rxTransaction = dataBroker.newReadOnlyTransaction();
-        CheckedFuture<Optional<AllowedDevices>, ReadFailedException> devicesFuture =
+        ListenableFuture<Optional<AllowedDevices>> devicesFuture =
                 rxTransaction.read(LogicalDatastoreType.OPERATIONAL, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
-
         try {
-            Optional<AllowedDevices> opt = devicesFuture.checkedGet();
-            if (opt.isPresent()) {
-                AllowedDevices devices = opt.get();
-                return devices;
-            }
-        } catch (ReadFailedException e) {
+            return devicesFuture.get().orNull();
+        } catch (ExecutionException | InterruptedException e) {
             LOG.error("Error trying to read the whitelist devices: {}", e);
+            return null;
         }
-
-        return null;
     }
 
     private List<Device> getDevicesAsList() {
index 6a5c11b677e21d66117f98b3be40c4dc035c841e..cb83bc9deb131e7114a518148e148477509d5300 100644 (file)
@@ -63,7 +63,7 @@ public class Configuration {
         }
     }
 
-    private String path;
+    private final String path;
     private Properties properties;
 
     public Configuration() {
index 62c4c51b32e595dcc7aee559b5f0b9326cb94ab4..e8b96e671b47b724bd05d35ab061db9e476fb6b5 100644 (file)
@@ -10,15 +10,16 @@ package org.opendaylight.netconf.callhome.mount;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
 import java.net.InetSocketAddress;
-import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
@@ -27,7 +28,6 @@ import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorizationProvider;
 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServer;
 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServerBuilder;
@@ -46,7 +46,7 @@ import org.slf4j.LoggerFactory;
 public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataTreeChangeListener<AllowedDevices> {
     private static final String APPNAME = "CallHomeServer";
     static final InstanceIdentifier<AllowedDevices> ALL_DEVICES = InstanceIdentifier.create(NetconfCallhomeServer.class)
-        .child(AllowedDevices.class);
+            .child(AllowedDevices.class);
 
     private static final Logger LOG = LoggerFactory.getLogger(IetfZeroTouchCallHomeServerProvider.class);
 
@@ -74,8 +74,8 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
         try {
             LOG.info("Initializing provider for {}", APPNAME);
             initializeServer();
-            listenerReg = dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(
-                    LogicalDatastoreType.CONFIGURATION, ALL_DEVICES), this);
+            listenerReg = dataBroker.registerDataTreeChangeListener(
+                    new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, ALL_DEVICES), this);
             LOG.info("Initialization complete for {}", APPNAME);
         } catch (IOException | Configuration.ConfigurationException e) {
             LOG.error("Unable to successfully initialize", e);
@@ -100,8 +100,8 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
     private void initializeServer() throws IOException {
         LOG.info("Initializing Call Home server instance");
         CallHomeAuthorizationProvider provider = this.getCallHomeAuthorization();
-        NetconfCallHomeServerBuilder builder = new NetconfCallHomeServerBuilder(
-                provider, mountDispacher, statusReporter);
+        NetconfCallHomeServerBuilder builder = new NetconfCallHomeServerBuilder(provider, mountDispacher,
+                                                                                statusReporter);
         if (port > 0) {
             builder.setBindAddress(new InetSocketAddress(port));
         }
@@ -115,7 +115,7 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
     void assertValid(Object obj, String description) {
         if (obj == null) {
             throw new RuntimeException(
-                String.format("Failed to find %s in IetfZeroTouchCallHomeProvider.initialize()", description));
+                    String.format("Failed to find %s in IetfZeroTouchCallHomeProvider.initialize()", description));
         }
     }
 
@@ -136,18 +136,18 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
     }
 
     @Override
-    public void onDataTreeChanged(Collection<DataTreeModification<AllowedDevices>> changes) {
+    public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<AllowedDevices>> changes) {
         // In case of any changes to the devices datatree, register the changed values with callhome server
         // As of now, no way to add a new callhome client key to the CallHomeAuthorization instance since
         // its created under CallHomeAuthorizationProvider.
         // Will have to redesign a bit here.
         // CallHomeAuthorization.
         ReadOnlyTransaction roConfigTx = dataBroker.newReadOnlyTransaction();
-        CheckedFuture<Optional<AllowedDevices>, ReadFailedException> devicesFuture =
-                roConfigTx.read(LogicalDatastoreType.CONFIGURATION, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
+        ListenableFuture<Optional<AllowedDevices>> devicesFuture = roConfigTx
+                .read(LogicalDatastoreType.CONFIGURATION, IetfZeroTouchCallHomeServerProvider.ALL_DEVICES);
 
         Set<InstanceIdentifier<?>> deletedDevices = new HashSet<>();
-        for (DataTreeModification<AllowedDevices> change: changes) {
+        for (DataTreeModification<AllowedDevices> change : changes) {
             DataObjectModification<AllowedDevices> rootNode = change.getRootNode();
             switch (rootNode.getModificationType()) {
                 case DELETE:
@@ -164,7 +164,7 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
             for (Device confDevice : getReadDevices(devicesFuture)) {
                 readAndUpdateStatus(confDevice);
             }
-        } catch (ReadFailedException e) {
+        } catch (ExecutionException | InterruptedException e) {
             LOG.error("Error trying to read the whitelist devices: {}", e);
         }
     }
@@ -178,9 +178,7 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
 
         int numRemoved = deletedDevices.size();
 
-        Iterator<InstanceIdentifier<?>> iterator = deletedDevices.iterator();
-        while (iterator.hasNext()) {
-            InstanceIdentifier<?> removedIID = iterator.next();
+        for (InstanceIdentifier<?> removedIID : deletedDevices) {
             LOG.info("Deleting the entry for callhome device {}", removedIID);
             opTx.delete(LogicalDatastoreType.OPERATIONAL, removedIID);
         }
@@ -190,43 +188,28 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
         }
     }
 
-    private List<Device> getReadDevices(CheckedFuture<Optional<AllowedDevices>, ReadFailedException> devicesFuture)
-            throws ReadFailedException {
-        Optional<AllowedDevices> opt = devicesFuture.checkedGet();
-        if (opt.isPresent()) {
-            AllowedDevices confDevices = opt.get();
-            if (confDevices != null) {
-                LOG.debug("Read {} devices", confDevices.getDevice().size());
-                return confDevices.getDevice();
-            }
-        }
-
-        LOG.debug("Failed to read devices");
-        return new ArrayList<>();
+    private List<Device> getReadDevices(
+            ListenableFuture<Optional<AllowedDevices>> devicesFuture) throws InterruptedException, ExecutionException {
+        Optional<AllowedDevices> opt = devicesFuture.get();
+        return opt.isPresent() ? opt.get().getDevice() : Collections.emptyList();
     }
 
-    private void readAndUpdateStatus(Device cfgDevice) throws ReadFailedException {
+    private void readAndUpdateStatus(Device cfgDevice) throws InterruptedException, ExecutionException {
         InstanceIdentifier<Device> deviceIID = InstanceIdentifier.create(NetconfCallhomeServer.class)
-                .child(AllowedDevices.class)
-                .child(Device.class, new DeviceKey(cfgDevice.getUniqueId()));
+                .child(AllowedDevices.class).child(Device.class, new DeviceKey(cfgDevice.getUniqueId()));
 
         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
-        CheckedFuture<Optional<Device>, ReadFailedException> deviceFuture = tx.read(
-            LogicalDatastoreType.OPERATIONAL, deviceIID);
+        ListenableFuture<Optional<Device>> deviceFuture = tx.read(LogicalDatastoreType.OPERATIONAL, deviceIID);
 
-        Optional<Device> opDevGet = deviceFuture.checkedGet();
+        Optional<Device> opDevGet = deviceFuture.get();
         Device1 devStatus = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.DISCONNECTED).build();
         if (opDevGet.isPresent()) {
             Device opDevice = opDevGet.get();
             devStatus = opDevice.getAugmentation(Device1.class);
         }
 
-        Device newOpDevice = new DeviceBuilder()
-                .addAugmentation(Device1.class, devStatus)
-                .setSshHostKey(cfgDevice.getSshHostKey())
-                .setUniqueId(cfgDevice.getUniqueId()).build();
-
-        cfgDevice = newOpDevice;
+        cfgDevice = new DeviceBuilder().addAugmentation(Device1.class, devStatus)
+                .setSshHostKey(cfgDevice.getSshHostKey()).setUniqueId(cfgDevice.getUniqueId()).build();
 
         tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIID, cfgDevice);
         tx.submit();