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.mockito.Matchers.anyString;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
16 import com.google.common.base.Charsets;
17 import com.google.common.base.Optional;
18 import com.google.common.collect.Sets;
19 import io.netty.channel.ChannelFuture;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.net.InetSocketAddress;
25 import java.net.Socket;
26 import java.util.Collection;
27 import java.util.List;
29 import junit.framework.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
34 import org.opendaylight.controller.netconf.api.NetconfMessage;
35 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
36 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
37 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
38 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
39 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
40 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
41 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
42 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
43 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
44 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
45 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
46 import org.opendaylight.controller.netconf.mapping.api.Capability;
47 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
48 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
49 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
50 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
51 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
52 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.w3c.dom.Document;
57 public class NetconfMonitoringITTest extends AbstractNetconfConfigTest {
59 private static final Logger logger = LoggerFactory.getLogger(NetconfITTest.class);
61 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
64 private DefaultCommitNotificationProducer commitNot;
65 private NetconfServerDispatcher dispatch;
67 private NetconfClientDispatcherImpl clientDispatcher;
69 private NetconfMonitoringServiceImpl monitoringService;
72 public void setUp() throws Exception {
73 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, NetconfITTest.FACTORIES));
75 monitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
77 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
78 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
80 .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
81 new NetconfMonitoringOperationService(monitoringService)));
84 dispatch = createDispatcher(factoriesListener);
85 ChannelFuture s = dispatch.createServer(tcpAddress);
88 clientDispatcher = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
91 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
92 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
93 return new HardcodedYangStoreService(yangDependencies);
96 private NetconfServerDispatcher createDispatcher(
97 NetconfOperationServiceFactoryListenerImpl factoriesListener) {
98 return super.createDispatcher(factoriesListener, getNetconfMonitoringListenerService(logger, monitoringService), commitNot);
101 static SessionMonitoringService getNetconfMonitoringListenerService(final Logger logger, final NetconfMonitoringServiceImpl monitor) {
102 return new SessionMonitoringService() {
104 public void onSessionUp(NetconfManagementSession session) {
105 logger.debug("Management session up {}", session);
106 monitor.onSessionUp(session);
110 public void onSessionDown(NetconfManagementSession session) {
111 logger.debug("Management session down {}", session);
112 monitor.onSessionDown(session);
119 public void testGetResponseFromMonitoring() throws Exception {
120 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client-monitoring", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
121 try (TestingNetconfClient netconfClient2 = new TestingNetconfClient("client-monitoring2", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
122 NetconfMessage response = netconfClient.sendMessage(loadGetMessage());
123 assertSessionElementsInResponse(response.getDocument(), 2);
125 NetconfMessage response = netconfClient.sendMessage(loadGetMessage());
126 assertSessionElementsInResponse(response.getDocument(), 1);
131 @Test(timeout = 13 * 10000)
132 public void testClientHelloWithAuth() throws Exception {
133 String fileName = "netconfMessages/client_hello_with_auth.xml";
134 String hello = XmlFileLoader.fileToString(fileName);
136 fileName = "netconfMessages/get.xml";
137 String get = XmlFileLoader.fileToString(fileName);
139 Socket sock = new Socket(tcpAddress.getHostName(), tcpAddress.getPort());
140 sock.getOutputStream().write(hello.getBytes(Charsets.UTF_8));
141 String separator = "]]>]]>";
143 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
144 sock.getOutputStream().write(get.getBytes(Charsets.UTF_8));
145 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
147 StringBuilder responseBuilder = new StringBuilder();
149 try (InputStream inputStream = sock.getInputStream();
150 InputStreamReader reader = new InputStreamReader(inputStream);
151 BufferedReader buff = new BufferedReader(reader)) {
153 while ((line = buff.readLine()) != null) {
155 responseBuilder.append(line);
156 responseBuilder.append(System.lineSeparator());
158 if(line.contains("</rpc-reply>"))
165 String helloMsg = responseBuilder.substring(0, responseBuilder.indexOf(separator));
166 Document doc = XmlUtil.readXmlToDocument(helloMsg);
167 assertContainsElementWithText(doc, "urn:ietf:params:netconf:capability:candidate:1.0");
169 String replyMsg = responseBuilder.substring(responseBuilder.indexOf(separator) + separator.length());
170 doc = XmlUtil.readXmlToDocument(replyMsg);
171 assertContainsElementWithText(doc, "tomas");
174 private void assertSessionElementsInResponse(Document document, int i) {
175 int elementSize = document.getElementsByTagName("session-id").getLength();
176 Assert.assertEquals("Incorrect number of session-id tags in " + XmlUtil.toString(document),i, elementSize);
179 private NetconfMessage loadGetMessage() throws Exception {
180 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/get.xml");
183 public static NetconfOperationProvider getNetconfOperationProvider() throws Exception {
184 NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class);
185 NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
186 doNothing().when(snap).close();
187 NetconfOperationService service = mock(NetconfOperationService.class);
188 Set<Capability> caps = Sets.newHashSet();
189 caps.add(new Capability() {
191 public String getCapabilityUri() {
192 return "namespaceModuleRevision";
196 public Optional<String> getModuleNamespace() {
197 return Optional.of("namespace");
201 public Optional<String> getModuleName() {
202 return Optional.of("name");
206 public Optional<String> getRevision() {
207 return Optional.of("revision");
211 public Optional<String> getCapabilitySchema() {
212 return Optional.of("content");
216 public Optional<List<String>> getLocation() {
217 return Optional.absent();
221 doReturn(caps).when(service).getCapabilities();
222 Set<NetconfOperationService> services = Sets.newHashSet(service);
223 doReturn(services).when(snap).getServices();
224 doReturn(snap).when(factoriesListener).openSnapshot(anyString());
226 return factoriesListener;