Search in sources :

Example 51 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class CopyConfigTest method testConfigFromInvalidUrl.

@Test
public void testConfigFromInvalidUrl() {
    final DocumentedException e = assertThrows(DocumentedException.class, () -> copyConfig("messages/mapping/copyConfigs/copyConfig_invalid_url.xml"));
    assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
    assertEquals(e.getErrorTag(), ErrorTag.INVALID_VALUE);
    assertEquals(e.getErrorType(), ErrorType.APPLICATION);
    assertTrue(e.getCause() instanceof MalformedURLException);
}
Also used : MalformedURLException(java.net.MalformedURLException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) Test(org.junit.Test)

Example 52 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class AbstractEdit method getSchemaNodeFromNamespace.

final SchemaTreeInference getSchemaNodeFromNamespace(final String namespace, final XmlElement element) throws DocumentedException {
    final XMLNamespace ns;
    try {
        ns = XMLNamespace.of(namespace);
    } catch (final IllegalArgumentException e) {
        throw new NetconfDocumentedException("Unable to create URI for namespace : " + namespace, e, ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
    }
    // Returns module with newest revision since findModuleByNamespace returns a set of modules and we only
    // need the newest one
    final EffectiveModelContext ctx = schemaContext.getCurrentContext();
    final Iterator<? extends @NonNull Module> it = ctx.findModules(ns).iterator();
    if (!it.hasNext()) {
        // No module is present with this namespace
        throw new NetconfDocumentedException("Unable to find module by namespace: " + namespace, ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
    }
    final Module module = it.next();
    final SchemaInferenceStack stack = SchemaInferenceStack.of(ctx);
    final String elementName = element.getName();
    try {
        // FIXME: This is a bit suspect. The element is formed using XML encoding, hence it corresponds to
        // enterDataTree(). But then we use the result of this method to create a NormalizedNode tree,
        // which contains ChoiceNode. This needs to be tested with something like to following:
        // 
        // module mod {
        // choice foo {
        // case bar {
        // leaf baz {
        // type string;
        // }
        // }
        // }
        // }
        stack.enterSchemaTree(QName.create(module.getQNameModule(), elementName));
    } catch (IllegalArgumentException e) {
        throw new DocumentedException("Unable to find node " + elementName + " with namespace: " + namespace + " in module: " + module, e, ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
    }
    return stack.toSchemaTreeInference();
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) NonNull(org.eclipse.jdt.annotation.NonNull) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) Module(org.opendaylight.yangtools.yang.model.api.Module) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 53 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class NetconfMessageUtil method extractCapabilitiesFromHello.

public static Collection<String> extractCapabilitiesFromHello(final Document doc) {
    XmlElement responseElement = XmlElement.fromDomDocument(doc);
    // Extract child element <capabilities> from <hello> with or without(fallback) the same namespace
    Optional<XmlElement> capabilitiesElement = responseElement.getOnlyChildElementWithSameNamespaceOptionally(XmlNetconfConstants.CAPABILITIES);
    if (capabilitiesElement.isEmpty()) {
        capabilitiesElement = responseElement.getOnlyChildElementOptionally(XmlNetconfConstants.CAPABILITIES);
    }
    List<XmlElement> caps = capabilitiesElement.get().getChildElements(XmlNetconfConstants.CAPABILITY);
    return Collections2.transform(caps, input -> {
        // Trim possible leading/tailing whitespace
        try {
            return input.getTextContent().trim();
        } catch (DocumentedException e) {
            LOG.trace("Error fetching input text content", e);
            return null;
        }
    });
}
Also used : NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Example 54 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class FutureCallbackTx method addCallback.

/**
 * Add callback to the future object and close transaction chain.
 *
 * @param listenableFuture
 *             future object
 * @param txType
 *             type of operation (READ, POST, PUT, DELETE)
 * @param dataFactory
 *             factory setting result
 * @param path unique identifier of a particular node instance in the data tree
 * @throws RestconfDocumentedException
 *             if the Future throws an exception
 */
// FIXME: this is a *synchronous operation* and has to die
static <T> void addCallback(final ListenableFuture<T> listenableFuture, final String txType, final FutureDataFactory<? super T> dataFactory, final YangInstanceIdentifier path) throws RestconfDocumentedException {
    try {
        final T result = listenableFuture.get();
        dataFactory.setResult(result);
        LOG.trace("Transaction({}) SUCCESSFUL", txType);
    } catch (InterruptedException e) {
        dataFactory.setFailureStatus();
        LOG.warn("Transaction({}) FAILED!", txType, e);
        throw new RestconfDocumentedException("Transaction failed", e);
    } catch (ExecutionException e) {
        dataFactory.setFailureStatus();
        LOG.warn("Transaction({}) FAILED!", txType, e);
        final Throwable cause = e.getCause();
        if (cause instanceof TransactionCommitFailedException) {
            /* If device send some error message we want this message to get to client
                   and not just to throw it away or override it with new generic message.
                   We search for NetconfDocumentedException that was send from netconfSB
                   and we create RestconfDocumentedException accordingly.
                */
            final List<Throwable> causalChain = Throwables.getCausalChain(cause);
            for (Throwable error : causalChain) {
                if (error instanceof DocumentedException) {
                    final ErrorTag errorTag = ((DocumentedException) error).getErrorTag();
                    if (errorTag.equals(ErrorTag.DATA_EXISTS)) {
                        LOG.trace("Operation via Restconf was not executed because data at {} already exists", path);
                        throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, "Data already exists", path));
                    } else if (errorTag.equals(ErrorTag.DATA_MISSING)) {
                        LOG.trace("Operation via Restconf was not executed because data at {} does not exist", path);
                        throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "Data does not exist", path));
                    }
                }
                if (error instanceof NetconfDocumentedException) {
                    throw new RestconfDocumentedException(error.getMessage(), ((NetconfDocumentedException) error).getErrorType(), ((NetconfDocumentedException) error).getErrorTag(), e);
                }
            }
            throw new RestconfDocumentedException("Transaction(" + txType + ") not committed correctly", e);
        } else {
            throw new RestconfDocumentedException("Transaction failed", e);
        }
    }
}
Also used : TransactionCommitFailedException(org.opendaylight.mdsal.common.api.TransactionCommitFailedException) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) DocumentedException(org.opendaylight.netconf.api.DocumentedException) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

DocumentedException (org.opendaylight.netconf.api.DocumentedException)54 Test (org.junit.Test)25 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)14 Document (org.w3c.dom.Document)9 NetconfDocumentedException (org.opendaylight.netconf.api.NetconfDocumentedException)6 ExecutionException (java.util.concurrent.ExecutionException)5 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)5 Element (org.w3c.dom.Element)5 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)4 List (java.util.List)3 XMLNamespace (org.opendaylight.yangtools.yang.common.XMLNamespace)3 XmlNodeConverter (io.lighty.codecs.util.XmlNodeConverter)2 DeserializationException (io.lighty.codecs.util.exception.DeserializationException)2 Response (io.lighty.netconf.device.response.Response)2 ResponseData (io.lighty.netconf.device.response.ResponseData)2 Reader (java.io.Reader)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CompletableFuture (java.util.concurrent.CompletableFuture)2