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 java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.UUID;
20 import java.util.stream.Collectors;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
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;
53 private final MountPointService mountPointService;
55 public NetconfCommandsImpl(final DataBroker db, final MountPointService mountPointService) {
56 LOG.debug("NetconfConsoleProviderImpl initialized");
58 this.mountPointService = mountPointService;
62 public Map<String, Map<String, String>> listDevices() {
63 final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
64 final Map<String, Map<String, String>> netconfNodes = new HashMap<>();
65 for (final Node node : topology.getNode()) {
66 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
67 final Map<String, String> attributes = new HashMap<>();
68 attributes.put(NetconfConsoleConstants.NETCONF_ID, node.getNodeId().getValue());
69 attributes.put(NetconfConsoleConstants.NETCONF_IP, netconfNode.getHost().getIpAddress().getIpv4Address().getValue());
70 attributes.put(NetconfConsoleConstants.NETCONF_PORT, netconfNode.getPort().getValue().toString());
71 attributes.put(NetconfConsoleConstants.STATUS, netconfNode.getConnectionStatus().name().toLowerCase());
72 netconfNodes.put(node.getNodeId().getValue(), attributes);
78 public Map<String, Map<String, List<String>>> showDevice(final String deviceIp, final String devicePort) {
79 final Map<String, Map<String, List<String>>> device = new HashMap<>();
80 List<Node> nodeList = new ArrayList<>();
81 if (devicePort != null) {
82 nodeList.add(NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker));
84 nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
86 if (nodeList != null) {
87 for (final Node node : nodeList) {
89 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
90 final Map<String, List<String>> attributes = new HashMap<>();
91 attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
92 attributes.put(NetconfConsoleConstants.NETCONF_IP, ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
93 attributes.put(NetconfConsoleConstants.NETCONF_PORT, ImmutableList.of(netconfNode.getPort().getValue().toString()));
94 attributes.put(NetconfConsoleConstants.STATUS, ImmutableList.of(netconfNode.getConnectionStatus().name()));
95 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
96 netconfNode.getAvailableCapabilities().getAvailableCapability().stream().map(AvailableCapability::getCapability).collect(Collectors.toList()));
97 device.put(node.getNodeId().getValue(), attributes);
105 public Map<String, Map<String, List<String>>> showDevice(final String deviceId) {
106 final Map<String, Map<String, List<String>>> device = new HashMap<>();
107 final List<Node> nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker);
108 if (nodeList != null && nodeList.size() > 0) {
109 for (final Node node : nodeList) {
110 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
111 final Map<String, List<String>> attributes = new HashMap<>();
112 attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
113 attributes.put(NetconfConsoleConstants.NETCONF_IP, ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
114 attributes.put(NetconfConsoleConstants.NETCONF_PORT, ImmutableList.of(netconfNode.getPort().getValue().toString()));
115 attributes.put(NetconfConsoleConstants.STATUS, ImmutableList.of(netconfNode.getConnectionStatus().name()));
116 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
117 netconfNode.getAvailableCapabilities().getAvailableCapability().stream().map(AvailableCapability::getCapability).collect(Collectors.toList()));
118 device.put(node.getNodeId().getValue(), attributes);
125 public void connectDevice(NetconfNode netconfNode, String netconfNodeId) {
127 if (!Strings.isNullOrEmpty(netconfNodeId)) {
128 nodeId = new NodeId(netconfNodeId);
130 nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
132 final Node node = new NodeBuilder()
133 .setKey(new NodeKey(nodeId))
135 .addAugmentation(NetconfNode.class, netconfNode)
138 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
139 transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
141 Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
144 public void onSuccess(Void result) {
145 LOG.debug("NetconfNode={} created successfully", netconfNode);
149 public void onFailure(Throwable t) {
150 LOG.error("Failed to created NetconfNode={}", netconfNode);
151 throw new RuntimeException(t);
157 public boolean disconnectDevice(String netconfNodeId) {
158 boolean result = false;
159 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
160 InstanceIdentifier<Node> iid = NetconfIidFactory.netconfNodeIid(netconfNodeId);
161 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
164 LOG.debug("Deleting netconf node: {}", netconfNodeId);
165 transaction.submit().checkedGet();
167 } catch (final TransactionCommitFailedException e) {
168 LOG.error("Unable to remove node with Iid {}", iid, e);
174 public boolean disconnectDevice(final String deviceIp, final String devicePort) {
175 final String netconfNodeId = NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker).getNodeId().getValue();
176 return disconnectDevice(netconfNodeId);
180 public String updateDevice(final String netconfNodeId, String username, String password, Map<String, String> updated) {
181 final Node node = NetconfConsoleUtils.read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(netconfNodeId), dataBroker);
183 if (node != null && node.getAugmentation(NetconfNode.class) != null) {
184 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
186 // Get NETCONF attributes to update if present else get their original values from NetconfNode instance
187 final String deviceIp = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_IP)) ?
188 netconfNode.getHost().getIpAddress().getIpv4Address().getValue() : updated.get(NetconfConsoleConstants.NETCONF_IP);
189 final String devicePort = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_PORT)) ?
190 netconfNode.getPort().getValue().toString() : updated.get(NetconfConsoleConstants.NETCONF_PORT);
191 final Boolean tcpOnly = (updated.get(NetconfConsoleConstants.TCP_ONLY).equals("true")) ? true : false;
192 final String newUsername = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.USERNAME)) ? updated.get(NetconfConsoleConstants.USERNAME) : username;
193 final String newPassword = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.PASSWORD)) ? updated.get(NetconfConsoleConstants.PASSWORD) : password;
195 final Credentials credentials = new LoginPasswordBuilder().setPassword(newPassword).setUsername(newUsername).build();
196 final NetconfNode updatedNetconfNode = new NetconfNodeBuilder()
197 .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
198 .setPort(new PortNumber(Integer.decode(devicePort)))
200 .setCredentials(credentials)
203 final Node updatedNode = new NodeBuilder()
204 .setKey(node.getKey())
205 .setNodeId(node.getNodeId())
206 .addAugmentation(NetconfNode.class, updatedNetconfNode)
209 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
210 transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
212 Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
215 public void onSuccess(Void result) {
216 LOG.debug("NetconfNode={} updated successfully", netconfNode);
220 public void onFailure(Throwable t) {
221 LOG.error("Failed to updated NetconfNode={}", netconfNode);
222 throw new RuntimeException(t);
226 return "NETCONF node: " + netconfNodeId + " updated successfully.";
228 return "NETCONF node: " + netconfNodeId + " does not exist to update";