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.inject.Inject;
30 import javax.xml.parsers.ParserConfigurationException;
32 import org.junit.Assert;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.junit.matchers.JUnitMatchers;
36 import org.junit.runner.RunWith;
37 import org.opendaylight.controller.netconf.api.NetconfMessage;
38 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
39 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
40 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
41 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
42 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
43 import org.ops4j.pax.exam.Configuration;
44 import org.ops4j.pax.exam.Option;
45 import org.ops4j.pax.exam.junit.PaxExam;
46 import org.ops4j.pax.exam.options.DefaultCompositeOption;
47 import org.ops4j.pax.exam.util.Filter;
48 import org.ops4j.pax.tinybundles.core.TinyBundles;
49 import org.osgi.framework.Constants;
50 import org.xml.sax.SAXException;
52 import com.google.common.base.Preconditions;
53 import com.google.common.base.Throwables;
56 @RunWith(PaxExam.class)
57 public class IdentityRefNetconfTest {
59 public static final int CLIENT_CONNECTION_TIMEOUT_MILLIS = 15000;
61 // Wait for controller to start
63 @Filter(timeout = 60 * 1000)
64 BindingAwareBroker broker;
67 public Option[] config() {
69 systemProperty("osgi.console").value("2401"),
70 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
71 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
72 systemPackages("sun.nio.ch"),
77 bindingAwareSalBundles(), configMinumumBundles(), baseModelBundles(), flowCapableModelBundles(),
78 junitAndMockitoBundles(),
80 // Classes from test-jars bundled for pax-exam test
81 streamBundle(TinyBundles.bundle()
82 .add(TestingNetconfClient.class)
83 .add(XmlFileLoader.class)
85 .add("/netconfMessages/editConfig_identities.xml",
86 XmlFileLoader.class.getResource("/netconfMessages/editConfig_identities.xml"))
87 .add("/netconfMessages/commit.xml",
88 XmlFileLoader.class.getResource("/netconfMessages/commit.xml"))
89 .add("/netconfMessages/getConfig.xml",
90 XmlFileLoader.class.getResource("/netconfMessages/getConfig.xml"))
92 .set(Constants.BUNDLE_SYMBOLICNAME, "TestingClient_bundle")
93 .set(Constants.EXPORT_PACKAGE, "org.opendaylight.controller.netconf.client.test, " +
94 "org.opendaylight.controller.netconf.util.test")
95 .build(TinyBundles.withBnd())));
98 private Option loggingModules() {
99 return new DefaultCompositeOption(
100 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
101 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
102 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
103 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject());
106 private Option testingModules() {
107 return new DefaultCompositeOption(
108 mavenBundle("org.opendaylight.controller", "yang-test").versionAsInProject());
111 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 18383);
114 public void testIdRef() throws Exception {
115 Preconditions.checkNotNull(broker, "Controller not initialized");
117 NioEventLoopGroup nettyThreadgroup = new NioEventLoopGroup();
118 NetconfClientDispatcher clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup,
119 CLIENT_CONNECTION_TIMEOUT_MILLIS);
121 NetconfMessage edit = xmlFileToNetconfMessage("netconfMessages/editConfig_identities.xml");
122 NetconfMessage commit = xmlFileToNetconfMessage("netconfMessages/commit.xml");
123 NetconfMessage getConfig = xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
125 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", tcpAddress, CLIENT_CONNECTION_TIMEOUT_MILLIS, clientDispatcher)) {
126 sendMessage(edit, netconfClient);
127 sendMessage(commit, netconfClient);
128 sendMessage(getConfig, netconfClient, "id-test",
129 "<prefix:afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</prefix:afi>",
130 "<prefix:afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</prefix:afi>",
131 "<prefix:safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</prefix:safi>",
132 "<prefix:safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</prefix:safi>");
134 clientDispatcher.close();
135 } catch (Exception e) {
136 fail(Throwables.getStackTraceAsString(e));
140 private void sendMessage(NetconfMessage edit, TestingNetconfClient netconfClient, String... containingResponse)
141 throws ExecutionException, InterruptedException, TimeoutException {
142 NetconfMessage response = netconfClient.sendRequest(edit).get();
143 if (containingResponse == null) {
144 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString("<ok/>"));
146 for (String resp : containingResponse) {
147 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString(resp));
152 public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
153 ParserConfigurationException {
154 return XmlFileLoader.xmlFileToNetconfMessage(fileName);