Adjust to RPC method signature update
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / util / NetconfTopologyRPCProvider.java
1 /*
2  * Copyright (c) 2017 Brocade Communication Systems and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.sal.connect.util;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.CreateDeviceInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.CreateDeviceOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.CreateDeviceOutputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.DeleteDeviceInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.DeleteDeviceOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.DeleteDeviceOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeTopologyService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPw;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPwBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.login.pw.LoginPassword;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.login.pw.LoginPasswordBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class NetconfTopologyRPCProvider implements NetconfNodeTopologyService {
49     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyRPCProvider.class);
50
51     private final AAAEncryptionService encryptionService;
52     private final DataBroker dataBroker;
53     private final String topologyId;
54
55     public NetconfTopologyRPCProvider(final DataBroker dataBroker,
56                                       final AAAEncryptionService encryptionService,
57                                       final String topologyId) {
58         this.dataBroker = dataBroker;
59         this.encryptionService = Preconditions.checkNotNull(encryptionService);
60         this.topologyId = Preconditions.checkNotNull(topologyId);
61     }
62
63     @Override
64     public ListenableFuture<RpcResult<CreateDeviceOutput>> createDevice(final CreateDeviceInput input) {
65         final NetconfNode node = this.encryptPassword(input);
66         final SettableFuture<RpcResult<CreateDeviceOutput>> futureResult = SettableFuture.create();
67         final NodeId nodeId = new NodeId(input.getNodeId());
68         writeToConfigDS(node, nodeId, futureResult);
69         return futureResult;
70     }
71
72     @VisibleForTesting
73     public NetconfNode encryptPassword(final CreateDeviceInput input) {
74         final NetconfNodeBuilder builder = new NetconfNodeBuilder();
75         builder.fieldsFrom(input);
76
77         final Credentials credentials = handleEncryption(input.getCredentials());
78         builder.setCredentials(credentials);
79
80         return builder.build();
81     }
82
83     private Credentials handleEncryption(final Credentials credentials) {
84         if (credentials instanceof LoginPw) {
85             final LoginPassword loginPassword = ((LoginPw) credentials).getLoginPassword();
86             final String encryptedPassword =
87                     encryptionService.encrypt(loginPassword.getPassword());
88
89             return new LoginPwBuilder().setLoginPassword(new LoginPasswordBuilder()
90                     .setPassword(encryptedPassword)
91                     .setUsername(loginPassword.getUsername()).build()).build();
92         }
93
94         // nothing else needs to be encrypted
95         return credentials;
96     }
97
98     private void writeToConfigDS(final NetconfNode node, final NodeId nodeId,
99             final SettableFuture<RpcResult<CreateDeviceOutput>> futureResult) {
100
101         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
102         final InstanceIdentifier<NetworkTopology> networkTopologyId =
103                 InstanceIdentifier.builder(NetworkTopology.class).build();
104         final InstanceIdentifier<NetconfNode> niid = networkTopologyId.child(Topology.class,
105                 new TopologyKey(new TopologyId(topologyId))).child(Node.class,
106                 new NodeKey(nodeId)).augmentation(NetconfNode.class);
107         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, niid, node, true);
108         final ListenableFuture<Void> future = writeTransaction.submit();
109         Futures.addCallback(future, new FutureCallback<Void>() {
110
111             @Override
112             public void onSuccess(final Void result) {
113                 LOG.info("add-netconf-node RPC: Added netconf node successfully.");
114                 futureResult.set(RpcResultBuilder.success(new CreateDeviceOutputBuilder().build()).build());
115             }
116
117             @Override
118             public void onFailure(final Throwable exception) {
119                 LOG.error("add-netconf-node RPC: Unable to add netconf node.", exception);
120                 futureResult.setException(exception);
121             }
122         }, MoreExecutors.directExecutor());
123     }
124
125
126     @Override
127     public ListenableFuture<RpcResult<DeleteDeviceOutput>> deleteDevice(final DeleteDeviceInput input) {
128         final NodeId nodeId = new NodeId(input.getNodeId());
129
130         final InstanceIdentifier<NetworkTopology> networkTopologyId =
131                 InstanceIdentifier.builder(NetworkTopology.class).build();
132         final InstanceIdentifier<Node> niid = networkTopologyId.child(Topology.class,
133                 new TopologyKey(new TopologyId(topologyId))).child(Node.class,
134                 new NodeKey(nodeId));
135
136         final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
137         wtx.delete(LogicalDatastoreType.CONFIGURATION, niid);
138
139         final ListenableFuture<Void> future = wtx.submit();
140         final SettableFuture<RpcResult<DeleteDeviceOutput>> rpcFuture = SettableFuture.create();
141
142         Futures.addCallback(future, new FutureCallback<Void>() {
143
144             @Override
145             public void onSuccess(final Void result) {
146                 LOG.info("delete-device RPC: Removed netconf node successfully.");
147                 rpcFuture.set(RpcResultBuilder.success(new DeleteDeviceOutputBuilder().build()).build());
148             }
149
150             @Override
151             public void onFailure(final Throwable exception) {
152                 LOG.error("delete-device RPC: Unable to remove netconf node.", exception);
153                 rpcFuture.setException(exception);
154             }
155         }, MoreExecutors.directExecutor());
156
157         return rpcFuture;
158     }
159 }