Bug 809: Enhancements to the toaster example
[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 import io.netty.channel.nio.NioEventLoopGroup;
23
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.TimeoutException;
28
29 import javax.inject.Inject;
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.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;
51
52 import com.google.common.base.Preconditions;
53 import com.google.common.base.Throwables;
54
55 @Ignore
56 @RunWith(PaxExam.class)
57 public class IdentityRefNetconfTest {
58
59     public static final int CLIENT_CONNECTION_TIMEOUT_MILLIS = 15000;
60
61     // Wait for controller to start
62     @Inject
63     @Filter(timeout = 60 * 1000)
64     BindingAwareBroker broker;
65
66     @Configuration
67     public Option[] config() {
68         return options(
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"),
73
74                 testingModules(),
75                 loggingModules(),
76                 mdSalCoreBundles(),
77                 bindingAwareSalBundles(), configMinumumBundles(), baseModelBundles(), flowCapableModelBundles(),
78                 junitAndMockitoBundles(),
79
80                 // Classes from test-jars bundled for pax-exam test
81                 streamBundle(TinyBundles.bundle()
82                         .add(TestingNetconfClient.class)
83                         .add(XmlFileLoader.class)
84
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"))
91
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())));
96     }
97
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(),
104                 mavenBundle("org.opendaylight.controller.thirdparty", "nagasena").versionAsInProject(),
105                 mavenBundle("org.opendaylight.controller.thirdparty", "nagasena-rta").versionAsInProject());
106
107
108     }
109
110     private Option testingModules() {
111         return new DefaultCompositeOption(
112                 mavenBundle("org.opendaylight.controller", "yang-test").versionAsInProject());
113     }
114
115     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 18383);
116
117     @Test
118     public void testIdRef() throws Exception {
119         Preconditions.checkNotNull(broker, "Controller not initialized");
120
121         NioEventLoopGroup nettyThreadgroup = new NioEventLoopGroup();
122         NetconfClientDispatcher clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup,
123                 CLIENT_CONNECTION_TIMEOUT_MILLIS);
124
125         NetconfMessage edit = xmlFileToNetconfMessage("netconfMessages/editConfig_identities.xml");
126         NetconfMessage commit = xmlFileToNetconfMessage("netconfMessages/commit.xml");
127         NetconfMessage getConfig = xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
128
129         try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", tcpAddress, CLIENT_CONNECTION_TIMEOUT_MILLIS, clientDispatcher)) {
130             sendMessage(edit, netconfClient);
131             sendMessage(commit, netconfClient);
132             sendMessage(getConfig, netconfClient, "id-test",
133                         "<prefix:afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</prefix:afi>",
134                         "<prefix:afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</prefix:afi>",
135                         "<prefix:safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</prefix:safi>",
136                         "<prefix:safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</prefix:safi>");
137
138             clientDispatcher.close();
139         } catch (Exception e) {
140             fail(Throwables.getStackTraceAsString(e));
141         }
142     }
143
144     private void sendMessage(NetconfMessage edit, TestingNetconfClient netconfClient, String... containingResponse)
145             throws ExecutionException, InterruptedException, TimeoutException {
146         NetconfMessage response = netconfClient.sendRequest(edit).get();
147         if (containingResponse == null) {
148             Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString("<ok/>"));
149         } else {
150             for (String resp : containingResponse) {
151                 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString(resp));
152             }
153         }
154     }
155
156     public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
157             ParserConfigurationException {
158         return XmlFileLoader.xmlFileToNetconfMessage(fileName);
159     }
160 }