Remove UriInfo from JSONRestconfService API methods 74/58974/1
authorTom Pantelis <tompantelis@gmail.com>
Wed, 14 Jun 2017 18:42:41 +0000 (14:42 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Wed, 14 Jun 2017 18:42:41 +0000 (14:42 -0400)
https://git.opendaylight.org/gerrit/#/c/48369/ added a UriInfo parameter
to several JSONRestconfService API methods in Carbon. However this was an
undocumented API change which will break downstream users. Further,
there is no available implementation of UriInfo so each user would have
to provide an implementation which would pretty much render the API
unusable. We do not need to expose UriInfo in this API so I've remove the
parameters.

We'll need to cherry-pick to stable/carbon for SR1 to minimize breakage.

Change-Id: I74285413c2a3a92510398a57ea82567b646c6d27
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/api/JSONRestconfService.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java
restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JSONRestconfServiceImplTest.java

index 3550ddee8749a3d27b56e3d0edbae898c4e64a5d..df3e3f1afbece575ad37d504c7c5665e9c5aeb8d 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.netconf.sal.restconf.api;
 
 import com.google.common.base.Optional;
 import javax.annotation.Nonnull;
-import javax.ws.rs.core.UriInfo;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.yangtools.yang.common.OperationFailedException;
 
@@ -32,7 +31,7 @@ public interface JSONRestconfService {
      * @param payload the payload data in JSON format.
      * @throws OperationFailedException if the request fails.
      */
-    void put(String uriPath, @Nonnull String payload, UriInfo uriInfo) throws OperationFailedException;
+    void put(String uriPath, @Nonnull String payload) throws OperationFailedException;
 
     /**
      * Issues a restconf POST request to the configuration data store.
@@ -42,7 +41,7 @@ public interface JSONRestconfService {
      * @param payload the payload data in JSON format.
      * @throws OperationFailedException if the request fails.
      */
-    void post(String uriPath, @Nonnull String payload, UriInfo uriInfo) throws OperationFailedException;
+    void post(String uriPath, @Nonnull String payload) throws OperationFailedException;
 
     /**
      * Issues a restconf DELETE request to the configuration data store.
@@ -62,7 +61,7 @@ public interface JSONRestconfService {
      * @return an Optional containing the data in JSON format if present.
      * @throws OperationFailedException if the request fails.
      */
-    Optional<String> get(String uriPath, LogicalDatastoreType datastoreType, UriInfo uriInfo)
+    Optional<String> get(String uriPath, LogicalDatastoreType datastoreType)
             throws OperationFailedException;
 
     /**
index 77c6dc99c641023f5e37bb29921e959163b64655..f6359a68b85c5014cf41aa0dde5bd6e789af366a 100644 (file)
@@ -14,9 +14,15 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
+import java.net.URI;
 import java.nio.charset.StandardCharsets;
+import java.util.Collections;
 import java.util.List;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.PathSegment;
+import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
@@ -42,7 +48,7 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
-    public void put(final String uriPath, final String payload, final UriInfo uriInfo) throws OperationFailedException {
+    public void put(final String uriPath, final String payload) throws OperationFailedException {
         Preconditions.checkNotNull(payload, "payload can't be null");
 
         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
@@ -54,7 +60,7 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab
         LOG.debug("Parsed NormalizedNode: {}", context.getData());
 
         try {
-            RestconfImpl.getInstance().updateConfigurationData(uriPath, context, uriInfo);
+            RestconfImpl.getInstance().updateConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
         } catch (final Exception e) {
             propagateExceptionAs(uriPath, e, "PUT");
         }
@@ -62,7 +68,7 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
-    public void post(final String uriPath, final String payload, final UriInfo uriInfo)
+    public void post(final String uriPath, final String payload)
             throws OperationFailedException {
         Preconditions.checkNotNull(payload, "payload can't be null");
 
@@ -75,7 +81,7 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab
         LOG.debug("Parsed NormalizedNode: {}", context.getData());
 
         try {
-            RestconfImpl.getInstance().createConfigurationData(uriPath, context, uriInfo);
+            RestconfImpl.getInstance().createConfigurationData(uriPath, context, new SimpleUriInfo(uriPath));
         } catch (final Exception e) {
             propagateExceptionAs(uriPath, e, "POST");
         }
@@ -95,12 +101,13 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
-    public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType, final UriInfo uriInfo)
+    public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
             throws OperationFailedException {
         LOG.debug("get: uriPath: {}", uriPath);
 
         try {
             NormalizedNodeContext readData;
+            final SimpleUriInfo uriInfo = new SimpleUriInfo(uriPath);
             if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
                 readData = RestconfImpl.getInstance().readConfigurationData(uriPath, uriInfo);
             } else {
@@ -225,4 +232,113 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab
             }
         }
     }
+
+    private static class SimpleUriInfo implements UriInfo {
+        private final String path;
+        private final MultivaluedMap<String, String> queryParams;
+
+        SimpleUriInfo(String path) {
+            this(path, new MultivaluedHashMap<>());
+        }
+
+        SimpleUriInfo(String path, MultivaluedMap<String, String> queryParams) {
+            this.path = path;
+            this.queryParams = queryParams;
+        }
+
+        @Override
+        public String getPath() {
+            return path;
+        }
+
+        @Override
+        public String getPath(boolean decode) {
+            return path;
+        }
+
+        @Override
+        public List<PathSegment> getPathSegments() {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public List<PathSegment> getPathSegments(boolean decode) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public URI getRequestUri() {
+            return URI.create(path);
+        }
+
+        @Override
+        public UriBuilder getRequestUriBuilder() {
+            return UriBuilder.fromUri(getRequestUri());
+        }
+
+        @Override
+        public URI getAbsolutePath() {
+            return getRequestUri();
+        }
+
+        @Override
+        public UriBuilder getAbsolutePathBuilder() {
+            return UriBuilder.fromUri(getAbsolutePath());
+        }
+
+        @Override
+        public URI getBaseUri() {
+            return URI.create("");
+        }
+
+        @Override
+        public UriBuilder getBaseUriBuilder() {
+            return UriBuilder.fromUri(getBaseUri());
+        }
+
+        @Override
+        public MultivaluedMap<String, String> getPathParameters() {
+            return new MultivaluedHashMap<>();
+        }
+
+        @Override
+        public MultivaluedMap<String, String> getPathParameters(boolean decode) {
+            return getPathParameters();
+        }
+
+        @Override
+        public MultivaluedMap<String, String> getQueryParameters() {
+            return queryParams;
+        }
+
+        @Override
+        public MultivaluedMap<String, String> getQueryParameters(boolean decode) {
+            return getQueryParameters();
+        }
+
+        @Override
+        public List<String> getMatchedURIs() {
+            return Collections.emptyList();
+        }
+
+        @Override
+        public List<String> getMatchedURIs(boolean decode) {
+            return getMatchedURIs();
+        }
+
+        @Override
+        public List<Object> getMatchedResources() {
+            return Collections.emptyList();
+        }
+
+        @Override
+        public URI resolve(URI uri) {
+            return uri;
+        }
+
+        @Override
+        public URI relativize(URI uri) {
+            return uri;
+        }
+    }
 }
index f0b797c0257f0662ab9c737c357cd3c58280c2f0..89d5412ef55d5e03f0d804d7bcfeeb658b657d08 100644 (file)
@@ -29,13 +29,9 @@ import com.google.common.util.concurrent.Futures;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response.Status;
-import javax.ws.rs.core.UriBuilder;
-import javax.ws.rs.core.UriInfo;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -140,11 +136,7 @@ public class JSONRestconfServiceImplTest {
         when(result.getStatus()).thenReturn(Status.OK);
         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        this.service.put(uriPath, payload, uriInfo);
+        this.service.put(uriPath, payload);
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
                 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
@@ -178,11 +170,7 @@ public class JSONRestconfServiceImplTest {
         final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
         final String payload = loadData("/full-versions/testCont1Data.json");
 
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        this.service.put(uriPath, payload, uriInfo);
+        this.service.put(uriPath, payload);
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
                 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
@@ -214,11 +202,7 @@ public class JSONRestconfServiceImplTest {
         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
 
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        this.service.put(uriPath, payload, uriInfo);
+        this.service.put(uriPath, payload);
     }
 
     @SuppressWarnings("rawtypes")
@@ -231,13 +215,7 @@ public class JSONRestconfServiceImplTest {
         final String uriPath = null;
         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
 
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        final UriBuilder uriBuilder = UriBuilder.fromPath("");
-        Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
-        this.service.post(uriPath, payload, uriInfo);
+        this.service.post(uriPath, payload);
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
                 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
@@ -278,13 +256,7 @@ public class JSONRestconfServiceImplTest {
         final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
         final String payload = loadData("/full-versions/testCont1Data.json");
 
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        final UriBuilder uriBuilder = UriBuilder.fromPath("");
-        Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
-        this.service.post(uriPath, payload, uriInfo);
+        this.service.post(uriPath, payload);
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
                 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
@@ -311,15 +283,8 @@ public class JSONRestconfServiceImplTest {
         final String uriPath = null;
         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
 
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        final UriBuilder uriBuilder = UriBuilder.fromPath("");
-        Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
-
         try {
-            this.service.post(uriPath, payload, uriInfo);
+            this.service.post(uriPath, payload);
         } catch (final OperationFailedException e) {
             assertNotNull(e.getCause());
             throw e.getCause();
@@ -365,21 +330,13 @@ public class JSONRestconfServiceImplTest {
         doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
                 Mockito.anyString());
         final String uriPath = "ietf-interfaces:interfaces";
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
+        this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
     }
 
     @Test(expected = OperationFailedException.class)
     public void testGetFailure() throws Exception {
         final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
+        this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
     }
 
     @SuppressWarnings("rawtypes")
@@ -474,14 +431,8 @@ public class JSONRestconfServiceImplTest {
         }
 
         final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
-        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
-        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
-        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
-        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
-        Mockito.when(uriInfo.getQueryParameters(false)).thenReturn(value);
-        Mockito.when(value.getFirst("depth")).thenReturn("");
-
-        final Optional<String> optionalResp = this.service.get(uriPath, datastoreType, uriInfo);
+
+        final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
         assertEquals("Response present", true, optionalResp.isPresent());
         final String jsonResp = optionalResp.get();