+ public static Node<?> toDomNode(Element xmlElement, Optional<DataSchemaNode> schema,
+ Optional<XmlCodecProvider> codecProvider) {
+ if (schema.isPresent()) {
+ return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
+ }
+ return toDomNode(xmlElement);
+ }
+
+ public static CompositeNode fromElement(Element xmlElement) {
+ CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
+ node.setQName(qNameFromElement(xmlElement));
+
+ return node.toInstance();
+ }
+
+ public static QName qNameFromElement(Element xmlElement) {
+ String namespace = xmlElement.getNamespaceURI();
+ String localName = xmlElement.getLocalName();
+ return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
+ }
+
+ private static Node<?> toNodeWithSchema(Element xmlElement, DataSchemaNode schema, XmlCodecProvider codecProvider) {
+ checkQName(xmlElement, schema.getQName());
+ if (schema instanceof DataNodeContainer) {
+ return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider);
+ } else if (schema instanceof LeafSchemaNode) {
+ return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider);
+ } else if (schema instanceof LeafListSchemaNode) {
+ return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider);
+
+ }
+ return null;
+ }
+
+ private static Node<?> toSimpleNodeWithType(Element xmlElement, LeafSchemaNode schema,
+ XmlCodecProvider codecProvider) {
+ TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
+ String text = xmlElement.getTextContent();
+ Object value;
+ if (codec != null) {
+ value = codec.deserialize(text);
+
+ } else {
+ value = xmlElement.getTextContent();
+ }
+
+ Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
+ return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
+ }
+
+ private static Node<?> toSimpleNodeWithType(Element xmlElement, LeafListSchemaNode schema,
+ XmlCodecProvider codecProvider) {
+ TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
+ String text = xmlElement.getTextContent();
+ Object value;
+ if (codec != null) {
+ value = codec.deserialize(text);
+
+ } else {
+ value = xmlElement.getTextContent();
+ }
+
+ Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
+ return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
+ }
+
+ private static Node<?> toCompositeNodeWithSchema(Element xmlElement, QName qName, DataNodeContainer schema,
+ XmlCodecProvider codecProvider) {
+ List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()));
+ Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
+ return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
+ }
+
+ public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
+
+ public static Optional<ModifyAction> getModifyOperationFromAttributes(Element xmlElement) {
+ Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
+ if(attributeNodeNS == null)
+ return Optional.absent();
+
+ ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
+ Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
+
+ return Optional.of(action);
+ }
+
+ private static void checkQName(Element xmlElement, QName qName) {
+ checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
+ checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
+ }
+
+ public static final Optional<DataSchemaNode> findFirstSchema(QName qname, Set<DataSchemaNode> dataSchemaNode) {
+ if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
+ for (DataSchemaNode dsn : dataSchemaNode) {
+ if (qname.isEqualWithoutRevision(dsn.getQName())) {
+ return Optional.<DataSchemaNode> of(dsn);
+ } else if (dsn instanceof ChoiceNode) {
+ for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
+ Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
+ if (foundDsn != null && foundDsn.isPresent()) {
+ return foundDsn;
+ }
+ }
+ }
+ }
+ }
+ return Optional.absent();
+ }
+
+ public static Node<?> toDomNode(Document doc) {
+ return toDomNode(doc.getDocumentElement());
+ }
+
+ private static Node<?> toDomNode(Element element) {
+ QName qname = qNameFromElement(element);
+
+ ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
+ NodeList nodes = element.getChildNodes();
+ boolean isSimpleObject = true;
+ String value = null;
+ for (int i = 0; i < nodes.getLength(); i++) {
+ org.w3c.dom.Node child = nodes.item(i);
+ if (child instanceof Element) {
+ isSimpleObject = false;
+ values.add(toDomNode((Element) child));
+ }
+ if (isSimpleObject && child instanceof org.w3c.dom.Text) {
+ value = element.getTextContent();
+ if (!Strings.isNullOrEmpty(value)) {
+ isSimpleObject = true;
+ }
+ }
+ }
+ if (isSimpleObject) {
+ return new SimpleNodeTOImpl<>(qname, null, value);
+ }
+ return ImmutableCompositeNode.create(qname, values.build());
+ }
+
+ public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
+ return forEachChild(element.getChildNodes(), new Function<Element, Optional<Node<?>>>() {
+
+ @Override
+ public Optional<Node<?>> apply(Element input) {
+ if (context.isPresent()) {
+ QName partialQName = qNameFromElement(input);
+ Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
+ if (schemaNode.isPresent()) {
+ return Optional.<Node<?>> fromNullable(//
+ toNodeWithSchema(input, schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER));
+ }
+ }
+ return Optional.<Node<?>> fromNullable(toDomNode(input));
+ }
+
+ });
+
+ }
+
+ /**
+ * Converts XML Document containing notification data from Netconf device to
+ * Data DOM Nodes. <br>
+ * By specification defined in <a
+ * href="http://tools.ietf.org/search/rfc6020#section-7.14">RFC 6020</a>
+ * there are xml elements containing notifications metadata, like eventTime
+ * or root notification element which specifies namespace for which is
+ * notification defined in yang model. Those elements MUST be stripped off
+ * notifications body. This method returns pure notification body which
+ * begins in element which is equal to notifications name defined in
+ * corresponding yang model. Rest of notification metadata are obfuscated,
+ * thus Data DOM contains only pure notification body.
+ *
+ * @param document
+ * XML Document containing notification body
+ * @param notifications
+ * Notifications Definition Schema
+ * @return Data DOM Nodes containing xml notification body definition or
+ * <code>null</code> if there is no NotificationDefinition with
+ * Element with equal notification QName defined in XML Document.
+ */
+ public static CompositeNode notificationToDomNodes(final Document document,
+ final Optional<Set<NotificationDefinition>> notifications) {
+ if (notifications.isPresent() && (document != null) && (document.getDocumentElement() != null)) {
+ final NodeList originChildNodes = document.getDocumentElement().getChildNodes();
+
+ for (int i = 0; i < originChildNodes.getLength(); i++) {
+ org.w3c.dom.Node child = originChildNodes.item(i);
+ if (child instanceof Element) {
+ final Element childElement = (Element) child;
+ final QName partialQName = qNameFromElement(childElement);
+ final Optional<NotificationDefinition> notificationDef = findNotification(partialQName,
+ notifications.get());
+ if (notificationDef.isPresent()) {
+ final Set<DataSchemaNode> dataNodes = notificationDef.get().getChildNodes();
+ final List<Node<?>> domNodes = toDomNodes(childElement,
+ Optional.<Set<DataSchemaNode>> fromNullable(dataNodes));
+ return ImmutableCompositeNode.create(notificationDef.get().getQName(), domNodes);
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ private static Optional<NotificationDefinition> findNotification(final QName notifName,
+ final Set<NotificationDefinition> notifications) {
+ if ((notifName != null) && (notifications != null)) {
+ for (final NotificationDefinition notification : notifications) {
+ if ((notification != null) && notifName.isEqualWithoutRevision(notification.getQName())) {
+ return Optional.<NotificationDefinition>fromNullable(notification);
+ }
+ }
+ }
+ return Optional.<NotificationDefinition>absent();
+ }
+
+ private static final <T> List<T> forEachChild(NodeList nodes, Function<Element, Optional<T>> forBody) {
+ ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
+ for (int i = 0; i < nodes.getLength(); i++) {
+ org.w3c.dom.Node child = nodes.item(i);
+ if (child instanceof Element) {
+ Optional<T> result = forBody.apply((Element) child);
+ if (result.isPresent()) {
+ ret.add(result.get());
+ }
+ }
+ }
+ return ret.build();
+ }
+