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.Collections;
18 import java.util.HashMap;
19 import java.util.List;
21 import java.util.UUID;
22 import java.util.stream.Collectors;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.netconf.console.api.NetconfCommands;
28 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
29 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
30 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class NetconfCommandsImpl implements NetconfCommands {
52 private static final Logger LOG = LoggerFactory.getLogger(NetconfCommandsImpl.class);
54 private final DataBroker dataBroker;
56 public NetconfCommandsImpl(final DataBroker db) {
57 LOG.debug("NetconfConsoleProviderImpl initialized");
62 public Map<String, Map<String, String>> listDevices() {
63 final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.OPERATIONAL,
64 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
65 if (topology == null) {
66 return new HashMap<>();
68 final Map<String, Map<String, String>> netconfNodes = new HashMap<>();
69 for (final Node node : topology.getNode()) {
70 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
71 final Map<String, String> attributes = new HashMap<>();
72 attributes.put(NetconfConsoleConstants.NETCONF_ID, node.getNodeId().getValue());
73 attributes.put(NetconfConsoleConstants.NETCONF_IP,
74 netconfNode.getHost().getIpAddress().getIpv4Address().getValue());
75 attributes.put(NetconfConsoleConstants.NETCONF_PORT, netconfNode.getPort().getValue().toString());
76 attributes.put(NetconfConsoleConstants.STATUS, netconfNode.getConnectionStatus().name().toLowerCase());
77 netconfNodes.put(node.getNodeId().getValue(), attributes);
83 public Map<String, Map<String, List<String>>> showDevice(final String deviceIp, final String devicePort) {
84 final Map<String, Map<String, List<String>>> device = new HashMap<>();
85 List<Node> nodeList = new ArrayList<>();
86 if (devicePort != null) {
87 nodeList.add(NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker));
89 nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
91 if (nodeList != null) {
92 for (final Node node : nodeList) {
94 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
95 final Map<String, List<String>> attributes = new HashMap<>();
96 attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
97 attributes.put(NetconfConsoleConstants.NETCONF_IP,
98 ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
99 attributes.put(NetconfConsoleConstants.NETCONF_PORT,
100 ImmutableList.of(netconfNode.getPort().getValue().toString()));
101 attributes.put(NetconfConsoleConstants.STATUS,
102 ImmutableList.of(netconfNode.getConnectionStatus().name()));
103 if (netconfNode.getConnectionStatus().equals(
104 NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
105 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
106 netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
107 .map(AvailableCapability::getCapability).collect(Collectors.toList()));
109 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList(""));
111 device.put(node.getNodeId().getValue(), attributes);
119 public Map<String, Map<String, List<String>>> showDevice(final String deviceId) {
120 final Map<String, Map<String, List<String>>> device = new HashMap<>();
121 final List<Node> nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker);
122 if (nodeList != null && nodeList.size() > 0) {
123 for (final Node node : nodeList) {
124 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
125 final Map<String, List<String>> attributes = new HashMap<>();
126 attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
127 attributes.put(NetconfConsoleConstants.NETCONF_IP,
128 ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
129 attributes.put(NetconfConsoleConstants.NETCONF_PORT,
130 ImmutableList.of(netconfNode.getPort().getValue().toString()));
131 attributes.put(NetconfConsoleConstants.STATUS,
132 ImmutableList.of(netconfNode.getConnectionStatus().name()));
133 if (netconfNode.getConnectionStatus().equals(NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
134 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
135 netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
136 .map(AvailableCapability::getCapability).collect(Collectors.toList()));
138 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList(""));
140 device.put(node.getNodeId().getValue(), attributes);
147 public void connectDevice(NetconfNode netconfNode, String netconfNodeId) {
149 if (!Strings.isNullOrEmpty(netconfNodeId)) {
150 nodeId = new NodeId(netconfNodeId);
152 nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
154 final Node node = new NodeBuilder()
155 .setKey(new NodeKey(nodeId))
157 .addAugmentation(NetconfNode.class, netconfNode)
160 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
161 transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
163 Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
166 public void onSuccess(Void result) {
167 LOG.debug("NetconfNode={} created successfully", netconfNode);
171 public void onFailure(Throwable throwable) {
172 LOG.error("Failed to created NetconfNode={}", netconfNode);
173 throw new RuntimeException(throwable);
175 }, MoreExecutors.directExecutor());
179 public boolean disconnectDevice(String netconfNodeId) {
180 boolean result = false;
181 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
182 InstanceIdentifier<Node> iid = NetconfIidFactory.netconfNodeIid(netconfNodeId);
183 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
186 LOG.debug("Deleting netconf node: {}", netconfNodeId);
187 transaction.submit().checkedGet();
189 } catch (final TransactionCommitFailedException e) {
190 LOG.error("Unable to remove node with Iid {}", iid, e);
196 public boolean disconnectDevice(final String deviceIp, final String devicePort) {
197 final String netconfNodeId = NetconfConsoleUtils
198 .getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker).getNodeId().getValue();
199 return disconnectDevice(netconfNodeId);
203 public String updateDevice(final String netconfNodeId, String username, String password,
204 Map<String, String> updated) {
205 final Node node = NetconfConsoleUtils
206 .read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(netconfNodeId), dataBroker);
208 if (node != null && node.getAugmentation(NetconfNode.class) != null) {
209 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
211 // Get NETCONF attributes to update if present else get their original values from NetconfNode instance
212 final String deviceIp = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_IP))
213 ? netconfNode.getHost().getIpAddress().getIpv4Address().getValue()
214 : updated.get(NetconfConsoleConstants.NETCONF_IP);
215 final String devicePort = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_PORT))
216 ? netconfNode.getPort().getValue().toString() : updated.get(NetconfConsoleConstants.NETCONF_PORT);
217 final Boolean tcpOnly = updated.get(NetconfConsoleConstants.TCP_ONLY).equals("true");
218 final Boolean isSchemaless =
219 updated.get(NetconfConsoleConstants.SCHEMALESS).equals("true");
220 final String newUsername = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.USERNAME))
221 ? updated.get(NetconfConsoleConstants.USERNAME) : username;
222 final String newPassword = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.PASSWORD))
223 ? updated.get(NetconfConsoleConstants.PASSWORD) : password;
225 final Credentials credentials =
226 new LoginPasswordBuilder().setPassword(newPassword).setUsername(newUsername).build();
227 final NetconfNode updatedNetconfNode = new NetconfNodeBuilder()
228 .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
229 .setPort(new PortNumber(Integer.decode(devicePort)))
231 .setSchemaless(isSchemaless)
232 .setCredentials(credentials)
235 final Node updatedNode = new NodeBuilder()
236 .setKey(node.getKey())
237 .setNodeId(node.getNodeId())
238 .addAugmentation(NetconfNode.class, updatedNetconfNode)
241 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
242 transaction.put(LogicalDatastoreType.CONFIGURATION,
243 NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
245 Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
248 public void onSuccess(Void result) {
249 LOG.debug("NetconfNode={} updated successfully", netconfNode);
253 public void onFailure(Throwable throwable) {
254 LOG.error("Failed to updated NetconfNode={}", netconfNode);
255 throw new RuntimeException(throwable);
257 }, MoreExecutors.directExecutor());
259 return "NETCONF node: " + netconfNodeId + " updated successfully.";
261 return "NETCONF node: " + netconfNodeId + " does not exist to update";