Introducing simple merge strategy for config subsystem
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfMonitoringITTest.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.mockito.Matchers.anyString;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
15
16 import com.google.common.base.Charsets;
17 import com.google.common.base.Optional;
18 import com.google.common.collect.Sets;
19 import io.netty.channel.ChannelFuture;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.net.InetSocketAddress;
25 import java.net.Socket;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Set;
29 import junit.framework.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
34 import org.opendaylight.controller.netconf.api.NetconfMessage;
35 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
36 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
37 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
38 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
39 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
40 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
41 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
42 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
43 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
44 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
45 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
46 import org.opendaylight.controller.netconf.mapping.api.Capability;
47 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
48 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
49 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
50 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
51 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
52 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.w3c.dom.Document;
56
57 public class NetconfMonitoringITTest extends AbstractNetconfConfigTest {
58
59     private static final Logger logger =  LoggerFactory.getLogger(NetconfITTest.class);
60
61     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
62
63     @Mock
64     private DefaultCommitNotificationProducer commitNot;
65     private NetconfServerDispatcher dispatch;
66
67     private NetconfClientDispatcherImpl clientDispatcher;
68
69     private NetconfMonitoringServiceImpl monitoringService;
70
71     @Before
72     public void setUp() throws Exception {
73         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, NetconfITTest.FACTORIES));
74
75         monitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
76
77         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
78         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
79         factoriesListener
80                 .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
81                         new NetconfMonitoringOperationService(monitoringService)));
82
83
84         dispatch = createDispatcher(factoriesListener);
85         ChannelFuture s = dispatch.createServer(tcpAddress);
86         s.await();
87
88         clientDispatcher = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
89     }
90
91     private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
92         final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
93         return new HardcodedYangStoreService(yangDependencies);
94     }
95
96     private NetconfServerDispatcher createDispatcher(
97                                                      NetconfOperationServiceFactoryListenerImpl factoriesListener) {
98         return super.createDispatcher(factoriesListener, getNetconfMonitoringListenerService(logger, monitoringService), commitNot);
99     }
100
101     static SessionMonitoringService getNetconfMonitoringListenerService(final Logger logger, final NetconfMonitoringServiceImpl monitor) {
102         return new SessionMonitoringService() {
103             @Override
104             public void onSessionUp(NetconfManagementSession session) {
105                 logger.debug("Management session up {}", session);
106                 monitor.onSessionUp(session);
107             }
108
109             @Override
110             public void onSessionDown(NetconfManagementSession session) {
111                 logger.debug("Management session down {}", session);
112                 monitor.onSessionDown(session);
113             }
114         };
115     }
116
117
118     @Test
119     public void testGetResponseFromMonitoring() throws Exception {
120         try (TestingNetconfClient netconfClient = new TestingNetconfClient("client-monitoring", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
121         try (TestingNetconfClient netconfClient2 = new TestingNetconfClient("client-monitoring2", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
122             NetconfMessage response = netconfClient.sendMessage(loadGetMessage());
123             assertSessionElementsInResponse(response.getDocument(), 2);
124         }
125             NetconfMessage response = netconfClient.sendMessage(loadGetMessage());
126             assertSessionElementsInResponse(response.getDocument(), 1);
127         }
128     }
129
130
131     @Test(timeout = 13 * 10000)
132     public void testClientHelloWithAuth() throws Exception {
133         String fileName = "netconfMessages/client_hello_with_auth.xml";
134         String hello = XmlFileLoader.fileToString(fileName);
135
136         fileName = "netconfMessages/get.xml";
137         String get = XmlFileLoader.fileToString(fileName);
138
139         Socket sock = new Socket(tcpAddress.getHostName(), tcpAddress.getPort());
140         sock.getOutputStream().write(hello.getBytes(Charsets.UTF_8));
141         String separator = "]]>]]>";
142
143         sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
144         sock.getOutputStream().write(get.getBytes(Charsets.UTF_8));
145         sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
146
147         StringBuilder responseBuilder = new StringBuilder();
148
149         try (InputStream inputStream = sock.getInputStream();
150              InputStreamReader reader = new InputStreamReader(inputStream);
151              BufferedReader buff = new BufferedReader(reader)) {
152             String line;
153             while ((line = buff.readLine()) != null) {
154
155                 responseBuilder.append(line);
156                 responseBuilder.append(System.lineSeparator());
157
158                 if(line.contains("</rpc-reply>"))
159                     break;
160             }
161         }
162
163         sock.close();
164
165         String helloMsg = responseBuilder.substring(0, responseBuilder.indexOf(separator));
166         Document doc = XmlUtil.readXmlToDocument(helloMsg);
167         assertContainsElementWithText(doc, "urn:ietf:params:netconf:capability:candidate:1.0");
168
169         String replyMsg = responseBuilder.substring(responseBuilder.indexOf(separator) + separator.length());
170         doc = XmlUtil.readXmlToDocument(replyMsg);
171         assertContainsElementWithText(doc, "tomas");
172     }
173
174     private void assertSessionElementsInResponse(Document document, int i) {
175         int elementSize = document.getElementsByTagName("session-id").getLength();
176         Assert.assertEquals("Incorrect number of session-id tags in " + XmlUtil.toString(document),i, elementSize);
177     }
178
179     private NetconfMessage loadGetMessage() throws Exception {
180         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/get.xml");
181     }
182
183     public static NetconfOperationProvider getNetconfOperationProvider() throws Exception {
184         NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class);
185         NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
186         doNothing().when(snap).close();
187         NetconfOperationService service = mock(NetconfOperationService.class);
188         Set<Capability> caps = Sets.newHashSet();
189         caps.add(new Capability() {
190             @Override
191             public String getCapabilityUri() {
192                 return "namespaceModuleRevision";
193             }
194
195             @Override
196             public Optional<String> getModuleNamespace() {
197                 return Optional.of("namespace");
198             }
199
200             @Override
201             public Optional<String> getModuleName() {
202                 return Optional.of("name");
203             }
204
205             @Override
206             public Optional<String> getRevision() {
207                 return Optional.of("revision");
208             }
209
210             @Override
211             public Optional<String> getCapabilitySchema() {
212                 return Optional.of("content");
213             }
214
215             @Override
216             public Optional<List<String>> getLocation() {
217                 return Optional.absent();
218             }
219         });
220
221         doReturn(caps).when(service).getCapabilities();
222         Set<NetconfOperationService> services = Sets.newHashSet(service);
223         doReturn(services).when(snap).getServices();
224         doReturn(snap).when(factoriesListener).openSnapshot(anyString());
225
226         return factoriesListener;
227     }
228
229
230 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.