Merge "Bug 849: Fixed NPE in Translated Data Change Events."
[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 io.netty.util.HashedWheelTimer;
24 import io.netty.util.Timer;
25 import java.io.IOException;
26 import java.net.InetSocketAddress;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.TimeoutException;
29
30 import javax.xml.parsers.ParserConfigurationException;
31
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.NetconfClientDispatcherImpl;
39 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
40 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
41 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
42 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
43 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
44 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
45 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
46 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
47 import org.ops4j.pax.exam.Configuration;
48 import org.ops4j.pax.exam.Option;
49 import org.ops4j.pax.exam.junit.PaxExam;
50 import org.ops4j.pax.exam.options.DefaultCompositeOption;
51 import org.ops4j.pax.exam.util.Filter;
52 import org.ops4j.pax.tinybundles.core.TinyBundles;
53 import org.osgi.framework.Constants;
54 import org.xml.sax.SAXException;
55
56 import com.google.common.base.Preconditions;
57 import com.google.common.base.Throwables;
58
59 import io.netty.channel.nio.NioEventLoopGroup;
60 import io.netty.util.concurrent.GlobalEventExecutor;
61
62 @Ignore
63 @RunWith(PaxExam.class)
64 public class IdentityRefNetconfTest {
65
66     public static final int CLIENT_CONNECTION_TIMEOUT_MILLIS = 15000;
67
68     // Wait for controller to start
69
70     // FIXME move this (pax) test to different module
71     // pax jars contain guava classes that clash with real guava dependencies in non-pax tests
72     //
73     //@Inject
74     @Filter(timeout = 60 * 1000)
75     BindingAwareBroker broker;
76
77     @Configuration
78     public Option[] config() {
79         return options(
80                 systemProperty("osgi.console").value("2401"),
81                 systemProperty("osgi.bundles.defaultStartLevel").value("4"),
82                 systemProperty("pax.exam.osgi.unresolved.fail").value("true"),
83                 systemPackages("sun.nio.ch"),
84
85                 testingModules(),
86                 loggingModules(),
87                 mdSalCoreBundles(),
88                 bindingAwareSalBundles(), configMinumumBundles(), baseModelBundles(), flowCapableModelBundles(),
89                 junitAndMockitoBundles(),
90
91                 // Classes from test-jars bundled for pax-exam test
92                 streamBundle(TinyBundles.bundle()
93                         .add(TestingNetconfClient.class)
94                         .add(XmlFileLoader.class)
95
96                         .add("/netconfMessages/editConfig_identities.xml",
97                                 XmlFileLoader.class.getResource("/netconfMessages/editConfig_identities.xml"))
98                         .add("/netconfMessages/commit.xml",
99                                 XmlFileLoader.class.getResource("/netconfMessages/commit.xml"))
100                         .add("/netconfMessages/getConfig.xml",
101                                 XmlFileLoader.class.getResource("/netconfMessages/getConfig.xml"))
102
103                         .set(Constants.BUNDLE_SYMBOLICNAME, "TestingClient_bundle")
104                         .set(Constants.EXPORT_PACKAGE, "org.opendaylight.controller.netconf.client.test, " +
105                                 "org.opendaylight.controller.netconf.util.test")
106                         .build(TinyBundles.withBnd())));
107     }
108
109     private Option loggingModules() {
110         return new DefaultCompositeOption(
111                 mavenBundle("org.slf4j", "slf4j-api").versionAsInProject(),
112                 mavenBundle("org.slf4j", "log4j-over-slf4j").versionAsInProject(),
113                 mavenBundle("ch.qos.logback", "logback-core").versionAsInProject(),
114                 mavenBundle("ch.qos.logback", "logback-classic").versionAsInProject(),
115                 mavenBundle("org.openexi", "nagasena").versionAsInProject(),
116                 mavenBundle("org.openexi", "nagasena-rta").versionAsInProject());
117
118
119     }
120
121     private Option testingModules() {
122         return new DefaultCompositeOption(
123                 mavenBundle("org.opendaylight.controller", "yang-test").versionAsInProject());
124     }
125
126     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 18383);
127
128     @Test
129     public void testIdRef() throws Exception {
130         Preconditions.checkNotNull(broker, "Controller not initialized");
131
132
133         NetconfMessage edit = xmlFileToNetconfMessage("netconfMessages/editConfig_identities.xml");
134         NetconfMessage commit = xmlFileToNetconfMessage("netconfMessages/commit.xml");
135         NetconfMessage getConfig = xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
136
137         NioEventLoopGroup nettyThreadgroup = new NioEventLoopGroup();
138         Timer timer = new HashedWheelTimer();
139         NetconfClientDispatcherImpl clientDispatcher = new NetconfClientDispatcherImpl(nettyThreadgroup, nettyThreadgroup, timer);
140         try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", clientDispatcher, getClientConfiguration(tcpAddress))) {
141             sendMessage(edit, netconfClient);
142             sendMessage(commit, netconfClient);
143             sendMessage(getConfig, netconfClient, "id-test",
144                         "<prefix:afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</prefix:afi>",
145                         "<prefix:afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</prefix:afi>",
146                         "<prefix:safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</prefix:safi>",
147                         "<prefix:safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</prefix:safi>");
148
149             clientDispatcher.close();
150         } catch (Exception e) {
151             fail(Throwables.getStackTraceAsString(e));
152         } finally {
153             nettyThreadgroup.shutdownGracefully().get();
154             timer.stop();
155         }
156     }
157
158     private void sendMessage(NetconfMessage edit, TestingNetconfClient netconfClient, String... containingResponse)
159             throws ExecutionException, InterruptedException, TimeoutException {
160         NetconfMessage response = netconfClient.sendRequest(edit).get();
161         if (containingResponse == null) {
162             Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString("<ok/>"));
163         } else {
164             for (String resp : containingResponse) {
165                 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString(resp));
166             }
167         }
168     }
169
170     public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
171             ParserConfigurationException {
172         return XmlFileLoader.xmlFileToNetconfMessage(fileName);
173     }
174
175     public NetconfClientConfiguration getClientConfiguration(final InetSocketAddress tcpAddress) {
176         final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
177         b.withAddress(tcpAddress);
178         b.withSessionListener(new SimpleNetconfClientSessionListener());
179         b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
180                 CLIENT_CONNECTION_TIMEOUT_MILLIS));
181         b.withConnectionTimeoutMillis(CLIENT_CONNECTION_TIMEOUT_MILLIS);
182         return b.build();
183     }
184 }