Merge "Bug 615: Removed xtend from Topology Manager"
[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 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;
21
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;
27 import java.util.Set;
28
29 import javax.management.InstanceNotFoundException;
30 import javax.management.Notification;
31 import javax.management.NotificationListener;
32
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;
65
66 import com.google.common.collect.Lists;
67 import com.google.common.collect.Sets;
68
69 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
70
71     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
72
73
74
75     private NetconfClientDispatcher clientDispatcher;
76
77     DefaultCommitNotificationProducer commitNotifier;
78
79     @Before
80     public void setUp() throws Exception {
81         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(NetconfITTest.getModuleFactoriesS().toArray(
82                 new ModuleFactory[0])));
83
84         NetconfMonitoringServiceImpl monitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
85
86         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
87         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
88         factoriesListener
89                 .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
90                         new NetconfMonitoringOperationService(monitoringService)));
91
92
93         commitNotifier = new DefaultCommitNotificationProducer(platformMBeanServer);
94         NetconfServerDispatcher dispatch = createDispatcher(factoriesListener, mockSessionMonitoringService(), commitNotifier);
95         ChannelFuture s = dispatch.createServer(tcpAddress);
96         s.await();
97
98         clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
99     }
100
101     @After
102     public void cleanUp(){
103         commitNotifier.close();
104     }
105
106     private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
107         final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
108         return new HardcodedYangStoreService(yangDependencies);
109     }
110
111
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;
117     }
118
119
120
121     @Test
122     public void testNetconfCommitNotifications() throws Exception {
123
124         VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
125         VerifyingPersister mockedAggregator = mockAggregator();
126
127         try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", tcpAddress, 4000, clientDispatcher)) {
128             try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
129                     platformMBeanServer, mockedAggregator)) {
130
131
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");
138
139                     response = netconfClient.sendMessage(loadEditConfigMessage());
140                     assertContainsElementWithName(response.getDocument(), "ok");
141                     response = netconfClient.sendMessage(loadCommitMessage());
142                     assertContainsElementWithName(response.getDocument(), "ok");
143                 }
144             }
145         }
146
147         notificationVerifier.assertNotificationCount(2);
148         notificationVerifier.assertNotificationContent(0, 0, 0, 9);
149         notificationVerifier.assertNotificationContent(1, 4, 4, 9);
150
151         mockedAggregator.assertSnapshotCount(2);
152         // Capabilities are stripped for persister
153         mockedAggregator.assertSnapshotContent(0, 0, 0, 1);
154         mockedAggregator.assertSnapshotContent(1, 4, 4, 3);
155     }
156
157     private VerifyingPersister mockAggregator() throws IOException {
158         return new VerifyingPersister();
159     }
160
161     private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
162         VerifyingNotificationListener listener = new VerifyingNotificationListener();
163         platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
164         return listener;
165     }
166
167     private NetconfMessage loadGetConfigMessage() throws Exception {
168         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
169     }
170
171     private NetconfMessage loadEditConfigMessage() throws Exception {
172         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig.xml");
173     }
174
175     private NetconfMessage loadCommitMessage() throws Exception {
176         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
177     }
178
179
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());
189
190         return factoriesListener;
191     }
192
193     private static class VerifyingNotificationListener implements NotificationListener {
194         public List<Notification> notifications = Lists.newArrayList();
195
196         @Override
197         public void handleNotification(Notification notification, Object handback) {
198             this.notifications.add(notification);
199         }
200
201         void assertNotificationCount(Object size) {
202             assertEquals(size, notifications.size());
203         }
204
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);
215         }
216     }
217
218     private static class VerifyingPersister implements Persister {
219
220         public List<ConfigSnapshotHolder> snapshots = Lists.newArrayList();
221         private Persister mockedPersister;
222
223         public VerifyingPersister() throws IOException {
224             Persister mockedAggregator = mock(Persister.class);
225
226             doAnswer(new Answer<Object>() {
227                 @Override
228                 public Object answer(InvocationOnMock invocation) throws Throwable {
229                     ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
230                     snapshots.add(configSnapshot);
231                     return null;
232                 }
233             }).when(mockedAggregator).persistConfig(any(ConfigSnapshotHolder.class));
234
235             this.mockedPersister = mockedAggregator;
236         }
237
238         void assertSnapshotCount(Object size) {
239             assertEquals(size, snapshots.size());
240         }
241
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);
250         }
251
252         @Override
253         public void persistConfig(ConfigSnapshotHolder configSnapshotHolder) throws IOException {
254             mockedPersister.persistConfig(configSnapshotHolder);
255         }
256
257         @Override
258         public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
259             return mockedPersister.loadLastConfigs();
260         }
261
262         @Override
263         public void close() {
264             mockedPersister.close();
265         }
266     }
267 }