Split out odl-netconf-device.yang
[netconf.git] / netconf / netconf-console / src / test / java / org / opendaylight / netconf / console / impl / NetconfCommandsImplTest.java
index 8837088abc8259db299d41b07de0d00411d8c6fd..b6825bc056db0ddd231cc57c20d4de211cba05c1 100644 (file)
@@ -5,21 +5,16 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.console.impl;
 
 import static junit.framework.TestCase.assertFalse;
-import static junit.framework.TestCase.assertNull;
 import static junit.framework.TestCase.assertTrue;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 
-import com.google.common.collect.ClassToInstanceMap;
-import com.google.common.collect.ImmutableClassToInstanceMap;
 import com.google.common.collect.ImmutableList;
-import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.collect.ImmutableMap;
 import java.util.ArrayList;
-import java.util.EnumMap;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -27,41 +22,32 @@ import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
-import javassist.ClassPool;
+import org.awaitility.Awaitility;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.binding.impl.BindingDOMDataBrokerAdapter;
-import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
-import org.opendaylight.controller.sal.core.spi.data.DOMStore;
-import org.opendaylight.mdsal.binding.dom.codec.gen.impl.DataObjectSerializerGenerator;
-import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
-import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
-import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
-import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
-import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
-import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.mdsal.dom.api.DOMSchemaServiceExtension;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.WriteTransaction;
+import org.opendaylight.mdsal.binding.dom.adapter.test.ConcurrentDataBrokerTestCustomizer;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
 import org.opendaylight.netconf.console.utils.NetconfIidFactory;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.HostBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilities;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilitiesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.ConnectionOper.ConnectionStatus;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.AvailableCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.AvailableCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.available.capabilities.AvailableCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.connection.oper.available.capabilities.AvailableCapabilityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.network.topology.topology.topology.types.TopologyNetconf;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
@@ -69,56 +55,35 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
-import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+import org.opendaylight.yangtools.yang.common.Uint16;
 
 public class NetconfCommandsImplTest {
-
     private static final String NODE_ID = "NodeID";
     private static final String IP = "192.168.1.1";
     private static final int PORT = 1234;
-    private static final NetconfNodeConnectionStatus.ConnectionStatus CONN_STATUS =
-            NetconfNodeConnectionStatus.ConnectionStatus.Connected;
+    private static final ConnectionStatus CONN_STATUS = ConnectionStatus.Connected;
     private static final String CAP_PREFIX = "prefix";
 
+    private static BindingRuntimeContext RUNTIME_CONTEXT;
+
     private DataBroker dataBroker;
-    private SchemaContext schemaContext;
     private NetconfCommandsImpl netconfCommands;
 
+    @BeforeClass
+    public static void beforeClass() {
+        RUNTIME_CONTEXT = BindingRuntimeHelpers.createRuntimeContext(TopologyNetconf.class);
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        RUNTIME_CONTEXT = null;
+    }
+
     @Before
     public void setUp() throws Exception {
-        schemaContext = YangParserTestUtils.parseYangResources(NetconfCommandsImplTest.class,
-            "/schemas/network-topology@2013-10-21.yang", "/schemas/ietf-inet-types@2013-07-15.yang",
-            "/schemas/yang-ext.yang", "/schemas/netconf-node-topology.yang");
-        schemaContext.getModules();
-        final DOMSchemaService schemaService = createSchemaService();
-
-        final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
-        final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
-
-        final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
-        datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
-        datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
-
-        final DOMDataBroker domDataBroker =
-                new SerializedDOMDataBroker(datastores, MoreExecutors.newDirectExecutorService());
-
-        final ClassPool pool = ClassPool.getDefault();
-        final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(pool));
-        final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
-        final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
-        codecRegistry
-                .onBindingRuntimeContextUpdated(BindingRuntimeContext.create(moduleInfoBackedContext, schemaContext));
-
-        final GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
-        final BindingToNormalizedNodeCodec bindingToNormalized =
-                new BindingToNormalizedNodeCodec(loading, codecRegistry);
-        bindingToNormalized.onGlobalContextUpdated(schemaContext);
-        dataBroker = new BindingDOMDataBrokerAdapter(domDataBroker, bindingToNormalized);
+        ConcurrentDataBrokerTestCustomizer customizer = new ConcurrentDataBrokerTestCustomizer(true);
+        dataBroker = customizer.createDataBroker();
+        customizer.updateSchema(RUNTIME_CONTEXT);
 
         netconfCommands = new NetconfCommandsImpl(dataBroker);
     }
