Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfITMonitoringTest.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.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;
17
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;
28 import java.util.Set;
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;
39
40 public class NetconfITMonitoringTest extends AbstractNetconfConfigTest {
41
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();
45
46     @Override
47     protected InetSocketAddress getTcpServerAddress() {
48         return TCP_ADDRESS;
49     }
50
51     @Test
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))) {
55                 Thread.sleep(500);
56                 final NetconfMessage response = netconfClient2.sendMessage(getGet());
57                 assertSessionElementsInResponse(response.getDocument(), 2);
58             }
59             Thread.sleep(500);
60             final NetconfMessage response = netconfClient.sendMessage(getGet());
61             assertSessionElementsInResponse(response.getDocument(), 1);
62         }
63     }
64
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);
69
70         fileName = "netconfMessages/get.xml";
71         final String get = XmlFileLoader.fileToString(fileName);
72
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 = "]]>]]>";
76
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));
80
81         final StringBuilder responseBuilder = new StringBuilder();
82
83         try (InputStream inputStream = sock.getInputStream();
84              InputStreamReader reader = new InputStreamReader(inputStream);
85              BufferedReader buff = new BufferedReader(reader)) {
86             String line;
87             while ((line = buff.readLine()) != null) {
88
89                 responseBuilder.append(line);
90                 responseBuilder.append(System.lineSeparator());
91
92                 if(line.contains("</rpc-reply>"))
93                     break;
94             }
95         }
96
97         sock.close();
98
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");
102
103         final String replyMsg = responseBuilder.substring(responseBuilder.indexOf(separator) + separator.length());
104         doc = XmlUtil.readXmlToDocument(replyMsg);
105         assertContainsElementWithText(doc, "tomas");
106     }
107
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);
111     }
112
113     public static AggregatedNetconfOperationServiceFactory getNetconfOperationProvider() throws Exception {
114         final AggregatedNetconfOperationServiceFactory factoriesListener = mock(AggregatedNetconfOperationServiceFactory.class);
115         final NetconfOperationService snap = mock(NetconfOperationService.class);
116         try {
117             doNothing().when(snap).close();
118         } catch (final Exception e) {
119             // not happening
120             throw new IllegalStateException(e);
121         }
122         final Set<Capability> caps = Sets.newHashSet();
123         caps.add(TESTING_CAPABILITY);
124
125         doReturn(caps).when(factoriesListener).getCapabilities();
126         doReturn(snap).when(factoriesListener).createService(anyString());
127
128         AutoCloseable mock = mock(AutoCloseable.class);
129         doNothing().when(mock).close();
130         doReturn(mock).when(factoriesListener).registerCapabilityListener(any(CapabilityListener.class));
131
132         return factoriesListener;
133     }
134
135     private static class TestingCapability implements Capability {
136         @Override
137         public String getCapabilityUri() {
138             return "namespaceModuleRevision";
139         }
140
141         @Override
142         public Optional<String> getModuleNamespace() {
143             return Optional.of("namespace");
144         }
145
146         @Override
147         public Optional<String> getModuleName() {
148             return Optional.of("name");
149         }
150
151         @Override
152         public Optional<String> getRevision() {
153             return Optional.of("revision");
154         }
155
156         @Override
157         public Optional<String> getCapabilitySchema() {
158             return Optional.of("content");
159         }
160
161         @Override
162         public List<String> getLocation() {
163             return Collections.emptyList();
164         }
165     }
166 }