Remove unused exceptions
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / CallhomeStatusReporter.java
index 6f547f885cf8d5485852f1e1ba8ab50621d82a7f..8a4979502b29b1521144d07e71fa3895c254190d 100644 (file)
@@ -11,10 +11,8 @@ package org.opendaylight.netconf.callhome.mount;
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
+import java.security.GeneralSecurityException;
 import java.security.PublicKey;
-import java.security.spec.InvalidKeySpecException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -63,14 +61,14 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
     private final DataBroker dataBroker;
     private final ListenerRegistration<CallhomeStatusReporter> reg;
 
-    CallhomeStatusReporter(DataBroker broker) {
+    CallhomeStatusReporter(final DataBroker broker) {
         this.dataBroker = broker;
         this.reg = dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
             NETCONF_TOPO_IID.child(Node.class)), this);
     }
 
     @Override
-    public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
+    public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<Node>> changes) {
         for (DataTreeModification<Node> change: changes) {
             final DataObjectModification<Node> rootNode = change.getRootNode();
             final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
@@ -80,7 +78,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
                     if (isNetconfNode(rootNode.getDataAfter())) {
                         NodeId nodeId = getNodeId(identifier);
                         if (nodeId != null) {
-                            NetconfNode nnode = rootNode.getDataAfter().getAugmentation(NetconfNode.class);
+                            NetconfNode nnode = rootNode.getDataAfter().augmentation(NetconfNode.class);
                             handledNetconfNode(nodeId, nnode);
                         }
                     }
@@ -99,16 +97,16 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
         }
     }
 
