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