Fixed response errors for HTTP PUT
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfDataServiceImpl.java
index a8f6b91bb90cb38f92ed95b219378c7d34ced6e4..075fd1a09dcbeab16814071f8a1bc8466d10c7cd 100644 (file)
@@ -12,19 +12,20 @@ import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsCo
 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_LOCATION_PATH_PART;
 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_PATH;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import java.time.Clock;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.List;
 import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.Optional;
 import javax.annotation.Nonnull;
+import javax.ws.rs.Path;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.context.WriterParameters;
@@ -32,7 +33,6 @@ import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.common.patch.PatchStatusContext;
-import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
@@ -58,6 +58,7 @@ import org.slf4j.LoggerFactory;
 /**
  * Implementation of {@link RestconfDataService}.
  */
+@Path("/")
 public class RestconfDataServiceImpl implements RestconfDataService {
 
     private static final Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
@@ -73,10 +74,10 @@ public class RestconfDataServiceImpl implements RestconfDataService {
                                    final TransactionChainHandler transactionChainHandler,
             final DOMMountPointServiceHandler mountPointServiceHandler,
             final RestconfStreamsSubscriptionService delegRestconfSubscrService) {
-        this.schemaContextHandler = schemaContextHandler;
-        this.transactionChainHandler = transactionChainHandler;
-        this.mountPointServiceHandler = mountPointServiceHandler;
-        this.delegRestconfSubscrService = delegRestconfSubscrService;
+        this.schemaContextHandler = Objects.requireNonNull(schemaContextHandler);
+        this.transactionChainHandler = Objects.requireNonNull(transactionChainHandler);
+        this.mountPointServiceHandler = Objects.requireNonNull(mountPointServiceHandler);
+        this.delegRestconfSubscrService = Objects.requireNonNull(delegRestconfSubscrService);
     }
 
     @Override
@@ -136,15 +137,15 @@ public class RestconfDataServiceImpl implements RestconfDataService {
                 instanceIdentifier, uriInfo, tagged);
 
         final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
-        final DOMTransactionChain transactionChain;
+        final TransactionChainHandler localTransactionChainHandler;
         if (mountPoint == null) {
-            transactionChain = this.transactionChainHandler.get();
+            localTransactionChainHandler = this.transactionChainHandler;
         } else {
-            transactionChain = transactionChainOfMountPoint(mountPoint);
+            localTransactionChainHandler = transactionChainOfMountPoint(mountPoint);
         }
 
         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
-                instanceIdentifier, mountPoint, transactionChain);
+                instanceIdentifier, mountPoint, localTransactionChainHandler);
         final NormalizedNode<?, ?> node =
                 ReadDataTransactionUtil.readData(identifier, parameters.getContent(), transactionNode, withDefa,
                         schemaContextRef, uriInfo);
@@ -193,7 +194,8 @@ public class RestconfDataServiceImpl implements RestconfDataService {
                         insertUsed = true;
                         insert = entry.getValue().iterator().next();
                     } else {
-                        throw new RestconfDocumentedException("Insert parameter can be used only once.");
+                        throw new RestconfDocumentedException("Insert parameter can be used only once.",
+                                RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.BAD_ELEMENT);
                     }
                     break;
                 case "point":
@@ -201,11 +203,13 @@ public class RestconfDataServiceImpl implements RestconfDataService {
                         pointUsed = true;
                         point = entry.getValue().iterator().next();
                     } else {
-                        throw new RestconfDocumentedException("Point parameter can be used only once.");
+                        throw new RestconfDocumentedException("Point parameter can be used only once.",
+                                RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.BAD_ELEMENT);
                     }
                     break;
                 default:
-                    throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey());
+                    throw new RestconfDocumentedException("Bad parameter for post: " + entry.getKey(),
+                            RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.BAD_ELEMENT);
             }
         }
 
@@ -219,18 +223,18 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         PutDataTransactionUtil.validateListKeysEqualityInPayloadAndUri(payload);
 
         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
