Capture server key before returning from callback
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / CallHomeAuthProviderImpl.java
index 83869ad286e7f5f23e0305e00eedc4ace990218b..a115bec6c897fedefb04685ddf00cb89cf13c47d 100644 (file)
@@ -12,20 +12,18 @@ import com.google.common.net.InetAddresses;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
+import java.security.GeneralSecurityException;
 import java.security.PublicKey;
-import java.security.spec.InvalidKeySpecException;
 import java.util.Collection;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 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;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netconf.callhome.protocol.AuthorizedKeysDecoder;
 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorization;
 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorization.Builder;
@@ -46,14 +44,14 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
     private static final InstanceIdentifier<Global> GLOBAL_PATH =
             InstanceIdentifier.create(NetconfCallhomeServer.class).child(Global.class);
     private static final DataTreeIdentifier<Global> GLOBAL =
-            new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, GLOBAL_PATH);
+            DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, GLOBAL_PATH);
 
     private static final InstanceIdentifier<Device> ALLOWED_DEVICES_PATH =
             InstanceIdentifier.create(NetconfCallhomeServer.class).child(AllowedDevices.class).child(Device.class);
     private static final DataTreeIdentifier<Device> ALLOWED_DEVICES =
-            new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, ALLOWED_DEVICES_PATH);
+            DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, ALLOWED_DEVICES_PATH);
     private static final DataTreeIdentifier<Device> ALLOWED_OP_DEVICES =
-            new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, ALLOWED_DEVICES_PATH);
+            DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, ALLOWED_DEVICES_PATH);
 
     private final GlobalConfig globalConfig = new GlobalConfig();
     private final DeviceConfig deviceConfig = new DeviceConfig();
@@ -64,7 +62,7 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
 
     private final CallhomeStatusReporter statusReporter;
 
-    CallHomeAuthProviderImpl(DataBroker broker) {
+    CallHomeAuthProviderImpl(final DataBroker broker) {
         configReg = broker.registerDataTreeChangeListener(GLOBAL, globalConfig);
         deviceReg = broker.registerDataTreeChangeListener(ALLOWED_DEVICES, deviceConfig);
         deviceOpReg = broker.registerDataTreeChangeListener(ALLOWED_OP_DEVICES, deviceOp);
@@ -73,7 +71,8 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
 
     @Nonnull
     @Override
-    public CallHomeAuthorization provideAuth(SocketAddress remoteAddress, PublicKey serverKey) {
+    public CallHomeAuthorization provideAuth(@Nonnull final SocketAddress remoteAddress,
+            @Nonnull final PublicKey serverKey) {
         Device deviceSpecific = deviceConfig.get(serverKey);
         String sessionName;
         Credentials deviceCred;
@@ -113,13 +112,13 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
     }
 
     @Override
-    public void close() throws Exception {
+    public void close() {
         configReg.close();
         deviceReg.close();
         deviceOpReg.close();
     }
 
-    private String fromRemoteAddress(SocketAddress remoteAddress) {
+    private static String fromRemoteAddress(final SocketAddress remoteAddress) {
         if (remoteAddress instanceof InetSocketAddress) {
             InetSocketAddress socketAddress = (InetSocketAddress) remoteAddress;
             return InetAddresses.toAddrString(socketAddress.getAddress()) + ":" + socketAddress.getPort();
@@ -127,21 +126,21 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
         return remoteAddress.toString();
     }
 
-    private class DeviceConfig implements DataTreeChangeListener<Device> {
+    private static class DeviceConfig implements DataTreeChangeListener<Device> {
 
         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 final Collection<DataTreeModification<Device>> mods) {
             for (DataTreeModification<Device> dataTreeModification : mods) {
                 DataObjectModification<Device> rootNode = dataTreeModification.getRootNode();
                 process(rootNode);
             }
         }
 
-        private void process(DataObjectModification<Device> deviceMod) {
+        private void process(final DataObjectModification<Device> deviceMod) {
             Device before = deviceMod.getDataBefore();
             Device after = deviceMod.getDataAfter();
 
@@ -159,7 +158,7 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
             }
         }
 
-        private void putDevice(Device device) {
+        private void putDevice(final Device device) {
             PublicKey key = publicKey(device);
             if (key == null) {
                 return;
@@ -167,7 +166,7 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
             byPublicKey.put(key, device);
         }
 
-        private void removeDevice(Device device) {
+        private void removeDevice(final Device device) {
             PublicKey key = publicKey(device);
             if (key == null) {
                 return;
@@ -175,34 +174,34 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
             byPublicKey.remove(key);
         }
 
-        private PublicKey publicKey(Device device) {
+        private PublicKey publicKey(final Device device) {
             String hostKey = device.getSshHostKey();
             try {
                 return keyDecoder.decodePublicKey(hostKey);
-            } catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchProviderException e) {
+            } catch (GeneralSecurityException e) {
                 LOG.error("Unable to decode SSH key for {}. Ignoring update for this device", device.getUniqueId(), e);
                 return null;
             }
         }
 
-        private Device get(PublicKey key) {
+        private Device get(final PublicKey key) {
             return byPublicKey.get(key);
         }
     }
 
-    private class DeviceOp implements DataTreeChangeListener<Device> {
+    private static 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 final Collection<DataTreeModification<Device>> mods) {
             for (DataTreeModification<Device> dataTreeModification : mods) {
                 DataObjectModification<Device> rootNode = dataTreeModification.getRootNode();
                 process(rootNode);
             }
         }
 
-        private void process(DataObjectModification<Device> deviceMod) {
+        private void process(final DataObjectModification<Device> deviceMod) {
             Device before = deviceMod.getDataBefore();
             Device after = deviceMod.getDataAfter();
 
@@ -220,17 +219,17 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
             }
         }
 
-        private void putDevice(Device device) {
+        private void putDevice(final Device device) {
             String key = device.getSshHostKey();
             byPublicKey.put(key, device);
         }
 
-        private void removeDevice(Device device) {
+        private void removeDevice(final Device device) {
             String key = device.getSshHostKey();
             byPublicKey.remove(key);
         }
 
-        Device get(PublicKey serverKey) {
+        Device get(final PublicKey serverKey) {
             String skey = "";
 
             try {
@@ -243,12 +242,12 @@ public class CallHomeAuthProviderImpl implements CallHomeAuthorizationProvider,
         }
     }
 
-    private class GlobalConfig implements DataTreeChangeListener<Global> {
+    private static class GlobalConfig implements DataTreeChangeListener<Global> {
 
         private volatile Global current = null;
 
         @Override
-        public void onDataTreeChanged(Collection<DataTreeModification<Global>> mods) {
+        public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<Global>> mods) {
             for (DataTreeModification<Global> dataTreeModification : mods) {
                 current = dataTreeModification.getRootNode().getDataAfter();
             }