Add blueprint wiring for netconf-console
[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 java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
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.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.netconf.console.api.NetconfCommands;
26 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
27 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
28 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
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.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class NetconfCommandsImpl implements NetconfCommands {
48
49     private static final Logger LOG = LoggerFactory.getLogger(NetconfCommandsImpl.class);
50
51     private final DataBroker dataBroker;
52
53     public NetconfCommandsImpl(final DataBroker db) {
54         LOG.debug("NetconfConsoleProviderImpl initialized");
55         this.dataBroker = db;
56     }
57
58     @Override
59     public Map<String, Map<String, String>> listDevices() {
60         final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
61         if (topology == null) {
62             return new HashMap<>();
63         }
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);
73         }
74         return netconfNodes;
75     }
76
77     @Override
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));
83         } else {
84             nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
85         }
86         if (nodeList != null) {
87             for (final Node node : nodeList) {
88                 if (node != null) {
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);
98                 }
99             }
100         }
101         return device;
102     }
103
104     @Override
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);
119             }
120         }
121         return device;
122     }
123
124     @Override
125     public void connectDevice(NetconfNode netconfNode, String netconfNodeId) {
126         final NodeId nodeId;
127         if (!Strings.isNullOrEmpty(netconfNodeId)) {
128             nodeId = new NodeId(netconfNodeId);
129         } else {
130             nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
131         }
132         final Node node = new NodeBuilder()
133                 .setKey(new NodeKey(nodeId))
134                 .setNodeId(nodeId)
135                 .addAugmentation(NetconfNode.class, netconfNode)
136                 .build();
137
138         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
139         transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
140
141         Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
142
143             @Override
144             public void onSuccess(Void result) {
145                 LOG.debug("NetconfNode={} created successfully", netconfNode);
146             }
147
148             @Override
149             public void onFailure(Throwable t) {
150                 LOG.error("Failed to created NetconfNode={}", netconfNode);
151                 throw new RuntimeException(t);
152             }
153         });
154     }
155
156     @Override
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);
162
163         try {
164             LOG.debug("Deleting netconf node: {}", netconfNodeId);
165             transaction.submit().checkedGet();
166             result = true;
167         } catch (final TransactionCommitFailedException e) {
168             LOG.error("Unable to remove node with Iid {}", iid, e);
169         }
170         return result;
171     }
172
173     @Override
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);
177     }
178
179     @Override
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);
182
183         if (node != null && node.getAugmentation(NetconfNode.class) != null) {
184             final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
185
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;
194
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)))
199                     .setTcpOnly(tcpOnly)
200                     .setCredentials(credentials)
201                     .build();
202
203             final Node updatedNode = new NodeBuilder()
204                     .setKey(node.getKey())
205                     .setNodeId(node.getNodeId())
206                     .addAugmentation(NetconfNode.class, updatedNetconfNode)
207                     .build();
208
209             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
210             transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
211
212             Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
213
214                 @Override
215                 public void onSuccess(Void result) {
216                     LOG.debug("NetconfNode={} updated successfully", netconfNode);
217                 }
218
219                 @Override
220                 public void onFailure(Throwable t) {
221                     LOG.error("Failed to updated NetconfNode={}", netconfNode);
222                     throw new RuntimeException(t);
223                 }
224             });
225
226             return "NETCONF node: " + netconfNodeId + " updated successfully.";
227         } else {
228             return "NETCONF node: " + netconfNodeId + " does not exist to update";
229         }
230     }
231 }