Search in sources :

Example 41 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement 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 42 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class CopyConfig method copyToCandidate.

private void copyToCandidate(final XmlElement operationElement) throws DocumentedException {
    final XmlElement source = getSourceElement(operationElement);
    final List<XmlElement> configElements = getConfigElement(source).getChildElements();
    // <copy-config>, unlike <edit-config>, always replaces entire configuration,
    // so remove old configuration first:
    final DOMDataTreeReadWriteTransaction rwTx = transactionProvider.getOrCreateTransaction();
    rwTx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty(), EMPTY_ROOT_NODE);
    // Then create nodes present in the <config> element:
    for (final XmlElement element : configElements) {
        final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
        parseIntoNormalizedNode(getSchemaNodeFromNamespace(element.getNamespace(), element), element, ImmutableNormalizedNodeStreamWriter.from(resultHolder));
        final NormalizedNode data = resultHolder.getResult();
        final YangInstanceIdentifier path = YangInstanceIdentifier.create(data.getIdentifier());
        // Doing merge instead of put to support top-level list:
        rwTx.merge(LogicalDatastoreType.CONFIGURATION, path, data);
    }
}
Also used : DOMDataTreeReadWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)

Example 43 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement 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 XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class FilterContentValidator method validateNode.

/**
 * Recursively checks filter elements against the schema. Returns tree of nodes QNames as they appear in filter.
 *
 * @param element          element to check
 * @param parentNodeSchema parent node schema
 * @param tree             parent node tree
 * @return tree
 * @throws ValidationException if filter content is not valid
 */
private FilterTree validateNode(final XmlElement element, final DataSchemaNode parentNodeSchema, final FilterTree tree) throws ValidationException {
    final List<XmlElement> childElements = element.getChildElements();
    for (final XmlElement childElement : childElements) {
        try {
            final Deque<DataSchemaNode> path = findSchemaNodeByNameAndNamespace(parentNodeSchema, childElement.getName(), XMLNamespace.of(childElement.getNamespace()));
            if (path.isEmpty()) {
                throw new ValidationException(element, childElement);
            }
            FilterTree subtree = tree;
            for (final DataSchemaNode dataSchemaNode : path) {
                subtree = subtree.addChild(dataSchemaNode);
            }
            final DataSchemaNode childSchema = path.getLast();
            validateNode(childElement, childSchema, subtree);
        } catch (IllegalArgumentException | MissingNameSpaceException e) {
            throw new RuntimeException("Wrong namespace in element + " + childElement.toString(), e);
        }
    }
    return tree;
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) MissingNameSpaceException(org.opendaylight.netconf.api.xml.MissingNameSpaceException) XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Example 45 with XmlElement

use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.

the class AbstractConfigOperation method getElementsByTagName.

protected static NodeList getElementsByTagName(final XmlElement parent, final String key) throws DocumentedException {
    final Element domParent = parent.getDomElement();
    final NodeList elementsByTagName;
    if (Strings.isNullOrEmpty(domParent.getPrefix())) {
        elementsByTagName = domParent.getElementsByTagName(key);
    } else {
        elementsByTagName = domParent.getElementsByTagNameNS(parent.getNamespace(), key);
    }
    return elementsByTagName;
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList)

Aggregations

XmlElement (org.opendaylight.netconf.api.xml.XmlElement)50 Element (org.w3c.dom.Element)21 Document (org.w3c.dom.Document)16 DocumentedException (org.opendaylight.netconf.api.DocumentedException)13 Test (org.junit.Test)8 QName (org.opendaylight.yangtools.yang.common.QName)7 NodeList (org.w3c.dom.NodeList)6 DOMSource (javax.xml.transform.dom.DOMSource)5 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)4 URISyntaxException (java.net.URISyntaxException)3 DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)3 NormalizedNodeResult (org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult)3 SAXException (org.xml.sax.SAXException)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