50adf4b2007d401af900059ced278ee228725ef0
[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 package org.opendaylight.netconf.console.impl;
9
10 import com.google.common.base.Strings;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Locale;
19 import java.util.Map;
20 import java.util.UUID;
21 import java.util.concurrent.ExecutionException;
22 import java.util.stream.Collectors;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.WriteTransaction;
25 import org.opendaylight.mdsal.common.api.CommitInfo;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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.opendaylight.yangtools.yang.common.Uint16;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class NetconfCommandsImpl implements NetconfCommands {
52
53     private static final Logger LOG = LoggerFactory.getLogger(NetconfCommandsImpl.class);
54
55     private final DataBroker dataBroker;
56
57     public NetconfCommandsImpl(final DataBroker db) {
58         LOG.debug("NetconfConsoleProviderImpl initialized");
59         this.dataBroker = db;
60     }
61
62     @Override
63     public Map<String, Map<String, String>> listDevices() {
64         final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.OPERATIONAL,
65                 NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
66         if (topology == null) {
67             return new HashMap<>();
68         }
69         final Map<String, Map<String, String>> netconfNodes = new HashMap<>();
70         for (final Node node : topology.getNode()) {
71             final NetconfNode netconfNode = node.augmentation(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,
75                     netconfNode.getHost().getIpAddress().getIpv4Address().getValue());
76             attributes.put(NetconfConsoleConstants.NETCONF_PORT, netconfNode.getPort().getValue().toString());
77             attributes.put(NetconfConsoleConstants.STATUS, netconfNode.getConnectionStatus().name()
78                     .toLowerCase(Locale.ROOT));
79             netconfNodes.put(node.getNodeId().getValue(), attributes);
80         }
81         return netconfNodes;
82     }
83
84     @Override
85     public Map<String, Map<String, List<String>>> showDevice(final String deviceIp, final String devicePort) {
86         final Map<String, Map<String, List<String>>> device = new HashMap<>();
87         List<Node> nodeList = new ArrayList<>();
88         if (devicePort != null) {
89             nodeList.add(NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker));
90         } else {
91             nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
92         }
93         if (nodeList != null) {
94             for (final Node node : nodeList) {
95                 if (node != null) {
96                     final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
97                     final Map<String, List<String>> attributes = new HashMap<>();
98                     attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
99                     attributes.put(NetconfConsoleConstants.NETCONF_IP,
100                             ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
101                     attributes.put(NetconfConsoleConstants.NETCONF_PORT,
102                             ImmutableList.of(netconfNode.getPort().getValue().toString()));
103                     attributes.put(NetconfConsoleConstants.STATUS,
104                             ImmutableList.of(netconfNode.getConnectionStatus().name()));
105                     if (netconfNode.getConnectionStatus().equals(
106                             NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
107                         attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
108                                 netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
109                                         .map(AvailableCapability::getCapability).collect(Collectors.toList()));
110                     } else {
111                         attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList(""));
112                     }
113                     device.put(node.getNodeId().getValue(), attributes);
114                 }
115             }
116         }
117         return device;
118     }
119
120     @Override
121     public Map<String, Map<String, List<String>>> showDevice(final String deviceId) {
122         final Map<String, Map<String, List<String>>> device = new HashMap<>();
123         final List<Node> nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker);
124         if (nodeList != null && nodeList.size() > 0) {
125             for (final Node node : nodeList) {
126                 final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
127                 final Map<String, List<String>> attributes = new HashMap<>();
128                 attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
129                 attributes.put(NetconfConsoleConstants.NETCONF_IP,
130                         ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue()));
131                 attributes.put(NetconfConsoleConstants.NETCONF_PORT,
132                         ImmutableList.of(netconfNode.getPort().getValue().toString()));
133                 attributes.put(NetconfConsoleConstants.STATUS,
134                         ImmutableList.of(netconfNode.getConnectionStatus().name()));
135                 if (netconfNode.getConnectionStatus().equals(NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
136                     attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
137                             netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
138                                     .map(AvailableCapability::getCapability).collect(Collectors.toList()));
139                 } else {
140                     attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList(""));
141                 }
142                 device.put(node.getNodeId().getValue(), attributes);
143             }
144         }
145         return device;
146     }
147
148     @Override
149     public void connectDevice(final NetconfNode netconfNode, final String netconfNodeId) {
150         final NodeId nodeId;
151         if (!Strings.isNullOrEmpty(netconfNodeId)) {
152             nodeId = new NodeId(netconfNodeId);
153         } else {
154             nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
155         }
156         final Node node = new NodeBuilder()
157                 .withKey(new NodeKey(nodeId))
158                 .setNodeId(nodeId)
159                 .addAugmentation(NetconfNode.class, netconfNode)
160                 .build();
161
162         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
163         transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
164
165         transaction.commit().addCallback(new FutureCallback<CommitInfo>() {
166             @Override
167             public void onSuccess(final CommitInfo result) {
168                 LOG.debug("NetconfNode={} created successfully", netconfNode);
169             }
170
171             @Override
172             public void onFailure(final Throwable throwable) {
173                 LOG.error("Failed to created NetconfNode={}", netconfNode, throwable);
174             }
175         }, MoreExecutors.directExecutor());
176     }
177
178     @Override
179     public boolean disconnectDevice(final String netconfNodeId) {
180         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
181         final InstanceIdentifier<Node> iid = NetconfIidFactory.netconfNodeIid(netconfNodeId);
182         transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
183
184         try {
185             LOG.debug("Deleting netconf node: {}", netconfNodeId);
186             transaction.commit().get();
187             return true;
188         } catch (final InterruptedException | ExecutionException e) {
189             LOG.error("Unable to remove node with Iid {}", iid, e);
190             return false;
191         }
192     }
193
194     @Override
195     public boolean disconnectDevice(final String deviceIp, final String devicePort) {
196         final String netconfNodeId = NetconfConsoleUtils
197                 .getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker).getNodeId().getValue();
198         return disconnectDevice(netconfNodeId);
199     }
200
201     @Override
202     public String updateDevice(final String netconfNodeId, final String username, final String password,
203                                final Map<String, String> updated) {
204         final Node node = NetconfConsoleUtils
205                 .read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(netconfNodeId), dataBroker);
206
207         if (node != null && node.augmentation(NetconfNode.class) != null) {
208             final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
209
210             // Get NETCONF attributes to update if present else get their original values from NetconfNode instance
211             final String deviceIp = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_IP))
212                     ? netconfNode.getHost().getIpAddress().getIpv4Address().getValue()
213                     : updated.get(NetconfConsoleConstants.NETCONF_IP);
214             final String devicePort = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_PORT))
215                     ? netconfNode.getPort().getValue().toString() : updated.get(NetconfConsoleConstants.NETCONF_PORT);
216             final Boolean tcpOnly = updated.get(NetconfConsoleConstants.TCP_ONLY).equals("true");
217             final Boolean isSchemaless =
218                     updated.get(NetconfConsoleConstants.SCHEMALESS).equals("true");
219             final String newUsername = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.USERNAME))
220                     ? updated.get(NetconfConsoleConstants.USERNAME) : username;
221             final String newPassword = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.PASSWORD))
222                     ? updated.get(NetconfConsoleConstants.PASSWORD) : password;
223
224             final Credentials credentials =
225                     new LoginPasswordBuilder().setPassword(newPassword).setUsername(newUsername).build();
226             final NetconfNode updatedNetconfNode = new NetconfNodeBuilder()
227                     .setHost(new Host(new IpAddress(new Ipv4Address(deviceIp))))
228                     .setPort(new PortNumber(Uint16.valueOf(Integer.decode(devicePort))))
229                     .setTcpOnly(tcpOnly)
230                     .setSchemaless(isSchemaless)
231                     .setCredentials(credentials)
232                     .build();
233
234             final Node updatedNode = new NodeBuilder()
235                     .withKey(node.key())
236                     .setNodeId(node.getNodeId())
237                     .addAugmentation(NetconfNode.class, updatedNetconfNode)
238                     .build();
239
240             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
241             transaction.put(LogicalDatastoreType.CONFIGURATION,
242                     NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
243
244             transaction.commit().addCallback(new FutureCallback<CommitInfo>() {
245                 @Override
246                 public void onSuccess(final CommitInfo result) {
247                     LOG.debug("NetconfNode={} updated successfully", netconfNode);
248                 }
249
250                 @Override
251                 public void onFailure(final Throwable throwable) {
252                     LOG.error("Failed to updated NetconfNode={}", netconfNode, throwable);
253                 }
254             }, MoreExecutors.directExecutor());
255
256             return "NETCONF node: " + netconfNodeId + " updated successfully.";
257         } else {
258             return "NETCONF node: " + netconfNodeId + " does not exist to update";
259         }
260     }
261 }