Merge "Bug 664 Update security.xml in web bundle"
[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 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;
30
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;
38
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;
50
51 @Ignore
52 @RunWith(PaxExam.class)
53 public class IdentityRefNetconfTest {
54
55     public static final int CLIENT_CONNECTION_TIMEOUT_MILLIS = 15000;
56
57     // Wait for controller to start
58     @Inject
59     @Filter(timeout = 60 * 1000)
60     BindingAwareBroker broker;
61
62     @Configuration
63     public Option[] config() {
64         return options(
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"),
69
70                 testingModules(),
71                 loggingModules(),
72                 mdSalCoreBundles(),
73                 bindingAwareSalBundles(), configMinumumBundles(), baseModelBundles(), flowCapableModelBundles(),
74                 junitAndMockitoBundles());
75     }
76
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());
83     }
84
85     private Option testingModules() {
86         return new DefaultCompositeOption(
87                 mavenBundle("org.opendaylight.controller", "yang-test").versionAsInProject());
88     }
89
90     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 18383);
91
92
93     @Test
94     public void testIdRef() throws Exception {
95         try {
96             Preconditions.checkNotNull(broker, "Controller not initialized");
97
98             NioEventLoopGroup nettyThreadgroup = new NioEventLoopGroup();
99             NetconfClientDispatcher clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup,
100                     CLIENT_CONNECTION_TIMEOUT_MILLIS);
101
102             NetconfMessage edit = xmlFileToNetconfMessage("netconfMessages/editConfig_identities.xml");
103             NetconfMessage commit = xmlFileToNetconfMessage("netconfMessages/commit.xml");
104             NetconfMessage getConfig = xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
105
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>");
114             }
115
116             clientDispatcher.close();
117         } catch (Exception e) {
118             fail(Throwables.getStackTraceAsString(e));
119         }
120     }
121
122
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/>"));
128         } else {
129             for (String resp : containingResponse) {
130                 Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString(resp));
131             }
132         }
133     }
134
135     public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
136             ParserConfigurationException {
137         return new NetconfMessage(xmlFileToDocument(fileName));
138     }
139
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);
146             return doc;
147         }
148     }
149 }