Merge "BUG 2676 : Introduce API for accessing akka dispatchers"
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfConfigPersisterITTest.java
1 /*
2  * Copyright (c) 2013 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.controller.netconf.it;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.mock;
14 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithName;
15 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertElementsCount;
16 import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToDocument;
17
18 import com.google.common.collect.Lists;
19 import java.io.IOException;
20 import java.lang.management.ManagementFactory;
21 import java.net.InetSocketAddress;
22 import java.net.SocketAddress;
23 import java.util.List;
24 import javax.management.InstanceNotFoundException;
25 import javax.management.Notification;
26 import javax.management.NotificationListener;
27 import org.junit.Test;
28 import org.mockito.invocation.InvocationOnMock;
29 import org.mockito.stubbing.Answer;
30 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
31 import org.opendaylight.controller.config.persist.api.Persister;
32 import org.opendaylight.controller.netconf.api.NetconfMessage;
33 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
34 import org.opendaylight.controller.netconf.client.TestingNetconfClient;
35 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
36 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
37 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.xml.sax.SAXException;
41
42 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
43
44     public static final int PORT = 12026;
45     private static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
46
47
48     @Override
49     protected SocketAddress getTcpServerAddress() {
50         return TCP_ADDRESS;
51     }
52
53     @Override
54     protected DefaultCommitNotificationProducer getNotificationProducer() {
55         return new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
56     }
57
58     @Test
59     public void testNetconfCommitNotifications() throws Exception {
60         final VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
61         final VerifyingPersister mockedAggregator = mockAggregator();
62
63         try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) {
64             try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
65                     platformMBeanServer, mockedAggregator)) {
66
67                 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) {
68                     NetconfMessage response = netconfClient.sendMessage(loadGetConfigMessage());
69                     assertContainsElementWithName(response.getDocument(), "modules");
70                     assertContainsElementWithName(response.getDocument(), "services");
71                     response = netconfClient.sendMessage(loadCommitMessage());
72                     assertContainsElementWithName(response.getDocument(), "ok");
73
74                     response = netconfClient.sendMessage(loadEditConfigMessage());
75                     assertContainsElementWithName(response.getDocument(), "ok");
76                     response = netconfClient.sendMessage(loadCommitMessage());
77                     assertContainsElementWithName(response.getDocument(), "ok");
78                 }
79             }
80         }
81
82         notificationVerifier.assertNotificationCount(2);
83         notificationVerifier.assertNotificationContent(0, 0, 0, 8);
84         notificationVerifier.assertNotificationContent(1, 4, 3, 8);
85
86         mockedAggregator.assertSnapshotCount(2);
87         // Capabilities are stripped for persister
88         mockedAggregator.assertSnapshotContent(0, 0, 0, 1);
89         mockedAggregator.assertSnapshotContent(1, 4, 3, 3);
90     }
91
92     private VerifyingPersister mockAggregator() throws IOException {
93         return new VerifyingPersister();
94     }
95
96     private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
97         final VerifyingNotificationListener listener = new VerifyingNotificationListener();
98         platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
99         return listener;
100     }
101
102     private NetconfMessage loadGetConfigMessage() throws Exception {
103         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
104     }
105
106     private NetconfMessage loadEditConfigMessage() throws Exception {
107         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig.xml");
108     }
109
110     private NetconfMessage loadCommitMessage() throws Exception {
111         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
112     }
113
114     private static class VerifyingNotificationListener implements NotificationListener {
115         public List<Notification> notifications = Lists.newArrayList();
116
117         @Override
118         public void handleNotification(final Notification notification, final Object handback) {
119             this.notifications.add(notification);
120         }
121
122         void assertNotificationCount(final Object size) {
123             assertEquals(size, notifications.size());
124         }
125
126         void assertNotificationContent(final int notificationIndex, final int expectedModulesSize, final int expectedServicesSize, final int expectedCapsSize) {
127             final Notification notification = notifications.get(notificationIndex);
128             assertEquals(CommitJMXNotification.class, notification.getClass());
129             final int capsSize = ((CommitJMXNotification) notification).getCapabilities().size();
130             assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
131             final Element configSnapshot = ((CommitJMXNotification) notification).getConfigSnapshot();
132             final int modulesSize = configSnapshot.getElementsByTagName("module").getLength();
133             assertEquals("Expected modules count", expectedModulesSize, modulesSize);
134             final int servicesSize = configSnapshot.getElementsByTagName("instance").getLength();
135             assertEquals("Expected services count", expectedServicesSize, servicesSize);
136         }
137     }
138
139     private static class VerifyingPersister implements Persister {
140
141         public List<ConfigSnapshotHolder> snapshots = Lists.newArrayList();
142         private Persister mockedPersister;
143
144         public VerifyingPersister() throws IOException {
145             final Persister mockedAggregator = mock(Persister.class);
146
147             doAnswer(new Answer<Object>() {
148                 @Override
149                 public Object answer(final InvocationOnMock invocation) throws Throwable {
150                     final ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
151                     snapshots.add(configSnapshot);
152                     return null;
153                 }
154             }).when(mockedAggregator).persistConfig(any(ConfigSnapshotHolder.class));
155
156             this.mockedPersister = mockedAggregator;
157         }
158
159         void assertSnapshotCount(final Object size) {
160             assertEquals(size, snapshots.size());
161         }
162
163         void assertSnapshotContent(final int notificationIndex, final int expectedModulesSize, final int expectedServicesSize, final int expectedCapsSize)
164                 throws SAXException, IOException {
165             final ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex);
166             final int capsSize = snapshot.getCapabilities().size();
167             assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
168             final Document configSnapshot = readXmlToDocument(snapshot.getConfigSnapshot());
169             assertElementsCount(configSnapshot, "module", expectedModulesSize);
170             assertElementsCount(configSnapshot, "instance", expectedServicesSize);
171         }
172
173         @Override
174         public void persistConfig(final ConfigSnapshotHolder configSnapshotHolder) throws IOException {
175             mockedPersister.persistConfig(configSnapshotHolder);
176         }
177
178         @Override
179         public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
180             return mockedPersister.loadLastConfigs();
181         }
182
183         @Override
184         public void close() {
185             mockedPersister.close();
186         }
187     }
188 }