Search in sources :

Example 1 with MissingNameSpaceException

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

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

the class SchemalessMessageTransformer method toNotification.

@Override
public DOMNotification toNotification(final NetconfMessage message) {
    final Map.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);
    }
    final DOMSourceAnyxmlNode notificationPayload = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(notificationNoRev)).withValue(new DOMSource(stripped.getValue().getDomElement())).build();
    final ContainerNode notificationBody = Builders.containerBuilder().withNodeIdentifier(SCHEMALESS_NOTIFICATION_PAYLOAD).withChild(notificationPayload).build();
    return new NetconfMessageTransformer.NetconfDeviceNotification(notificationBody, stripped.getKey());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) QName(org.opendaylight.yangtools.yang.common.QName) Instant(java.time.Instant) MissingNameSpaceException(org.opendaylight.netconf.api.xml.MissingNameSpaceException) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) Map(java.util.Map)

Example 3 with MissingNameSpaceException

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

Aggregations

MissingNameSpaceException (org.opendaylight.netconf.api.xml.MissingNameSpaceException)3 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)3 Instant (java.time.Instant)2 DOMSource (javax.xml.transform.dom.DOMSource)2 QName (org.opendaylight.yangtools.yang.common.QName)2 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)1 DOMSourceAnyxmlNode (org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode)1 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)1 XmlParserStream (org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream)1 ImmutableNormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter)1 NormalizedNodeResult (org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult)1 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)1 NotificationDefinition (org.opendaylight.yangtools.yang.model.api.NotificationDefinition)1 Element (org.w3c.dom.Element)1 SAXException (org.xml.sax.SAXException)1