0c5810aa1e4fef153e376c8306a45e9e230c3599
[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 java.util.concurrent.Future;
18 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.CreateDeviceInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.DeleteDeviceInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeTopologyService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPw;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPwBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.login.pw.LoginPassword;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.login.pw.LoginPasswordBuilder;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class NetconfTopologyRPCProvider implements NetconfNodeTopologyService {
46     private final AAAEncryptionService encryptionService;
47     private final DataBroker dataBroker;
48     private final String topologyId;
49     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyRPCProvider.class);
50
51     public NetconfTopologyRPCProvider(final DataBroker dataBroker,
52                                       final AAAEncryptionService encryptionService,
53                                       final String topologyId) {
54         this.dataBroker = dataBroker;
55         this.encryptionService = Preconditions.checkNotNull(encryptionService);
56         this.topologyId = Preconditions.checkNotNull(topologyId);
57     }
58
59     @Override
60     public Future<RpcResult<Void>> createDevice(final CreateDeviceInput input) {
61         final NetconfNode node = this.encryptPassword(input);
62         final SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
63         final NodeId nodeId = new NodeId(input.getNodeId());
64         writeToConfigDS(node, nodeId, futureResult);
65         return futureResult;
66     }
67
68     @VisibleForTesting
69     public NetconfNode encryptPassword(final CreateDeviceInput input) {
70         final NetconfNodeBuilder builder = new NetconfNodeBuilder();
71         builder.fieldsFrom(input);
72
73         final Credentials credentials = handleEncryption(input.getCredentials());
74         builder.setCredentials(credentials);
75
76         return builder.build();
77     }
78
79     private Credentials handleEncryption(final Credentials credentials) {
80         if (credentials instanceof LoginPw) {
81             final LoginPassword loginPassword = ((LoginPw) credentials).getLoginPassword();
82             final String encryptedPassword =
83                     encryptionService.encrypt(loginPassword.getPassword());
84
85             return new LoginPwBuilder().setLoginPassword(new LoginPasswordBuilder()
86                     .setPassword(encryptedPassword)
87                     .setUsername(loginPassword.getUsername()).build()).build();
88         }
89
90         // nothing else needs to be encrypted
91         return credentials;
92     }
93
94     private void writeToConfigDS(final NetconfNode node, final NodeId nodeId,
95                                  final SettableFuture<RpcResult<Void>> futureResult) {
96
97         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
98         final InstanceIdentifier<NetworkTopology> networkTopologyId =
99                 InstanceIdentifier.builder(NetworkTopology.class).build();
100         final InstanceIdentifier<NetconfNode> niid = networkTopologyId.child(Topology.class,
101                 new TopologyKey(new TopologyId(topologyId))).child(Node.class,
102                 new NodeKey(nodeId)).augmentation(NetconfNode.class);
103         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, niid, node, true);
104         final ListenableFuture<Void> future = writeTransaction.submit();
105         Futures.addCallback(future, new FutureCallback<Void>() {
106
107             @Override
108             public void onSuccess(final Void result) {
109                 LOG.info("add-netconf-node RPC: Added netconf node successfully.");
110                 futureResult.set(RpcResultBuilder.<Void>success().build());
111             }
112
113             @Override
114             public void onFailure(final Throwable exception) {
115                 LOG.error("add-netconf-node RPC: Unable to add netconf node.", exception);
116                 futureResult.setException(exception);
117             }
118         }, MoreExecutors.directExecutor());
119     }
120
121
122     @Override
123     public Future<RpcResult<Void>> deleteDevice(final DeleteDeviceInput input) {
124         final NodeId nodeId = new NodeId(input.getNodeId());
125
126         final InstanceIdentifier<NetworkTopology> networkTopologyId =
127                 InstanceIdentifier.builder(NetworkTopology.class).build();
128         final InstanceIdentifier<Node> niid = networkTopologyId.child(Topology.class,
129                 new TopologyKey(new TopologyId(topologyId))).child(Node.class,
130                 new NodeKey(nodeId));
131
132         final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
133         wtx.delete(LogicalDatastoreType.CONFIGURATION, niid);
134
135         final ListenableFuture<Void> future = wtx.submit();
136         final SettableFuture<RpcResult<Void>> rpcFuture = SettableFuture.create();
137
138         Futures.addCallback(future, new FutureCallback<Void>() {
139
140             @Override
141             public void onSuccess(final Void result) {
142                 LOG.info("delete-device RPC: Removed netconf node successfully.");
143                 rpcFuture.set(RpcResultBuilder.<Void>success().build());
144             }
145
146             @Override
147             public void onFailure(final Throwable exception) {
148                 LOG.error("delete-device RPC: Unable to remove netconf node.", exception);
149                 rpcFuture.setException(exception);
150             }
151         }, MoreExecutors.directExecutor());
152
153         return rpcFuture;
154     }
155 }