Fixes from modernizer maven plugin in rfc8040 module
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / JSONRestconfServiceRfc8040Impl.java
index 5efca62de73e4feba25732a261c68a3751e57ae6..3470c593f650953bfe9756ca52873d7894e4d223 100644 (file)
@@ -7,8 +7,8 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -17,11 +17,14 @@ import java.io.InputStream;
 import java.lang.annotation.Annotation;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
-import javax.annotation.Nullable;
+import java.util.Optional;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
@@ -40,6 +43,7 @@ import org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch.PatchJsonBody
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.JSONRestconfService;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.TransactionServicesWrapper;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant;
+import org.opendaylight.restconf.nb.rfc8040.services.wrapper.ServicesWrapper;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
 import org.opendaylight.yangtools.yang.common.OperationFailedException;
 import org.opendaylight.yangtools.yang.common.RpcError;
@@ -49,11 +53,12 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Implementation of the JSONRestconfService interface using the restconf Draft18 implementation.
+ * Implementation of the JSONRestconfService interface using the RFC8040 implementation.
  *
  * @author Thomas Pantelis
  */
-public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, AutoCloseable {
+@Singleton
+public final class JSONRestconfServiceRfc8040Impl implements JSONRestconfService {
     private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceRfc8040Impl.class);
 
     private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
@@ -62,7 +67,8 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
     private final DOMMountPointServiceHandler mountPointServiceHandler;
     private final SchemaContextHandler schemaContextHandler;
 
-    public JSONRestconfServiceRfc8040Impl(final TransactionServicesWrapper services,
+    @Inject
+    public JSONRestconfServiceRfc8040Impl(final ServicesWrapper services,
             final DOMMountPointServiceHandler mountPointServiceHandler,
             final SchemaContextHandler schemaContextHandler) {
         this.services = services;
@@ -73,7 +79,7 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     public void put(final String uriPath, final String payload) throws OperationFailedException {
-        Preconditions.checkNotNull(payload, "payload can't be null");
+        requireNonNull(payload, "payload can't be null");
 
         LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
 
@@ -91,9 +97,8 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
-    public void post(final String uriPath, final String payload)
-            throws OperationFailedException {
-        Preconditions.checkNotNull(payload, "payload can't be null");
+    public void post(final String uriPath, final String payload) throws OperationFailedException {
+        requireNonNull(payload, "payload can't be null");
 
         LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
 
@@ -147,7 +152,7 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
             }
 
             LOG.debug("Data missing - returning absent");
-            return Optional.absent();
+            return Optional.empty();
         }
     }
 
@@ -156,7 +161,7 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
     @Override
     public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
             throws OperationFailedException {
-        Preconditions.checkNotNull(uriPath, "uriPath can't be null");
+        requireNonNull(uriPath, "uriPath can't be null");
 
         final String actualInput = input.isPresent() ? input.get() : null;
 
@@ -180,7 +185,7 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
             propagateExceptionAs(uriPath, e, "RPC");
         }
 
-        return Optional.fromNullable(output);
+        return Optional.ofNullable(output);
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
@@ -189,7 +194,7 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
             throws OperationFailedException {
 
         String output = null;
-        Preconditions.checkNotNull(payload, "payload can't be null");
+        requireNonNull(payload, "payload can't be null");
 
         LOG.debug("patch: uriPath: {}, payload: {}", uriPath, payload);
 
@@ -208,14 +213,10 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
         } catch (final Exception e) {
             propagateExceptionAs(uriPath, e, "PATCH");
         }
-        return Optional.fromNullable(output);
+        return Optional.ofNullable(output);
     }
 
-    @Override
-    public void close() {
-    }
-
-    private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, @Nullable final String payload,
+    private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, final @Nullable String payload,
             final boolean isPost) throws OperationFailedException {
         final InstanceIdentifierContext<?> instanceIdentifierContext = ParserIdentifier.toInstanceIdentifier(
                 uriPath, schemaContextHandler.get(), Optional.of(mountPointServiceHandler.get()));
@@ -225,20 +226,15 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
         }
 
         final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
-        try {
-            return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
-        } catch (final IOException e) {
-            propagateExceptionAs(uriPath, e, "GET");
-            return null;
-        }
+        return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
     }
 
-    private  String toJson(final PatchStatusContext patchStatusContext) throws IOException {
+    private static String toJson(final PatchStatusContext patchStatusContext) throws IOException {
         final PatchJsonBodyWriter writer = new PatchJsonBodyWriter();
         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         writer.writeTo(patchStatusContext, PatchStatusContext.class, null, EMPTY_ANNOTATIONS,
                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
-        return outputStream.toString(StandardCharsets.UTF_8.name());
+        return outputStream.toString(StandardCharsets.UTF_8);
     }
 
     private static String toJson(final NormalizedNodeContext readData) throws IOException {
@@ -246,7 +242,7 @@ public class JSONRestconfServiceRfc8040Impl implements JSONRestconfService, Auto
         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
                 MediaType.APPLICATION_JSON_TYPE, null, outputStream);
-        return outputStream.toString(StandardCharsets.UTF_8.name());
+        return outputStream.toString(StandardCharsets.UTF_8);
     }
 
     private static boolean isDataMissing(final Exception exception) {