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.config.yang.store.api.YangStoreException;
22 import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
23 import org.opendaylight.controller.netconf.api.NetconfMessage;
24 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
25 import org.opendaylight.controller.netconf.client.NetconfClient;
26 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
27 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
28 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
29 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
30 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
31 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener;
32 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
33 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshot;
34 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
35 import org.opendaylight.controller.netconf.mapping.api.Capability;
36 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
37 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
38 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
39 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
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.anyLong;
55 import static org.mockito.Mockito.doReturn;
56 import static org.mockito.Mockito.mock;
58 public class NetconfMonitoringITTest extends AbstractNetconfConfigTest {
60 private static final Logger logger = LoggerFactory.getLogger(NetconfITTest.class);
62 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
65 private DefaultCommitNotificationProducer commitNot;
66 private NetconfServerDispatcher dispatch;
68 private NetconfClientDispatcher clientDispatcher;
70 private NetconfMonitoringServiceImpl monitoringService;
73 public void setUp() throws Exception {
74 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(NetconfITTest.getModuleFactoriesS().toArray(
75 new ModuleFactory[0])));
77 monitoringService = new NetconfMonitoringServiceImpl(getFactoriesListener());
79 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
80 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
82 .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(
83 new NetconfMonitoringOperationService(monitoringService)));
86 dispatch = createDispatcher(factoriesListener);
87 ChannelFuture s = dispatch.createServer(tcpAddress);
90 clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
93 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
94 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
95 return new HardcodedYangStoreService(yangDependencies);
98 private NetconfServerDispatcher createDispatcher(
99 NetconfOperationServiceFactoryListenerImpl factoriesListener) {
100 return super.createDispatcher(factoriesListener, getNetconfMonitoringListenerService(logger, monitoringService), commitNot);
103 static SessionMonitoringService getNetconfMonitoringListenerService(final Logger logger, final NetconfMonitoringServiceImpl monitor) {
104 return new SessionMonitoringService() {
106 public void onSessionUp(NetconfManagementSession session) {
107 logger.debug("Management session up {}", session);
108 monitor.onSessionUp(session);
112 public void onSessionDown(NetconfManagementSession session) {
113 logger.debug("Management session down {}", session);
114 monitor.onSessionDown(session);
121 public void testGetResponseFromMonitoring() throws Exception {
122 try (NetconfClient netconfClient = new NetconfClient("client-monitoring", tcpAddress, 4000, clientDispatcher)) {
123 try (NetconfClient netconfClient2 = new NetconfClient("client-monitoring2", tcpAddress, 4000, clientDispatcher)) {
124 NetconfMessage response = netconfClient.sendMessage(loadGetMessage());
125 assertSessionElementsInResponse(response.getDocument(), 2);
127 NetconfMessage response = netconfClient.sendMessage(loadGetMessage());
128 assertSessionElementsInResponse(response.getDocument(), 1);
133 @Test(timeout = 5 * 10000)
134 public void testClientHelloWithAuth() throws Exception {
135 String fileName = "netconfMessages/client_hello_with_auth.xml";
136 String hello = XmlFileLoader.fileToString(fileName);
138 fileName = "netconfMessages/get.xml";
139 String get = XmlFileLoader.fileToString(fileName);
141 Socket sock = new Socket(tcpAddress.getHostName(), tcpAddress.getPort());
142 sock.getOutputStream().write(hello.getBytes(Charsets.UTF_8));
143 String separator = "]]>]]>";
145 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
146 sock.getOutputStream().write(get.getBytes(Charsets.UTF_8));
147 sock.getOutputStream().write(separator.getBytes(Charsets.UTF_8));
149 StringBuilder responseBuilder = new StringBuilder();
151 try (InputStream inputStream = sock.getInputStream();
152 InputStreamReader reader = new InputStreamReader(inputStream);
153 BufferedReader buff = new BufferedReader(reader)) {
155 while ((line = buff.readLine()) != null) {
157 responseBuilder.append(line);
158 responseBuilder.append(System.lineSeparator());
160 if(line.contains("</rpc-reply>"))
165 org.junit.Assert.assertThat(responseBuilder.toString(), JUnitMatchers.containsString("<capability>urn:ietf:params:netconf:capability:candidate:1.0</capability>"));
166 org.junit.Assert.assertThat(responseBuilder.toString(), JUnitMatchers.containsString("<username>tomas</username>"));
169 private void assertSessionElementsInResponse(Document document, int i) {
170 int elementSize = document.getElementsByTagName("session-id").getLength();
171 Assert.assertEquals(i, elementSize);
174 private NetconfMessage loadGetMessage() throws Exception {
175 return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/get.xml");
178 public static NetconfOperationServiceFactoryListener getFactoriesListener() {
179 NetconfOperationServiceFactoryListener factoriesListener = mock(NetconfOperationServiceFactoryListener.class);
180 NetconfOperationServiceSnapshot snap = mock(NetconfOperationServiceSnapshot.class);
181 NetconfOperationService service = mock(NetconfOperationService.class);
182 Set<Capability> caps = Sets.newHashSet();
183 caps.add(new Capability() {
185 public String getCapabilityUri() {
186 return "namespaceModuleRevision";
190 public Optional<String> getModuleNamespace() {
191 return Optional.of("namespace");
195 public Optional<String> getModuleName() {
196 return Optional.of("name");
200 public Optional<String> getRevision() {
201 return Optional.of("revision");
205 public Optional<String> getCapabilitySchema() {
206 return Optional.of("content");
210 public Optional<List<String>> getLocation() {
211 return Optional.absent();
215 doReturn(caps).when(service).getCapabilities();
216 Set<NetconfOperationService> services = Sets.newHashSet(service);
217 doReturn(services).when(snap).getServices();
218 doReturn(snap).when(factoriesListener).getSnapshot(anyLong());
220 return factoriesListener;