-    private boolean isNetconfNode(Node node) {
-        return node.getAugmentation(NetconfNode.class) != null;
+    private static boolean isNetconfNode(final Node node) {
+        return node.augmentation(NetconfNode.class) != null;
     }
 
-    private NodeId getNodeId(final InstanceIdentifier<?> path) {
+    private static NodeId getNodeId(final InstanceIdentifier<?> path) {
         NodeKey key = path.firstKeyOf(Node.class);
         return key != null ? key.getNodeId() : null;
     }
 
-    private void handledNetconfNode(NodeId nodeId, NetconfNode nnode) {
+    private void handledNetconfNode(final NodeId nodeId, final NetconfNode nnode) {
         NetconfNodeConnectionStatus.ConnectionStatus csts = nnode.getConnectionStatus();
 
         switch (csts) {
@@ -124,7 +122,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
         }
     }
 
-    private void handleConnectedNetconfNode(NodeId nodeId) {
+    private void handleConnectedNetconfNode(final NodeId nodeId) {
         // Fully connected, all services for remote device are
         // available from the MountPointService.
         LOG.debug("NETCONF Node: {} is fully connected", nodeId.getValue());
@@ -142,7 +140,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
         }
     }
 
-    private void handleDisconnectedNetconfNode(NodeId nodeId) {
+    private void handleDisconnectedNetconfNode(final NodeId nodeId) {
         LOG.debug("NETCONF Node: {} disconnected", nodeId.getValue());
 
         Device opDev = readAndGetDevice(nodeId);
@@ -158,7 +156,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
         }
     }
 
-    private void handleUnableToConnectNetconfNode(NodeId nodeId) {
+    private void handleUnableToConnectNetconfNode(final NodeId nodeId) {
         // The maximum configured number of reconnect attempts
         // have been reached. No more reconnects will be
         // attempted by the Netconf Connector.
@@ -177,19 +175,19 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
         }
     }
 
-    void asForceListedDevice(String id, PublicKey serverKey) {
+    void asForceListedDevice(final String id, final PublicKey serverKey) {
         NodeId nid = new NodeId(id);
         Device device = newDevice(id, serverKey, Device1.DeviceStatus.DISCONNECTED);
         writeDevice(nid, device);
     }
 
-    void asUnlistedDevice(String id, PublicKey serverKey) {
+    void asUnlistedDevice(final String id, final PublicKey serverKey) {
         NodeId nid = new NodeId(id);
         Device device = newDevice(id, serverKey, Device1.DeviceStatus.FAILEDNOTALLOWED);
         writeDevice(nid, device);
     }
 
-    private Device newDevice(String id, PublicKey serverKey, Device1.DeviceStatus status) {
+    private static Device newDevice(final String id, final PublicKey serverKey, final Device1.DeviceStatus status) {
         String sshEncodedKey = serverKey.toString();
         try {
             sshEncodedKey = AuthorizedKeysDecoder.encodePublicKey(serverKey);
@@ -199,19 +197,19 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
         Device1 d1 = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.FAILEDNOTALLOWED).build();
         DeviceBuilder builder = new DeviceBuilder()
                 .setUniqueId(id)
-                .setKey(new DeviceKey(id))
+                .withKey(new DeviceKey(id))
                 .setSshHostKey(sshEncodedKey)
                 .addAugmentation(Device1.class, d1);
 
         return builder.build();
     }
 
-    private Device readAndGetDevice(NodeId nodeId) {
+    private Device readAndGetDevice(final NodeId nodeId) {
         return readDevice(nodeId).orNull();
     }
 
     @Nonnull
-    private Optional<Device> readDevice(NodeId nodeId) {
+    private Optional<Device> readDevice(final NodeId nodeId) {
         ReadOnlyTransaction opTx = dataBroker.newReadOnlyTransaction();
 
         InstanceIdentifier<Device> deviceIID = buildDeviceInstanceIdentifier(nodeId);
@@ -223,51 +221,51 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
         }
     }
 
-    private void writeDevice(NodeId nodeId, Device modifiedDevice) {
+    private void writeDevice(final NodeId nodeId, final Device modifiedDevice) {
         ReadWriteTransaction opTx = dataBroker.newReadWriteTransaction();
         opTx.merge(LogicalDatastoreType.OPERATIONAL, buildDeviceInstanceIdentifier(nodeId), modifiedDevice);
-        opTx.submit();
+        opTx.commit();
     }
 
-    private InstanceIdentifier<Device> buildDeviceInstanceIdentifier(NodeId nodeId) {
+    private static InstanceIdentifier<Device> buildDeviceInstanceIdentifier(final NodeId nodeId) {
         return InstanceIdentifier.create(NetconfCallhomeServer.class)
                 .child(AllowedDevices.class)
                 .child(Device.class, new DeviceKey(nodeId.getValue()));
     }
 
-    private Device withConnectedStatus(Device opDev) {
+    private static Device withConnectedStatus(final Device opDev) {
         Device1 status = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.CONNECTED).build();
         return new DeviceBuilder().addAugmentation(Device1.class, status).setUniqueId(opDev.getUniqueId())
                 .setSshHostKey(opDev.getSshHostKey()).build();
     }
 
-    private Device withFailedStatus(Device opDev) {
+    private static Device withFailedStatus(final Device opDev) {
         Device1 status = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.FAILED).build();
         return new DeviceBuilder().addAugmentation(Device1.class, status).setUniqueId(opDev.getUniqueId())
                 .setSshHostKey(opDev.getSshHostKey()).build();
     }
 
-    private Device withDisconnectedStatus(Device opDev) {
+    private static Device withDisconnectedStatus(final Device opDev) {
         Device1 status = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.DISCONNECTED).build();
         return new DeviceBuilder().addAugmentation(Device1.class, status).setUniqueId(opDev.getUniqueId())
                 .setSshHostKey(opDev.getSshHostKey()).build();
     }
 
-    private Device withFailedAuthStatus(Device opDev) {
+    private static Device withFailedAuthStatus(final Device opDev) {
         Device1 status = new Device1Builder().setDeviceStatus(Device1.DeviceStatus.FAILEDAUTHFAILURE).build();
         return new DeviceBuilder().addAugmentation(Device1.class, status).setUniqueId(opDev.getUniqueId())
                 .setSshHostKey(opDev.getSshHostKey()).build();
     }
 
-    private void setDeviceStatus(Device device) {
+    private void setDeviceStatus(final Device device) {
         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
         InstanceIdentifier<Device> deviceIId =
                 InstanceIdentifier.create(NetconfCallhomeServer.class)
                         .child(AllowedDevices.class)
-                        .child(Device.class, device.getKey());
+                        .child(Device.class, device.key());
 
         tx.merge(LogicalDatastoreType.OPERATIONAL, deviceIId, device);
-        tx.submit();
+        tx.commit();
     }
 
     private AllowedDevices getDevices() {
@@ -288,7 +286,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
     }
 
     @Override
-    public void reportFailedAuth(PublicKey sshKey) {
+    public void reportFailedAuth(final PublicKey sshKey) {
         AuthorizedKeysDecoder decoder = new AuthorizedKeysDecoder();
 
         for (Device device : getDevicesAsList()) {
@@ -305,7 +303,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
                     setDeviceStatus(failedDevice);
                     return;
                 }
-            } catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchProviderException e) {
+            } catch (GeneralSecurityException e) {
                 LOG.error("Failed decoding a device key with host key: {} {}", keyString, e);
                 return;
             }
@@ -316,7 +314,7 @@ class CallhomeStatusReporter implements DataTreeChangeListener<Node>, StatusReco
     }
 
     @Override
-    public void close() throws Exception {
+    public void close() {
         reg.close();
     }
 }