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 junit.framework.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.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithName;
18 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertElementsCount;
19 import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToDocument;
20 import io.netty.channel.ChannelFuture;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.InetSocketAddress;
25 import java.util.Collection;
26 import java.util.List;
29 import javax.management.InstanceNotFoundException;
30 import javax.management.Notification;
31 import javax.management.NotificationListener;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.invocation.InvocationOnMock;
37 import org.mockito.stubbing.Answer;
38 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
39 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
40 import org.opendaylight.controller.config.persist.api.Persister;
41 import org.opendaylight.controller.config.spi.ModuleFactory;
42 import org.opendaylight.controller.netconf.api.NetconfMessage;
43 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
44 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
45 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
46 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
47 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
48 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
49 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
50 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
51 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
52 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
53 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
54 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
55 import org.opendaylight.controller.netconf.mapping.api.Capability;
56 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
57 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
58 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
59 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
60 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
61 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
62 import org.w3c.dom.Document;
63 import org.w3c.dom.Element;
64 import org.xml.sax.SAXException;
66 import com.google.common.collect.Lists;
67 import com.google.common.collect.Sets;
69 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
71 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
75 private NetconfClientDispatcher clientDispatcher;
77 DefaultCommitNotificationProducer commitNotifier;
80 public void setUp() throws Exception {
81 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,NetconfITTest.getModuleFactoriesS().toArray(
82 new ModuleFactory[0])));
84 NetconfMonitoringServiceImpl monitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
86 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
87 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
89 .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
90 new NetconfMonitoringOperationService(monitoringService)));
93 commitNotifier = new DefaultCommitNotificationProducer(platformMBeanServer);
94 NetconfServerDispatcher dispatch = createDispatcher(factoriesListener, mockSessionMonitoringService(), commitNotifier);
95 ChannelFuture s = dispatch.createServer(tcpAddress);
98 clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
102 public void cleanUp(){
103 commitNotifier.close();
106 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
107 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
108 return new HardcodedYangStoreService(yangDependencies);
112 protected SessionMonitoringService mockSessionMonitoringService() {
113 SessionMonitoringService mockedSessionMonitor = mock(SessionMonitoringService.class);
114 doNothing().when(mockedSessionMonitor).onSessionUp(any(NetconfManagementSession.class));
115 doNothing().when(mockedSessionMonitor).onSessionDown(any(NetconfManagementSession.class));
116 return mockedSessionMonitor;
122 public void testNetconfCommitNotifications() throws Exception {
124 VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
125 VerifyingPersister mockedAggregator = mockAggregator();
127 try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", tcpAddress, 4000, clientDispatcher)) {
128 try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
129 platformMBeanServer, mockedAggregator)) {
132 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", tcpAddress, 4000, clientDispatcher)) {
133 NetconfMessage response = netconfClient.sendMessage(loadGetConfigMessage());
134 assertContainsElementWithName(response.getDocument(), "modules");
135 assertContainsElementWithName(response.getDocument(), "services");
136 response = netconfClient.sendMessage(loadCommitMessage());
137 assertContainsElementWithName(response.getDocument(), "ok");
139 response = netconfClient.sendMessage(loadEditConfigMessage());
140 assertContainsElementWithName(response.getDocument(), "ok");
141 response = netconfClient.sendMessage(loadCommitMessage());
142 assertContainsElementWithName(response.getDocument(), "ok");
147 notificationVerifier.assertNotificationCount(2);
148 notificationVerifier.assertNotificationContent(0, 0, 0, 9);
149 notificationVerifier.assertNotificationContent(1, 4, 4, 9);
151 mockedAggregator.assertSnapshotCount(2);
152 // Capabilities are stripped for persister
153 mockedAggregator.assertSnapshotContent(0, 0, 0, 1);
154 mockedAggregator.assertSnapshotContent(1, 4, 4, 3);
157 private VerifyingPersister mockAggregator() throws IOException {
158 return new VerifyingPersister();
161 private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
162 VerifyingNotificationListener listener = new VerifyingNotificationListener();
163 platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
167 private NetconfMessage loadGetConfigMessage() throws Exception {
168 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
171 private NetconfMessage loadEditConfigMessage() throws Exception {
172 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig.xml");
175 private NetconfMessage loadCommitMessage() throws Exception {
176 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
180 public NetconfOperationProvider getNetconfOperationProvider() {
181 NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class);
182 NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
183 NetconfOperationService service = mock(NetconfOperationService.class);
184 Set<Capability> caps = Sets.newHashSet();
185 doReturn(caps).when(service).getCapabilities();
186 Set<NetconfOperationService> services = Sets.newHashSet(service);
187 doReturn(services).when(snap).getServices();
188 doReturn(snap).when(factoriesListener).openSnapshot(anyString());
190 return factoriesListener;
193 private static class VerifyingNotificationListener implements NotificationListener {
194 public List<Notification> notifications = Lists.newArrayList();
197 public void handleNotification(Notification notification, Object handback) {
198 this.notifications.add(notification);
201 void assertNotificationCount(Object size) {
202 assertEquals(size, notifications.size());
205 void assertNotificationContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize) {
206 Notification notification = notifications.get(notificationIndex);
207 assertEquals(CommitJMXNotification.class, notification.getClass());
208 int capsSize = ((CommitJMXNotification) notification).getCapabilities().size();
209 assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
210 Element configSnapshot = ((CommitJMXNotification) notification).getConfigSnapshot();
211 int modulesSize = configSnapshot.getElementsByTagName("module").getLength();
212 assertEquals("Expected modules count", expectedModulesSize, modulesSize);
213 int servicesSize = configSnapshot.getElementsByTagName("instance").getLength();
214 assertEquals("Expected services count", expectedServicesSize, servicesSize);
218 private static class VerifyingPersister implements Persister {
220 public List<ConfigSnapshotHolder> snapshots = Lists.newArrayList();
221 private Persister mockedPersister;
223 public VerifyingPersister() throws IOException {
224 Persister mockedAggregator = mock(Persister.class);
226 doAnswer(new Answer<Object>() {
228 public Object answer(InvocationOnMock invocation) throws Throwable {
229 ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
230 snapshots.add(configSnapshot);
233 }).when(mockedAggregator).persistConfig(any(ConfigSnapshotHolder.class));
235 this.mockedPersister = mockedAggregator;
238 void assertSnapshotCount(Object size) {
239 assertEquals(size, snapshots.size());
242 void assertSnapshotContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize)
243 throws SAXException, IOException {
244 ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex);
245 int capsSize = snapshot.getCapabilities().size();
246 assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
247 Document configSnapshot = readXmlToDocument(snapshot.getConfigSnapshot());
248 assertElementsCount(configSnapshot, "module", expectedModulesSize);
249 assertElementsCount(configSnapshot, "instance", expectedServicesSize);
253 public void persistConfig(ConfigSnapshotHolder configSnapshotHolder) throws IOException {
254 mockedPersister.persistConfig(configSnapshotHolder);
258 public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
259 return mockedPersister.loadLastConfigs();
263 public void close() {
264 mockedPersister.close();