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 com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import io.netty.channel.nio.NioEventLoopGroup;
13 import org.junit.Assert;
14 import org.junit.Ignore;
15 import org.junit.Test;
16 import org.junit.matchers.JUnitMatchers;
17 import org.junit.runner.RunWith;
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.netconf.client.NetconfClient;
20 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
23 import org.ops4j.pax.exam.Configuration;
24 import org.ops4j.pax.exam.Option;
25 import org.ops4j.pax.exam.junit.PaxExam;
26 import org.ops4j.pax.exam.options.DefaultCompositeOption;
27 import org.ops4j.pax.exam.util.Filter;
28 import org.w3c.dom.Document;
29 import org.xml.sax.SAXException;
31 import javax.inject.Inject;
32 import javax.xml.parsers.ParserConfigurationException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.net.InetSocketAddress;
36 import java.util.concurrent.ExecutionException;
37 import java.util.concurrent.TimeoutException;
39 import static org.junit.Assert.fail;
40 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.baseModelBundles;
41 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.bindingAwareSalBundles;
42 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.configMinumumBundles;
43 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.flowCapableModelBundles;
44 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.junitAndMockitoBundles;
45 import static org.opendaylight.controller.test.sal.binding.it.TestHelper.mdSalCoreBundles;
46 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
47 import static org.ops4j.pax.exam.CoreOptions.options;
48 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
49 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
52 @RunWith(PaxExam.class)
53 public class IdentityRefNetconfTest {
55 public static final int CLIENT_CONNECTION_TIMEOUT_MILLIS = 15000;
57 // Wait for controller to start
59 @Filter(timeout = 60 * 1000)
60 BindingAwareBroker broker;
63 public Option[] config() {
65 systemProperty("osgi.console").value("2401"),
66 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
67 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
68 systemPackages("sun.nio.ch"),
73 bindingAwareSalBundles(), configMinumumBundles(), baseModelBundles(), flowCapableModelBundles(),
74 junitAndMockitoBundles());
77 private Option loggingModules() {
78 return new DefaultCompositeOption(
79 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
80 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
81 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
82 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject());
85 private Option testingModules() {
86 return new DefaultCompositeOption(
87 mavenBundle("org.opendaylight.controller", "yang-test").versionAsInProject());
90 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 18383);
94 public void testIdRef() throws Exception {
96 Preconditions.checkNotNull(broker, "Controller not initialized");
98 NioEventLoopGroup nettyThreadgroup = new NioEventLoopGroup();
99 NetconfClientDispatcher clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup,
100 CLIENT_CONNECTION_TIMEOUT_MILLIS);
102 NetconfMessage edit = xmlFileToNetconfMessage("netconfMessages/editConfig_identities.xml");
103 NetconfMessage commit = xmlFileToNetconfMessage("netconfMessages/commit.xml");
104 NetconfMessage getConfig = xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
106 try (NetconfClient netconfClient = new NetconfClient("client", tcpAddress, CLIENT_CONNECTION_TIMEOUT_MILLIS, clientDispatcher)) {
107 sendMessage(edit, netconfClient);
108 sendMessage(commit, netconfClient);
109 sendMessage(getConfig, netconfClient, "id-test",
110 "<afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</afi>",
111 "<afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</afi>",
112 "<safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</safi>",
113 "<safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</safi>");
116 clientDispatcher.close();
117 } catch (Exception e) {
118 fail(Throwables.getStackTraceAsString(e));
123 private void sendMessage(NetconfMessage edit, NetconfClient netconfClient, String... containingResponse)
124 throws ExecutionException, InterruptedException, TimeoutException {
125 NetconfMessage response = netconfClient.sendRequest(edit).get();
126 if (containingResponse == null) {
127 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString("<ok/>"));
129 for (String resp : containingResponse) {
130 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString(resp));
135 public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
136 ParserConfigurationException {
137 return new NetconfMessage(xmlFileToDocument(fileName));
140 public static Document xmlFileToDocument(final String fileName) throws IOException, SAXException,
141 ParserConfigurationException {
142 // TODO xml messages from netconf-util test-jar cannot be loaded here(in OSGi), since test jar is not a bundle
143 try (InputStream resourceAsStream = IdentityRefNetconfTest.class.getClassLoader().getResourceAsStream(fileName)) {
144 Preconditions.checkNotNull(resourceAsStream);
145 final Document doc = XmlUtil.readXmlToDocument(resourceAsStream);