Search in sources :

Example 21 with XmlElement

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

the class SimulatedGetConfig method loadInitialConfigXMLFile.

private static DataList loadInitialConfigXMLFile(final File file) {
    LOG.info("Loading initial config xml file: {}", file.getName());
    DataList configData = new DataList();
    try {
        Element element = XmlUtil.readXmlToElement(file);
        XmlElement xmlElement = XmlElement.fromDomElement(element);
        List<XmlElement> xmlElementList = xmlElement.getChildElements();
        configData.setConfigList(xmlElementList);
    } catch (IOException e) {
        LOG.info("IO exception loading xml file: {} ", e.getMessage());
    } catch (SAXException e) {
        LOG.info("SAXException {}", e.getMessage());
    }
    return configData;
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 22 with XmlElement

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

the class EXIParameters method fromXmlElement.

public static EXIParameters fromXmlElement(final XmlElement root) throws UnsupportedOption {
    final CodingMode coding;
    final NodeList alignmentElements = root.getElementsByTagName(EXI_PARAMETER_ALIGNMENT);
    if (alignmentElements.getLength() > 0) {
        final Element alignmentElement = (Element) alignmentElements.item(0);
        final String alignmentTextContent = alignmentElement.getTextContent().trim();
        switch(alignmentTextContent) {
            case EXI_PARAMETER_BYTE_ALIGNED:
                coding = CodingMode.BYTE_PACKED;
                break;
            case EXI_PARAMETER_COMPRESSED:
                coding = CodingMode.COMPRESSION;
                break;
            case EXI_PARAMETER_PRE_COMPRESSION:
                coding = CodingMode.PRE_COMPRESSION;
                break;
            case EXI_PARAMETER_BIT_PACKED:
                coding = CodingMode.BIT_PACKED;
                break;
            default:
                LOG.warn("Unexpected value in alignmentTextContent: {} , using default value", alignmentTextContent);
                coding = CodingMode.BIT_PACKED;
                break;
        }
    } else {
        coding = CodingMode.BIT_PACKED;
    }
    final FidelityOptions fidelity = FidelityOptions.createDefault();
    final NodeList fidelityElements = root.getElementsByTagName(EXI_PARAMETER_FIDELITY);
    if (fidelityElements.getLength() > 0) {
        final Element fidelityElement = (Element) fidelityElements.item(0);
        fidelity.setFidelity(FidelityOptions.FEATURE_DTD, fidelityElement.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0);
        fidelity.setFidelity(FidelityOptions.FEATURE_LEXICAL_VALUE, fidelityElement.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0);
        fidelity.setFidelity(FidelityOptions.FEATURE_COMMENT, fidelityElement.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0);
        fidelity.setFidelity(FidelityOptions.FEATURE_PI, fidelityElement.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0);
        fidelity.setFidelity(FidelityOptions.FEATURE_PREFIX, fidelityElement.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0);
    }
    final EXISchema schema;
    final NodeList schemaElements = root.getElementsByTagName(EXI_PARAMETER_SCHEMAS);
    if (schemaElements.getLength() > 0) {
        final Element schemaElement = (Element) schemaElements.item(0);
        final String schemaName = schemaElement.getTextContent().trim();
        schema = EXISchema.forOption(schemaName);
        checkArgument(schema != null, "Unsupported schema name %s", schemaName);
    } else {
        schema = EXISchema.NONE;
    }
    return new EXIParameters(coding, fidelity, schema);
}
Also used : CodingMode(org.opendaylight.netconf.shaded.exificient.core.CodingMode) NodeList(org.w3c.dom.NodeList) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) FidelityOptions(org.opendaylight.netconf.shaded.exificient.core.FidelityOptions)

Example 23 with XmlElement

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

the class NetconfHelloMessage method isHelloMessage.

private static boolean isHelloMessage(final Document document) {
    final XmlElement element = XmlElement.fromDomElement(document.getDocumentElement());
    if (!HELLO_TAG.equals(element.getName())) {
        return false;
    }
    final Optional<String> optNamespace = element.getNamespaceOptionally();
    // accept even if hello has no namespace
    return optNamespace.isEmpty() || XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(optNamespace.get());
}
Also used : XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Example 24 with XmlElement

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

the class NetconfMessageTransformer method toNotification.

