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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.