Search in sources :

Example 41 with DocumentedException

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

the class FilterContentValidator method validate.

/**
 * Validates filter content against this validator schema context. If the filter is valid,
 * method returns {@link YangInstanceIdentifier} of node which can be used as root for data selection.
 *
 * @param filterContent filter content
 * @return YangInstanceIdentifier
 * @throws DocumentedException if filter content validation failed
 */
public YangInstanceIdentifier validate(final XmlElement filterContent) throws DocumentedException {
    final XMLNamespace namespace;
    try {
        namespace = XMLNamespace.of(filterContent.getNamespace());
    } catch (final IllegalArgumentException e) {
        throw new RuntimeException("Wrong namespace in element + " + filterContent.toString(), e);
    }
    try {
        final Module module = schemaContext.getCurrentContext().findModules(namespace).iterator().next();
        final DataSchemaNode schema = getRootDataSchemaNode(module, namespace, filterContent.getName());
        final FilterTree filterTree = validateNode(filterContent, schema, new FilterTree(schema.getQName(), Type.OTHER, schema));
        return getFilterDataRoot(filterTree, filterContent, YangInstanceIdentifier.builder());
    } catch (final ValidationException e) {
        LOG.debug("Filter content isn't valid", e);
        throw new DocumentedException("Validation failed. Cause: " + e.getMessage(), e, ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
    }
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) DocumentedException(org.opendaylight.netconf.api.DocumentedException) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) Module(org.opendaylight.yangtools.yang.model.api.Module)

Example 42 with DocumentedException

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

the class CopyConfig method copyToUrl.

private void copyToUrl(final XmlElement urlElement, final XmlElement operationElement) throws DocumentedException {
    final String url = urlElement.getTextContent();
    if (!url.startsWith("file:")) {
        throw new DocumentedException("Unsupported <url> protocol: " + url, ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
    }
    // Read data from datastore:
    final XmlElement source = getSourceElement(operationElement).getOnlyChildElement();
    final ContainerNode data = readData(source);
    // Transform NN to XML:
    final Document document = operationElement.getDomElement().getOwnerDocument();
    final Node node = transformNormalizedNode(document, data);
    // Save XML to file:
    final String xml = XmlUtil.toString((Element) node);
    try {
        final Path file = Paths.get(new URI(url));
        Files.write(file, xml.getBytes(StandardCharsets.UTF_8));
    } catch (URISyntaxException | IllegalArgumentException e) {
        throw new DocumentedException("Invalid URI: " + url, e, ErrorType.RPC, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
    } catch (IOException e) {
        throw new DocumentedException("Failed to write : " + url, e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
    }
}
Also used : SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) Path(java.nio.file.Path) DocumentedException(org.opendaylight.netconf.api.DocumentedException) Node(org.w3c.dom.Node) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Document(org.w3c.dom.Document) URI(java.net.URI)

Example 43 with DocumentedException

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

the class EditConfig method handleWithNoSubsequentOperations.

@Override
protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
    final XmlElement targetElement = extractTargetElement(operationElement, OPERATION_NAME);
    final Datastore targetDatastore = Datastore.valueOf(targetElement.getName());
    if (targetDatastore == Datastore.running) {
        throw new DocumentedException("edit-config on running datastore is not supported", ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
    }
    final ModifyAction defaultAction = getDefaultOperation(operationElement);
    final XmlElement configElement = getConfigElement(operationElement);
    for (final XmlElement element : configElement.getChildElements()) {
        final SplittingNormalizedNodeMetadataStreamWriter writer = new SplittingNormalizedNodeMetadataStreamWriter(defaultAction);
        parseIntoNormalizedNode(getSchemaNodeFromNamespace(element.getNamespace(), element), element, writer);
        executeOperations(writer.getDataTreeChanges());
    }
    return document.createElement(XmlNetconfConstants.OK);
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) ModifyAction(org.opendaylight.netconf.api.ModifyAction) XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Example 44 with DocumentedException

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

the class EditConfig method executeChange.

private void executeChange(final DOMDataTreeReadWriteTransaction rwtx, final DataTreeChange change) throws DocumentedException {
    final YangInstanceIdentifier path = change.getPath();
    final NormalizedNode changeData = change.getChangeRoot();
    switch(change.getAction()) {
        case NONE:
            return;
        case MERGE:
            mergeParentMixin(rwtx, path, changeData);
            rwtx.merge(LogicalDatastoreType.CONFIGURATION, path, changeData);
            break;
        case CREATE:
            try {
                if (rwtx.exists(LogicalDatastoreType.CONFIGURATION, path).get()) {
                    throw new DocumentedException("Data already exists, cannot execute CREATE operation", ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, ErrorSeverity.ERROR);
                }
                mergeParentMixin(rwtx, path, changeData);
                rwtx.put(LogicalDatastoreType.CONFIGURATION, path, changeData);
            } catch (final InterruptedException | ExecutionException e) {
                LOG.warn("Read from datastore failed when trying to read data for create operation {}", change, e);
            }
            break;
        case REPLACE:
            mergeParentMixin(rwtx, path, changeData);
            rwtx.put(LogicalDatastoreType.CONFIGURATION, path, changeData);
            break;
        case DELETE:
            try {
                if (!rwtx.exists(LogicalDatastoreType.CONFIGURATION, path).get()) {
                    throw new DocumentedException("Data is missing, cannot execute DELETE operation", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, ErrorSeverity.ERROR);
                }
                rwtx.delete(LogicalDatastoreType.CONFIGURATION, path);
            } catch (final InterruptedException | ExecutionException e) {
                LOG.warn("Read from datastore failed when trying to read data for delete operation {}", change, e);
            }
            break;
        case REMOVE:
            rwtx.delete(LogicalDatastoreType.CONFIGURATION, path);
            break;
        default:
            LOG.warn("Unknown/not implemented operation, not executing");
    }
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ExecutionException(java.util.concurrent.ExecutionException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 45 with DocumentedException

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

the class CopyConfigTest method testTargetMissing.

@Test
public void testTargetMissing() {
    final DocumentedException e = assertThrows(DocumentedException.class, () -> copyConfig("messages/mapping/copyConfigs/copyConfig_no_target.xml"));
    assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
    assertEquals(e.getErrorTag(), ErrorTag.MISSING_ATTRIBUTE);
    assertEquals(e.getErrorType(), ErrorType.PROTOCOL);
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) Test(org.junit.Test)

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