2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.it;
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;
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;
42 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
44 public static final int PORT = 12026;
45 private static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
49 protected SocketAddress getTcpServerAddress() {
54 protected DefaultCommitNotificationProducer getNotificationProducer() {
55 return new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
59 public void testNetconfCommitNotifications() throws Exception {
60 final VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
61 final VerifyingPersister mockedAggregator = mockAggregator();
63 try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) {
64 try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
65 platformMBeanServer, mockedAggregator)) {
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");
74 response = netconfClient.sendMessage(loadEditConfigMessage());
75 assertContainsElementWithName(response.getDocument(), "ok");
76 response = netconfClient.sendMessage(loadCommitMessage());
77 assertContainsElementWithName(response.getDocument(), "ok");
82 notificationVerifier.assertNotificationCount(2);
83 notificationVerifier.assertNotificationContent(0, 0, 0, 8);
84 notificationVerifier.assertNotificationContent(1, 4, 3, 8);
86 mockedAggregator.assertSnapshotCount(2);
87 // Capabilities are stripped for persister
88 mockedAggregator.assertSnapshotContent(0, 0, 0, 1);
89 mockedAggregator.assertSnapshotContent(1, 4, 3, 3);
92 private VerifyingPersister mockAggregator() throws IOException {
93 return new VerifyingPersister();
96 private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
97 final VerifyingNotificationListener listener = new VerifyingNotificationListener();
98 platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
102 private NetconfMessage loadGetConfigMessage() throws Exception {
103 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
106 private NetconfMessage loadEditConfigMessage() throws Exception {
107 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig.xml");
110 private NetconfMessage loadCommitMessage() throws Exception {
111 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
114 private static class VerifyingNotificationListener implements NotificationListener {
115 public List<Notification> notifications = Lists.newArrayList();
118 public void handleNotification(final Notification notification, final Object handback) {
119 this.notifications.add(notification);
122 void assertNotificationCount(final Object size) {
123 assertEquals(size, notifications.size());
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);
139 private static class VerifyingPersister implements Persister {
141 public List<ConfigSnapshotHolder> snapshots = Lists.newArrayList();
142 private Persister mockedPersister;
144 public VerifyingPersister() throws IOException {
145 final Persister mockedAggregator = mock(Persister.class);
147 doAnswer(new Answer<Object>() {
149 public Object answer(final InvocationOnMock invocation) throws Throwable {
150 final ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
151 snapshots.add(configSnapshot);
154 }).when(mockedAggregator).persistConfig(any(ConfigSnapshotHolder.class));
156 this.mockedPersister = mockedAggregator;
159 void assertSnapshotCount(final Object size) {
160 assertEquals(size, snapshots.size());
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);
174 public void persistConfig(final ConfigSnapshotHolder configSnapshotHolder) throws IOException {
175 mockedPersister.persistConfig(configSnapshotHolder);
179 public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
180 return mockedPersister.loadLastConfigs();
184 public void close() {
185 mockedPersister.close();