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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
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.rev100924.Host;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.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.credentials.Credentials;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ModuleType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.Modules;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.Module;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.ModuleKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.connector.netconf.rev150803.SalNetconfConnector;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class NetconfCommandsImpl implements NetconfCommands {
55 private static final Logger LOG = LoggerFactory.getLogger(NetconfCommandsImpl.class);
57 private final DataBroker dataBroker;
58 private final MountPointService mountPointService;
60 public NetconfCommandsImpl(final DataBroker db, final MountPointService mountPointService) {
61 LOG.debug("NetconfConsoleProviderImpl initialized");
63 this.mountPointService = mountPointService;
67 public Map<String, Map<String, String>> listDevices() {
68 final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
69 final Map<String, Map<String, String>> netconfNodes = new HashMap<>();
70 for (final Node node : topology.getNode()) {
71 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
72 final Map<String, String> attributes = new HashMap<>();
73 attributes.put(NetconfConsoleConstants.NETCONF_ID, node.getNodeId().getValue());
74 attributes.put(NetconfConsoleConstants.NETCONF_IP, 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, ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
98 attributes.put(NetconfConsoleConstants.NETCONF_PORT, ImmutableList.of(netconfNode.getPort().getValue().toString()));
99 attributes.put(NetconfConsoleConstants.STATUS, ImmutableList.of(netconfNode.getConnectionStatus().name()));
100 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, netconfNode.getAvailableCapabilities().getAvailableCapability());
101 device.put(node.getNodeId().getValue(), attributes);
109 public Map<String, Map<String, List<String>>> showDevice(final String deviceId) {
110 final Map<String, Map<String, List<String>>> device = new HashMap<>();
111 final List<Node> nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker);
112 if (nodeList != null && nodeList.size() > 0) {
113 for (final Node node : nodeList) {
114 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
115 final Map<String, List<String>> attributes = new HashMap<>();
116 attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
117 attributes.put(NetconfConsoleConstants.NETCONF_IP, ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
118 attributes.put(NetconfConsoleConstants.NETCONF_PORT, ImmutableList.of(netconfNode.getPort().getValue().toString()));
119 attributes.put(NetconfConsoleConstants.STATUS, ImmutableList.of(netconfNode.getConnectionStatus().name()));
120 attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, netconfNode.getAvailableCapabilities().getAvailableCapability());
121 device.put(node.getNodeId().getValue(), attributes);
128 public void connectDevice(NetconfNode netconfNode, String netconfNodeId) {
130 if (!Strings.isNullOrEmpty(netconfNodeId)) {
131 nodeId = new NodeId(netconfNodeId);
133 nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
135 final Node node = new NodeBuilder()
136 .setKey(new NodeKey(nodeId))
138 .addAugmentation(NetconfNode.class, netconfNode)
141 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
142 transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
144 Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
147 public void onSuccess(Void result) {
148 LOG.debug("NetconfNode={} created successfully", netconfNode);
152 public void onFailure(Throwable t) {
153 LOG.error("Failed to created NetconfNode={}", netconfNode);
154 throw new RuntimeException(t);
160 public boolean disconnectDevice(String netconfNodeId) {
161 boolean result = false;
162 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
163 InstanceIdentifier<Node> iid = NetconfIidFactory.netconfNodeIid(netconfNodeId);
164 transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
167 LOG.debug("Deleting netconf node: {}", netconfNodeId);
168 transaction.submit().checkedGet();
170 } catch (final TransactionCommitFailedException e) {
171 LOG.error("Unable to remove node with Iid {}", iid, e);
177 public boolean disconnectDevice(final String deviceIp, final String devicePort) {
178 final String netconfNodeId = NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker).getNodeId().getValue();
179 return disconnectDevice(netconfNodeId);
183 public String updateDevice(final String netconfNodeId, String username, String password, Map<String, String> updated) {
184 final Node node = NetconfConsoleUtils.read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(netconfNodeId), dataBroker);
186 if (node != null && node.getAugmentation(NetconfNode.class) != null) {
187 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
189 // Get NETCONF attributes to update if present else get their original values from NetconfNode instance
190 final String deviceIp = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_IP)) ?
191 netconfNode.getHost().getIpAddress().getIpv4Address().getValue() : updated.get(NetconfConsoleConstants.NETCONF_IP);
192 final String devicePort = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_PORT)) ?
193 netconfNode.getPort().getValue().toString() : updated.get(NetconfConsoleConstants.NETCONF_PORT);
194 final Boolean tcpOnly = (updated.get(NetconfConsoleConstants.TCP_ONLY).equals("true")) ? true : false;
195 final String newUsername = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.USERNAME)) ? updated.get(NetconfConsoleConstants.USERNAME) : username;
196 final String newPassword = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.PASSWORD)) ? updated.get(NetconfConsoleConstants.PASSWORD) : password;
198 final Credentials credentials = new LoginPasswordBuilder().setPassword(newPassword).setUsername(newUsername).build();
199 final NetconfNode updatedNetconfNode = new NetconfNodeBuilder()
200 .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
201 .setPort(new PortNumber(Integer.decode(devicePort)))
203 .setCredentials(credentials)
206 final Node updatedNode = new NodeBuilder()
207 .setKey(node.getKey())
208 .setNodeId(node.getNodeId())
209 .addAugmentation(NetconfNode.class, updatedNetconfNode)
212 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
213 transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
215 Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
218 public void onSuccess(Void result) {
219 LOG.debug("NetconfNode={} updated successfully", netconfNode);
223 public void onFailure(Throwable t) {
224 LOG.error("Failed to updated NetconfNode={}", netconfNode);
225 throw new RuntimeException(t);
229 return "NETCONF node: " + netconfNodeId + " updated successfully.";
231 return "NETCONF node: " + netconfNodeId + " does not exist to update";