Split out odl-netconf-device.yang
[netconf.git] / netconf / netconf-console / src / test / java / org / opendaylight / netconf / console / impl / NetconfCommandsImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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 static junit.framework.TestCase.assertFalse;
11 import static junit.framework.TestCase.assertTrue;
12 import static org.junit.Assert.assertEquals;
13
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import org.awaitility.Awaitility;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.mdsal.binding.api.DataBroker;
31 import org.opendaylight.mdsal.binding.api.WriteTransaction;
32 import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
33 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
34 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
37 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
38 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.ConnectionOper.ConnectionStatus;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.AvailableCapabilities;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.AvailableCapabilitiesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.available.capabilities.AvailableCapability;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.available.capabilities.AvailableCapabilityBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNodeBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.network.topology.topology.topology.types.TopologyNetconf;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
58 import org.opendaylight.yangtools.yang.common.Uint16;
59
60 public class NetconfCommandsImplTest {
61     private static final String NODE_ID = "NodeID";
62     private static final String IP = "192.168.1.1";
63     private static final int PORT = 1234;
64     private static final ConnectionStatus CONN_STATUS = ConnectionStatus.Connected;
65     private static final String CAP_PREFIX = "prefix";
66
67     private static BindingRuntimeContext RUNTIME_CONTEXT;
68
69     private DataBroker dataBroker;
70     private NetconfCommandsImpl netconfCommands;
71
72     @BeforeClass
73     public static void beforeClass() {
74         RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
75     }
76
77     @AfterClass
78     public static void afterClass() {
79         RUNTIME_CONTEXT = null;
80     }
81
82     @Before
83     public void setUp() throws Exception {
84         ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
85         dataBroker = customizer.createDataBroker();
86         customizer.updateSchema(RUNTIME_CONTEXT);
87
88         netconfCommands = new NetconfCommandsImpl(dataBroker);
89     }
90
91     @Test
92     public void testListDevice() throws TimeoutException, InterruptedException, ExecutionException {
93         createTopology(LogicalDatastoreType.OPERATIONAL);
94
95         final Map<?, ?> map = netconfCommands.listDevices();
96         map.containsKey(NetconfConsoleConstants.NETCONF_ID);
97         assertTrue(map.containsKey(NODE_ID));
98
99         final Map<?, ?> mapNode = (Map<?, ?>) map.get(NODE_ID);
100         assertBaseNodeAttributes(mapNode);
101     }
102
103     @Test
104     public void testShowDevice() throws TimeoutException, InterruptedException, ExecutionException {
105         createTopology(LogicalDatastoreType.OPERATIONAL);
106
107         final Map<?, ?> mapCorrect = netconfCommands.showDevice(IP, String.valueOf(PORT));
108         mapCorrect.containsKey(NetconfConsoleConstants.NETCONF_ID);
109         assertTrue(mapCorrect.containsKey(NODE_ID));
110
111         assertBaseNodeAttributesImmutableList((Map<?, ?>) mapCorrect.get(NODE_ID));
112
113         final Map<?, ?> mapWrongPort = netconfCommands.showDevice(IP, "1");
114         assertFalse(mapWrongPort.containsKey(NODE_ID));
115
116         final Map<?, ?> mapWrongIP = netconfCommands.showDevice("1.1.1.1", String.valueOf(PORT));
117         assertFalse(mapWrongIP.containsKey(NODE_ID));
118
119         final Map<?, ?> mapId = netconfCommands.showDevice(NODE_ID);
120         assertTrue(mapId.containsKey(NODE_ID));
121         assertBaseNodeAttributesImmutableList((Map<?, ?>) mapId.get(NODE_ID));
122     }
123
124     @Test
125     public void testConnectDisconnectDevice() throws InterruptedException, TimeoutException, ExecutionException {
126         final NetconfNode netconfNode = new NetconfNodeBuilder()
127                 .setPort(new PortNumber(Uint16.valueOf(7777)))
128                 .setHost(new Host(new IpAddress(new Ipv4Address("10.10.1.1"))))
129                 .build();
130
131         createTopology(LogicalDatastoreType.CONFIGURATION);
132         netconfCommands.connectDevice(netconfNode, "netconf-ID");
133
134         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
135             final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
136                     NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
137             final Collection<Node> nodes = topology.nonnullNode().values();
138             if (nodes.size() != 2) {
139                 return false;
140             }
141
142             final Optional<Node> storedNode = nodes.stream().filter(node ->
143                     node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
144
145             assertTrue(storedNode.isPresent());
146
147             NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
148             assertEquals(7777, storedNetconfNode.getPort().getValue().longValue());
149             assertEquals("10.10.1.1", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
150             return true;
151         });
152
153         netconfCommands.disconnectDevice("netconf-ID");
154
155         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
156             final Topology topologyDeleted = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
157                     NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
158             final Collection<Node> nodesDeleted = topologyDeleted.nonnullNode().values();
159             if (nodesDeleted.size() != 1) {
160                 return false;
161             }
162
163             final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
164                     node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
165
166             assertFalse(storedNodeDeleted.isPresent());
167             return true;
168         });
169     }
170
171     @Test
172     public void testUpdateDevice() throws TimeoutException, InterruptedException, ExecutionException {
173         //We need both, read data from OPERATIONAL DS and update data in CONFIGURATIONAL DS
174         createTopology(LogicalDatastoreType.OPERATIONAL);
175         createTopology(LogicalDatastoreType.CONFIGURATION);
176
177         final Map<String, String> update = new HashMap<>();
178         update.put(NetconfConsoleConstants.NETCONF_IP, "7.7.7.7");
179         update.put(NetconfConsoleConstants.TCP_ONLY, "true");
180         update.put(NetconfConsoleConstants.SCHEMALESS, "true");
181
182         netconfCommands.updateDevice(NODE_ID, "admin", "admin", update);
183
184         Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
185             final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
186                     NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
187             final Collection<Node> nodes = topology.nonnullNode().values();
188             if (nodes.size() != 1) {
189                 return false;
190             }
191
192             final Optional<Node> storedNode = nodes.stream().filter(node ->
193                     node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
194             assertTrue(storedNode.isPresent());
195
196             NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
197             assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
198             return true;
199         });
200     }
201
202     private void createTopology(final LogicalDatastoreType dataStoreType)
203             throws TimeoutException, InterruptedException, ExecutionException {
204         final Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
205
206         final Topology topology = new TopologyBuilder()
207                 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
208                 .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName()))
209                 .setNode(ImmutableMap.of(node.key(), node)).build();
210
211         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
212         writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
213         writeTransaction.commit().get(2, TimeUnit.SECONDS);
214     }
215
216     private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
217             final ConnectionStatus cs, final String notificationCapabilityPrefix) {
218
219         final Host host = new Host(new IpAddress(new Ipv4Address(ip)));
220         final PortNumber port = new PortNumber(Uint16.valueOf(portNumber));
221
222         final List<AvailableCapability> avCapList = new ArrayList<>();
223         avCapList.add(new AvailableCapabilityBuilder()
224                 .setCapabilityOrigin(AvailableCapability.CapabilityOrigin.UserDefined)
225                 .setCapability(notificationCapabilityPrefix + "_availableCapabilityString1").build());
226         final AvailableCapabilities avCaps =
227                 new AvailableCapabilitiesBuilder().setAvailableCapability(avCapList).build();
228
229         return new NodeBuilder()
230                 .setNodeId(new NodeId(nodeIdent))
231                 .addAugmentation(new NetconfNodeBuilder()
232                     .setConnectionStatus(cs).setHost(host).setPort(port).setAvailableCapabilities(avCaps).build())
233                 .build();
234     }
235
236     private static void assertBaseNodeAttributes(final Map<?, ?> mapNode) {
237         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
238         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
239         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
240         assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
241
242         assertEquals(NODE_ID, mapNode.get(NetconfConsoleConstants.NETCONF_ID));
243         assertEquals(IP, mapNode.get(NetconfConsoleConstants.NETCONF_IP));
244         assertEquals(String.valueOf(PORT), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
245         assertEquals(CONN_STATUS.name().toLowerCase(), mapNode.get(NetconfConsoleConstants.STATUS));
246     }
247
248     private static void assertBaseNodeAttributesImmutableList(final Map<?, ?> mapNode) {
249         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_ID));
250         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_IP));
251         assertTrue(mapNode.containsKey(NetconfConsoleConstants.NETCONF_PORT));
252         assertTrue(mapNode.containsKey(NetconfConsoleConstants.STATUS));
253
254         assertEquals(ImmutableList.of(NODE_ID), mapNode.get(NetconfConsoleConstants.NETCONF_ID));
255         assertEquals(ImmutableList.of(IP), mapNode.get(NetconfConsoleConstants.NETCONF_IP));
256         assertEquals(ImmutableList.of(String.valueOf(PORT)), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
257         assertEquals(ImmutableList.of(CONN_STATUS.name()), mapNode.get(NetconfConsoleConstants.STATUS));
258     }
259 }