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 org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.anyString;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
17 import com.google.common.base.Charsets;
18 import com.google.common.base.Optional;
19 import com.google.common.collect.Sets;
20 import java.io.BufferedReader;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.net.InetSocketAddress;
24 import java.net.Socket;
25 import java.util.Collections;
26 import java.util.List;
28 import org.junit.Test;
29 import org.opendaylight.controller.netconf.api.NetconfMessage;
30 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
31 import org.opendaylight.controller.netconf.client.TestingNetconfClient;
32 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
33 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
34 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
35 import org.opendaylight.controller.netconf.mapping.api.Capability;
36 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
37 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
38 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
39 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
40 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
41 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
42 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
43 import org.slf4j.Logger;
44 import org.w3c.dom.Document;
46 public class NetconfITMonitoringTest extends AbstractNetconfConfigTest {
48 public static final int PORT = 12025;
49 public static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
50 public static final TestingCapability TESTING_CAPABILITY = new TestingCapability();
52 private NetconfMonitoringServiceImpl netconfMonitoringService;
55 protected void setUpTestInitial() {
56 netconfMonitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
60 protected SessionMonitoringService getNetconfMonitoringService() throws Exception {
61 return netconfMonitoringService;
65 protected Iterable<NetconfOperationServiceFactory> getAdditionalServiceFactories() {
66 return Collections.<NetconfOperationServiceFactory>singletonList(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
67 new NetconfMonitoringOperationService(netconfMonitoringService)));
71 protected InetSocketAddress getTcpServerAddress() {
75 static SessionMonitoringService getNetconfMonitoringListenerService(final Logger LOG, final NetconfMonitoringServiceImpl monitor) {
76 return new SessionMonitoringService() {
78 public void onSessionUp(final NetconfManagementSession session) {
79 LOG.debug("Management session up {}", session);
80 monitor.onSessionUp(session);
84 public void onSessionDown(final NetconfManagementSession session) {
85 LOG.debug("Management session down {}", session);
86 monitor.onSessionDown(session);
92 public void testGetResponseFromMonitoring() throws Exception {
93 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client-monitoring", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 10000))) {
94 try (TestingNetconfClient netconfClient2 = new TestingNetconfClient("client-monitoring2", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 10000))) {
96 final NetconfMessage response = netconfClient2.sendMessage(getGet());
97 assertSessionElementsInResponse(response.getDocument(), 2);
100 final NetconfMessage response = netconfClient.sendMessage(getGet());
101 assertSessionElementsInResponse(response.getDocument(), 1);
106 @Test(timeout = 13 * 10000)
107 public void testClientHelloWithAuth() throws Exception {
108 String fileName = "netconfMessages/client_hello_with_auth.xml";
109 final String hello = XmlFileLoader.fileToString(fileName);
111 fileName = "netconfMessages/get.xml";
112 final String get = XmlFileLoader.fileToString(fileName);
114 final Socket sock = new Socket(TCP_ADDRESS.getHostName(), TCP_ADDRESS.getPort());
115 sock.getOutputStream().write(hello.getBytes(Charsets.UTF_8));
116 final String separator = "]]>]]>";
118 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
119 sock.getOutputStream().write(get.getBytes(Charsets.UTF_8));
120 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
122 final StringBuilder responseBuilder = new StringBuilder();
124 try (InputStream inputStream = sock.getInputStream();
125 InputStreamReader reader = new InputStreamReader(inputStream);
126 BufferedReader buff = new BufferedReader(reader)) {
128 while ((line = buff.readLine()) != null) {
130 responseBuilder.append(line);
131 responseBuilder.append(System.lineSeparator());
133 if(line.contains("</rpc-reply>"))
140 final String helloMsg = responseBuilder.substring(0, responseBuilder.indexOf(separator));
141 Document doc = XmlUtil.readXmlToDocument(helloMsg);
142 assertContainsElementWithText(doc, "urn:ietf:params:netconf:capability:candidate:1.0");
144 final String replyMsg = responseBuilder.substring(responseBuilder.indexOf(separator) + separator.length());
145 doc = XmlUtil.readXmlToDocument(replyMsg);
146 assertContainsElementWithText(doc, "tomas");
149 private void assertSessionElementsInResponse(final Document document, final int i) {
150 final int elementSize = document.getElementsByTagName("session-id").getLength();
151 assertEquals("Incorrect number of session-id tags in " + XmlUtil.toString(document), i, elementSize);
154 public static NetconfOperationProvider getNetconfOperationProvider() {
155 final NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class);
156 final NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
158 doNothing().when(snap).close();
159 } catch (final Exception e) {
161 throw new IllegalStateException(e);
163 final NetconfOperationService service = mock(NetconfOperationService.class);
164 final Set<Capability> caps = Sets.newHashSet();
165 caps.add(TESTING_CAPABILITY);
167 doReturn(caps).when(service).getCapabilities();
168 final Set<NetconfOperationService> services = Sets.newHashSet(service);
169 doReturn(services).when(snap).getServices();
170 doReturn(snap).when(factoriesListener).openSnapshot(anyString());
172 return factoriesListener;
175 private static class TestingCapability implements Capability {
177 public String getCapabilityUri() {
178 return "namespaceModuleRevision";
182 public Optional<String> getModuleNamespace() {
183 return Optional.of("namespace");
187 public Optional<String> getModuleName() {
188 return Optional.of("name");
192 public Optional<String> getRevision() {
193 return Optional.of("revision");
197 public Optional<String> getCapabilitySchema() {
198 return Optional.of("content");
202 public List<String> getLocation() {
203 return Collections.emptyList();