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 com.google.common.base.Charsets;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Sets;
13 import io.netty.channel.ChannelFuture;
14 import junit.framework.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.matchers.JUnitMatchers;
18 import org.mockito.Mock;
19 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
20 import org.opendaylight.controller.config.spi.ModuleFactory;
21 import org.opendaylight.controller.netconf.api.NetconfMessage;
22 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
23 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
24 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
25 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
26 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
27 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
28 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
29 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
30 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
31 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
32 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
33 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
34 import org.opendaylight.controller.netconf.mapping.api.Capability;
35 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
36 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
37 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
38 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
39 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.w3c.dom.Document;
44 import java.io.BufferedReader;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.InputStreamReader;
48 import java.net.InetSocketAddress;
49 import java.net.Socket;
50 import java.util.Collection;
51 import java.util.List;
54 import static org.mockito.Matchers.anyString;
55 import static org.mockito.Mockito.doNothing;
56 import static org.mockito.Mockito.doReturn;
57 import static org.mockito.Mockito.mock;
59 public class NetconfMonitoringITTest extends AbstractNetconfConfigTest {
61 private static final Logger logger = LoggerFactory.getLogger(NetconfITTest.class);
63 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
66 private DefaultCommitNotificationProducer commitNot;
67 private NetconfServerDispatcher dispatch;
69 private NetconfClientDispatcher clientDispatcher;
71 private NetconfMonitoringServiceImpl monitoringService;
74 public void setUp() throws Exception {
75 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(NetconfITTest.getModuleFactoriesS().toArray(
76 new ModuleFactory[0])));
78 monitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
80 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
81 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
83 .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
84 new NetconfMonitoringOperationService(monitoringService)));
87 dispatch = createDispatcher(factoriesListener);
88 ChannelFuture s = dispatch.createServer(tcpAddress);
91 clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
94 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
95 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
96 return new HardcodedYangStoreService(yangDependencies);
99 private NetconfServerDispatcher createDispatcher(
100 NetconfOperationServiceFactoryListenerImpl factoriesListener) {
101 return super.createDispatcher(factoriesListener, getNetconfMonitoringListenerService(logger, monitoringService), commitNot);
104 static SessionMonitoringService getNetconfMonitoringListenerService(final Logger logger, final NetconfMonitoringServiceImpl monitor) {
105 return new SessionMonitoringService() {
107 public void onSessionUp(NetconfManagementSession session) {
108 logger.debug("Management session up {}", session);
109 monitor.onSessionUp(session);
113 public void onSessionDown(NetconfManagementSession session) {
114 logger.debug("Management session down {}", session);
115 monitor.onSessionDown(session);
122 public void testGetResponseFromMonitoring() throws Exception {
123 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client-monitoring", tcpAddress, 4000, clientDispatcher)) {
124 try (TestingNetconfClient netconfClient2 = new TestingNetconfClient("client-monitoring2", tcpAddress, 4000, clientDispatcher)) {
125 NetconfMessage response = netconfClient.sendMessage(loadGetMessage());
126 assertSessionElementsInResponse(response.getDocument(), 2);
128 NetconfMessage response = netconfClient.sendMessage(loadGetMessage());
129 assertSessionElementsInResponse(response.getDocument(), 1);
134 @Test(timeout = 5 * 10000)
135 public void testClientHelloWithAuth() throws Exception {
136 String fileName = "netconfMessages/client_hello_with_auth.xml";
137 String hello = XmlFileLoader.fileToString(fileName);
139 fileName = "netconfMessages/get.xml";
140 String get = XmlFileLoader.fileToString(fileName);
142 Socket sock = new Socket(tcpAddress.getHostName(), tcpAddress.getPort());
143 sock.getOutputStream().write(hello.getBytes(Charsets.UTF_8));
144 String separator = "]]>]]>";
146 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
147 sock.getOutputStream().write(get.getBytes(Charsets.UTF_8));
148 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
150 StringBuilder responseBuilder = new StringBuilder();
152 try (InputStream inputStream = sock.getInputStream();
153 InputStreamReader reader = new InputStreamReader(inputStream);
154 BufferedReader buff = new BufferedReader(reader)) {
156 while ((line = buff.readLine()) != null) {
158 responseBuilder.append(line);
159 responseBuilder.append(System.lineSeparator());
161 if(line.contains("</rpc-reply>"))
168 org.junit.Assert.assertThat(responseBuilder.toString(), JUnitMatchers.containsString("<capability>urn:ietf:params:netconf:capability:candidate:1.0</capability>"));
169 org.junit.Assert.assertThat(responseBuilder.toString(), JUnitMatchers.containsString("<username>tomas</username>"));
172 private void assertSessionElementsInResponse(Document document, int i) {
173 int elementSize = document.getElementsByTagName("session-id").getLength();
174 Assert.assertEquals("Incorrect number of session-id tags in " + XmlUtil.toString(document),i, elementSize);
177 private NetconfMessage loadGetMessage() throws Exception {
178 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/get.xml");
181 public static NetconfOperationProvider getNetconfOperationProvider() throws Exception {
182 NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class);
183 NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
184 doNothing().when(snap).close();
185 NetconfOperationService service = mock(NetconfOperationService.class);
186 Set<Capability> caps = Sets.newHashSet();
187 caps.add(new Capability() {
189 public String getCapabilityUri() {
190 return "namespaceModuleRevision";
194 public Optional<String> getModuleNamespace() {
195 return Optional.of("namespace");
199 public Optional<String> getModuleName() {
200 return Optional.of("name");
204 public Optional<String> getRevision() {
205 return Optional.of("revision");
209 public Optional<String> getCapabilitySchema() {
210 return Optional.of("content");
214 public Optional<List<String>> getLocation() {
215 return Optional.absent();
219 doReturn(caps).when(service).getCapabilities();
220 Set<NetconfOperationService> services = Sets.newHashSet(service);
221 doReturn(services).when(snap).getServices();
222 doReturn(snap).when(factoriesListener).openSnapshot(anyString());
224 return factoriesListener;