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 ch.ethz.ssh2.Connection;
12 import ch.ethz.ssh2.Session;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Sets;
15 import io.netty.channel.ChannelFuture;
16 import junit.framework.Assert;
17 import org.apache.commons.io.IOUtils;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
23 import org.opendaylight.controller.config.spi.ModuleFactory;
24 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
25 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
26 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
27 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
28 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
29 import org.opendaylight.controller.netconf.StubUserManager;
30 import org.opendaylight.controller.netconf.api.NetconfMessage;
31 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
32 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
33 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
34 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
35 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
36 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
37 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
38 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener;
39 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
40 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshot;
41 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
42 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
43 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
44 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
45 import org.opendaylight.controller.netconf.util.xml.XmlElement;
46 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.w3c.dom.Document;
50 import org.w3c.dom.Element;
51 import org.w3c.dom.NamedNodeMap;
52 import org.w3c.dom.Node;
53 import org.xml.sax.SAXException;
55 import javax.management.ObjectName;
56 import javax.xml.parsers.ParserConfigurationException;
57 import java.io.IOException;
58 import java.io.InputStream;
59 import java.lang.management.ManagementFactory;
60 import java.net.InetSocketAddress;
61 import java.util.ArrayList;
62 import java.util.Arrays;
63 import java.util.Collection;
64 import java.util.Collections;
65 import java.util.List;
67 import java.util.concurrent.ExecutionException;
68 import java.util.concurrent.TimeoutException;
70 import static java.util.Collections.emptyList;
71 import static junit.framework.Assert.assertEquals;
72 import static junit.framework.Assert.assertNotNull;
73 import static junit.framework.Assert.assertTrue;
74 import static org.mockito.Matchers.anyLong;
75 import static org.mockito.Mockito.doReturn;
76 import static org.mockito.Mockito.mock;
78 public class NetconfITTest extends AbstractNetconfConfigTest {
80 // TODO refactor, pull common code up to AbstractNetconfITTest
82 private static final Logger logger = LoggerFactory.getLogger(NetconfITTest.class);
84 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
85 private static final InetSocketAddress sshAddress = new InetSocketAddress("127.0.0.1", 10830);
86 private static final String USERNAME = "netconf";
87 private static final String PASSWORD = "netconf";
89 private NetconfMessage getConfig, getConfigCandidate, editConfig,
90 closeSession, startExi, stopExi;
91 private DefaultCommitNotificationProducer commitNot;
92 private NetconfServerDispatcher dispatch;
94 private NetconfClientDispatcher clientDispatcher;
97 public void setUp() throws Exception {
98 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
99 new ModuleFactory[0])));
103 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
104 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
107 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
109 dispatch = createDispatcher(factoriesListener);
110 ChannelFuture s = dispatch.createServer(tcpAddress);
113 clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
116 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
117 return super.createDispatcher(factoriesListener, getNetconfMonitoringListenerService(), commitNot);
120 static NetconfMonitoringServiceImpl getNetconfMonitoringListenerService() {
121 NetconfOperationServiceFactoryListener factoriesListener = mock(NetconfOperationServiceFactoryListener.class);
122 NetconfOperationServiceSnapshot snap = mock(NetconfOperationServiceSnapshot.class);
123 doReturn(Collections.<NetconfOperationService>emptySet()).when(snap).getServices();
124 doReturn(snap).when(factoriesListener).getSnapshot(anyLong());
125 return new NetconfMonitoringServiceImpl(factoriesListener);
129 public void tearDown() throws Exception {
131 clientDispatcher.close();
134 private void loadMessages() throws IOException, SAXException, ParserConfigurationException {
135 this.editConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/edit_config.xml");
136 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
137 this.getConfigCandidate = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig_candidate.xml");
138 this.startExi = XmlFileLoader
139 .xmlFileToNetconfMessage("netconfMessages/startExi.xml");
140 this.stopExi = XmlFileLoader
141 .xmlFileToNetconfMessage("netconfMessages/stopExi.xml");
142 this.closeSession = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/closeSession.xml");
145 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
146 final Collection<InputStream> yangDependencies = getBasicYangs();
147 return new HardcodedYangStoreService(yangDependencies);
150 static Collection<InputStream> getBasicYangs() throws IOException {
151 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
152 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
153 "/META-INF/yang/ietf-inet-types.yang");
154 final Collection<InputStream> yangDependencies = new ArrayList<>();
155 List<String> failedToFind = new ArrayList<>();
156 for (String path : paths) {
157 InputStream resourceAsStream = NetconfITTest.class.getResourceAsStream(path);
158 if (resourceAsStream == null) {
159 failedToFind.add(path);
161 yangDependencies.add(resourceAsStream);
164 assertEquals("Some yang files were not found", emptyList(), failedToFind);
165 return yangDependencies;
168 protected List<ModuleFactory> getModuleFactories() {
169 return getModuleFactoriesS();
172 static List<ModuleFactory> getModuleFactoriesS() {
173 return Lists.newArrayList(new TestImplModuleFactory(), new DepTestImplModuleFactory(),
174 new NetconfTestImplModuleFactory());
178 public void testNetconfClientDemonstration() throws Exception {
179 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", tcpAddress, 4000, clientDispatcher)) {
181 Set<String> capabilitiesFromNetconfServer = netconfClient.getCapabilities();
182 long sessionId = netconfClient.getSessionId();
184 // NetconfMessage can be created :
185 // new NetconfMessage(XmlUtil.readXmlToDocument("<xml/>"));
187 NetconfMessage response = netconfClient.sendMessage(getConfig);
188 response.getDocument();
193 public void testTwoSessions() throws Exception {
194 try (TestingNetconfClient netconfClient = new TestingNetconfClient("1", tcpAddress, 10000, clientDispatcher)) {
195 try (TestingNetconfClient netconfClient2 = new TestingNetconfClient("2", tcpAddress, 10000, clientDispatcher)) {
202 public void waitingTest() throws Exception {
203 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
204 transaction.createModule(DepTestImplModuleFactory.NAME, "eb");
205 transaction.commit();
206 Thread.currentThread().suspend();
210 public void rpcReplyContainsAllAttributesTest() throws Exception {
211 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
212 final String rpc = "<rpc message-id=\"5\" a=\"a\" b=\"44\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
213 + "<get/>" + "</rpc>";
214 final Document doc = XmlUtil.readXmlToDocument(rpc);
215 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
216 assertNotNull(message);
217 final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
218 final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
220 assertSameAttributes(expectedAttributes, returnedAttributes);
224 private void assertSameAttributes(final NamedNodeMap expectedAttributes, final NamedNodeMap returnedAttributes) {
225 assertNotNull("Expecting 4 attributes", returnedAttributes);
226 assertEquals(expectedAttributes.getLength(), returnedAttributes.getLength());
228 for (int i = 0; i < expectedAttributes.getLength(); i++) {
229 final Node expAttr = expectedAttributes.item(i);
230 final Node attr = returnedAttributes.item(i);
231 assertEquals(expAttr.getNodeName(), attr.getNodeName());
232 assertEquals(expAttr.getNamespaceURI(), attr.getNamespaceURI());
233 assertEquals(expAttr.getTextContent(), attr.getTextContent());
238 public void rpcReplyErrorContainsAllAttributesTest() throws Exception {
239 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
240 final String rpc = "<rpc message-id=\"1\" a=\"adada\" b=\"4\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
241 + "<commit/>" + "</rpc>";
242 final Document doc = XmlUtil.readXmlToDocument(rpc);
243 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
244 final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
245 final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
247 assertSameAttributes(expectedAttributes, returnedAttributes);
252 public void rpcOutputContainsCorrectNamespace() throws Exception {
253 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
254 ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, "instanceD");
255 ObjectName impl = transaction.createModule(NetconfTestImplModuleFactory.NAME, "instance");
256 NetconfTestImplModuleMXBean proxy = configRegistryClient
257 .newMXBeanProxy(impl, NetconfTestImplModuleMXBean.class);
258 proxy.setTestingDep(dep);
259 proxy.setSimpleShort((short) 0);
261 transaction.commit();
263 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
264 final String expectedNamespace = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
266 final String rpc = "<rpc message-id=\"5\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
270 + "<context-instance>/modules/module[type='impl-netconf'][name='instance']</context-instance>"
271 + "<arg1>argument1</arg1>" + "</no-arg>" + "</rpc>";
272 final Document doc = XmlUtil.readXmlToDocument(rpc);
273 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
275 final Element rpcReply = message.getDocument().getDocumentElement();
276 final XmlElement resultElement = XmlElement.fromDomElement(rpcReply).getOnlyChildElement();
277 assertEquals("result", resultElement.getName());
279 final String namespace = resultElement.getNamespaceAttribute();
280 assertEquals(expectedNamespace, namespace);
286 public void testStartExi() throws Exception {
287 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
290 Document rpcReply = netconfClient.sendMessage(this.startExi)
292 assertIsOK(rpcReply);
294 ExiParameters exiParams = new ExiParameters();
295 exiParams.setParametersFromXmlElement(XmlElement.fromDomDocument(this.startExi.getDocument()));
297 netconfClient.getClientSession().addExiDecoder(ExiDecoderHandler.HANDLER_NAME, new ExiDecoderHandler(exiParams));
298 netconfClient.getClientSession().addExiEncoder(ExiEncoderHandler.HANDLER_NAME, new ExiEncoderHandler(exiParams));
300 rpcReply = netconfClient.sendMessage(this.editConfig)
302 assertIsOK(rpcReply);
304 rpcReply = netconfClient.sendMessage(this.stopExi)
306 assertIsOK(rpcReply);
313 public void testCloseSession() throws Exception {
314 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
317 Document rpcReply = netconfClient.sendMessage(this.editConfig)
319 assertIsOK(rpcReply);
321 rpcReply = netconfClient.sendMessage(this.closeSession)
324 assertIsOK(rpcReply);
329 public void testEditConfig() throws Exception {
330 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
331 // send edit_config.xml
332 final Document rpcReply = netconfClient.sendMessage(this.editConfig).getDocument();
333 assertIsOK(rpcReply);
338 public void testValidate() throws Exception {
339 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
341 Document rpcReply = netconfClient.sendMessage(getConfigCandidate).getDocument();
342 assertEquals("data", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
345 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/validate.xml"))
347 assertIsOK(rpcReply);
351 private void assertIsOK(final Document rpcReply) {
352 assertEquals("rpc-reply", rpcReply.getDocumentElement().getLocalName());
353 assertEquals("ok", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
356 private Document assertGetConfigWorks(final TestingNetconfClient netconfClient) throws InterruptedException, ExecutionException, TimeoutException {
357 return assertGetConfigWorks(netconfClient, this.getConfig);
360 private Document assertGetConfigWorks(final TestingNetconfClient netconfClient, final NetconfMessage getConfigMessage)
361 throws InterruptedException, ExecutionException, TimeoutException {
362 final NetconfMessage rpcReply = netconfClient.sendMessage(getConfigMessage);
363 assertNotNull(rpcReply);
364 assertEquals("data", XmlElement.fromDomDocument(rpcReply.getDocument()).getOnlyChildElement().getName());
365 return rpcReply.getDocument();
369 public void testGetConfig() throws Exception {
370 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
371 assertGetConfigWorks(netconfClient);
376 public void createYangTestBasedOnYuma() throws Exception {
377 try (TestingNetconfClient netconfClient = createSession(tcpAddress, "1")) {
378 Document rpcReply = netconfClient.sendMessage(
379 XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_yang-test.xml"))
381 assertEquals("rpc-reply", rpcReply.getDocumentElement().getTagName());
382 assertIsOK(rpcReply);
383 assertGetConfigWorks(netconfClient, this.getConfigCandidate);
384 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
386 assertIsOK(rpcReply);
388 final ObjectName on = new ObjectName(
389 "org.opendaylight.controller:instanceName=impl-dep-instance,type=Module,moduleFactoryName=impl-dep");
390 Set<ObjectName> cfgBeans = configRegistryClient.lookupConfigBeans();
391 assertEquals(cfgBeans, Sets.newHashSet(on));
395 private TestingNetconfClient createSession(final InetSocketAddress address, final String expected) throws Exception {
396 final TestingNetconfClient netconfClient = new TestingNetconfClient("test " + address.toString(), address, 5000, clientDispatcher);
397 assertEquals(expected, Long.toString(netconfClient.getSessionId()));
398 return netconfClient;
401 private void startSSHServer() throws Exception {
402 logger.info("Creating SSH server");
403 StubUserManager um = new StubUserManager(USERNAME, PASSWORD);
405 try (InputStream is = getClass().getResourceAsStream("/RSA.pk")) {
406 pem = IOUtils.toString(is);
408 AuthProvider ap = new AuthProvider(um, pem);
409 Thread sshServerThread = new Thread(NetconfSSHServer.start(10830, tcpAddress, ap));
410 sshServerThread.setDaemon(true);
411 sshServerThread.start();
412 logger.info("SSH server on");
416 public void sshTest() throws Exception {
418 logger.info("creating connection");
419 Connection conn = new Connection(sshAddress.getHostName(), sshAddress.getPort());
420 Assert.assertNotNull(conn);
421 logger.info("connection created");
423 boolean isAuthenticated = conn.authenticateWithPassword(USERNAME, PASSWORD);
424 assertTrue(isAuthenticated);
425 logger.info("user authenticated");
426 final Session sess = conn.openSession();
427 sess.startSubSystem("netconf");
428 logger.info("user authenticated");
429 sess.getStdin().write(XmlUtil.toString(this.getConfig.getDocument()).getBytes());
435 byte[] bytes = new byte[1024];
438 c = sess.getStdout().read(bytes);
439 } catch (IOException e) {
440 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
442 logger.info("got data:" + bytes);