2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.websockets.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
15 import java.io.FileNotFoundException;
16 import java.io.UnsupportedEncodingException;
18 import javax.ws.rs.client.Entity;
19 import javax.ws.rs.core.Application;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import org.glassfish.jersey.server.ResourceConfig;
23 import org.glassfish.jersey.test.JerseyTest;
24 import org.junit.BeforeClass;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
29 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
30 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
31 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
32 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
33 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
34 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
35 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
36 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Element;
41 import org.w3c.dom.Node;
43 public class RestStreamTest extends JerseyTest {
45 private static SchemaContext schemaContextYangsIetf;
47 private BrokerFacade brokerFacade;
48 private RestconfImpl restconfImpl;
51 public static void init() throws FileNotFoundException, ReactorException {
52 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
56 protected Application configure() {
57 /* enable/disable Jersey logs to console */
58 // enable(TestProperties.LOG_TRAFFIC);
59 // enable(TestProperties.DUMP_ENTITY);
60 // enable(TestProperties.RECORD_LOG_LEVEL);
61 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
63 final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContextYangsIetf);
64 brokerFacade = mock(BrokerFacade.class);
65 restconfImpl = RestconfImpl.getInstance();
66 restconfImpl.setBroker(brokerFacade);
67 restconfImpl.setControllerContext(controllerContext);
69 ResourceConfig resourceConfig = new ResourceConfig();
70 resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
71 new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext),
72 new JsonNormalizedNodeBodyReader(controllerContext),
73 new RestconfDocumentedExceptionMapper(controllerContext));
74 return resourceConfig;
78 @Ignore // Sporadic failures where jersey does not correctly pass post data to XmlNormalizedNodeBodyReader.readFrom
79 public void testCallRpcCallGet() throws UnsupportedEncodingException, InterruptedException {
80 createAndSubscribe(null);
84 @Ignore // Sporadic failures where jersey does not correctly pass post data to XmlNormalizedNodeBodyReader.readFrom
85 public void testCallRpcCallGetLeaves() throws UnsupportedEncodingException, InterruptedException {
86 createAndSubscribe("odl-leaf-nodes-only", "true");
89 private void createAndSubscribe(String queryParamName, Object... values)
90 throws UnsupportedEncodingException, InterruptedException {
91 String uri = "/operations/sal-remote:create-data-change-event-subscription";
92 String rpcInput = getRpcInput();
93 final Response responseWithStreamName = post(uri, MediaType.APPLICATION_XML, rpcInput);
94 final Document xmlResponse = responseWithStreamName.readEntity(Document.class);
95 assertNotNull(xmlResponse);
96 final Element outputElement = xmlResponse.getDocumentElement();
97 assertEquals("output",outputElement.getLocalName());
99 final Node streamNameElement = outputElement.getFirstChild();
100 assertEquals("stream-name",streamNameElement.getLocalName());
101 assertEquals("data-change-event-subscription/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0/"
102 + "datastore=CONFIGURATION/scope=BASE",streamNameElement.getTextContent());
104 uri = "/streams/stream/data-change-event-subscription/ietf-interfaces:interfaces/ietf-interfaces:interface/"
105 + "eth0/datastore=CONFIGURATION/scope=BASE";
106 final Response responseWithRedirectionUri = get(uri, MediaType.APPLICATION_XML, null);
107 final URI websocketServerUri = responseWithRedirectionUri.getLocation();
108 assertNotNull(websocketServerUri);
109 assertTrue(websocketServerUri.toString().matches(".*ws://localhost:[\\d]+/data-change-event-subscription/"
110 + "ietf-interfaces:interfaces/ietf-interfaces:interface/eth0.*"));
113 private Response post(final String uri, final String mediaType, final String data) {
114 return target(uri).request(mediaType).post(Entity.entity(data, mediaType));
117 private Response get(final String uri, final String mediaType, String queryParam, Object... values) {
118 if (queryParam != null) {
119 return target(uri).queryParam(queryParam, values).request(mediaType).get();
121 return target(uri).request(mediaType).get();
125 private static String getRpcInput() {
126 final StringBuilder sb = new StringBuilder();
127 sb.append("<input xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote\">");
128 sb.append("<path xmlns:int=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">/"
129 + "int:interfaces/int:interface[int:name='eth0']</path>");
130 sb.append("</input>");
131 return sb.toString();