@Override
public synchronized DOMNotification toNotification(final NetconfMessage message) {
    final Entry<Instant, XmlElement> stripped = NetconfMessageTransformUtil.stripNotification(message);
    final QName notificationNoRev;
    try {
        notificationNoRev = QName.create(stripped.getValue().getNamespace(), stripped.getValue().getName()).withoutRevision();
    } catch (final MissingNameSpaceException e) {
        throw new IllegalArgumentException("Unable to parse notification " + message + ", cannot find namespace", e);
    }
    Collection<? extends NotificationDefinition> notificationDefinitions = mappedNotifications.get(notificationNoRev);
    Element element = stripped.getValue().getDomElement();
    NestedNotificationInfo nestedNotificationInfo = null;
    if (notificationDefinitions.isEmpty()) {
        // check if notification is nested notification
        Optional<NestedNotificationInfo> nestedNotificationOptional = findNestedNotification(message, element);
        if (nestedNotificationOptional.isPresent()) {
            nestedNotificationInfo = nestedNotificationOptional.get();
            notificationDefinitions = Collections.singletonList(nestedNotificationInfo.notificationDefinition);
            element = (Element) nestedNotificationInfo.notificationNode;
        }
    }
    Preconditions.checkArgument(notificationDefinitions.size() > 0, "Unable to parse notification %s, unknown notification. Available notifications: %s", notificationDefinitions, mappedNotifications.keySet());
    final NotificationDefinition mostRecentNotification = getMostRecentNotification(notificationDefinitions);
    final ContainerNode content;
    try {
        final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
        final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
        final XmlParserStream xmlParser = XmlParserStream.create(writer, mountContext, SchemaInferenceStack.ofInstantiatedPath(mountContext.getEffectiveModelContext(), mostRecentNotification.getPath()).toInference(), strictParsing);
        xmlParser.traverse(new DOMSource(element));
        content = (ContainerNode) resultHolder.getResult();
    } catch (XMLStreamException | URISyntaxException | IOException | SAXException | UnsupportedOperationException e) {
        throw new IllegalArgumentException(String.format("Failed to parse notification %s", element), e);
    }
    if (nestedNotificationInfo != null) {
        return new NetconfDeviceTreeNotification(content, // FIXME: improve this to cache the path
        mostRecentNotification.getPath().asAbsolute(), stripped.getKey(), nestedNotificationInfo.domDataTreeIdentifier);
    }
    return new NetconfDeviceNotification(content, stripped.getKey());
}
Also used : XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) DOMSource(javax.xml.transform.dom.DOMSource) QName(org.opendaylight.yangtools.yang.common.QName) Instant(java.time.Instant) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) MissingNameSpaceException(org.opendaylight.netconf.api.xml.MissingNameSpaceException) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) NotificationDefinition(org.opendaylight.yangtools.yang.model.api.NotificationDefinition) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)

Example 25 with XmlElement

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

the class NetconfMessageTransformer method parseResult.

private NormalizedNode parseResult(final NetconfMessage message, final OperationDefinition operationDefinition) {
    final Optional<XmlElement> okResponseElement = XmlElement.fromDomDocument(message.getDocument()).getOnlyChildElementWithSameNamespaceOptionally("ok");
    if (operationDefinition.getOutput().getChildNodes().isEmpty()) {
        Preconditions.checkArgument(okResponseElement.isPresent(), "Unexpected content in response of rpc: %s, %s", operationDefinition.getQName(), message);
        return null;
    } else {
        if (okResponseElement.isPresent()) {
            LOG.debug("Received response <ok/> for RPC with defined Output");
            return null;
        }
        Element element = message.getDocument().getDocumentElement();
        try {
            final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
            final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
            final XmlParserStream xmlParser = XmlParserStream.create(writer, mountContext, // FIXME: we should have a cached inference here
            SchemaInferenceStack.ofInstantiatedPath(mountContext.getEffectiveModelContext(), operationDefinition.getOutput().getPath()).toInference(), strictParsing);
            xmlParser.traverse(new DOMSource(element));
            return resultHolder.getResult();
        } catch (XMLStreamException | URISyntaxException | IOException | SAXException e) {
            throw new IllegalArgumentException(String.format("Failed to parse RPC response %s", element), e);
        }
    }
}
Also used : XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) DOMSource(javax.xml.transform.dom.DOMSource) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

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