Search in sources :

Example 21 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class RestconfErrorTest method testRestConfDocumentedException_WithAppTag.

@Test
public void testRestConfDocumentedException_WithAppTag() {
    String expectedMessage = "Message";
    ErrorType expectedErrorType = ErrorType.RPC;
    ErrorTag expectedErrorTag = ErrorTag.IN_USE;
    String expectedErrorAppTag = "application.tag";
    RestconfError error = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag);
    validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, (String) null, error);
}
Also used : ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Test(org.junit.Test)

Example 22 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class QueryParamsTest method optionalParamMultipleTest.

/**
 * Test when parameter is present more than once.
 */
@Test
public void optionalParamMultipleTest() {
    final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, () -> QueryParams.optionalParam(ContentParam.uriName, List.of("config", "nonconfig", "all")));
    final List<RestconfError> errors = ex.getErrors();
    assertEquals(1, errors.size());
    final RestconfError error = errors.get(0);
    assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
    assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) Test(org.junit.Test)

Example 23 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class PatchDataTransactionUtil method patchData.

/**
 * Process edit operations of one {@link PatchContext}. Close {@link DOMTransactionChain} if any inside of object
 * {@link RestconfStrategy} provided as a parameter.
 *
 * @param context       Patch context to be processed
 * @param strategy      object that perform the actual DS operations
 * @param schemaContext Global schema context
 * @return {@link PatchStatusContext}
 */
public static PatchStatusContext patchData(final PatchContext context, final RestconfStrategy strategy, final EffectiveModelContext schemaContext) {
    final List<PatchStatusEntity> editCollection = new ArrayList<>();
    boolean noError = true;
    final RestconfTransaction transaction = strategy.prepareWriteExecution();
    for (final PatchEntity patchEntity : context.getData()) {
        if (noError) {
            switch(patchEntity.getOperation()) {
                case CREATE:
                    try {
                        createDataWithinTransaction(patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext, transaction);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, Lists.newArrayList(e.getErrors())));
                        noError = false;
                    }
                    break;
                case DELETE:
                    try {
                        deleteDataWithinTransaction(patchEntity.getTargetNode(), transaction);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, Lists.newArrayList(e.getErrors())));
                        noError = false;
                    }
                    break;
                case MERGE:
                    try {
                        mergeDataWithinTransaction(patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext, transaction);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, Lists.newArrayList(e.getErrors())));
                        noError = false;
                    }
                    break;
                case REPLACE:
                    try {
                        replaceDataWithinTransaction(patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext, transaction);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, Lists.newArrayList(e.getErrors())));
                        noError = false;
                    }
                    break;
                case REMOVE:
                    try {
                        removeDataWithinTransaction(patchEntity.getTargetNode(), transaction);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, Lists.newArrayList(e.getErrors())));
                        noError = false;
                    }
                    break;
                default:
                    editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, Lists.newArrayList(new RestconfError(ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED, "Not supported Yang Patch operation"))));
                    noError = false;
                    break;
            }
        } else {
            break;
        }
    }
    // if no errors then submit transaction, otherwise cancel
    if (noError) {
        final ResponseFactory response = new ResponseFactory(Status.OK);
        final FluentFuture<? extends CommitInfo> future = transaction.commit();
        try {
            FutureCallbackTx.addCallback(future, PATCH_TX_TYPE, response, null);
        } catch (final RestconfDocumentedException e) {
            // if errors occurred during transaction commit then patch failed and global errors are reported
            return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), false, Lists.newArrayList(e.getErrors()));
        }
        return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), true, null);
    } else {
        transaction.cancel();
        return new PatchStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), false, null);
    }
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) PatchStatusContext(org.opendaylight.restconf.common.patch.PatchStatusContext) PatchStatusEntity(org.opendaylight.restconf.common.patch.PatchStatusEntity) PatchEntity(org.opendaylight.restconf.common.patch.PatchEntity) ArrayList(java.util.ArrayList) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) RestconfTransaction(org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction)

Example 24 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class BrokerFacadeTest method test503.

@Test
public void test503() throws Exception {
    final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.TRANSPORT, ErrorTag.RESOURCE_DENIED.elementBody(), "Master is down. Please try again.");
    doReturn(immediateFailedFluentFuture(new ReadFailedException("Read from transaction failed", error))).when(readTransaction).read(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class));
    final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, () -> brokerFacade.readConfigurationData(this.instanceID, "explicit"));
    final List<RestconfError> errors = ex.getErrors();
    assertEquals(1, errors.size());
    assertEquals("getErrorTag", ErrorTags.RESOURCE_DENIED_TRANSPORT, errors.get(0).getErrorTag());
    assertEquals("getErrorType", ErrorType.TRANSPORT, errors.get(0).getErrorType());
    assertEquals("getErrorMessage", "Master is down. Please try again.", errors.get(0).getErrorMessage());
}
Also used : ReadFailedException(org.opendaylight.mdsal.common.api.ReadFailedException) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) RpcError(org.opendaylight.yangtools.yang.common.RpcError) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) LogicalDatastoreType(org.opendaylight.mdsal.common.api.LogicalDatastoreType) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) Test(org.junit.Test)

Example 25 with RestconfError

use of org.opendaylight.restconf.common.errors.RestconfError in project netconf by opendaylight.

the class XmlPatchStatusBodyWriter 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();
}
Also used : RestconfError(org.opendaylight.restconf.common.errors.RestconfError)

Aggregations

RestconfError (org.opendaylight.restconf.common.errors.RestconfError)30 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)19 Test (org.junit.Test)16 InputStream (java.io.InputStream)6 JerseyTest (org.glassfish.jersey.test.JerseyTest)4 ErrorTag (org.opendaylight.yangtools.yang.common.ErrorTag)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Response (javax.ws.rs.core.Response)3 ErrorType (org.opendaylight.yangtools.yang.common.ErrorType)3 MediaType (javax.ws.rs.core.MediaType)2 Status (javax.ws.rs.core.Response.Status)2 Ignore (org.junit.Ignore)2 PatchEntity (org.opendaylight.restconf.common.patch.PatchEntity)2 PatchStatusContext (org.opendaylight.restconf.common.patch.PatchStatusContext)2 PatchStatusEntity (org.opendaylight.restconf.common.patch.PatchStatusEntity)2 RpcError (org.opendaylight.yangtools.yang.common.RpcError)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1