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 io.netty.channel.EventLoopGroup;
17 import io.netty.channel.nio.NioEventLoopGroup;
18 import io.netty.util.HashedWheelTimer;
19 import junit.framework.Assert;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
25 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
26 import org.opendaylight.controller.config.spi.ModuleFactory;
27 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
28 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
29 import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
30 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
31 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
32 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
33 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
34 import org.opendaylight.controller.netconf.StubUserManager;
35 import org.opendaylight.controller.netconf.api.NetconfMessage;
36 import org.opendaylight.controller.netconf.client.NetconfClient;
37 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
38 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
39 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
40 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
41 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory;
42 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
43 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
44 import org.opendaylight.controller.netconf.impl.mapping.ExiDecoderHandler;
45 import org.opendaylight.controller.netconf.impl.mapping.ExiEncoderHandler;
46 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
47 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener;
48 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
49 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshot;
50 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
51 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
52 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
53 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
54 import org.opendaylight.controller.netconf.util.xml.ExiParameters;
55 import org.opendaylight.controller.netconf.util.xml.XmlElement;
56 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.w3c.dom.Document;
60 import org.w3c.dom.Element;
61 import org.w3c.dom.NamedNodeMap;
62 import org.w3c.dom.Node;
63 import org.xml.sax.SAXException;
65 import javax.management.ObjectName;
66 import javax.xml.parsers.ParserConfigurationException;
67 import java.io.IOException;
68 import java.io.InputStream;
69 import java.lang.management.ManagementFactory;
70 import java.net.InetSocketAddress;
71 import java.util.ArrayList;
72 import java.util.Arrays;
73 import java.util.Collection;
74 import java.util.Collections;
75 import java.util.List;
77 import java.util.concurrent.TimeUnit;
79 import static java.util.Collections.emptyList;
80 import static junit.framework.Assert.assertEquals;
81 import static junit.framework.Assert.assertNotNull;
82 import static junit.framework.Assert.assertTrue;
83 import static org.mockito.Matchers.anyLong;
84 import static org.mockito.Mockito.doReturn;
85 import static org.mockito.Mockito.mock;
87 public class NetconfITTest extends AbstractConfigTest {
89 // TODO refactor, pull common code up to AbstractNetconfITTest
91 private static final Logger logger = LoggerFactory.getLogger(NetconfITTest.class);
93 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
94 private static final InetSocketAddress sshAddress = new InetSocketAddress("127.0.0.1", 10830);
95 private static final String USERNAME = "netconf";
96 private static final String PASSWORD = "netconf";
98 private NetconfMessage getConfig, getConfigCandidate, editConfig,
99 closeSession, startExi, stopExi;
100 private DefaultCommitNotificationProducer commitNot;
101 private NetconfServerDispatcher dispatch;
102 private EventLoopGroup nettyThreadgroup;
104 private NetconfClientDispatcher clientDispatcher;
107 public void setUp() throws Exception {
108 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
109 new ModuleFactory[0])));
113 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
114 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
116 nettyThreadgroup = new NioEventLoopGroup();
118 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
120 dispatch = createDispatcher(factoriesListener);
121 ChannelFuture s = dispatch.createServer(tcpAddress);
124 clientDispatcher = new NetconfClientDispatcher( nettyThreadgroup, nettyThreadgroup);
127 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
128 SessionIdProvider idProvider = new SessionIdProvider();
129 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
130 new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider);
132 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
133 factoriesListener, commitNot, idProvider, getNetconfMonitoringListenerService());
135 NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
136 serverNegotiatorFactory, listenerFactory);
137 return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
140 static NetconfMonitoringServiceImpl getNetconfMonitoringListenerService() {
141 NetconfOperationServiceFactoryListener factoriesListener = mock(NetconfOperationServiceFactoryListener.class);
142 NetconfOperationServiceSnapshot snap = mock(NetconfOperationServiceSnapshot.class);
143 doReturn(Collections.<NetconfOperationService>emptySet()).when(snap).getServices();
144 doReturn(snap).when(factoriesListener).getSnapshot(anyLong());
145 return new NetconfMonitoringServiceImpl(factoriesListener);
149 public void tearDown() throws Exception {
151 nettyThreadgroup.shutdownGracefully();
152 clientDispatcher.close();
155 private void loadMessages() throws IOException, SAXException, ParserConfigurationException {
156 this.editConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/edit_config.xml");
157 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
158 this.getConfigCandidate = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig_candidate.xml");
159 this.startExi = XmlFileLoader
160 .xmlFileToNetconfMessage("netconfMessages/startExi.xml");
161 this.stopExi = XmlFileLoader
162 .xmlFileToNetconfMessage("netconfMessages/stopExi.xml");
163 this.closeSession = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/closeSession.xml");
166 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
167 final Collection<InputStream> yangDependencies = getBasicYangs();
168 return new HardcodedYangStoreService(yangDependencies);
171 static Collection<InputStream> getBasicYangs() throws IOException {
172 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
173 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
174 "/META-INF/yang/ietf-inet-types.yang");
175 final Collection<InputStream> yangDependencies = new ArrayList<>();
176 List<String> failedToFind = new ArrayList<>();
177 for (String path : paths) {
178 InputStream resourceAsStream = NetconfITTest.class.getResourceAsStream(path);
179 if (resourceAsStream == null) {
180 failedToFind.add(path);
182 yangDependencies.add(resourceAsStream);
185 assertEquals("Some yang files were not found",emptyList(), failedToFind);
186 return yangDependencies;
189 protected List<ModuleFactory> getModuleFactories() {
190 return getModuleFactoriesS();
192 static List<ModuleFactory> getModuleFactoriesS() {
193 return Lists.newArrayList(new TestImplModuleFactory(), new DepTestImplModuleFactory(),
194 new NetconfTestImplModuleFactory());
198 public void testNetconfClientDemonstration() throws Exception {
199 try (NetconfClient netconfClient = new NetconfClient("client", tcpAddress, 4000, clientDispatcher)) {
201 Set<String> capabilitiesFromNetconfServer = netconfClient.getCapabilities();
202 long sessionId = netconfClient.getSessionId();
204 // NetconfMessage can be created :
205 // new NetconfMessage(XmlUtil.readXmlToDocument("<xml/>"));
207 NetconfMessage response = netconfClient.sendMessage(getConfig);
208 response.getDocument();
213 public void testTwoSessions() throws Exception {
214 try (NetconfClient netconfClient = new NetconfClient("1", tcpAddress, 10000, clientDispatcher)) {
215 try (NetconfClient netconfClient2 = new NetconfClient("2", tcpAddress, 10000, clientDispatcher)) {
222 public void waitingTest() throws Exception {
223 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
224 transaction.createModule(DepTestImplModuleFactory.NAME, "eb");
225 transaction.commit();
226 Thread.currentThread().suspend();
230 public void rpcReplyContainsAllAttributesTest() throws Exception {
231 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
232 final String rpc = "<rpc message-id=\"5\" a=\"a\" b=\"44\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
233 + "<get/>" + "</rpc>";
234 final Document doc = XmlUtil.readXmlToDocument(rpc);
235 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
236 assertNotNull(message);
237 final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
238 final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
240 assertSameAttributes(expectedAttributes, returnedAttributes);
244 private void assertSameAttributes(final NamedNodeMap expectedAttributes, final NamedNodeMap returnedAttributes) {
245 assertNotNull("Expecting 4 attributes", returnedAttributes);
246 assertEquals(expectedAttributes.getLength(), returnedAttributes.getLength());
248 for (int i = 0; i < expectedAttributes.getLength(); i++) {
249 final Node expAttr = expectedAttributes.item(i);
250 final Node attr = returnedAttributes.item(i);
251 assertEquals(expAttr.getNodeName(), attr.getNodeName());
252 assertEquals(expAttr.getNamespaceURI(), attr.getNamespaceURI());
253 assertEquals(expAttr.getTextContent(), attr.getTextContent());
258 public void rpcReplyErrorContainsAllAttributesTest() throws Exception {
259 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
260 final String rpc = "<rpc message-id=\"1\" a=\"adada\" b=\"4\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
261 + "<commit/>" + "</rpc>";
262 final Document doc = XmlUtil.readXmlToDocument(rpc);
263 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
264 final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
265 final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
267 assertSameAttributes(expectedAttributes, returnedAttributes);
272 public void rpcOutputContainsCorrectNamespace() throws Exception {
273 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
274 ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, "instanceD");
275 ObjectName impl = transaction.createModule(NetconfTestImplModuleFactory.NAME, "instance");
276 NetconfTestImplModuleMXBean proxy = configRegistryClient
277 .newMXBeanProxy(impl, NetconfTestImplModuleMXBean.class);
278 proxy.setTestingDep(dep);
279 proxy.setSimpleShort((short)0);
281 transaction.commit();
283 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
284 final String expectedNamespace = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
286 final String rpc = "<rpc message-id=\"5\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
290 + "<context-instance>/modules/module[type='impl-netconf'][name='instance']</context-instance>"
291 + "<arg1>argument1</arg1>" + "</no-arg>" + "</rpc>";
292 final Document doc = XmlUtil.readXmlToDocument(rpc);
293 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
295 final Element rpcReply = message.getDocument().getDocumentElement();
296 final XmlElement resultElement = XmlElement.fromDomElement(rpcReply).getOnlyChildElement();
297 assertEquals("result", resultElement.getName());
299 final String namespace = resultElement.getNamespaceAttribute();
300 assertEquals(expectedNamespace, namespace);
306 public void testStartExi() throws Exception {
307 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
310 Document rpcReply = netconfClient.sendMessage(this.startExi)
312 assertIsOK(rpcReply);
314 ExiParameters exiParams = new ExiParameters();
315 exiParams.setParametersFromXmlElement(XmlElement.fromDomDocument(this.startExi.getDocument()));
317 netconfClient.getClientSession().addExiDecoder(ExiDecoderHandler.HANDLER_NAME, new ExiDecoderHandler(exiParams));
318 netconfClient.getClientSession().addExiEncoder(ExiEncoderHandler.HANDLER_NAME, new ExiEncoderHandler(exiParams));
320 rpcReply = netconfClient.sendMessage(this.editConfig)
322 assertIsOK(rpcReply);
324 rpcReply = netconfClient.sendMessage(this.stopExi)
326 assertIsOK(rpcReply);
332 public void testCloseSession() throws Exception {
333 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
336 Document rpcReply = netconfClient.sendMessage(this.editConfig)
338 assertIsOK(rpcReply);
340 rpcReply = netconfClient.sendMessage(this.closeSession)
343 assertIsOK(rpcReply);
348 public void testEditConfig() throws Exception {
349 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
350 // send edit_config.xml
351 final Document rpcReply = netconfClient.sendMessage(this.editConfig).getDocument();
352 assertIsOK(rpcReply);
357 public void testValidate() throws Exception {
358 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
360 Document rpcReply = netconfClient.sendMessage(getConfigCandidate).getDocument();
361 assertEquals("data", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
364 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/validate.xml"))
366 assertIsOK(rpcReply);
370 private void assertIsOK(final Document rpcReply) {
371 assertEquals("rpc-reply", rpcReply.getDocumentElement().getLocalName());
372 assertEquals("ok", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
375 private Document assertGetConfigWorks(final NetconfClient netconfClient) throws InterruptedException {
376 return assertGetConfigWorks(netconfClient, this.getConfig);
379 private Document assertGetConfigWorks(final NetconfClient netconfClient, final NetconfMessage getConfigMessage)
380 throws InterruptedException {
381 final NetconfMessage rpcReply = netconfClient.sendMessage(getConfigMessage);
382 assertNotNull(rpcReply);
383 assertEquals("data", XmlElement.fromDomDocument(rpcReply.getDocument()).getOnlyChildElement().getName());
384 return rpcReply.getDocument();
388 public void testGetConfig() throws Exception {
389 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
390 assertGetConfigWorks(netconfClient);
395 public void createYangTestBasedOnYuma() throws Exception {
396 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
397 Document rpcReply = netconfClient.sendMessage(
398 XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_yang-test.xml"))
400 assertEquals("rpc-reply", rpcReply.getDocumentElement().getTagName());
401 assertIsOK(rpcReply);
402 assertGetConfigWorks(netconfClient, this.getConfigCandidate);
403 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
405 assertIsOK(rpcReply);
407 final ObjectName on = new ObjectName(
408 "org.opendaylight.controller:instanceName=impl-dep-instance,type=Module,moduleFactoryName=impl-dep");
409 Set<ObjectName> cfgBeans = configRegistryClient.lookupConfigBeans();
410 assertEquals(cfgBeans, Sets.newHashSet(on));
414 private NetconfClient createSession(final InetSocketAddress address, final String expected) throws Exception {
415 final NetconfClient netconfClient = new NetconfClient("test " + address.toString(), address, 5000, clientDispatcher);
416 assertEquals(expected, Long.toString(netconfClient.getSessionId()));
417 return netconfClient;
420 private void startSSHServer() throws Exception{
421 logger.info("Creating SSH server");
422 StubUserManager um = new StubUserManager(USERNAME,PASSWORD);
423 InputStream is = getClass().getResourceAsStream("/RSA.pk");
424 AuthProvider ap = new AuthProvider(um, is);
425 Thread sshServerThread = new Thread(NetconfSSHServer.start(10830,tcpAddress,ap));
426 sshServerThread.setDaemon(true);
427 sshServerThread.start();
428 logger.info("SSH server on");
432 public void sshTest() throws Exception {
434 logger.info("creating connection");
435 Connection conn = new Connection(sshAddress.getHostName(),sshAddress.getPort());
436 Assert.assertNotNull(conn);
437 logger.info("connection created");
439 boolean isAuthenticated = conn.authenticateWithPassword(USERNAME,PASSWORD);
440 assertTrue(isAuthenticated);
441 logger.info("user authenticated");
442 final Session sess = conn.openSession();
443 sess.startSubSystem("netconf");
444 logger.info("user authenticated");
445 sess.getStdin().write(XmlUtil.toString(this.getConfig.getDocument()).getBytes());
450 byte[] bytes = new byte[1024];
453 c = sess.getStdout().read(bytes);
454 } catch (IOException e) {
455 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
457 logger.info("got data:"+bytes);