BUG-9262 Add rpc for remove-device
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / util / NetconfTopologyRPCProvider.java
index ae89354266cf4f21c1e2cd7aa79422c246f3659a..c963022e06d7c00820fac5e6b792b6695117ef9b 100644 (file)
@@ -9,9 +9,9 @@ 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;
@@ -19,8 +19,8 @@ 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;
@@ -57,24 +57,23 @@ 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());
+        final NodeId nodeId = new NodeId(input.getNodeId());
         writeToConfigDS(node, nodeId, topologyId, futureResult);
         return futureResult;
     }
 
     @VisibleForTesting
-    public NetconfNode encryptPassword(AddNetconfNodeInput input) {
-        NetconfNodeBuilder builder = new NetconfNodeBuilder();
+    public NetconfNode encryptPassword(final CreateDeviceInput input) {
+        final NetconfNodeBuilder builder = new NetconfNodeBuilder();
         builder.fieldsFrom(input);
 
         final Credentials credentials = handleEncryption(input.getCredentials());
         builder.setCredentials(credentials);
 
-        NetconfNode node = builder.build();
-        return node;
+        return builder.build();
     }
 
     private Credentials handleEncryption(final Credentials credentials) {
@@ -88,35 +87,69 @@ public class NetconfTopologyRPCProvider implements NetconfNodeTopologyService {
                     .setUsername(loginPassword.getUsername()).build()).build();
         }
 
-        // anything else doesnt need to be encrypted
+        // 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 String topologyId,
                                  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;
+    }
 }