Replace deprecated Futures.addCallback by the newer version
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / impl / NetconfCommandsImpl.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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
9 package org.opendaylight.netconf.console.impl;
10
11 import com.google.common.base.Strings;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.UUID;
21 import java.util.stream.Collectors;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.netconf.console.api.NetconfCommands;
27 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
28 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
29 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
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.NodeBuilder;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class NetconfCommandsImpl implements NetconfCommands {
49
50     private static final Logger LOG = LoggerFactory.getLogger(NetconfCommandsImpl.class);
51
52     private final DataBroker dataBroker;
53
54     public NetconfCommandsImpl(final DataBroker db) {
55         LOG.debug("NetconfConsoleProviderImpl initialized");
56         this.dataBroker = db;
57     }
58
59     @Override
60     public Map<String, Map<String, String>> listDevices() {
61         final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.OPERATIONAL,
62                 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
63         if (topology == null) {
64             return new HashMap<>();
65         }
66         final Map<String, Map<String, String>> netconfNodes = new HashMap<>();
67         for (final Node node : topology.getNode()) {
68             final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
69             final Map<String, String> attributes = new HashMap<>();
70             attributes.put(NetconfConsoleConstants.NETCONF_ID, node.getNodeId().getValue());
71             attributes.put(NetconfConsoleConstants.NETCONF_IP,
72                     netconfNode.getHost().getIpAddress().getIpv4Address().getValue());
73             attributes.put(NetconfConsoleConstants.NETCONF_PORT, netconfNode.getPort().getValue().toString());
74             attributes.put(NetconfConsoleConstants.STATUS, netconfNode.getConnectionStatus().name().toLowerCase());
75             netconfNodes.put(node.getNodeId().getValue(), attributes);
76         }
77         return netconfNodes;
78     }
79
80     @Override
81     public Map<String, Map<String, List<String>>> showDevice(final String deviceIp, final String devicePort) {
82         final Map<String, Map<String, List<String>>> device = new HashMap<>();
83         List<Node> nodeList = new ArrayList<>();
84         if (devicePort != null) {
85             nodeList.add(NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker));
86         } else {
87             nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
88         }
89         if (nodeList != null) {
90             for (final Node node : nodeList) {
91                 if (node != null) {
92                     final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
93                     final Map<String, List<String>> attributes = new HashMap<>();
94                     attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
95                     attributes.put(NetconfConsoleConstants.NETCONF_IP,
96                             ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
97                     attributes.put(NetconfConsoleConstants.NETCONF_PORT,
98                             ImmutableList.of(netconfNode.getPort().getValue().toString()));
99                     attributes.put(NetconfConsoleConstants.STATUS,
100                             ImmutableList.of(netconfNode.getConnectionStatus().name()));
101                     attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
102                             netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
103                                     .map(AvailableCapability::getCapability).collect(Collectors.toList()));
104                     device.put(node.getNodeId().getValue(), attributes);
105                 }
106             }
107         }
108         return device;
109     }
110
111     @Override
112     public Map<String, Map<String, List<String>>> showDevice(final String deviceId) {
113         final Map<String, Map<String, List<String>>> device = new HashMap<>();
114         final List<Node> nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker);
115         if (nodeList != null && nodeList.size() > 0) {
116             for (final Node node : nodeList) {
117                 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
118                 final Map<String, List<String>> attributes = new HashMap<>();
119                 attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
120                 attributes.put(NetconfConsoleConstants.NETCONF_IP,
121                         ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
122                 attributes.put(NetconfConsoleConstants.NETCONF_PORT,
123                         ImmutableList.of(netconfNode.getPort().getValue().toString()));
124                 attributes.put(NetconfConsoleConstants.STATUS,
125                         ImmutableList.of(netconfNode.getConnectionStatus().name()));
126                 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
127                         netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
128                                 .map(AvailableCapability::getCapability).collect(Collectors.toList()));
129                 device.put(node.getNodeId().getValue(), attributes);
130             }
131         }
132         return device;
133     }
134
135     @Override
136     public void connectDevice(NetconfNode netconfNode, String netconfNodeId) {
137         final NodeId nodeId;
138         if (!Strings.isNullOrEmpty(netconfNodeId)) {
139             nodeId = new NodeId(netconfNodeId);
140         } else {
141             nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
142         }
143         final Node node = new NodeBuilder()
144                 .setKey(new NodeKey(nodeId))
145                 .setNodeId(nodeId)
146                 .addAugmentation(NetconfNode.class, netconfNode)
147                 .build();
148
149         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
150         transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
151
152         Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
153
154             @Override
155             public void onSuccess(Void result) {
156                 LOG.debug("NetconfNode={} created successfully", netconfNode);
157             }
158
159             @Override
160             public void onFailure(Throwable throwable) {
161                 LOG.error("Failed to created NetconfNode={}", netconfNode);
162                 throw new RuntimeException(throwable);
163             }
164         }, MoreExecutors.directExecutor());
165     }
166
167     @Override
168     public boolean disconnectDevice(String netconfNodeId) {
169         boolean result = false;
170         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
171         InstanceIdentifier<Node> iid = NetconfIidFactory.netconfNodeIid(netconfNodeId);
172         transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
173
174         try {
175             LOG.debug("Deleting netconf node: {}", netconfNodeId);
176             transaction.submit().checkedGet();
177             result = true;
178         } catch (final TransactionCommitFailedException e) {
179             LOG.error("Unable to remove node with Iid {}", iid, e);
180         }
181         return result;
182     }
183
184     @Override
185     public boolean disconnectDevice(final String deviceIp, final String devicePort) {
186         final String netconfNodeId = NetconfConsoleUtils
187                 .getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker).getNodeId().getValue();
188         return disconnectDevice(netconfNodeId);
189     }
190
191     @Override
192     public String updateDevice(final String netconfNodeId, String username, String password,
193                                Map<String, String> updated) {
194         final Node node = NetconfConsoleUtils
195                 .read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(netconfNodeId), dataBroker);
196
197         if (node != null && node.getAugmentation(NetconfNode.class) != null) {
198             final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
199
200             // Get NETCONF attributes to update if present else get their original values from NetconfNode instance
201             final String deviceIp = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_IP))
202                     ? netconfNode.getHost().getIpAddress().getIpv4Address().getValue()
203                     : updated.get(NetconfConsoleConstants.NETCONF_IP);
204             final String devicePort = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_PORT))
205                     ? netconfNode.getPort().getValue().toString() : updated.get(NetconfConsoleConstants.NETCONF_PORT);
206             final Boolean tcpOnly = updated.get(NetconfConsoleConstants.TCP_ONLY).equals("true");
207             final Boolean isSchemaless =
208                     updated.get(NetconfConsoleConstants.SCHEMALESS).equals("true");
209             final String newUsername = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.USERNAME))
210                     ? updated.get(NetconfConsoleConstants.USERNAME) : username;
211             final String newPassword = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.PASSWORD))
212                     ? updated.get(NetconfConsoleConstants.PASSWORD) : password;
213
214             final Credentials credentials =
215                     new LoginPasswordBuilder().setPassword(newPassword).setUsername(newUsername).build();
216             final NetconfNode updatedNetconfNode = new NetconfNodeBuilder()
217                     .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
218                     .setPort(new PortNumber(Integer.decode(devicePort)))
219                     .setTcpOnly(tcpOnly)
220                     .setSchemaless(isSchemaless)
221                     .setCredentials(credentials)
222                     .build();
223
224             final Node updatedNode = new NodeBuilder()
225                     .setKey(node.getKey())
226                     .setNodeId(node.getNodeId())
227                     .addAugmentation(NetconfNode.class, updatedNetconfNode)
228                     .build();
229
230             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
231             transaction.put(LogicalDatastoreType.CONFIGURATION,
232                     NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
233
234             Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
235
236                 @Override
237                 public void onSuccess(Void result) {
238                     LOG.debug("NetconfNode={} updated successfully", netconfNode);
239                 }
240
241                 @Override
242                 public void onFailure(Throwable throwable) {
243                     LOG.error("Failed to updated NetconfNode={}", netconfNode);
244                     throw new RuntimeException(throwable);
245                 }
246             }, MoreExecutors.directExecutor());
247
248             return "NETCONF node: " + netconfNodeId + " updated successfully.";
249         } else {
250             return "NETCONF node: " + netconfNodeId + " does not exist to update";
251         }
252     }
253 }