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.netconf.it;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.opendaylight.controller.config.util.xml.XmlUtil.readXmlToDocument;
17 import static org.opendaylight.netconf.util.test.XmlUnitUtil.assertContainsElementWithName;
18 import static org.opendaylight.netconf.util.test.XmlUnitUtil.assertElementsCount;
20 import com.google.common.collect.HashBiMap;
21 import com.google.common.collect.Lists;
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24 import java.net.SocketAddress;
25 import java.util.List;
26 import javax.management.InstanceNotFoundException;
27 import javax.management.Notification;
28 import javax.management.NotificationListener;
29 import org.junit.Test;
30 import org.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32 import org.opendaylight.controller.config.api.jmx.notifications.CommitJMXNotification;
33 import org.opendaylight.controller.config.api.jmx.notifications.ConfigJMXNotification;
34 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
35 import org.opendaylight.controller.config.persist.api.Persister;
36 import org.opendaylight.controller.config.persist.impl.ConfigPersisterNotificationHandler;
37 import org.opendaylight.netconf.api.NetconfMessage;
38 import org.opendaylight.netconf.client.TestingNetconfClient;
39 import org.opendaylight.netconf.util.test.XmlFileLoader;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
42 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
43 import org.w3c.dom.Document;
44 import org.xml.sax.SAXException;
46 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
48 public static final int PORT = 12026;
49 private static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
52 protected SocketAddress getTcpServerAddress() {
57 public void testNetconfCommitNotifications() throws Exception {
58 final VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
59 final VerifyingPersister mockedAggregator = mockAggregator();
61 try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) {
62 try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
63 platformMBeanServer, mockedAggregator, configSubsystemFacadeFactory)) {
65 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) {
66 NetconfMessage response = netconfClient.sendMessage(loadEditConfigMessage());
67 assertContainsElementWithName(response.getDocument(), "ok");
68 response = netconfClient.sendMessage(loadCommitMessage());
69 assertContainsElementWithName(response.getDocument(), "ok");
71 response = netconfClient.sendMessage(loadGetConfigMessage());
72 assertContainsElementWithName(response.getDocument(), "modules");
73 assertContainsElementWithName(response.getDocument(), "services");
78 notificationVerifier.assertNotificationCount(1);
80 mockedAggregator.assertSnapshotCount(1);
81 // Capabilities are stripped for persister
82 mockedAggregator.assertSnapshotContent(0, 4, 3, 3);
86 protected BindingRuntimeContext getBindingRuntimeContext() {
87 final BindingRuntimeContext ret = super.getBindingRuntimeContext();
88 doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
89 doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
90 final HashBiMap<String, String> toBeReturned = HashBiMap.create();
91 toBeReturned.put("two", "Two");
92 toBeReturned.put("one", "One");
93 toBeReturned.put("version1", "Version1");
94 doReturn(toBeReturned).when(ret).getEnumMapping(anyString());
98 private VerifyingPersister mockAggregator() throws IOException {
99 return new VerifyingPersister();
102 private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
103 final VerifyingNotificationListener listener = new VerifyingNotificationListener();
104 platformMBeanServer.addNotificationListener(ConfigJMXNotification.OBJECT_NAME, listener, null, null);
108 private NetconfMessage loadGetConfigMessage() throws Exception {
109 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
112 private NetconfMessage loadEditConfigMessage() throws Exception {
113 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig.xml");
116 private NetconfMessage loadCommitMessage() throws Exception {
117 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
120 private static class VerifyingNotificationListener implements NotificationListener {
121 public List<Notification> notifications = Lists.newArrayList();
124 public void handleNotification(final Notification notification, final Object handback) {
125 this.notifications.add(notification);
128 void assertNotificationCount(final Object size) {
129 assertEquals(size, notifications.size());
132 void assertNotificationContent(final int notificationIndex) {
133 final Notification notification = notifications.get(notificationIndex);
134 assertEquals(CommitJMXNotification.class, notification.getClass());
138 private static class VerifyingPersister implements Persister {
140 public List<ConfigSnapshotHolder> snapshots = Lists.newArrayList();
141 private Persister mockedPersister;
143 public VerifyingPersister() throws IOException {
144 final Persister mockedAggregator = mock(Persister.class);
146 doAnswer(new Answer<Object>() {
148 public Object answer(final InvocationOnMock invocation) throws Throwable {
149 final ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
150 snapshots.add(configSnapshot);
153 }).when(mockedAggregator).persistConfig(any(ConfigSnapshotHolder.class));
155 this.mockedPersister = mockedAggregator;
158 void assertSnapshotCount(final Object size) {
159 assertEquals(size, snapshots.size());
162 void assertSnapshotContent(final int notificationIndex, final int expectedModulesSize, final int expectedServicesSize, final int expectedCapsSize)
163 throws SAXException, IOException {
164 final ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex);
165 final int capsSize = snapshot.getCapabilities().size();
166 assertEquals("Expected capabilities count should be " + expectedCapsSize + " but was " + snapshot.getCapabilities(), expectedCapsSize, capsSize);
167 final Document configSnapshot = readXmlToDocument(snapshot.getConfigSnapshot());
168 assertElementsCount(configSnapshot, "module", expectedModulesSize);
169 assertElementsCount(configSnapshot, "instance", expectedServicesSize);
173 public void persistConfig(final ConfigSnapshotHolder configSnapshotHolder) throws IOException {
174 mockedPersister.persistConfig(configSnapshotHolder);
178 public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
179 return mockedPersister.loadLastConfigs();
183 public void close() {
184 mockedPersister.close();