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 com.google.common.base.Optional;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Sets;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.nio.NioEventLoopGroup;
17 import io.netty.util.HashedWheelTimer;
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.api.ModuleIdentifier;
23 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
24 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
25 import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
26 import org.opendaylight.controller.config.manager.impl.jmx.RootRuntimeBeanRegistratorImpl;
27 import org.opendaylight.controller.config.persist.api.Persister;
28 import org.opendaylight.controller.config.spi.ModuleFactory;
29 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
30 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
31 import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
32 import org.opendaylight.controller.config.yang.test.impl.Asdf;
33 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
34 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
35 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
36 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplRuntimeMXBean;
37 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplRuntimeRegistrator;
38 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
39 import org.opendaylight.controller.netconf.api.NetconfMessage;
40 import org.opendaylight.controller.netconf.client.NetconfClient;
41 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
42 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
43 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
44 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
45 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory;
46 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
47 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
48 import org.opendaylight.controller.netconf.impl.mapping.ExiDecoderHandler;
49 import org.opendaylight.controller.netconf.impl.mapping.ExiEncoderHandler;
50 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
51 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
52 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
53 import org.opendaylight.controller.netconf.util.xml.ExiParameters;
54 import org.opendaylight.controller.netconf.util.xml.XmlElement;
55 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
56 import org.w3c.dom.Document;
57 import org.w3c.dom.Element;
58 import org.w3c.dom.NamedNodeMap;
59 import org.w3c.dom.Node;
60 import org.xml.sax.SAXException;
62 import javax.management.ObjectName;
63 import javax.net.ssl.SSLContext;
64 import javax.xml.parsers.ParserConfigurationException;
65 import java.io.IOException;
66 import java.io.InputStream;
67 import java.lang.management.ManagementFactory;
68 import java.net.InetSocketAddress;
69 import java.util.ArrayList;
70 import java.util.Arrays;
71 import java.util.Collection;
72 import java.util.List;
74 import java.util.concurrent.TimeUnit;
76 import static junit.framework.Assert.assertEquals;
77 import static junit.framework.Assert.assertNotNull;
78 import static org.mockito.Mockito.doReturn;
79 import static org.mockito.Mockito.mock;
80 import static org.mockito.internal.util.Checks.checkNotNull;
82 public class NetconfITTest extends AbstractConfigTest {
84 // private static final Logger logger =
85 // LoggerFactory.getLogger(NetconfITTest.class);
88 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
90 private NetconfMessage getConfig, getConfigCandidate, editConfig,
91 closeSession, startExi, stopExi;
92 private DefaultCommitNotificationProducer commitNot;
93 private NetconfServerDispatcher dispatch;
94 private EventLoopGroup nettyThreadgroup;
96 private NetconfClientDispatcher clientDispatcher;
99 public void setUp() throws Exception {
100 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
101 new ModuleFactory[0])));
105 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
106 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
108 nettyThreadgroup = new NioEventLoopGroup();
110 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
112 dispatch = createDispatcher(Optional.<SSLContext> absent(), factoriesListener);
113 ChannelFuture s = dispatch.createServer(tcpAddress);
116 clientDispatcher = new NetconfClientDispatcher(Optional.<SSLContext>absent(), nettyThreadgroup, nettyThreadgroup);
119 private NetconfServerDispatcher createDispatcher(Optional<SSLContext> sslC,
120 NetconfOperationServiceFactoryListenerImpl factoriesListener) {
121 SessionIdProvider idProvider = new SessionIdProvider();
122 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
123 new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider);
125 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
126 factoriesListener, commitNot, idProvider);
128 NetconfServerDispatcher.ServerSslChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerSslChannelInitializer(
129 sslC, serverNegotiatorFactory, listenerFactory);
130 return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
134 public void tearDown() throws Exception {
136 nettyThreadgroup.shutdownGracefully();
137 clientDispatcher.close();
140 private void loadMessages() throws IOException, SAXException, ParserConfigurationException {
141 this.editConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/edit_config.xml");
142 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
143 this.getConfigCandidate = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig_candidate.xml");
144 this.startExi = XmlFileLoader
145 .xmlFileToNetconfMessage("netconfMessages/startExi.xml");
146 this.stopExi = XmlFileLoader
147 .xmlFileToNetconfMessage("netconfMessages/stopExi.xml");
148 this.closeSession = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/closeSession.xml");
151 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
152 final Collection<InputStream> yangDependencies = getBasicYangs();
153 return new HardcodedYangStoreService(yangDependencies);
156 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",
159 "/META-INF/yang/ietf-inet-types.yang");
160 final Collection<InputStream> yangDependencies = new ArrayList<>();
161 for (String path : paths) {
162 final InputStream is = checkNotNull(NetconfITTest.class.getResourceAsStream(path), path + " not found");
163 yangDependencies.add(is);
165 return yangDependencies;
168 protected List<ModuleFactory> getModuleFactories() {
169 return getModuleFactoriesS();
171 static List<ModuleFactory> getModuleFactoriesS() {
172 return Lists.newArrayList(new TestImplModuleFactory(), new DepTestImplModuleFactory(),
173 new NetconfTestImplModuleFactory());
177 public void testNetconfClientDemonstration() throws Exception {
178 try (NetconfClient netconfClient = new NetconfClient("client", tcpAddress, 4000, clientDispatcher)) {
180 Set<String> capabilitiesFromNetconfServer = netconfClient.getCapabilities();
181 long sessionId = netconfClient.getSessionId();
183 // NetconfMessage can be created :
184 // new NetconfMessage(XmlUtil.readXmlToDocument("<xml/>"));
186 NetconfMessage response = netconfClient.sendMessage(getConfig);
187 response.getDocument();
192 public void testTwoSessions() throws Exception {
193 try (NetconfClient netconfClient = new NetconfClient("1", tcpAddress, 4000, clientDispatcher)) {
194 try (NetconfClient netconfClient2 = new NetconfClient("2", tcpAddress, 4000, clientDispatcher)) {
199 @Test(timeout = 10000)
200 public void testPersister() throws Exception {
201 Persister persister = mock(Persister.class);
202 doReturn("mockPersister").when(persister).toString();
203 doReturn(Optional.absent()).when(persister).loadLastConfig();
204 ConfigPersisterNotificationHandler h = new ConfigPersisterNotificationHandler(persister, tcpAddress, ManagementFactory.getPlatformMBeanServer());
210 public void waitingTest() throws Exception {
211 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
212 transaction.createModule(DepTestImplModuleFactory.NAME, "eb");
213 transaction.commit();
214 Thread.currentThread().suspend();
218 public void rpcReplyContainsAllAttributesTest() throws Exception {
219 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
220 final String rpc = "<rpc message-id=\"5\" a=\"a\" b=\"44\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
221 + "<get/>" + "</rpc>";
222 final Document doc = XmlUtil.readXmlToDocument(rpc);
223 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
224 assertNotNull(message);
225 final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
226 final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
228 assertSameAttributes(expectedAttributes, returnedAttributes);
232 private void assertSameAttributes(final NamedNodeMap expectedAttributes, final NamedNodeMap returnedAttributes) {
233 assertNotNull("Expecting 4 attributes", returnedAttributes);
234 assertEquals(expectedAttributes.getLength(), returnedAttributes.getLength());
236 for (int i = 0; i < expectedAttributes.getLength(); i++) {
237 final Node expAttr = expectedAttributes.item(i);
238 final Node attr = returnedAttributes.item(i);
239 assertEquals(expAttr.getNodeName(), attr.getNodeName());
240 assertEquals(expAttr.getNamespaceURI(), attr.getNamespaceURI());
241 assertEquals(expAttr.getTextContent(), attr.getTextContent());
246 public void rpcReplyErrorContainsAllAttributesTest() throws Exception {
247 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
248 final String rpc = "<rpc message-id=\"1\" a=\"adada\" b=\"4\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
249 + "<commit/>" + "</rpc>";
250 final Document doc = XmlUtil.readXmlToDocument(rpc);
251 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
252 final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
253 final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
255 assertSameAttributes(expectedAttributes, returnedAttributes);
260 public void rpcOutputContainsCorrectNamespace() throws Exception {
261 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
262 ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, "instanceD");
263 ObjectName impl = transaction.createModule(NetconfTestImplModuleFactory.NAME, "instance");
264 NetconfTestImplModuleMXBean proxy = configRegistryClient
265 .newMXBeanProxy(impl, NetconfTestImplModuleMXBean.class);
266 proxy.setTestingDep(dep);
267 registerRuntimeBean();
269 transaction.commit();
271 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
272 final String expectedNamespace = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
274 final String rpc = "<rpc message-id=\"5\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
278 + "<context-instance>/data/modules/module[name='impl-netconf']/instance[name='instance']</context-instance>"
279 + "<arg1>argument1</arg1>" + "</no-arg>" + "</rpc>";
280 final Document doc = XmlUtil.readXmlToDocument(rpc);
281 final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
283 final Element rpcReply = message.getDocument().getDocumentElement();
284 final XmlElement resultElement = XmlElement.fromDomElement(rpcReply).getOnlyChildElement();
285 assertEquals("result", resultElement.getName());
287 final String namespace = resultElement.getNamespaceAttribute();
288 assertEquals(expectedNamespace, namespace);
292 private void registerRuntimeBean() {
293 BaseJMXRegistrator baseJMXRegistrator = new BaseJMXRegistrator(ManagementFactory.getPlatformMBeanServer());
294 RootRuntimeBeanRegistratorImpl runtimeBeanRegistrator = baseJMXRegistrator
295 .createRuntimeBeanRegistrator(new ModuleIdentifier(NetconfTestImplModuleFactory.NAME, "instance"));
296 NetconfTestImplRuntimeRegistrator reg = new NetconfTestImplRuntimeRegistrator(runtimeBeanRegistrator);
297 reg.register(new NetconfTestImplRuntimeMXBean() {
299 public Asdf getAsdf() {
304 public Long getCreatedSessions() {
309 public String noArg(String arg1) {
310 return "from no arg";
317 public void testStartExi() throws Exception {
318 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
321 Document rpcReply = netconfClient.sendMessage(this.startExi)
323 assertIsOK(rpcReply);
325 ExiParameters exiParams = new ExiParameters();
326 exiParams.setParametersFromXmlElement(XmlElement.fromDomDocument(this.startExi.getDocument()));
328 netconfClient.getClientSession().addExiDecoder(ExiDecoderHandler.HANDLER_NAME, new ExiDecoderHandler(exiParams));
329 netconfClient.getClientSession().addExiEncoder(ExiEncoderHandler.HANDLER_NAME, new ExiEncoderHandler(exiParams));
331 rpcReply = netconfClient.sendMessage(this.editConfig)
333 assertIsOK(rpcReply);
335 rpcReply = netconfClient.sendMessage(this.stopExi)
337 assertIsOK(rpcReply);
343 public void testCloseSession() throws Exception {
344 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
347 Document rpcReply = netconfClient.sendMessage(this.editConfig)
349 assertIsOK(rpcReply);
351 rpcReply = netconfClient.sendMessage(this.closeSession)
354 assertIsOK(rpcReply);
359 public void testEditConfig() throws Exception {
360 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
361 // send edit_config.xml
362 final Document rpcReply = netconfClient.sendMessage(this.editConfig).getDocument();
363 assertIsOK(rpcReply);
368 public void testValidate() throws Exception {
369 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
371 Document rpcReply = netconfClient.sendMessage(getConfigCandidate).getDocument();
372 assertEquals("data", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
375 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/validate.xml"))
377 assertIsOK(rpcReply);
381 private void assertIsOK(final Document rpcReply) {
382 assertEquals("rpc-reply", rpcReply.getDocumentElement().getLocalName());
383 assertEquals("ok", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
388 // TODO can only send NetconfMessage - it must be valid xml
389 public void testClientHelloWithAuth() throws Exception {
390 final String fileName = "netconfMessages/client_hello_with_auth.xml";
391 // final InputStream resourceAsStream =
392 // AbstractListenerTest.class.getResourceAsStream(fileName);
393 // assertNotNull(resourceAsStream);
394 try (NetconfClient netconfClient = new NetconfClient("test", tcpAddress, 5000, clientDispatcher)) {
395 // IOUtils.copy(resourceAsStream, netconfClient.getStream());
396 // netconfClient.getOutputStream().write(NetconfMessageFactory.endOfMessage);
397 // server should not write anything back
398 // assertEquals(null, netconfClient.readMessage());
399 assertGetConfigWorks(netconfClient);
403 private Document assertGetConfigWorks(final NetconfClient netconfClient) throws InterruptedException {
404 return assertGetConfigWorks(netconfClient, this.getConfig);
407 private Document assertGetConfigWorks(final NetconfClient netconfClient, final NetconfMessage getConfigMessage)
408 throws InterruptedException {
409 final NetconfMessage rpcReply = netconfClient.sendMessage(getConfigMessage);
410 assertNotNull(rpcReply);
411 assertEquals("data", XmlElement.fromDomDocument(rpcReply.getDocument()).getOnlyChildElement().getName());
412 return rpcReply.getDocument();
416 public void testGetConfig() throws Exception {
417 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
418 assertGetConfigWorks(netconfClient);
423 public void createYangTestBasedOnYuma() throws Exception {
424 try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
425 Document rpcReply = netconfClient.sendMessage(
426 XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_yang-test.xml"))
428 assertEquals("rpc-reply", rpcReply.getDocumentElement().getTagName());
429 assertIsOK(rpcReply);
430 assertGetConfigWorks(netconfClient, this.getConfigCandidate);
431 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
433 assertIsOK(rpcReply);
435 final ObjectName on = new ObjectName(
436 "org.opendaylight.controller:instanceName=impl-dep-instance,type=Module,moduleFactoryName=impl-dep");
437 Set<ObjectName> cfgBeans = configRegistryClient.lookupConfigBeans();
438 assertEquals(cfgBeans, Sets.newHashSet(on));
442 private NetconfClient createSession(final InetSocketAddress address, final String expected) throws Exception {
443 final NetconfClient netconfClient = new NetconfClient("test " + address.toString(), address, 5000, clientDispatcher);
444 assertEquals(expected, Long.toString(netconfClient.getSessionId()));
445 return netconfClient;