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
9 package org.opendaylight.controller.netconf.it;
11 import static java.util.Collections.emptyList;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.lang.management.ManagementFactory;
22 import java.net.InetSocketAddress;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.TimeoutException;
32 import javax.management.ObjectName;
33 import javax.xml.parsers.ParserConfigurationException;
35 import junit.framework.Assert;
37 import org.apache.commons.io.IOUtils;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
43 import org.opendaylight.controller.config.spi.ModuleFactory;
44 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
45 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
46 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
47 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
48 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
49 import org.opendaylight.controller.netconf.StubUserManager;
50 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
51 import org.opendaylight.controller.netconf.api.NetconfMessage;
52 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
53 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
54 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
55 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
56 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
57 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
58 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
59 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
60 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
61 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
62 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
63 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
64 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
65 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
66 import org.opendaylight.controller.netconf.util.xml.XmlElement;
67 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.w3c.dom.Document;
71 import org.w3c.dom.Element;
72 import org.w3c.dom.NamedNodeMap;
73 import org.w3c.dom.Node;
74 import org.xml.sax.SAXException;
76 import ch.ethz.ssh2.Connection;
77 import ch.ethz.ssh2.Session;
79 import com.google.common.collect.Lists;
80 import com.google.common.collect.Sets;
81 import io.netty.channel.ChannelFuture;
83 public class NetconfITTest extends AbstractNetconfConfigTest {
85 // TODO refactor, pull common code up to AbstractNetconfITTest
87 private static final Logger logger = LoggerFactory.getLogger(NetconfITTest.class);
89 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
90 private static final InetSocketAddress sshAddress = new InetSocketAddress("127.0.0.1", 10830);
91 private static final String USERNAME = "netconf";
92 private static final String PASSWORD = "netconf";
94 private NetconfMessage getConfig, getConfigCandidate, editConfig,
95 closeSession, startExi, stopExi;
96 private DefaultCommitNotificationProducer commitNot;
97 private NetconfServerDispatcher dispatch;
99 private NetconfClientDispatcherImpl clientDispatcher;
102 public void setUp() throws Exception {
103 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,getModuleFactories().toArray(
104 new ModuleFactory[0])));
108 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
109 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
112 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
114 dispatch = createDispatcher(factoriesListener);
115 ChannelFuture s = dispatch.createServer(tcpAddress);
118 clientDispatcher = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
121 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
122 return super.createDispatcher(factoriesListener, getNetconfMonitoringListenerService(), commitNot);
125 static NetconfMonitoringServiceImpl getNetconfMonitoringListenerService() {
126 NetconfOperationProvider netconfOperationProvider = mock(NetconfOperationProvider.class);
127 NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
128 doReturn(Collections.<NetconfOperationService>emptySet()).when(snap).getServices();
129 doReturn(snap).when(netconfOperationProvider).openSnapshot(anyString());
130 return new NetconfMonitoringServiceImpl(netconfOperationProvider);
134 public void tearDown() throws Exception {
136 clientDispatcher.close();
139 private void loadMessages() throws IOException, SAXException, ParserConfigurationException {
140 this.editConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/edit_config.xml");
141 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
142 this.getConfigCandidate = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig_candidate.xml");
143 this.startExi = XmlFileLoader
144 .xmlFileToNetconfMessage("netconfMessages/startExi.xml");
145 this.stopExi = XmlFileLoader
146 .xmlFileToNetconfMessage("netconfMessages/stopExi.xml");
147 this.closeSession = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/closeSession.xml");
150 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
151 final Collection<InputStream> yangDependencies = getBasicYangs();
152 return new HardcodedYangStoreService(yangDependencies);
155 static Collection<InputStream> getBasicYangs() throws IOException {
157 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
158 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
159 "/META-INF/yang/ietf-inet-types.yang");
160 final Collection<InputStream> yangDependencies = new ArrayList<>();
161 List<String> failedToFind = new ArrayList<>();
162 for (String path : paths) {
163 InputStream resourceAsStream = NetconfITTest.class.getResourceAsStream(path);
164 if (resourceAsStream == null) {
165 failedToFind.add(path);
167 yangDependencies.add(resourceAsStream);
170 assertEquals("Some yang files were not found", emptyList(), failedToFind);
171 return yangDependencies;
174 protected List<ModuleFactory> getModuleFactories() {
175 return getModuleFactoriesS();
178 static List<ModuleFactory> getModuleFactoriesS() {
179 return Lists.newArrayList(new TestImplModuleFactory(), new DepTestImplModuleFactory(),
180 new NetconfTestImplModuleFactory());
184 public void testNetconfClientDemonstration() throws Exception {
185 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
187 Set<String> capabilitiesFromNetconfServer = netconfClient.getCapabilities();
188 long sessionId = netconfClient.getSessionId();
190 // NetconfMessage can be created :
191 // new NetconfMessage(XmlUtil.readXmlToDocument("<xml/>"));
193 NetconfMessage response = netconfClient.sendMessage(getConfig);
194 response.getDocument();
199 public void testTwoSessions() throws Exception {
200 try (TestingNetconfClient netconfClient = new TestingNetconfClient("1", clientDispatcher, getClientConfiguration(tcpAddress, 10000))) {
201 try (TestingNetconfClient netconfClient2 = new TestingNetconfClient("2", clientDispatcher, getClientConfiguration(tcpAddress, 10000))) {
208 public void waitingTest() throws Exception {
209 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
210 transaction.createModule(DepTestImplModuleFactory.NAME, "eb");
211 transaction.commit();
212 Thread.currentThread().suspend();
216 public void rpcReplyContainsAllAttributesTest() throws Exception {
217 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
218 final String rpc = "<rpc message-id=\"5\" a=\"a\" b=\"44\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
219 + "<get/>" + "</rpc>";
220 final Document doc = XmlUtil.readXmlToDocument(rpc);
221 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
222 assertNotNull(message);
223 final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
224 final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
226 assertSameAttributes(expectedAttributes, returnedAttributes);
230 private void assertSameAttributes(final NamedNodeMap expectedAttributes, final NamedNodeMap returnedAttributes) {
231 assertNotNull("Expecting 4 attributes", returnedAttributes);
232 assertEquals(expectedAttributes.getLength(), returnedAttributes.getLength());
234 for (int i = 0; i < expectedAttributes.getLength(); i++) {
235 final Node expAttr = expectedAttributes.item(i);
236 final Node attr = returnedAttributes.item(i);
237 assertEquals(expAttr.getNodeName(), attr.getNodeName());
238 assertEquals(expAttr.getNamespaceURI(), attr.getNamespaceURI());
239 assertEquals(expAttr.getTextContent(), attr.getTextContent());
244 public void rpcReplyErrorContainsAllAttributesTest() throws Exception {
245 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
246 final String rpc = "<rpc message-id=\"1\" a=\"adada\" b=\"4\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
247 + "<commit/>" + "</rpc>";
248 final Document doc = XmlUtil.readXmlToDocument(rpc);
249 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
250 final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
251 final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
253 assertSameAttributes(expectedAttributes, returnedAttributes);
258 public void rpcOutputContainsCorrectNamespace() throws Exception {
259 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
260 ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, "instanceD");
261 ObjectName impl = transaction.createModule(NetconfTestImplModuleFactory.NAME, "instance");
262 NetconfTestImplModuleMXBean proxy = configRegistryClient
263 .newMXBeanProxy(impl, NetconfTestImplModuleMXBean.class);
264 proxy.setTestingDep(dep);
265 proxy.setSimpleShort((short) 0);
267 transaction.commit();
269 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
270 final String expectedNamespace = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
272 final String rpc = "<rpc message-id=\"5\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
276 + "<context-instance>/modules/module[type='impl-netconf'][name='instance']</context-instance>"
277 + "<arg1>argument1</arg1>" + "</no-arg>" + "</rpc>";
278 final Document doc = XmlUtil.readXmlToDocument(rpc);
279 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
281 final Element rpcReply = message.getDocument().getDocumentElement();
282 final XmlElement resultElement = XmlElement.fromDomElement(rpcReply).getOnlyChildElement();
283 assertEquals("result", resultElement.getName());
285 final String namespace = resultElement.getNamespaceAttribute();
286 assertEquals(expectedNamespace, namespace);
291 public void testCloseSession() throws Exception {
292 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
295 Document rpcReply = netconfClient.sendMessage(this.editConfig)
297 assertIsOK(rpcReply);
299 rpcReply = netconfClient.sendMessage(this.closeSession)
302 assertIsOK(rpcReply);
307 public void testEditConfig() throws Exception {
308 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
309 // send edit_config.xml
310 final Document rpcReply = netconfClient.sendMessage(this.editConfig).getDocument();
311 assertIsOK(rpcReply);
316 public void testValidate() throws Exception {
317 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
319 Document rpcReply = netconfClient.sendMessage(getConfigCandidate).getDocument();
320 assertEquals("data", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
323 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/validate.xml"))
325 assertIsOK(rpcReply);
329 private void assertIsOK(final Document rpcReply) throws NetconfDocumentedException {
330 assertEquals("rpc-reply", rpcReply.getDocumentElement().getLocalName());
331 assertEquals("ok", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
334 private Document assertGetConfigWorks(final TestingNetconfClient netconfClient) throws InterruptedException, ExecutionException, TimeoutException, NetconfDocumentedException {
335 return assertGetConfigWorks(netconfClient, this.getConfig);
338 private Document assertGetConfigWorks(final TestingNetconfClient netconfClient, final NetconfMessage getConfigMessage)
339 throws InterruptedException, ExecutionException, TimeoutException, NetconfDocumentedException {
340 final NetconfMessage rpcReply = netconfClient.sendMessage(getConfigMessage);
341 assertNotNull(rpcReply);
342 assertEquals("data", XmlElement.fromDomDocument(rpcReply.getDocument()).getOnlyChildElement().getName());
343 return rpcReply.getDocument();
347 public void testGetConfig() throws Exception {
348 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
349 assertGetConfigWorks(netconfClient);
354 public void createYangTestBasedOnYuma() throws Exception {
355 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
356 Document rpcReply = netconfClient.sendMessage(
357 XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_yang-test.xml"))
359 assertEquals("rpc-reply", rpcReply.getDocumentElement().getTagName());
360 assertIsOK(rpcReply);
361 assertGetConfigWorks(netconfClient, this.getConfigCandidate);
362 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
364 assertIsOK(rpcReply);
366 final ObjectName on = new ObjectName(
367 "org.opendaylight.controller:instanceName=impl-dep-instance,type=Module,moduleFactoryName=impl-dep");
368 Set<ObjectName> cfgBeans = configRegistryClient.lookupConfigBeans();
369 assertEquals(cfgBeans, Sets.newHashSet(on));
373 private TestingNetconfClient createSession(final InetSocketAddress address, final String expected) throws Exception {
374 final TestingNetconfClient netconfClient = new TestingNetconfClient("test " + address.toString(), clientDispatcher, getClientConfiguration(address, 5000));
375 assertEquals(expected, Long.toString(netconfClient.getSessionId()));
376 return netconfClient;
379 private void startSSHServer() throws Exception {
380 logger.info("Creating SSH server");
381 StubUserManager um = new StubUserManager(USERNAME, PASSWORD);
383 try (InputStream is = getClass().getResourceAsStream("/RSA.pk")) {
384 pem = IOUtils.toString(is);
386 AuthProvider ap = new AuthProvider(um, pem);
387 Thread sshServerThread = new Thread(NetconfSSHServer.start(10830, tcpAddress, ap));
388 sshServerThread.setDaemon(true);
389 sshServerThread.start();
390 logger.info("SSH server on");
394 public void sshTest() throws Exception {
396 logger.info("creating connection");
397 Connection conn = new Connection(sshAddress.getHostName(), sshAddress.getPort());
398 Assert.assertNotNull(conn);
399 logger.info("connection created");
401 boolean isAuthenticated = conn.authenticateWithPassword(USERNAME, PASSWORD);
402 assertTrue(isAuthenticated);
403 logger.info("user authenticated");
404 final Session sess = conn.openSession();
405 sess.startSubSystem("netconf");
406 logger.info("user authenticated");
407 sess.getStdin().write(XmlUtil.toString(this.getConfig.getDocument()).getBytes());
413 byte[] bytes = new byte[1024];
416 c = sess.getStdout().read(bytes);
417 } catch (IOException e) {
418 throw new IllegalStateException("IO exception while reading data on ssh bridge.");
420 logger.info("got data:" + bytes);