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.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
18 import com.google.common.base.Charsets;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.Sets;
21 import java.io.BufferedReader;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.net.InetSocketAddress;
25 import java.net.Socket;
26 import java.util.Collections;
27 import java.util.List;
29 import org.junit.Test;
30 import org.opendaylight.controller.config.util.capability.Capability;
31 import org.opendaylight.controller.config.util.xml.XmlUtil;
32 import org.opendaylight.controller.netconf.api.NetconfMessage;
33 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
34 import org.opendaylight.controller.netconf.client.TestingNetconfClient;
35 import org.opendaylight.controller.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
36 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
37 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
38 import org.w3c.dom.Document;
40 public class NetconfITMonitoringTest extends AbstractNetconfConfigTest {
42 public static final int PORT = 12025;
43 public static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
44 public static final TestingCapability TESTING_CAPABILITY = new TestingCapability();
47 protected InetSocketAddress getTcpServerAddress() {
52 public void testGetResponseFromMonitoring() throws Exception {
53 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client-monitoring", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 10000))) {
54 try (TestingNetconfClient netconfClient2 = new TestingNetconfClient("client-monitoring2", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 10000))) {
56 final NetconfMessage response = netconfClient2.sendMessage(getGet());
57 assertSessionElementsInResponse(response.getDocument(), 2);
60 final NetconfMessage response = netconfClient.sendMessage(getGet());
61 assertSessionElementsInResponse(response.getDocument(), 1);
65 @Test(timeout = 13 * 10000)
66 public void testClientHelloWithAuth() throws Exception {
67 String fileName = "netconfMessages/client_hello_with_auth.xml";
68 final String hello = XmlFileLoader.fileToString(fileName);
70 fileName = "netconfMessages/get.xml";
71 final String get = XmlFileLoader.fileToString(fileName);
73 final Socket sock = new Socket(TCP_ADDRESS.getHostName(), TCP_ADDRESS.getPort());
74 sock.getOutputStream().write(hello.getBytes(Charsets.UTF_8));
75 final String separator = "]]>]]>";
77 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
78 sock.getOutputStream().write(get.getBytes(Charsets.UTF_8));
79 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
81 final StringBuilder responseBuilder = new StringBuilder();
83 try (InputStream inputStream = sock.getInputStream();
84 InputStreamReader reader = new InputStreamReader(inputStream);
85 BufferedReader buff = new BufferedReader(reader)) {
87 while ((line = buff.readLine()) != null) {
89 responseBuilder.append(line);
90 responseBuilder.append(System.lineSeparator());
92 if(line.contains("</rpc-reply>"))
99 final String helloMsg = responseBuilder.substring(0, responseBuilder.indexOf(separator));
100 Document doc = XmlUtil.readXmlToDocument(helloMsg);
101 assertContainsElementWithText(doc, "urn:ietf:params:netconf:capability:candidate:1.0");
103 final String replyMsg = responseBuilder.substring(responseBuilder.indexOf(separator) + separator.length());
104 doc = XmlUtil.readXmlToDocument(replyMsg);
105 assertContainsElementWithText(doc, "tomas");
108 private void assertSessionElementsInResponse(final Document document, final int i) {
109 final int elementSize = document.getElementsByTagName("session-id").getLength();
110 assertEquals("Incorrect number of session-id tags in " + XmlUtil.toString(document), i, elementSize);
113 public static AggregatedNetconfOperationServiceFactory getNetconfOperationProvider() throws Exception {
114 final AggregatedNetconfOperationServiceFactory factoriesListener = mock(AggregatedNetconfOperationServiceFactory.class);
115 final NetconfOperationService snap = mock(NetconfOperationService.class);
117 doNothing().when(snap).close();
118 } catch (final Exception e) {
120 throw new IllegalStateException(e);
122 final Set<Capability> caps = Sets.newHashSet();
123 caps.add(TESTING_CAPABILITY);
125 doReturn(caps).when(factoriesListener).getCapabilities();
126 doReturn(snap).when(factoriesListener).createService(anyString());
128 AutoCloseable mock = mock(AutoCloseable.class);
129 doNothing().when(mock).close();
130 doReturn(mock).when(factoriesListener).registerCapabilityListener(any(CapabilityListener.class));
132 return factoriesListener;
135 private static class TestingCapability implements Capability {
137 public String getCapabilityUri() {
138 return "namespaceModuleRevision";
142 public Optional<String> getModuleNamespace() {
143 return Optional.of("namespace");
147 public Optional<String> getModuleName() {
148 return Optional.of("name");
152 public Optional<String> getRevision() {
153 return Optional.of("revision");
157 public Optional<String> getCapabilitySchema() {
158 return Optional.of("content");
162 public List<String> getLocation() {
163 return Collections.emptyList();