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.pax;
10 import static org.junit.Assert.fail;
11 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.baseModelBundles;
12 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.bindingAwareSalBundles;
13 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.configMinumumBundles;
14 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.flowCapableModelBundles;
15 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.junitAndMockitoBundles;
16 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.mdSalCoreBundles;
17 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
18 import static org.ops4j.pax.exam.CoreOptions.options;
19 import static org.ops4j.pax.exam.CoreOptions.streamBundle;
20 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
21 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
22 import io.netty.channel.nio.NioEventLoopGroup;
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.TimeoutException;
29 import javax.xml.parsers.ParserConfigurationException;
31 import org.junit.Assert;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.junit.matchers.JUnitMatchers;
35 import org.junit.runner.RunWith;
36 import org.opendaylight.controller.netconf.api.NetconfMessage;
37 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
38 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
39 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
40 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
41 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
42 import org.ops4j.pax.exam.Configuration;
43 import org.ops4j.pax.exam.Option;
44 import org.ops4j.pax.exam.junit.PaxExam;
45 import org.ops4j.pax.exam.options.DefaultCompositeOption;
46 import org.ops4j.pax.exam.util.Filter;
47 import org.ops4j.pax.tinybundles.core.TinyBundles;
48 import org.osgi.framework.Constants;
49 import org.xml.sax.SAXException;
51 import com.google.common.base.Preconditions;
52 import com.google.common.base.Throwables;
55 @RunWith(PaxExam.class)
56 public class IdentityRefNetconfTest {
58 public static final int CLIENT_CONNECTION_TIMEOUT_MILLIS = 15000;
60 // Wait for controller to start
62 // FIXME move this (pax) test to different module
63 // pax jars contain guava classes that clash with real guava dependencies in non-pax tests
66 @Filter(timeout = 60 * 1000)
67 BindingAwareBroker broker;
70 public Option[] config() {
72 systemProperty("osgi.console").value("2401"),
73 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
74 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
75 systemPackages("sun.nio.ch"),
80 bindingAwareSalBundles(), configMinumumBundles(), baseModelBundles(), flowCapableModelBundles(),
81 junitAndMockitoBundles(),
83 // Classes from test-jars bundled for pax-exam test
84 streamBundle(TinyBundles.bundle()
85 .add(TestingNetconfClient.class)
86 .add(XmlFileLoader.class)
88 .add("/netconfMessages/editConfig_identities.xml",
89 XmlFileLoader.class.getResource("/netconfMessages/editConfig_identities.xml"))
90 .add("/netconfMessages/commit.xml",
91 XmlFileLoader.class.getResource("/netconfMessages/commit.xml"))
92 .add("/netconfMessages/getConfig.xml",
93 XmlFileLoader.class.getResource("/netconfMessages/getConfig.xml"))
95 .set(Constants.BUNDLE_SYMBOLICNAME, "TestingClient_bundle")
96 .set(Constants.EXPORT_PACKAGE, "org.opendaylight.controller.netconf.client.test, " +
97 "org.opendaylight.controller.netconf.util.test")
98 .build(TinyBundles.withBnd())));
101 private Option loggingModules() {
102 return new DefaultCompositeOption(
103 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
104 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
105 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
106 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(),
107 mavenBundle("org.opendaylight.controller.thirdparty", "nagasena").versionAsInProject(),
108 mavenBundle("org.opendaylight.controller.thirdparty", "nagasena-rta").versionAsInProject());
113 private Option testingModules() {
114 return new DefaultCompositeOption(
115 mavenBundle("org.opendaylight.controller", "yang-test").versionAsInProject());
118 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 18383);
121 public void testIdRef() throws Exception {
122 Preconditions.checkNotNull(broker, "Controller not initialized");
124 NioEventLoopGroup nettyThreadgroup = new NioEventLoopGroup();
125 NetconfClientDispatcher clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup,
126 CLIENT_CONNECTION_TIMEOUT_MILLIS);
128 NetconfMessage edit = xmlFileToNetconfMessage("netconfMessages/editConfig_identities.xml");
129 NetconfMessage commit = xmlFileToNetconfMessage("netconfMessages/commit.xml");
130 NetconfMessage getConfig = xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
132 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", tcpAddress, CLIENT_CONNECTION_TIMEOUT_MILLIS, clientDispatcher)) {
133 sendMessage(edit, netconfClient);
134 sendMessage(commit, netconfClient);
135 sendMessage(getConfig, netconfClient, "id-test",
136 "<prefix:afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</prefix:afi>",
137 "<prefix:afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</prefix:afi>",
138 "<prefix:safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</prefix:safi>",
139 "<prefix:safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</prefix:safi>");
141 clientDispatcher.close();
142 } catch (Exception e) {
143 fail(Throwables.getStackTraceAsString(e));
147 private void sendMessage(NetconfMessage edit, TestingNetconfClient netconfClient, String... containingResponse)
148 throws ExecutionException, InterruptedException, TimeoutException {
149 NetconfMessage response = netconfClient.sendRequest(edit).get();
150 if (containingResponse == null) {
151 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString("<ok/>"));
153 for (String resp : containingResponse) {
154 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString(resp));
159 public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
160 ParserConfigurationException {
161 return XmlFileLoader.xmlFileToNetconfMessage(fileName);