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