@@ -159,37 +124,48 @@ public class NetconfCommandsImplTest {
     @Test
     public void testConnectDisconnectDevice() throws InterruptedException, TimeoutException, ExecutionException {
         final NetconfNode netconfNode = new NetconfNodeBuilder()
-                .setPort(new PortNumber(7777)).setHost(HostBuilder.getDefaultInstance("10.10.1.1")).build();
+                .setPort(new PortNumber(Uint16.valueOf(7777)))
+                .setHost(new Host(new IpAddress(new Ipv4Address("10.10.1.1"))))
+                .build();
 
         createTopology(LogicalDatastoreType.CONFIGURATION);
         netconfCommands.connectDevice(netconfNode, "netconf-ID");
-        NetconfConsoleUtils.waitForUpdate("10.10.1.1");
 
-        final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
-                NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
-        final List<Node> nodes = topology.getNode();
-        assertEquals(2, nodes.size());
+        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
+            final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
+                    NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
+            final Collection<Node> nodes = topology.nonnullNode().values();
+            if (nodes.size() != 2) {
+                return false;
+            }
 
-        final Optional<Node> storedNode = nodes.stream().filter(node ->
-                node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
+            final Optional<Node> storedNode = nodes.stream().filter(node ->
+                    node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
 
-        assertTrue(storedNode.isPresent());
+            assertTrue(storedNode.isPresent());
 
-        NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
-        assertEquals(7777, storedNetconfNode.getPort().getValue().longValue());
-        assertEquals("10.10.1.1", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
+            NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
+            assertEquals(7777, storedNetconfNode.getPort().getValue().longValue());
+            assertEquals("10.10.1.1", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
+            return true;
+        });
 
         netconfCommands.disconnectDevice("netconf-ID");
 
-        final Topology topologyDeleted = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
-                NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
-        final List<Node> nodesDeleted = topologyDeleted.getNode();
-        assertEquals(1, nodesDeleted.size());
+        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
+            final Topology topologyDeleted = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
+                    NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
+            final Collection<Node> nodesDeleted = topologyDeleted.nonnullNode().values();
+            if (nodesDeleted.size() != 1) {
+                return false;
+            }
 
-        final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
-                node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
+            final Optional<Node> storedNodeDeleted = nodesDeleted.stream().filter(node ->
+                    node.key().getNodeId().getValue().equals("netconf-ID")).findFirst();
 
-        assertFalse(storedNodeDeleted.isPresent());
+            assertFalse(storedNodeDeleted.isPresent());
+            return true;
+        });
     }
 
     @Test
@@ -204,40 +180,33 @@ public class NetconfCommandsImplTest {
         update.put(NetconfConsoleConstants.SCHEMALESS, "true");
 
         netconfCommands.updateDevice(NODE_ID, "admin", "admin", update);
-        NetconfConsoleUtils.waitForUpdate("7.7.7.7");
 
-        final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
-                NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
-        final List<Node> nodes = topology.getNode();
-        assertEquals(1, nodes.size());
-
-        final Optional<Node> storedNode = nodes.stream().filter(node ->
-                node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
-        assertTrue(storedNode.isPresent());
+        Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> {
+            final Topology topology = NetconfConsoleUtils.read(LogicalDatastoreType.CONFIGURATION,
+                    NetconfIidFactory.NETCONF_TOPOLOGY_IID, dataBroker);
+            final Collection<Node> nodes = topology.nonnullNode().values();
+            if (nodes.size() != 1) {
+                return false;
+            }
 
-        NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
-        assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
-    }
+            final Optional<Node> storedNode = nodes.stream().filter(node ->
+                    node.key().getNodeId().getValue().equals(NODE_ID)).findFirst();
+            assertTrue(storedNode.isPresent());
 
-    @Test
-    public void testNetconfNodeFromIp() throws TimeoutException, InterruptedException, ExecutionException {
-        final List<Node> nodesNotExist = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
-        assertNull(nodesNotExist);
-        createTopology(LogicalDatastoreType.OPERATIONAL);
-        final List<Node> nodes = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker);
-        assertNotNull(nodes);
-        assertEquals(1, nodes.size());
+            NetconfNode storedNetconfNode = storedNode.get().augmentation(NetconfNode.class);
+            assertEquals("7.7.7.7", storedNetconfNode.getHost().getIpAddress().getIpv4Address().getValue());
+            return true;
+        });
     }
 
     private void createTopology(final LogicalDatastoreType dataStoreType)
             throws TimeoutException, InterruptedException, ExecutionException {
-        final List<Node> nodes = new ArrayList<>();
         final Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);
