X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2FRestconfImpl.java;h=ae48ca7196904a135429bd9aae88626f947924d1;hp=4e1adbc598be2807bccf1f0e8bd76926e141d55d;hb=3b721d88ed1083a463ecb73a6050de4bfedf1a78;hpb=d80c9663968b16cac94ae50f49f573358a4da6c5 diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java index 4e1adbc598..ae48ca7196 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java @@ -52,6 +52,8 @@ import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode; import org.opendaylight.yangtools.yang.data.api.Node; import org.opendaylight.yangtools.yang.data.api.SimpleNode; @@ -674,6 +676,7 @@ public class RestconfImpl implements RestconfService { MountInstance mountPoint = iiWithData.getMountPoint(); final CompositeNode value = this.normalizeNode(payload, iiWithData.getSchemaNode(), mountPoint); + validateListKeysEqualityInPayloadAndUri(iiWithData, payload); RpcResult status = null; try { @@ -695,6 +698,52 @@ public class RestconfImpl implements RestconfService { return Response.status(Status.INTERNAL_SERVER_ERROR).build(); } + /** + * Validates whether keys in {@code payload} are equal to values of keys in + * {@code iiWithData} for list schema node + * + * @throws RestconfDocumentedException + * if key values or key count in payload and URI isn't equal + * + */ + private void validateListKeysEqualityInPayloadAndUri(final InstanceIdWithSchemaNode iiWithData, + final CompositeNode payload) { + if (iiWithData.getSchemaNode() instanceof ListSchemaNode) { + final List keyDefinitions = ((ListSchemaNode) iiWithData.getSchemaNode()).getKeyDefinition(); + final PathArgument lastPathArgument = iiWithData.getInstanceIdentifier().getLastPathArgument(); + if (lastPathArgument instanceof NodeIdentifierWithPredicates) { + final Map uriKeyValues = ((NodeIdentifierWithPredicates) lastPathArgument) + .getKeyValues(); + isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions); + } + } + } + + private void isEqualUriAndPayloadKeyValues(final Map uriKeyValues, final CompositeNode payload, + final List keyDefinitions) { + for (QName keyDefinition : keyDefinitions) { + final Object uriKeyValue = uriKeyValues.get(keyDefinition); + // should be caught during parsing URI to InstanceIdentifier + if (uriKeyValue == null) { + throw new RestconfDocumentedException("Missing key " + keyDefinition + " in URI.", + ErrorType.PROTOCOL, ErrorTag.DATA_MISSING); + } + final List> payloadKeyValues = payload.getSimpleNodesByName(keyDefinition.getLocalName()); + if (payloadKeyValues.isEmpty()) { + throw new RestconfDocumentedException("Missing key " + keyDefinition.getLocalName() + + " in the message body.", ErrorType.PROTOCOL, + ErrorTag.DATA_MISSING); + } + + Object payloadKeyValue = payloadKeyValues.iterator().next().getValue(); + if (!uriKeyValue.equals(payloadKeyValue)) { + throw new RestconfDocumentedException("The value '"+uriKeyValue+ "' for key '" + keyDefinition.getLocalName() + + "' specified in the URI doesn't match the value '" + payloadKeyValue + "' specified in the message body. ", + ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); + } + } + } + @Override public Response createConfigurationData(final String identifier, final CompositeNode payload) { if( payload == null ) { @@ -878,7 +927,7 @@ public class RestconfImpl implements RestconfService { broker.registerToListenDataChanges(listener); final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder(); - UriBuilder port = uriBuilder.port(WebSocketServer.PORT); + UriBuilder port = uriBuilder.port(WebSocketServer.getInstance().getPort()); final URI uriToWebsocketServer = port.replacePath(streamName).build(); return Response.status(Status.OK).location(uriToWebsocketServer).build(); @@ -1190,9 +1239,9 @@ public class RestconfImpl implements RestconfService { if (!foundKey) { throw new RestconfDocumentedException( - "Missing key in URI \"" + listKey.getLocalName() + - "\" of list \"" + listSchemaNode.getQName().getLocalName() + "\"", - ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE ); + "Missing key '" + listKey.getLocalName() + + "' for list '" + listSchemaNode.getQName().getLocalName() + "' in the message body", + ErrorType.PROTOCOL, ErrorTag.DATA_MISSING ); } } }