BUG 1269 - fill HTTP POST response location attribute
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / RestconfImpl.java
index 4073c2fabd0b11164f03cc0ea3b90a03cf84afc5..4023412640cb3eb6e547085c06b37b541e487f10 100644 (file)
@@ -33,6 +33,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
@@ -581,7 +582,7 @@ public class RestconfImpl implements RestconfService {
         final YangInstanceIdentifier pathIdentifier = ((YangInstanceIdentifier) pathValue);
         String streamName = null;
         if (!Iterables.isEmpty(pathIdentifier.getPathArguments())) {
-            final String fullRestconfIdentifier = controllerContext.toFullRestconfIdentifier(pathIdentifier);
+            final String fullRestconfIdentifier = controllerContext.toFullRestconfIdentifier(pathIdentifier, null);
 
             LogicalDatastoreType datastore = parseEnumTypeParameter(value, LogicalDatastoreType.class,
                     DATASTORE_PARAM_NAME);
@@ -816,7 +817,6 @@ public class RestconfImpl implements RestconfService {
         validateListKeysEqualityInPayloadAndUri(iiWithData, payload.getData());
 
         final DOMMountPoint mountPoint = iiWithData.getMountPoint();
-
         final YangInstanceIdentifier normalizedII = iiWithData.getInstanceIdentifier();
 
         /*
@@ -1015,7 +1015,7 @@ public class RestconfImpl implements RestconfService {
     }
 
     @Override
-    public Response createConfigurationData(final String identifier, final Node<?> payload) {
+    public Response createConfigurationData(final String identifier, final Node<?> payload, final UriInfo uriInfo) {
         if (payload == null) {
             throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
         }
@@ -1048,6 +1048,10 @@ public class RestconfImpl implements RestconfService {
             final DataNodeContainer parentSchema = (DataNodeContainer) incompleteInstIdWithData.getSchemaNode();
             final DOMMountPoint mountPoint = incompleteInstIdWithData.getMountPoint();
             final Module module = findModule(mountPoint, payload);
+            if (module == null) {
+                throw new RestconfDocumentedException("Module was not found for \"" + payloadNS + "\"",
+                        ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
+            }
 
             final String payloadName = getName(payload);
             final DataSchemaNode schemaNode = ControllerContext.findInstanceDataChildByNameAndNamespace(
@@ -1077,11 +1081,17 @@ public class RestconfImpl implements RestconfService {
             throw new RestconfDocumentedException("Error creating data", e);
         }
 
-        return Response.status(Status.NO_CONTENT).build();
+
+        final ResponseBuilder responseBuilder = Response.status(Status.NO_CONTENT);
+        final URI location = resolveLocation(uriInfo, "config", mountPoint, normalizedII);
+        if (location != null) {
+            responseBuilder.location(location);
+        }
+        return responseBuilder.build();
     }
 
     @Override
-    public Response createConfigurationData(final Node<?> payload) {
+    public Response createConfigurationData(final Node<?> payload, final UriInfo uriInfo) {
         if (payload == null) {
             throw new RestconfDocumentedException("Input is required.", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
         }
@@ -1094,6 +1104,11 @@ public class RestconfImpl implements RestconfService {
         }
 
         final Module module = this.findModule(null, payload);
+        if (module == null) {
+            throw new RestconfDocumentedException(
+                    "Data has bad format. Root element node has incorrect namespace (XML format) or module name(JSON format)",
+                    ErrorType.PROTOCOL, ErrorTag.UNKNOWN_NAMESPACE);
+        }
 
         final String payloadName = getName(payload);
         final DataSchemaNode schemaNode = ControllerContext.findInstanceDataChildByNameAndNamespace(module,
@@ -1120,7 +1135,24 @@ public class RestconfImpl implements RestconfService {
             throw new RestconfDocumentedException("Error creating data", e);
         }
 
-        return Response.status(Status.NO_CONTENT).build();
+        final ResponseBuilder responseBuilder = Response.status(Status.NO_CONTENT);
+        final URI location = resolveLocation(uriInfo, "", mountPoint, normalizedII);
+        if (location != null) {
+            responseBuilder.location(location);
+        }
+        return responseBuilder.build();
+    }
+
+    private URI resolveLocation(final UriInfo uriInfo, final String uriBehindBase, final DOMMountPoint mountPoint, final YangInstanceIdentifier normalizedII) {
+        final UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
+        uriBuilder.path("config");
+        try {
+            uriBuilder.path(controllerContext.toFullRestconfIdentifier(normalizedII, mountPoint));
+        } catch (final Exception e) {
+            LOG.debug("Location for instance identifier"+normalizedII+"wasn't created", e);
+            return null;
+        }
+        return uriBuilder.build();
     }
 
     @Override