2 * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.netconf.console.impl;
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;
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;
48 public class NetconfCommandsImpl implements NetconfCommands {
50 private static final Logger LOG = LoggerFactory.getLogger(NetconfCommandsImpl.class);
52 private final DataBroker dataBroker;
54 public NetconfCommandsImpl(final DataBroker db) {
55 LOG.debug("NetconfConsoleProviderImpl initialized");
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<>();
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);
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));
87 nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
89 if (nodeList != null) {
90 for (final Node node : nodeList) {
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);
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);
136 public void connectDevice(NetconfNode netconfNode, String netconfNodeId) {
138 if (!Strings.isNullOrEmpty(netconfNodeId)) {
139 nodeId = new NodeId(netconfNodeId);
141 nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
143 final Node node = new NodeBuilder()
144 .setKey(new NodeKey(nodeId))
146 .addAugmentation(NetconfNode.class, netconfNode)
149 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
150 transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
152 Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
155 public void onSuccess(Void result) {
156 LOG.debug("NetconfNode={} created successfully", netconfNode);
160 public void onFailure(Throwable throwable) {
161 LOG.error("Failed to created NetconfNode={}", netconfNode);
162 throw new RuntimeException(throwable);
164 }, MoreExecutors.directExecutor());
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);
175 LOG.debug("Deleting netconf node: {}", netconfNodeId);
176 transaction.submit().checkedGet();
178 } catch (final TransactionCommitFailedException e) {
179 LOG.error("Unable to remove node with Iid {}", iid, e);
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);
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);
197 if (node != null && node.getAugmentation(NetconfNode.class) != null) {
198 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
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;
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)))
220 .setSchemaless(isSchemaless)
221 .setCredentials(credentials)
224 final Node updatedNode = new NodeBuilder()
225 .setKey(node.getKey())
226 .setNodeId(node.getNodeId())
227 .addAugmentation(NetconfNode.class, updatedNetconfNode)
230 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
231 transaction.put(LogicalDatastoreType.CONFIGURATION,
232 NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
234 Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
237 public void onSuccess(Void result) {
238 LOG.debug("NetconfNode={} updated successfully", netconfNode);
242 public void onFailure(Throwable throwable) {
243 LOG.error("Failed to updated NetconfNode={}", netconfNode);
244 throw new RuntimeException(throwable);
246 }, MoreExecutors.directExecutor());
248 return "NETCONF node: " + netconfNodeId + " updated successfully.";
250 return "NETCONF node: " + netconfNodeId + " does not exist to update";