-        final DOMTransactionChain transactionChain;
+        final TransactionChainHandler localTransactionChainHandler;
         final SchemaContextRef ref;
         if (mountPoint == null) {
-            transactionChain = this.transactionChainHandler.get();
+            localTransactionChainHandler = this.transactionChainHandler;
             ref = new SchemaContextRef(this.schemaContextHandler.get());
         } else {
-            transactionChain = transactionChainOfMountPoint(mountPoint);
+            localTransactionChainHandler = transactionChainOfMountPoint(mountPoint);
             ref = new SchemaContextRef(mountPoint.getSchemaContext());
         }
 
         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
-                payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
+                payload.getInstanceIdentifierContext(), mountPoint, localTransactionChainHandler);
         return PutDataTransactionUtil.putData(payload, ref, transactionNode, insert, point);
     }
 
@@ -284,17 +288,17 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         checkQueryParams(insertUsed, pointUsed, insert);
 
         final DOMMountPoint mountPoint = payload.getInstanceIdentifierContext().getMountPoint();
-        final DOMTransactionChain transactionChain;
+        final TransactionChainHandler localTransactionChainHandler;
         final SchemaContextRef ref;
         if (mountPoint == null) {
-            transactionChain = this.transactionChainHandler.get();
+            localTransactionChainHandler = this.transactionChainHandler;
             ref = new SchemaContextRef(this.schemaContextHandler.get());
         } else {
-            transactionChain = transactionChainOfMountPoint(mountPoint);
+            localTransactionChainHandler = transactionChainOfMountPoint(mountPoint);
             ref = new SchemaContextRef(mountPoint.getSchemaContext());
         }
         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
-                payload.getInstanceIdentifierContext(), mountPoint, transactionChain);
+                payload.getInstanceIdentifierContext(), mountPoint, localTransactionChainHandler);
         return PostDataTransactionUtil.postData(uriInfo, payload, transactionNode, ref, insert, point);
     }
 
@@ -305,15 +309,15 @@ public class RestconfDataServiceImpl implements RestconfDataService {
                 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
 
         final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
-        final DOMTransactionChain transactionChain;
+        final TransactionChainHandler localTransactionChainHandler;
         if (mountPoint == null) {
-            transactionChain = this.transactionChainHandler.get();
+            localTransactionChainHandler = this.transactionChainHandler;
         } else {
-            transactionChain = transactionChainOfMountPoint(mountPoint);
+            localTransactionChainHandler = transactionChainOfMountPoint(mountPoint);
         }
 
         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(instanceIdentifier, mountPoint,
-                transactionChain);
+                localTransactionChainHandler);
         return DeleteDataTransactionUtil.deleteData(transactionNode);
     }
 
@@ -327,18 +331,18 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         Preconditions.checkNotNull(context);
         final DOMMountPoint mountPoint = context.getInstanceIdentifierContext().getMountPoint();
 
-        final DOMTransactionChain transactionChain;
+        final TransactionChainHandler localTransactionChainHandler;
         final SchemaContextRef ref;
         if (mountPoint == null) {
-            transactionChain = this.transactionChainHandler.get();
+            localTransactionChainHandler = this.transactionChainHandler;
             ref = new SchemaContextRef(this.schemaContextHandler.get());
         } else {
-            transactionChain = transactionChainOfMountPoint(mountPoint);
+            localTransactionChainHandler = transactionChainOfMountPoint(mountPoint);
             ref = new SchemaContextRef(mountPoint.getSchemaContext());
         }
 
         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
-                context.getInstanceIdentifierContext(), mountPoint, transactionChain);
+                context.getInstanceIdentifierContext(), mountPoint, localTransactionChainHandler);
 
         return PatchDataTransactionUtil.patchData(context, transactionNode, ref);
     }
@@ -347,12 +351,12 @@ public class RestconfDataServiceImpl implements RestconfDataService {
      * Prepare transaction chain to access data of mount point.
      * @param mountPoint
      *            mount point reference
-     * @return {@link DOMTransactionChain}
+     * @return {@link TransactionChainHandler}
      */
-    private static DOMTransactionChain transactionChainOfMountPoint(@Nonnull final DOMMountPoint mountPoint) {
+    private static TransactionChainHandler transactionChainOfMountPoint(@Nonnull final DOMMountPoint mountPoint) {
         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
         if (domDataBrokerService.isPresent()) {
-            return domDataBrokerService.get().createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER);
+            return new TransactionChainHandler(domDataBrokerService.get());
         }
 
         final String errMsg = "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier();