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