BUG-2635 Prepare netconf monitoring service for md-sal monitoring.
[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.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;
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
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);
70
71         fileName = "netconfMessages/get.xml";
72         final String get = XmlFileLoader.fileToString(fileName);
73
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 = "]]>]]>";
77
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));
81
82         final StringBuilder responseBuilder = new StringBuilder();
83
84         try (InputStream inputStream = sock.getInputStream();
85              InputStreamReader reader = new InputStreamReader(inputStream);
86              BufferedReader buff = new BufferedReader(reader)) {
87             String line;
88             while ((line = buff.readLine()) != null) {
89
90                 responseBuilder.append(line);
91                 responseBuilder.append(System.lineSeparator());
92
93                 if(line.contains("</rpc-reply>"))
94                     break;
95             }
96         }
97
98         sock.close();
99
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");
103
104         final String replyMsg = responseBuilder.substring(responseBuilder.indexOf(separator) + separator.length());
105         doc = XmlUtil.readXmlToDocument(replyMsg);
106         assertContainsElementWithText(doc, "tomas");
107     }
108
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);
112     }
113
114     public static AggregatedNetconfOperationServiceFactory getNetconfOperationProvider() throws Exception {
115         final AggregatedNetconfOperationServiceFactory factoriesListener = mock(AggregatedNetconfOperationServiceFactory.class);
116         final NetconfOperationService snap = mock(NetconfOperationService.class);
117         try {
118             doNothing().when(snap).close();
119         } catch (final Exception e) {
120             // not happening
121             throw new IllegalStateException(e);
122         }
123         final Set<Capability> caps = Sets.newHashSet();
124         caps.add(TESTING_CAPABILITY);
125
126         doReturn(caps).when(factoriesListener).getCapabilities();
127         doReturn(snap).when(factoriesListener).createService(anyString());
128
129         AutoCloseable mock = mock(AutoCloseable.class);
130         doNothing().when(mock).close();
131         doReturn(mock).when(factoriesListener).registerCapabilityListener(any(CapabilityListener.class));
132
133         return factoriesListener;
134     }
135
136     private static class TestingCapability implements Capability {
137         @Override
138         public String getCapabilityUri() {
139             return "namespaceModuleRevision";
140         }
141
142         @Override
143         public Optional<String> getModuleNamespace() {
144             return Optional.of("namespace");
145         }
146
147         @Override
148         public Optional<String> getModuleName() {
149             return Optional.of("name");
150         }
151
152         @Override
153         public Optional<String> getRevision() {
154             return Optional.of("revision");
155         }
156
157         @Override
158         public Optional<String> getCapabilitySchema() {
159             return Optional.of("content");
160         }
161
162         @Override
163         public List<String> getLocation() {
164             return Collections.emptyList();
165         }
166     }
167 }