Split out odl-netconf-device.yang
[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 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.rev221225.credentials.Credentials;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.credentials.credentials.LoginPw;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.credentials.credentials.LoginPwBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.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 public class NetconfTopologyRPCProvider implements NetconfNodeTopologyService {
50     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyRPCProvider.class);
51
52     private final @NonNull InstanceIdentifier<Topology> topologyPath;
53     private final @NonNull AAAEncryptionService encryptionService;
54     private final @NonNull DataBroker dataBroker;
55
56     public NetconfTopologyRPCProvider(final DataBroker dataBroker, final AAAEncryptionService encryptionService,
57                                       final String topologyId) {
58         this.dataBroker = requireNonNull(dataBroker);
59         this.encryptionService = requireNonNull(encryptionService);
60         topologyPath = InstanceIdentifier.builder(NetworkTopology.class)
61                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
62                 .build();
63     }
64
65     protected final @NonNull InstanceIdentifier<Topology> topologyPath() {
66         return topologyPath;
67     }
68
69     @Override
70     public final ListenableFuture<RpcResult<CreateDeviceOutput>> createDevice(final CreateDeviceInput input) {
71         final NetconfNode node = encryptPassword(input);
72         final SettableFuture<RpcResult<CreateDeviceOutput>> futureResult = SettableFuture.create();
73         final NodeId nodeId = new NodeId(input.getNodeId());
74         writeToConfigDS(node, nodeId, futureResult);
75         return futureResult;
76     }
77
78     @Override
79     public final ListenableFuture<RpcResult<DeleteDeviceOutput>> deleteDevice(final DeleteDeviceInput input) {
80         final NodeId nodeId = new NodeId(input.getNodeId());
81
82         final InstanceIdentifier<Node> niid = topologyPath.child(Node.class, new NodeKey(nodeId));
83
84         final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
85         wtx.delete(LogicalDatastoreType.CONFIGURATION, niid);
86
87         final SettableFuture<RpcResult<DeleteDeviceOutput>> rpcFuture = SettableFuture.create();
88
89         wtx.commit().addCallback(new FutureCallback<CommitInfo>() {
90
91             @Override
92             public void onSuccess(final CommitInfo result) {
93                 LOG.info("delete-device RPC: Removed netconf node successfully.");
94                 rpcFuture.set(RpcResultBuilder.success(new DeleteDeviceOutputBuilder().build()).build());
95             }
96
97             @Override
98             public void onFailure(final Throwable exception) {
99                 LOG.error("delete-device RPC: Unable to remove netconf node.", exception);
100                 rpcFuture.setException(exception);
101             }
102         }, MoreExecutors.directExecutor());
103
104         return rpcFuture;
105     }
106
107     @VisibleForTesting
108     NetconfNode encryptPassword(final CreateDeviceInput input) {
109         final NetconfNodeBuilder builder = new NetconfNodeBuilder();
110         builder.fieldsFrom(input);
111
112         return builder.setCredentials(handleEncryption(input.getCredentials()))
113             .build();
114     }
115
116     private Credentials handleEncryption(final Credentials credentials) {
117         if (credentials instanceof LoginPw loginPw) {
118             final var loginPassword = loginPw.getLoginPassword();
119
120             return new LoginPwBuilder()
121                 .setLoginPassword(new LoginPasswordBuilder()
122                     .setUsername(loginPassword.getUsername())
123                     .setPassword(encryptionService.encrypt(loginPassword.getPassword()))
124                     .build())
125                 .build();
126         }
127
128         // nothing else needs to be encrypted
129         return credentials;
130     }
131
132     private void writeToConfigDS(final NetconfNode node, final NodeId nodeId,
133             final SettableFuture<RpcResult<CreateDeviceOutput>> futureResult) {
134
135         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
136         final InstanceIdentifier<NetconfNode> niid = topologyPath.child(Node.class,
137                 new NodeKey(nodeId)).augmentation(NetconfNode.class);
138         writeTransaction.mergeParentStructureMerge(LogicalDatastoreType.CONFIGURATION, niid, node);
139         writeTransaction.commit().addCallback(new FutureCallback<CommitInfo>() {
140
141             @Override
142             public void onSuccess(final CommitInfo result) {
143                 LOG.info("add-netconf-node RPC: Added netconf node successfully.");
144                 futureResult.set(RpcResultBuilder.success(new CreateDeviceOutputBuilder().build()).build());
145             }
146
147             @Override
148             public void onFailure(final Throwable exception) {
149                 LOG.error("add-netconf-node RPC: Unable to add netconf node.", exception);
150                 futureResult.setException(exception);
151             }
152         }, MoreExecutors.directExecutor());
153     }
154 }