Upgrade ietf-{inet,yang}-types to 2013-07-15
[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 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.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.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;
52
53 public class NetconfCommandsImpl implements NetconfCommands {
54
55     private static final Logger LOG = LoggerFactory.getLogger(NetconfCommandsImpl.class);
56
57     private final DataBroker dataBroker;
58     private final MountPointService mountPointService;
59
60     public NetconfCommandsImpl(final DataBroker db, final MountPointService mountPointService) {
61         LOG.debug("NetconfConsoleProviderImpl initialized");
62         this.dataBroker = db;
63         this.mountPointService = mountPointService;
64     }
65
66     @Override
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);
78         }
79         return netconfNodes;
80     }
81
82     @Override
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));
88         } else {
89             nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker);
90         }
91         if (nodeList != null) {
92             for (final Node node : nodeList) {
93                 if (node != null) {
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);
102                 }
103             }
104         }
105         return device;
106     }
107
108     @Override
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);
122             }
123         }
124         return device;
125     }
126
127     @Override
128     public void connectDevice(NetconfNode netconfNode, String netconfNodeId) {
129         final NodeId nodeId;
130         if (!Strings.isNullOrEmpty(netconfNodeId)) {
131             nodeId = new NodeId(netconfNodeId);
132         } else {
133             nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
134         }
135         final Node node = new NodeBuilder()
136                 .setKey(new NodeKey(nodeId))
137                 .setNodeId(nodeId)
138                 .addAugmentation(NetconfNode.class, netconfNode)
139                 .build();
140
141         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
142         transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
143
144         Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
145
146             @Override
147             public void onSuccess(Void result) {
148                 LOG.debug("NetconfNode={} created successfully", netconfNode);
149             }
150
151             @Override
152             public void onFailure(Throwable t) {
153                 LOG.error("Failed to created NetconfNode={}", netconfNode);
154                 throw new RuntimeException(t);
155             }
156         });
157     }
158
159     @Override
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);
165
166         try {
167             LOG.debug("Deleting netconf node: {}", netconfNodeId);
168             transaction.submit().checkedGet();
169             result = true;
170         } catch (final TransactionCommitFailedException e) {
171             LOG.error("Unable to remove node with Iid {}", iid, e);
172         }
173         return result;
174     }
175
176     @Override
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);
180     }
181
182     @Override
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);
185
186         if (node != null && node.getAugmentation(NetconfNode.class) != null) {
187             final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
188
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;
197
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)))
202                     .setTcpOnly(tcpOnly)
203                     .setCredentials(credentials)
204                     .build();
205
206             final Node updatedNode = new NodeBuilder()
207                     .setKey(node.getKey())
208                     .setNodeId(node.getNodeId())
209                     .addAugmentation(NetconfNode.class, updatedNetconfNode)
210                     .build();
211
212             final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
213             transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
214
215             Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
216
217                 @Override
218                 public void onSuccess(Void result) {
219                     LOG.debug("NetconfNode={} updated successfully", netconfNode);
220                 }
221
222                 @Override
223                 public void onFailure(Throwable t) {
224                     LOG.error("Failed to updated NetconfNode={}", netconfNode);
225                     throw new RuntimeException(t);
226                 }
227             });
228
229             return "NETCONF node: " + netconfNodeId + " updated successfully.";
230         } else {
231             return "NETCONF node: " + netconfNodeId + " does not exist to update";
232         }
233     }
234 }