Remove netconf from commons/opendaylight pom
[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 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.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithName;
18 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertElementsCount;
19
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.controller.netconf.api.NetconfMessage;
38 import org.opendaylight.controller.netconf.client.TestingNetconfClient;
39 import org.opendaylight.controller.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;
45
46 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
47
48     public static final int PORT = 12026;
49     private static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
50
51     @Override
52     protected SocketAddress getTcpServerAddress() {
53         return TCP_ADDRESS;
54     }
55
56     @Test
57     public void testNetconfCommitNotifications() throws Exception {
58         final VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
59         final VerifyingPersister mockedAggregator = mockAggregator();
60
61         try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) {
62             try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
63                     platformMBeanServer, mockedAggregator, configSubsystemFacadeFactory)) {
64
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");
70
71                     response = netconfClient.sendMessage(loadGetConfigMessage());
72                     assertContainsElementWithName(response.getDocument(), "modules");
73                     assertContainsElementWithName(response.getDocument(), "services");
74                 }
75             }
76         }
77
78         notificationVerifier.assertNotificationCount(1);
79
80         mockedAggregator.assertSnapshotCount(1);
81         // Capabilities are stripped for persister
82         mockedAggregator.assertSnapshotContent(0, 4, 3, 3);
83     }
84
85     @Override
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());
95         return ret;
96     }
97
98     private VerifyingPersister mockAggregator() throws IOException {
99         return new VerifyingPersister();
100     }
101
102     private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
103         final VerifyingNotificationListener listener = new VerifyingNotificationListener();
104         platformMBeanServer.addNotificationListener(ConfigJMXNotification.OBJECT_NAME, listener, null, null);
105         return listener;
106     }
107
108     private NetconfMessage loadGetConfigMessage() throws Exception {
109         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
110     }
111
112     private NetconfMessage loadEditConfigMessage() throws Exception {
113         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig.xml");
114     }
115
116     private NetconfMessage loadCommitMessage() throws Exception {
117         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
118     }
119
120     private static class VerifyingNotificationListener implements NotificationListener {
121         public List<Notification> notifications = Lists.newArrayList();
122
123         @Override
124         public void handleNotification(final Notification notification, final Object handback) {
125             this.notifications.add(notification);
126         }
127
128         void assertNotificationCount(final Object size) {
129             assertEquals(size, notifications.size());
130         }
131
132         void assertNotificationContent(final int notificationIndex) {
133             final Notification notification = notifications.get(notificationIndex);
134             assertEquals(CommitJMXNotification.class, notification.getClass());
135         }
136     }
137
138     private static class VerifyingPersister implements Persister {
139
140         public List<ConfigSnapshotHolder> snapshots = Lists.newArrayList();
141         private Persister mockedPersister;
142
143         public VerifyingPersister() throws IOException {
144             final Persister mockedAggregator = mock(Persister.class);
145
146             doAnswer(new Answer<Object>() {
147                 @Override
148                 public Object answer(final InvocationOnMock invocation) throws Throwable {
149                     final ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
150                     snapshots.add(configSnapshot);
151                     return null;
152                 }
153             }).when(mockedAggregator).persistConfig(any(ConfigSnapshotHolder.class));
154
155             this.mockedPersister = mockedAggregator;
156         }
157
158         void assertSnapshotCount(final Object size) {
159             assertEquals(size, snapshots.size());
160         }
161
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);
170         }
171
172         @Override
173         public void persistConfig(final ConfigSnapshotHolder configSnapshotHolder) throws IOException {
174             mockedPersister.persistConfig(configSnapshotHolder);
175         }
176
177         @Override
178         public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
179             return mockedPersister.loadLastConfigs();
180         }
181
182         @Override
183         public void close() {
184             mockedPersister.close();
185         }
186     }
187 }