use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.
the class RestconfDataServiceImpl method isEqualUriAndPayloadKeyValues.
private static void isEqualUriAndPayloadKeyValues(final Map<QName, Object> uriKeyValues, final MapEntryNode payload, final List<QName> keyDefinitions) {
final Map<QName, Object> mutableCopyUriKeyValues = new HashMap<>(uriKeyValues);
for (final QName keyDefinition : keyDefinitions) {
final Object uriKeyValue = RestconfDocumentedException.throwIfNull(mutableCopyUriKeyValues.remove(keyDefinition), ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "Missing key %s in URI.", keyDefinition);
final Object dataKeyValue = payload.getIdentifier().getValue(keyDefinition);
if (!uriKeyValue.equals(dataKeyValue)) {
final String errMsg = "The value '" + uriKeyValue + "' for key '" + keyDefinition.getLocalName() + "' specified in the URI doesn't match the value '" + dataKeyValue + "' specified in the message body. ";
throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
}
}
}
use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.
the class RestconfDataServiceImpl method readData.
@Override
public Response readData(final String identifier, final UriInfo uriInfo) {
final ReadDataParams readParams = QueryParams.newReadDataParams(uriInfo);
final EffectiveModelContext schemaContextRef = schemaContextHandler.get();
final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(identifier, schemaContextRef, Optional.of(mountPointService));
final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
// FIXME: this looks quite crazy, why do we even have it?
if (mountPoint == null && identifier != null && identifier.contains(STREAMS_PATH) && !identifier.contains(STREAM_PATH_PART)) {
createAllYangNotificationStreams(schemaContextRef, uriInfo);
}
final QueryParameters queryParams = QueryParams.newQueryParameters(readParams, instanceIdentifier);
final List<YangInstanceIdentifier> fieldPaths = queryParams.fieldPaths();
final RestconfStrategy strategy = getRestconfStrategy(mountPoint);
final NormalizedNode node;
if (fieldPaths != null && !fieldPaths.isEmpty()) {
node = ReadDataTransactionUtil.readData(readParams.content(), instanceIdentifier.getInstanceIdentifier(), strategy, readParams.withDefaults(), schemaContextRef, fieldPaths);
} else {
node = ReadDataTransactionUtil.readData(readParams.content(), instanceIdentifier.getInstanceIdentifier(), strategy, readParams.withDefaults(), schemaContextRef);
}
// FIXME: this is utter craziness, refactor it properly!
if (identifier != null && identifier.contains(STREAM_PATH) && identifier.contains(STREAM_ACCESS_PATH_PART) && identifier.contains(STREAM_LOCATION_PATH_PART)) {
final String value = (String) node.body();
final String streamName = value.substring(value.indexOf(NOTIFICATION_STREAM + '/'));
delegRestconfSubscrService.subscribeToStream(streamName, uriInfo);
}
if (node == null) {
throw new RestconfDocumentedException("Request could not be completed because the relevant data model content does not exist", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
}
switch(readParams.content()) {
case ALL:
case CONFIG:
final QName type = node.getIdentifier().getNodeType();
return Response.status(Status.OK).entity(NormalizedNodePayload.ofReadData(instanceIdentifier, node, queryParams)).header("ETag", '"' + type.getModule().getRevision().map(Revision::toString).orElse(null) + "-" + type.getLocalName() + '"').header("Last-Modified", FORMATTER.format(LocalDateTime.now(Clock.systemUTC()))).build();
default:
return Response.status(Status.OK).entity(NormalizedNodePayload.ofReadData(instanceIdentifier, node, queryParams)).build();
}
}
use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.
the class RestconfDataServiceImpl method validTopLevelNodeName.
/**
* Valid top level node name.
*
* @param path path of node
* @param payload data
*/
@VisibleForTesting
public static void validTopLevelNodeName(final YangInstanceIdentifier path, final NormalizedNodePayload payload) {
final QName dataNodeType = payload.getData().getIdentifier().getNodeType();
if (path.isEmpty()) {
if (!NETCONF_BASE_QNAME.equals(dataNodeType)) {
throw new RestconfDocumentedException("Instance identifier has to contain at least one path argument", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
}
} else {
final String identifierName = path.getLastPathArgument().getNodeType().getLocalName();
final String payloadName = dataNodeType.getLocalName();
if (!payloadName.equals(identifierName)) {
throw new RestconfDocumentedException("Payload name (" + payloadName + ") is different from identifier name (" + identifierName + ")", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
}
}
}
use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.
the class XmlPatchBodyReader method readValueNodes.
/**
* Read value nodes.
*
* @param element Element of current edit operation
* @param operation Name of current operation
* @return List of value elements
*/
private static List<Element> readValueNodes(@NonNull final Element element, @NonNull final PatchEditOperation operation) {
final Node valueNode = element.getElementsByTagName("value").item(0);
if (operation.isWithValue() && valueNode == null) {
throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
}
if (!operation.isWithValue() && valueNode != null) {
throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
}
if (valueNode == null) {
return null;
}
final List<Element> result = new ArrayList<>();
final NodeList childNodes = valueNode.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i) instanceof Element) {
result.add((Element) childNodes.item(i));
}
}
return result;
}
use of org.opendaylight.restconf.common.errors.RestconfDocumentedException in project netconf by opendaylight.
the class DeleteDataTransactionUtil method deleteData.
/**
* Delete data from DS via transaction.
*
* @param strategy object that perform the actual DS operations
* @return {@link Response}
*/
public static Response deleteData(final RestconfStrategy strategy, final YangInstanceIdentifier path) {
final RestconfTransaction transaction = strategy.prepareWriteExecution();
try {
transaction.delete(path);
} catch (RestconfDocumentedException e) {
// close transaction if any and pass exception further
transaction.cancel();
throw e;
}
final FluentFuture<? extends CommitInfo> future = transaction.commit();
final ResponseFactory response = new ResponseFactory(Status.NO_CONTENT);
// This method will close transactionChain if any
FutureCallbackTx.addCallback(future, DELETE_TX_TYPE, response, path);
return response.build();
}
Aggregations