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;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Sets;
23 import io.netty.channel.ChannelFuture;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.InetSocketAddress;
27 import java.util.Collection;
28 import java.util.List;
30 import javax.management.InstanceNotFoundException;
31 import javax.management.Notification;
32 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.netconf.api.NetconfMessage;
42 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
43 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
44 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
45 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
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 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
68 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
70 private NetconfClientDispatcher clientDispatcher;
71 private DefaultCommitNotificationProducer commitNotifier;
74 public void setUp() throws Exception {
75 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,NetconfITTest.FACTORIES));
77 NetconfMonitoringServiceImpl monitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
79 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
80 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
82 .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
83 new NetconfMonitoringOperationService(monitoringService)));
86 commitNotifier = new DefaultCommitNotificationProducer(platformMBeanServer);
87 NetconfServerDispatcher dispatch = createDispatcher(factoriesListener, mockSessionMonitoringService(), commitNotifier);
88 ChannelFuture s = dispatch.createServer(tcpAddress);
91 clientDispatcher = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
95 public void cleanUp(){
96 commitNotifier.close();
99 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
100 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
101 return new HardcodedYangStoreService(yangDependencies);
105 protected SessionMonitoringService mockSessionMonitoringService() {
106 SessionMonitoringService mockedSessionMonitor = mock(SessionMonitoringService.class);
107 doNothing().when(mockedSessionMonitor).onSessionUp(any(NetconfManagementSession.class));
108 doNothing().when(mockedSessionMonitor).onSessionDown(any(NetconfManagementSession.class));
109 return mockedSessionMonitor;
115 public void testNetconfCommitNotifications() throws Exception {
117 VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
118 VerifyingPersister mockedAggregator = mockAggregator();
120 try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
121 try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
122 platformMBeanServer, mockedAggregator)) {
125 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
126 NetconfMessage response = netconfClient.sendMessage(loadGetConfigMessage());
127 assertContainsElementWithName(response.getDocument(), "modules");
128 assertContainsElementWithName(response.getDocument(), "services");
129 response = netconfClient.sendMessage(loadCommitMessage());
130 assertContainsElementWithName(response.getDocument(), "ok");
132 response = netconfClient.sendMessage(loadEditConfigMessage());
133 assertContainsElementWithName(response.getDocument(), "ok");
134 response = netconfClient.sendMessage(loadCommitMessage());
135 assertContainsElementWithName(response.getDocument(), "ok");
140 notificationVerifier.assertNotificationCount(2);
141 notificationVerifier.assertNotificationContent(0, 0, 0, 8);
142 notificationVerifier.assertNotificationContent(1, 4, 3, 8);
144 mockedAggregator.assertSnapshotCount(2);
145 // Capabilities are stripped for persister
146 mockedAggregator.assertSnapshotContent(0, 0, 0, 1);
147 mockedAggregator.assertSnapshotContent(1, 4, 3, 3);
150 private VerifyingPersister mockAggregator() throws IOException {
151 return new VerifyingPersister();
154 private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
155 VerifyingNotificationListener listener = new VerifyingNotificationListener();
156 platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
160 private NetconfMessage loadGetConfigMessage() throws Exception {
161 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
164 private NetconfMessage loadEditConfigMessage() throws Exception {
165 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig.xml");
168 private NetconfMessage loadCommitMessage() throws Exception {
169 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
173 public NetconfOperationProvider getNetconfOperationProvider() {
174 NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class);
175 NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
176 NetconfOperationService service = mock(NetconfOperationService.class);
177 Set<Capability> caps = Sets.newHashSet();
178 doReturn(caps).when(service).getCapabilities();
179 Set<NetconfOperationService> services = Sets.newHashSet(service);
180 doReturn(services).when(snap).getServices();
181 doReturn(snap).when(factoriesListener).openSnapshot(anyString());
183 return factoriesListener;
186 private static class VerifyingNotificationListener implements NotificationListener {
187 public List<Notification> notifications = Lists.newArrayList();
190 public void handleNotification(Notification notification, Object handback) {
191 this.notifications.add(notification);
194 void assertNotificationCount(Object size) {
195 assertEquals(size, notifications.size());
198 void assertNotificationContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize) {
199 Notification notification = notifications.get(notificationIndex);
200 assertEquals(CommitJMXNotification.class, notification.getClass());
201 int capsSize = ((CommitJMXNotification) notification).getCapabilities().size();
202 assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
203 Element configSnapshot = ((CommitJMXNotification) notification).getConfigSnapshot();
204 int modulesSize = configSnapshot.getElementsByTagName("module").getLength();
205 assertEquals("Expected modules count", expectedModulesSize, modulesSize);
206 int servicesSize = configSnapshot.getElementsByTagName("instance").getLength();
207 assertEquals("Expected services count", expectedServicesSize, servicesSize);
211 private static class VerifyingPersister implements Persister {
213 public List<ConfigSnapshotHolder> snapshots = Lists.newArrayList();
214 private Persister mockedPersister;
216 public VerifyingPersister() throws IOException {
217 Persister mockedAggregator = mock(Persister.class);
219 doAnswer(new Answer<Object>() {
221 public Object answer(InvocationOnMock invocation) throws Throwable {
222 ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
223 snapshots.add(configSnapshot);
226 }).when(mockedAggregator).persistConfig(any(ConfigSnapshotHolder.class));
228 this.mockedPersister = mockedAggregator;
231 void assertSnapshotCount(Object size) {
232 assertEquals(size, snapshots.size());
235 void assertSnapshotContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize)
236 throws SAXException, IOException {
237 ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex);
238 int capsSize = snapshot.getCapabilities().size();
239 assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
240 Document configSnapshot = readXmlToDocument(snapshot.getConfigSnapshot());
241 assertElementsCount(configSnapshot, "module", expectedModulesSize);
242 assertElementsCount(configSnapshot, "instance", expectedServicesSize);
246 public void persistConfig(ConfigSnapshotHolder configSnapshotHolder) throws IOException {
247 mockedPersister.persistConfig(configSnapshotHolder);
251 public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
252 return mockedPersister.loadLastConfigs();
256 public void close() {
257 mockedPersister.close();