Merge "Fix findbugs violations in netconf"
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / util / NetconfTopologyRPCProvider.java
index 4edbedf6c265830028c82a72af1527aadd75afe5..0c5810aa1e4fef153e376c8306a45e9e230c3599 100644 (file)
@@ -7,10 +7,11 @@
  */
 package org.opendaylight.netconf.sal.connect.util;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.concurrent.Future;
@@ -18,13 +19,16 @@ import org.opendaylight.aaa.encrypt.AAAEncryptionService;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 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.TransactionCommitFailedException;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.AddNetconfNodeInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.CreateDeviceInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.DeleteDeviceInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeTopologyService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPw;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPwBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.login.pw.LoginPassword;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.login.pw.LoginPasswordBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
@@ -53,56 +57,99 @@ public class NetconfTopologyRPCProvider implements NetconfNodeTopologyService {
     }
 
     @Override
-    public Future<RpcResult<Void>> addNetconfNode(AddNetconfNodeInput input) {
-        NetconfNode node = this.encryptPassword(input);
+    public Future<RpcResult<Void>> createDevice(final CreateDeviceInput input) {
+        final NetconfNode node = this.encryptPassword(input);
         final SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
-        NodeId nodeId = new NodeId(input.getNodeId());
-        writeToConfigDS(node, nodeId, topologyId, futureResult);
+        final NodeId nodeId = new NodeId(input.getNodeId());
+        writeToConfigDS(node, nodeId, futureResult);
         return futureResult;
     }
 
-    private NetconfNode encryptPassword(AddNetconfNodeInput input) {
-        NetconfNodeBuilder builder = new NetconfNodeBuilder();
+    @VisibleForTesting
+    public NetconfNode encryptPassword(final CreateDeviceInput input) {
+        final NetconfNodeBuilder builder = new NetconfNodeBuilder();
         builder.fieldsFrom(input);
 
-        boolean encrypt = input.isEncrypt();
-        LoginPassword loginPassword = (LoginPassword) input.getCredentials();
-        if (encrypt) {
-            String encryptedPassword = encryptionService.encrypt(loginPassword.getPassword());
-            LoginPassword newCreds = new LoginPasswordBuilder().setPassword(encryptedPassword)
-                    .setUsername(loginPassword.getUsername()).build();
-            builder.setCredentials(newCreds);
+        final Credentials credentials = handleEncryption(input.getCredentials());
+        builder.setCredentials(credentials);
+
+        return builder.build();
+    }
+
+    private Credentials handleEncryption(final Credentials credentials) {
+        if (credentials instanceof LoginPw) {
+            final LoginPassword loginPassword = ((LoginPw) credentials).getLoginPassword();
+            final String encryptedPassword =
+                    encryptionService.encrypt(loginPassword.getPassword());
+
+            return new LoginPwBuilder().setLoginPassword(new LoginPasswordBuilder()
+                    .setPassword(encryptedPassword)
+                    .setUsername(loginPassword.getUsername()).build()).build();
         }
 
-        NetconfNode node = builder.build();
-        return node;
+        // nothing else needs to be encrypted
+        return credentials;
     }
 
-    private void writeToConfigDS(NetconfNode node, NodeId nodeId, String topologyId,
+    private void writeToConfigDS(final NetconfNode node, final NodeId nodeId,
                                  final SettableFuture<RpcResult<Void>> futureResult) {
 
-        WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
+        final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
         final InstanceIdentifier<NetworkTopology> networkTopologyId =
                 InstanceIdentifier.builder(NetworkTopology.class).build();
         final InstanceIdentifier<NetconfNode> niid = networkTopologyId.child(Topology.class,
                 new TopologyKey(new TopologyId(topologyId))).child(Node.class,
                 new NodeKey(nodeId)).augmentation(NetconfNode.class);
         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, niid, node, true);
-        final CheckedFuture<Void, TransactionCommitFailedException> future = writeTransaction.submit();
+        final ListenableFuture<Void> future = writeTransaction.submit();
         Futures.addCallback(future, new FutureCallback<Void>() {
 
             @Override
-            public void onSuccess(Void result) {
+            public void onSuccess(final Void result) {
                 LOG.info("add-netconf-node RPC: Added netconf node successfully.");
                 futureResult.set(RpcResultBuilder.<Void>success().build());
             }
 
             @Override
-            public void onFailure(Throwable exception) {
+            public void onFailure(final Throwable exception) {
                 LOG.error("add-netconf-node RPC: Unable to add netconf node.", exception);
                 futureResult.setException(exception);
             }
         }, MoreExecutors.directExecutor());
     }
 
+
+    @Override
+    public Future<RpcResult<Void>> deleteDevice(final DeleteDeviceInput input) {
+        final NodeId nodeId = new NodeId(input.getNodeId());
+
+        final InstanceIdentifier<NetworkTopology> networkTopologyId =
+                InstanceIdentifier.builder(NetworkTopology.class).build();
+        final InstanceIdentifier<Node> niid = networkTopologyId.child(Topology.class,
+                new TopologyKey(new TopologyId(topologyId))).child(Node.class,
+                new NodeKey(nodeId));
+
+        final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
+        wtx.delete(LogicalDatastoreType.CONFIGURATION, niid);
+
+        final ListenableFuture<Void> future = wtx.submit();
+        final SettableFuture<RpcResult<Void>> rpcFuture = SettableFuture.create();
+
+        Futures.addCallback(future, new FutureCallback<Void>() {
+
+            @Override
+            public void onSuccess(final Void result) {
+                LOG.info("delete-device RPC: Removed netconf node successfully.");
+                rpcFuture.set(RpcResultBuilder.<Void>success().build());
+            }
+
+            @Override
+            public void onFailure(final Throwable exception) {
+                LOG.error("delete-device RPC: Unable to remove netconf node.", exception);
+                rpcFuture.setException(exception);
+            }
+        }, MoreExecutors.directExecutor());
+
+        return rpcFuture;
+    }
 }