Merge "Cleanup utility classes"
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / pax / IdentityRefNetconfTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.it.pax;
9
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
23 import javax.inject.Inject;
24 import javax.xml.parsers.ParserConfigurationException;
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.base.Throwables;
28 import io.netty.channel.nio.NioEventLoopGroup;
29 import org.junit.Assert;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.junit.matchers.JUnitMatchers;
33 import org.junit.runner.RunWith;
34 import org.opendaylight.controller.netconf.api.NetconfMessage;
35 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
36 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
37 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
38 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
39 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
40 import org.ops4j.pax.exam.Configuration;
41 import org.ops4j.pax.exam.Option;
42 import org.ops4j.pax.exam.junit.PaxExam;
43 import org.ops4j.pax.exam.options.DefaultCompositeOption;
44 import org.ops4j.pax.exam.util.Filter;
45 import org.ops4j.pax.tinybundles.core.TinyBundles;
46 import org.osgi.framework.Constants;
47 import org.xml.sax.SAXException;
48
49 import java.io.IOException;
50 import java.net.InetSocketAddress;
51 import java.util.concurrent.ExecutionException;
52 import java.util.concurrent.TimeoutException;
53
54 @Ignore
55 @RunWith(PaxExam.class)
56 public class IdentityRefNetconfTest {
57
58     public static final int CLIENT_CONNECTION_TIMEOUT_MILLIS = 15000;
59
60     // Wait for controller to start
61     @Inject
62     @Filter(timeout = 60 * 1000)
63     BindingAwareBroker broker;
64
65     @Configuration
66     public Option[] config() {
67         return options(
68                 systemProperty("osgi.console").value("2401"),
69                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
70                 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
71                 systemPackages("sun.nio.ch"),
72
73                 testingModules(),
74                 loggingModules(),
75                 mdSalCoreBundles(),
76                 bindingAwareSalBundles(), configMinumumBundles(), baseModelBundles(), flowCapableModelBundles(),
77                 junitAndMockitoBundles(),
78
79                 // Classes from test-jars bundled for pax-exam test
80                 streamBundle(TinyBundles.bundle()
81                         .add(TestingNetconfClient.class)
82                         .add(XmlFileLoader.class)
83
84                         .add("/netconfMessages/editConfig_identities.xml",
85                                 XmlFileLoader.class.getResource("/netconfMessages/editConfig_identities.xml"))
86                         .add("/netconfMessages/commit.xml",
87                                 XmlFileLoader.class.getResource("/netconfMessages/commit.xml"))
88                         .add("/netconfMessages/getConfig.xml",
89                                 XmlFileLoader.class.getResource("/netconfMessages/getConfig.xml"))
90
91                         .set(Constants.BUNDLE_SYMBOLICNAME, "TestingClient_bundle")
92                         .set(Constants.EXPORT_PACKAGE, "org.opendaylight.controller.netconf.client.test, " +
93                                 "org.opendaylight.controller.netconf.util.test")
94                         .build(TinyBundles.withBnd())));
95     }
96
97     private Option loggingModules() {
98         return new DefaultCompositeOption(
99                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
100                 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
101                 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
102                 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject());
103     }
104
105     private Option testingModules() {
106         return new DefaultCompositeOption(
107                 mavenBundle("org.opendaylight.controller", "yang-test").versionAsInProject());
108     }
109
110     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 18383);
111
112     @Test
113     public void testIdRef() throws Exception {
114         Preconditions.checkNotNull(broker, "Controller not initialized");
115
116         NioEventLoopGroup nettyThreadgroup = new NioEventLoopGroup();
117         NetconfClientDispatcher clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup,
118                 CLIENT_CONNECTION_TIMEOUT_MILLIS);
119
120         NetconfMessage edit = xmlFileToNetconfMessage("netconfMessages/editConfig_identities.xml");
121         NetconfMessage commit = xmlFileToNetconfMessage("netconfMessages/commit.xml");
122         NetconfMessage getConfig = xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
123
124         try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", tcpAddress, CLIENT_CONNECTION_TIMEOUT_MILLIS, clientDispatcher)) {
125             sendMessage(edit, netconfClient);
126             sendMessage(commit, netconfClient);
127             sendMessage(getConfig, netconfClient, "id-test",
128                     "<afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</afi>",
129                     "<afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</afi>",
130                     "<safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</safi>",
131                     "<safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</safi>");
132
133             clientDispatcher.close();
134         } catch (Exception e) {
135             fail(Throwables.getStackTraceAsString(e));
136         }
137     }
138
139     private void sendMessage(NetconfMessage edit, TestingNetconfClient netconfClient, String... containingResponse)
140             throws ExecutionException, InterruptedException, TimeoutException {
141         NetconfMessage response = netconfClient.sendRequest(edit).get();
142         if (containingResponse == null) {
143             Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString("<ok/>"));
144         } else {
145             for (String resp : containingResponse) {
146                 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString(resp));
147             }
148         }
149     }
150
151     public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
152             ParserConfigurationException {
153         return XmlFileLoader.xmlFileToNetconfMessage(fileName);
154     }
155 }