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.netconf.api.Capability;
31 import org.opendaylight.controller.netconf.api.NetconfMessage;
32 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
33 import org.opendaylight.controller.netconf.client.TestingNetconfClient;
34 import org.opendaylight.controller.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
35 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
36 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
37 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
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);
66 @Test(timeout = 13 * 10000)
67 public void testClientHelloWithAuth() throws Exception {
68 String fileName = "netconfMessages/client_hello_with_auth.xml";
69 final String hello = XmlFileLoader.fileToString(fileName);
71 fileName = "netconfMessages/get.xml";
72 final String get = XmlFileLoader.fileToString(fileName);
74 final Socket sock = new Socket(TCP_ADDRESS.getHostName(), TCP_ADDRESS.getPort());
75 sock.getOutputStream().write(hello.getBytes(Charsets.UTF_8));
76 final String separator = "]]>]]>";
78 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
79 sock.getOutputStream().write(get.getBytes(Charsets.UTF_8));
80 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
82 final StringBuilder responseBuilder = new StringBuilder();
84 try (InputStream inputStream = sock.getInputStream();
85 InputStreamReader reader = new InputStreamReader(inputStream);
86 BufferedReader buff = new BufferedReader(reader)) {
88 while ((line = buff.readLine()) != null) {
90 responseBuilder.append(line);
91 responseBuilder.append(System.lineSeparator());
93 if(line.contains("</rpc-reply>"))
100 final String helloMsg = responseBuilder.substring(0, responseBuilder.indexOf(separator));
101 Document doc = XmlUtil.readXmlToDocument(helloMsg);
102 assertContainsElementWithText(doc, "urn:ietf:params:netconf:capability:candidate:1.0");
104 final String replyMsg = responseBuilder.substring(responseBuilder.indexOf(separator) + separator.length());
105 doc = XmlUtil.readXmlToDocument(replyMsg);
106 assertContainsElementWithText(doc, "tomas");
109 private void assertSessionElementsInResponse(final Document document, final int i) {
110 final int elementSize = document.getElementsByTagName("session-id").getLength();
111 assertEquals("Incorrect number of session-id tags in " + XmlUtil.toString(document), i, elementSize);
114 public static AggregatedNetconfOperationServiceFactory getNetconfOperationProvider() throws Exception {
115 final AggregatedNetconfOperationServiceFactory factoriesListener = mock(AggregatedNetconfOperationServiceFactory.class);
116 final NetconfOperationService snap = mock(NetconfOperationService.class);
118 doNothing().when(snap).close();
119 } catch (final Exception e) {
121 throw new IllegalStateException(e);
123 final Set<Capability> caps = Sets.newHashSet();
124 caps.add(TESTING_CAPABILITY);
126 doReturn(caps).when(factoriesListener).getCapabilities();
127 doReturn(snap).when(factoriesListener).createService(anyString());
129 AutoCloseable mock = mock(AutoCloseable.class);
130 doNothing().when(mock).close();
131 doReturn(mock).when(factoriesListener).registerCapabilityListener(any(CapabilityListener.class));
133 return factoriesListener;
136 private static class TestingCapability implements Capability {
138 public String getCapabilityUri() {
139 return "namespaceModuleRevision";
143 public Optional<String> getModuleNamespace() {
144 return Optional.of("namespace");
148 public Optional<String> getModuleName() {
149 return Optional.of("name");
153 public Optional<String> getRevision() {
154 return Optional.of("revision");
158 public Optional<String> getCapabilitySchema() {
159 return Optional.of("content");
163 public List<String> getLocation() {
164 return Collections.emptyList();