Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfConfigPersisterITTest.java
index 29afa93d373cb5e7a7b86c57b0f93b6cceeeb48e..ba435e8a366d16d3a776b8b2f502b7c9e6aa22d2 100644 (file)
  */
 package org.opendaylight.controller.netconf.it;
 
-import static junit.framework.Assert.assertEquals;
+import static org.junit.Assert.assertEquals;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.opendaylight.controller.config.util.xml.XmlUtil.readXmlToDocument;
 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithName;
 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertElementsCount;
-import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToDocument;
 
+import com.google.common.collect.HashBiMap;
+import com.google.common.collect.Lists;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.InetSocketAddress;
-import java.util.Collection;
+import java.net.SocketAddress;
 import java.util.List;
-import java.util.Set;
-
 import javax.management.InstanceNotFoundException;
 import javax.management.Notification;
 import javax.management.NotificationListener;
-
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
-import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
+import org.opendaylight.controller.config.api.jmx.notifications.CommitJMXNotification;
+import org.opendaylight.controller.config.api.jmx.notifications.ConfigJMXNotification;
 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
 import org.opendaylight.controller.config.persist.api.Persister;
-import org.opendaylight.controller.config.spi.ModuleFactory;
+import org.opendaylight.controller.config.persist.impl.ConfigPersisterNotificationHandler;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
-import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
-import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
-import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
-import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
-import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
-import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
-import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
-import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
-import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
-import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
-import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
-import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
-import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
-import org.opendaylight.controller.netconf.mapping.api.Capability;
-import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
-import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
-import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
-import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
-import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
+import org.opendaylight.controller.netconf.client.TestingNetconfClient;
 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
+import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
 import org.w3c.dom.Document;
-import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
-import io.netty.channel.ChannelFuture;
-
 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
 
-    private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
-
-    private NetconfClientDispatcher clientDispatcher;
-    private DefaultCommitNotificationProducer commitNotifier;
-
-    @Before
-    public void setUp() throws Exception {
-        super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,NetconfITTest.getModuleFactoriesS().toArray(
-                new ModuleFactory[0])));
-
-        NetconfMonitoringServiceImpl monitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
-
-        NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
-        factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
-        factoriesListener
-                .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
-                        new NetconfMonitoringOperationService(monitoringService)));
-
-
-        commitNotifier = new DefaultCommitNotificationProducer(platformMBeanServer);
-        NetconfServerDispatcher dispatch = createDispatcher(factoriesListener, mockSessionMonitoringService(), commitNotifier);
-        ChannelFuture s = dispatch.createServer(tcpAddress);
-        s.await();
-
-        clientDispatcher = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
-    }
-
-    @After
-    public void cleanUp(){
-        commitNotifier.close();
-    }
-
-    private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
-        final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
-        return new HardcodedYangStoreService(yangDependencies);
-    }
-
+    public static final int PORT = 12026;
+    private static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
 
-    protected SessionMonitoringService mockSessionMonitoringService() {
-        SessionMonitoringService mockedSessionMonitor = mock(SessionMonitoringService.class);
-        doNothing().when(mockedSessionMonitor).onSessionUp(any(NetconfManagementSession.class));
-        doNothing().when(mockedSessionMonitor).onSessionDown(any(NetconfManagementSession.class));
-        return mockedSessionMonitor;
+    @Override
+    protected SocketAddress getTcpServerAddress() {
+        return TCP_ADDRESS;
     }
 
-
-
     @Test
     public void testNetconfCommitNotifications() throws Exception {
+        final VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
+        final VerifyingPersister mockedAggregator = mockAggregator();
 
-        VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
-        VerifyingPersister mockedAggregator = mockAggregator();
-
-        try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
+        try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) {
             try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
-                    platformMBeanServer, mockedAggregator)) {
-
-
-                try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
-                    NetconfMessage response = netconfClient.sendMessage(loadGetConfigMessage());
-                    assertContainsElementWithName(response.getDocument(), "modules");
-                    assertContainsElementWithName(response.getDocument(), "services");
-                    response = netconfClient.sendMessage(loadCommitMessage());
-                    assertContainsElementWithName(response.getDocument(), "ok");
+                    platformMBeanServer, mockedAggregator, configSubsystemFacadeFactory)) {
 
-                    response = netconfClient.sendMessage(loadEditConfigMessage());
+                try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) {
+                    NetconfMessage response = netconfClient.sendMessage(loadEditConfigMessage());
                     assertContainsElementWithName(response.getDocument(), "ok");
                     response = netconfClient.sendMessage(loadCommitMessage());
                     assertContainsElementWithName(response.getDocument(), "ok");
+
+                    response = netconfClient.sendMessage(loadGetConfigMessage());
+                    assertContainsElementWithName(response.getDocument(), "modules");
+                    assertContainsElementWithName(response.getDocument(), "services");
                 }
             }
         }
 
-        notificationVerifier.assertNotificationCount(2);
-        notificationVerifier.assertNotificationContent(0, 0, 0, 9);
-        notificationVerifier.assertNotificationContent(1, 4, 4, 9);
+        notificationVerifier.assertNotificationCount(1);
 
