use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.
the class RestconfDocumentedExceptionMapper method getResponseStatusCode.
/**
* Deriving of the status code from the thrown exception. At the first step, status code is tried to be read using
* {@link RestconfDocumentedException#getStatus()}. If it is {@code null}, status code will be derived from status
* codes appended to error entries (the first that will be found). If there are not any error entries,
* {@link RestconfDocumentedExceptionMapper#DEFAULT_STATUS_CODE} will be used.
*
* @param exception Thrown exception.
* @return Derived status code.
*/
private static Status getResponseStatusCode(final RestconfDocumentedException exception) {
final Status status = exception.getStatus();
if (status != null) {
// status code that is specified directly as field in exception has the precedence over error entries
return status;
}
final List<RestconfError> errors = exception.getErrors();
if (errors.isEmpty()) {
// server error
return DEFAULT_STATUS_CODE;
}
final Set<Status> allStatusCodesOfErrorEntries = errors.stream().map(restconfError -> ErrorTags.statusOf(restconfError.getErrorTag())).collect(Collectors.toCollection(LinkedHashSet::new));
// entries, we should create WARN message
if (allStatusCodesOfErrorEntries.size() > 1) {
LOG.warn("An unexpected error occurred during translation of exception {} to response: " + "Different status codes have been found in appended error entries: {}. The first error " + "entry status code is chosen for response.", exception, allStatusCodesOfErrorEntries);
}
return allStatusCodesOfErrorEntries.iterator().next();
}
use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.
the class JsonPatchStatusBodyWriter method reportErrors.
private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
jsonWriter.name("errors");
jsonWriter.beginObject();
jsonWriter.name("error");
jsonWriter.beginArray();
for (final RestconfError restconfError : errors) {
jsonWriter.beginObject();
jsonWriter.name("error-type").value(restconfError.getErrorType().elementBody());
jsonWriter.name("error-tag").value(restconfError.getErrorTag().elementBody());
// optional node
if (restconfError.getErrorPath() != null) {
jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
}
// optional node
if (restconfError.getErrorMessage() != null) {
jsonWriter.name("error-message").value(restconfError.getErrorMessage());
}
// optional node
if (restconfError.getErrorInfo() != null) {
jsonWriter.name("error-info").value(restconfError.getErrorInfo());
}
jsonWriter.endObject();
}
jsonWriter.endArray();
jsonWriter.endObject();
}
use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.
the class BrokerFacade method patchConfigurationDataWithinTransaction.
public PatchStatusContext patchConfigurationDataWithinTransaction(final PatchContext patchContext) throws Exception {
final DOMMountPoint mountPoint = patchContext.getInstanceIdentifierContext().getMountPoint();
// get new transaction and schema context on server or on mounted device
final EffectiveModelContext schemaContext;
final DOMDataTreeReadWriteTransaction patchTransaction;
if (mountPoint == null) {
schemaContext = patchContext.getInstanceIdentifierContext().getSchemaContext();
patchTransaction = this.domDataBroker.newReadWriteTransaction();
} else {
schemaContext = modelContext(mountPoint);
final Optional<DOMDataBroker> optional = mountPoint.getService(DOMDataBroker.class);
if (optional.isPresent()) {
patchTransaction = optional.get().newReadWriteTransaction();
} else {
// if mount point does not have broker it is not possible to continue and global error is reported
LOG.error("Http Patch {} has failed - device {} does not support broker service", patchContext.getPatchId(), mountPoint.getIdentifier());
return new PatchStatusContext(patchContext.getPatchId(), null, false, ImmutableList.of(new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier())));
}
}
final List<PatchStatusEntity> editCollection = new ArrayList<>();
List<RestconfError> editErrors;
boolean withoutError = true;
for (final PatchEntity patchEntity : patchContext.getData()) {
final PatchEditOperation operation = patchEntity.getOperation();
switch(operation) {
case CREATE:
if (withoutError) {
try {
postDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
} catch (final RestconfDocumentedException e) {
LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
editErrors = new ArrayList<>();
editErrors.addAll(e.getErrors());
editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
withoutError = false;
}
}
break;
case REPLACE:
if (withoutError) {
try {
putDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
} catch (final RestconfDocumentedException e) {
LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
editErrors = new ArrayList<>();
editErrors.addAll(e.getErrors());
editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
withoutError = false;
}
}
break;
case DELETE:
case REMOVE:
if (withoutError) {
try {
deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode());
editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
} catch (final RestconfDocumentedException e) {
LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
editErrors = new ArrayList<>();
editErrors.addAll(e.getErrors());
editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
withoutError = false;
}
}
break;
case MERGE:
if (withoutError) {
try {
mergeDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
} catch (final RestconfDocumentedException e) {
LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
editErrors = new ArrayList<>();
editErrors.addAll(e.getErrors());
editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
withoutError = false;
}
}
break;
default:
LOG.error("Unsupported http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
break;
}
}
// if errors then cancel transaction and return error status
if (!withoutError) {
patchTransaction.cancel();
return new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), false, null);
}
// if no errors commit transaction
final CountDownLatch waiter = new CountDownLatch(1);
final FluentFuture<? extends CommitInfo> future = patchTransaction.commit();
final PatchStatusContextHelper status = new PatchStatusContextHelper();
future.addCallback(new FutureCallback<CommitInfo>() {
@Override
public void onSuccess(final CommitInfo result) {
status.setStatus(new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), true, null));
waiter.countDown();
}
@Override
public void onFailure(final Throwable throwable) {
// if commit failed it is global error
LOG.error("Http Patch {} transaction commit has failed", patchContext.getPatchId());
status.setStatus(new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), false, ImmutableList.of(new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, throwable.getMessage()))));
waiter.countDown();
}
}, MoreExecutors.directExecutor());
waiter.await();
return status.getStatus();
}
use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.
the class PatchJsonBodyWriter method reportErrors.
private static void reportErrors(final List<RestconfError> errors, final JsonWriter jsonWriter) throws IOException {
jsonWriter.name("errors");
jsonWriter.beginObject();
jsonWriter.name("error");
jsonWriter.beginArray();
for (final RestconfError restconfError : errors) {
jsonWriter.beginObject();
jsonWriter.name("error-type").value(restconfError.getErrorType().elementBody());
jsonWriter.name("error-tag").value(restconfError.getErrorTag().elementBody());
// optional node
if (restconfError.getErrorPath() != null) {
jsonWriter.name("error-path").value(restconfError.getErrorPath().toString());
}
// optional node
if (restconfError.getErrorMessage() != null) {
jsonWriter.name("error-message").value(restconfError.getErrorMessage());
}
// optional node
if (restconfError.getErrorInfo() != null) {
jsonWriter.name("error-info").value(restconfError.getErrorInfo());
}
jsonWriter.endObject();
}
jsonWriter.endArray();
jsonWriter.endObject();
}
use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.
the class PatchXmlBodyWriter method reportErrors.
private static void reportErrors(final List<RestconfError> errors, final XMLStreamWriter writer) throws XMLStreamException {
writer.writeStartElement("errors");
for (final RestconfError restconfError : errors) {
writer.writeStartElement("error-type");
writer.writeCharacters(restconfError.getErrorType().elementBody());
writer.writeEndElement();
writer.writeStartElement("error-tag");
writer.writeCharacters(restconfError.getErrorTag().elementBody());
writer.writeEndElement();
// optional node
if (restconfError.getErrorPath() != null) {
writer.writeStartElement("error-path");
writer.writeCharacters(restconfError.getErrorPath().toString());
writer.writeEndElement();
}
// optional node
if (restconfError.getErrorMessage() != null) {
writer.writeStartElement("error-message");
writer.writeCharacters(restconfError.getErrorMessage());
writer.writeEndElement();
}
// optional node
if (restconfError.getErrorInfo() != null) {
writer.writeStartElement("error-info");
writer.writeCharacters(restconfError.getErrorInfo());
writer.writeEndElement();
}
}
writer.writeEndElement();
}
Aggregations