Fixed POST data error response
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfDataServiceImpl.java
index a1424316e07492ec4a641268ccdf6ed358a177c0..87b20ee86bf8734a32f131d066cbb11dd93fbb35 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;
@@ -48,6 +48,8 @@ import org.opendaylight.restconf.nb.rfc8040.rests.utils.ReadDataTransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.slf4j.Logger;
@@ -56,14 +58,15 @@ import org.slf4j.LoggerFactory;
 /**
  * Implementation of {@link RestconfDataService}.
  */
+@Path("/")
 public class RestconfDataServiceImpl implements RestconfDataService {
 
     private static final Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
     private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
 
-    private final SchemaContextHandler schemaContextHandler;
-    private final TransactionChainHandler transactionChainHandler;
-    private final DOMMountPointServiceHandler mountPointServiceHandler;
+    private SchemaContextHandler schemaContextHandler;
+    private TransactionChainHandler transactionChainHandler;
+    private DOMMountPointServiceHandler mountPointServiceHandler;
 
     private final RestconfStreamsSubscriptionService delegRestconfSubscrService;
 
@@ -71,10 +74,23 @@ 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
+    public synchronized void updateHandlers(final Object... handlers) {
+        for (final Object object : handlers) {
+            if (object instanceof SchemaContextHandler) {
+                schemaContextHandler = (SchemaContextHandler) object;
+            } else if (object instanceof DOMMountPointServiceHandler) {
+                mountPointServiceHandler = (DOMMountPointServiceHandler) object;
+            } else if (object instanceof TransactionChainHandler) {
+                transactionChainHandler = (TransactionChainHandler) object;
+            }
+        }
     }
 
     @Override
@@ -121,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);
@@ -148,12 +164,13 @@ public class RestconfDataServiceImpl implements RestconfDataService {
                     RestconfError.ErrorTag.DATA_MISSING);
         }
 
-        if ((parameters.getContent().equals(RestconfDataServiceConstant.ReadData.ALL))
+        if (parameters.getContent().equals(RestconfDataServiceConstant.ReadData.ALL)
                     || parameters.getContent().equals(RestconfDataServiceConstant.ReadData.CONFIG)) {
+            final QName type = node.getNodeType();
             return Response.status(200)
                     .entity(new NormalizedNodeContext(instanceIdentifier, node, parameters))
-                    .header("ETag", '"' + node.getNodeType().getModule().getFormattedRevision()
-                        + node.getNodeType().getLocalName() + '"')
+                    .header("ETag", '"' + type.getModule().getRevision().map(Revision::toString).orElse(null)
+                        + type.getLocalName() + '"')
                     .header("Last-Modified", FORMATTER.format(LocalDateTime.now(Clock.systemUTC())))
                     .build();
         }
@@ -177,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":
@@ -185,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);
             }
         }
 
@@ -203,28 +223,30 @@ 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);
     }
 
     private static void checkQueryParams(final boolean insertUsed, final boolean pointUsed, final String insert) {
         if (pointUsed && !insertUsed) {
-            throw new RestconfDocumentedException("Point parameter can't be used without Insert parameter.");
+            throw new RestconfDocumentedException("Point parameter can't be used without Insert parameter.",
+                    RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.BAD_ELEMENT);
         }
         if (pointUsed && (insert.equals("first") || insert.equals("last"))) {
             throw new RestconfDocumentedException(
-                    "Point parameter can be used only with 'after' or 'before' values of Insert parameter.");
+                    "Point parameter can be used only with 'after' or 'before' values of Insert parameter.",
+                    RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.BAD_ELEMENT);
         }
     }
 
@@ -249,7 +271,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":
@@ -257,28 +280,30 @@ 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);
             }
         }
 
         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);
     }
 
@@ -289,15 +314,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);
     }
 
@@ -311,18 +336,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);
     }
@@ -331,12 +356,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();