-        mockedAggregator.assertSnapshotCount(2);
+        mockedAggregator.assertSnapshotCount(1);
         // Capabilities are stripped for persister
-        mockedAggregator.assertSnapshotContent(0, 0, 0, 1);
-        mockedAggregator.assertSnapshotContent(1, 4, 4, 3);
+        mockedAggregator.assertSnapshotContent(0, 4, 3, 3);
+    }
+
+    @Override
+    protected BindingRuntimeContext getBindingRuntimeContext() {
+        final BindingRuntimeContext ret = super.getBindingRuntimeContext();
+        doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
+        doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
+        final HashBiMap<String, String> toBeReturned = HashBiMap.create();
+        toBeReturned.put("two", "Two");
+        toBeReturned.put("one", "One");
+        toBeReturned.put("version1", "Version1");
+        doReturn(toBeReturned).when(ret).getEnumMapping(anyString());
+        return ret;
     }
 
     private VerifyingPersister mockAggregator() throws IOException {
@@ -157,8 +100,8 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
     }
 
     private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
-        VerifyingNotificationListener listener = new VerifyingNotificationListener();
-        platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
+        final VerifyingNotificationListener listener = new VerifyingNotificationListener();
+        platformMBeanServer.addNotificationListener(ConfigJMXNotification.OBJECT_NAME, listener, null, null);
         return listener;
     }
 
@@ -174,42 +117,21 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
     }
 
-
-    public NetconfOperationProvider getNetconfOperationProvider() {
-        NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class);
-        NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
-        NetconfOperationService service = mock(NetconfOperationService.class);
-        Set<Capability> caps = Sets.newHashSet();
-        doReturn(caps).when(service).getCapabilities();
-        Set<NetconfOperationService> services = Sets.newHashSet(service);
-        doReturn(services).when(snap).getServices();
-        doReturn(snap).when(factoriesListener).openSnapshot(anyString());
-
-        return factoriesListener;
-    }
-
     private static class VerifyingNotificationListener implements NotificationListener {
         public List<Notification> notifications = Lists.newArrayList();
 
         @Override
-        public void handleNotification(Notification notification, Object handback) {
+        public void handleNotification(final Notification notification, final Object handback) {
             this.notifications.add(notification);
         }
 
-        void assertNotificationCount(Object size) {
+        void assertNotificationCount(final Object size) {
             assertEquals(size, notifications.size());
         }
 
-        void assertNotificationContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize) {
-            Notification notification = notifications.get(notificationIndex);
+        void assertNotificationContent(final int notificationIndex) {
+            final Notification notification = notifications.get(notificationIndex);
             assertEquals(CommitJMXNotification.class, notification.getClass());
-            int capsSize = ((CommitJMXNotification) notification).getCapabilities().size();
-            assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
-            Element configSnapshot = ((CommitJMXNotification) notification).getConfigSnapshot();
-            int modulesSize = configSnapshot.getElementsByTagName("module").getLength();
-            assertEquals("Expected modules count", expectedModulesSize, modulesSize);
-            int servicesSize = configSnapshot.getElementsByTagName("instance").getLength();
-            assertEquals("Expected services count", expectedServicesSize, servicesSize);
         }
     }
 
@@ -219,12 +141,12 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
         private Persister mockedPersister;
 
         public VerifyingPersister() throws IOException {
-            Persister mockedAggregator = mock(Persister.class);
+            final Persister mockedAggregator = mock(Persister.class);
 
             doAnswer(new Answer<Object>() {
                 @Override
-                public Object answer(InvocationOnMock invocation) throws Throwable {
-                    ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
+                public Object answer(final InvocationOnMock invocation) throws Throwable {
+                    final ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
                     snapshots.add(configSnapshot);
                     return null;
                 }
@@ -233,22 +155,22 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
             this.mockedPersister = mockedAggregator;
         }
 
-        void assertSnapshotCount(Object size) {
+        void assertSnapshotCount(final Object size) {
             assertEquals(size, snapshots.size());
         }
 
-        void assertSnapshotContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize)
+        void assertSnapshotContent(final int notificationIndex, final int expectedModulesSize, final int expectedServicesSize, final int expectedCapsSize)
                 throws SAXException, IOException {
-            ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex);
-            int capsSize = snapshot.getCapabilities().size();
-            assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
-            Document configSnapshot = readXmlToDocument(snapshot.getConfigSnapshot());
+            final ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex);
+            final int capsSize = snapshot.getCapabilities().size();
+            assertEquals("Expected capabilities count should be " + expectedCapsSize + " but was " + snapshot.getCapabilities(), expectedCapsSize, capsSize);
+            final Document configSnapshot = readXmlToDocument(snapshot.getConfigSnapshot());
             assertElementsCount(configSnapshot, "module", expectedModulesSize);
             assertElementsCount(configSnapshot, "instance", expectedServicesSize);
         }
 
         @Override
-        public void persistConfig(ConfigSnapshotHolder configSnapshotHolder) throws IOException {
+        public void persistConfig(final ConfigSnapshotHolder configSnapshotHolder) throws IOException {
             mockedPersister.persistConfig(configSnapshotHolder);
         }