-        nodes.add(node);
 
         final Topology topology = new TopologyBuilder()
                 .withKey(new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
-                .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName())).setNode(nodes).build();
+                .setTopologyId(new TopologyId(TopologyNetconf.QNAME.getLocalName()))
+                .setNode(ImmutableMap.of(node.key(), node)).build();
 
         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
         writeTransaction.put(dataStoreType, NetconfIidFactory.NETCONF_TOPOLOGY_IID, topology);
@@ -245,10 +214,10 @@ public class NetconfCommandsImplTest {
     }
 
     private static Node getNetconfNode(final String nodeIdent, final String ip, final int portNumber,
-            final NetconfNodeConnectionStatus.ConnectionStatus cs, final String notificationCapabilityPrefix) {
+            final ConnectionStatus cs, final String notificationCapabilityPrefix) {
 
-        final Host host = HostBuilder.getDefaultInstance(ip);
-        final PortNumber port = new PortNumber(portNumber);
+        final Host host = new Host(new IpAddress(new Ipv4Address(ip)));
+        final PortNumber port = new PortNumber(Uint16.valueOf(portNumber));
 
         final List<AvailableCapability> avCapList = new ArrayList<>();
         avCapList.add(new AvailableCapabilityBuilder()
@@ -257,15 +226,11 @@ public class NetconfCommandsImplTest {
         final AvailableCapabilities avCaps =
                 new AvailableCapabilitiesBuilder().setAvailableCapability(avCapList).build();
 
-        final NetconfNode nn = new NetconfNodeBuilder().setConnectionStatus(cs).setHost(host).setPort(port)
-                .setAvailableCapabilities(avCaps).build();
-        final NodeId nodeId = new NodeId(nodeIdent);
-        final NodeKey nk = new NodeKey(nodeId);
-        final NodeBuilder nb = new NodeBuilder();
-        nb.withKey(nk);
-        nb.setNodeId(nodeId);
-        nb.addAugmentation(NetconfNode.class, nn);
-        return nb.build();
+        return new NodeBuilder()
+                .setNodeId(new NodeId(nodeIdent))
+                .addAugmentation(new NetconfNodeBuilder()
+                    .setConnectionStatus(cs).setHost(host).setPort(port).setAvailableCapabilities(avCaps).build())
+                .build();
     }
 
     private static void assertBaseNodeAttributes(final Map<?, ?> mapNode) {
@@ -291,37 +256,4 @@ public class NetconfCommandsImplTest {
         assertEquals(ImmutableList.of(String.valueOf(PORT)), mapNode.get(NetconfConsoleConstants.NETCONF_PORT));
         assertEquals(ImmutableList.of(CONN_STATUS.name()), mapNode.get(NetconfConsoleConstants.STATUS));
     }
-
-    private DOMSchemaService createSchemaService() {
-        return new DOMSchemaService() {
-
-            @Override
-            public SchemaContext getSessionContext() {
-                return schemaContext;
-            }
-
-            @Override
-            public SchemaContext getGlobalContext() {
-                return schemaContext;
-            }
-
-            @Override
-            public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
-                    final SchemaContextListener listener) {
-                listener.onGlobalContextUpdated(getGlobalContext());
-                return new AbstractListenerRegistration<SchemaContextListener>(listener) {
-
-                    @Override
-                    protected void removeRegistration() {
-                        // No-op
-                    }
-                };
-            }
-
-            @Override
-            public ClassToInstanceMap<DOMSchemaServiceExtension> getExtensions() {
-                return ImmutableClassToInstanceMap.of();
-            }
-        };
-    }
 }