+ private DataSchemaNode getSchemaNodeFromNamespace(final XmlElement element) throws DocumentedException {
+
+ try {
+ final Module module = schemaContext.getCurrentContext().findModuleByNamespaceAndRevision(new URI(element.getNamespace()), null);
+ DataSchemaNode dataSchemaNode = module.getDataChildByName(element.getName());
+ if (dataSchemaNode != null) {
+ return dataSchemaNode;
+ }
+ } catch (URISyntaxException e) {
+ LOG.debug("Error during parsing of element namespace, this should not happen since namespace of an xml " +
+ "element is valid and if the xml was parsed then the URI should be as well");
+ throw new IllegalArgumentException("Unable to parse element namespace, this should not happen since " +
+ "namespace of an xml element is valid and if the xml was parsed then the URI should be as well");
+ }
+ throw new DocumentedException("Unable to find node with namespace: " + element.getNamespace() + "in schema context: " + schemaContext.getCurrentContext().toString(),
+ ErrorType.application,
+ ErrorTag.unknown_namespace,
+ ErrorSeverity.error);
+ }
+
+ protected Element serializeNodeWithParentStructure(Document document, YangInstanceIdentifier dataRoot, NormalizedNode node) {
+ if (!dataRoot.equals(ROOT)) {
+ return (Element) transformNormalizedNode(document,
+ ImmutableNodes.fromInstanceId(schemaContext.getCurrentContext(), dataRoot, node),
+ ROOT);
+ }
+ return (Element) transformNormalizedNode(document, node, ROOT);
+ }
+
+ /**
+ *
+ * @param operationElement operation element
+ * @return if Filter is present and not empty returns Optional of the InstanceIdentifier to the read location in datastore.
+ * empty filter returns Optional.absent() which should equal an empty <data/> container in the response.
+ * if filter is not present we want to read the entire datastore - return ROOT.
+ * @throws DocumentedException
+ */
+ protected Optional<YangInstanceIdentifier> getDataRootFromFilter(XmlElement operationElement) throws DocumentedException {
+ Optional<XmlElement> filterElement = operationElement.getOnlyChildElementOptionally(FILTER);
+ if (filterElement.isPresent()) {
+ if (filterElement.get().getChildElements().size() == 0) {
+ return Optional.absent();
+ }
+ return Optional.of(getInstanceIdentifierFromFilter(filterElement.get()));
+ } else {
+ return Optional.of(ROOT);
+ }
+ }
+
+ @VisibleForTesting
+ protected YangInstanceIdentifier getInstanceIdentifierFromFilter(XmlElement filterElement) throws DocumentedException {
+
+ if (filterElement.getChildElements().size() != 1) {
+ throw new DocumentedException("Multiple filter roots not supported yet",
+ ErrorType.application, ErrorTag.operation_not_supported, ErrorSeverity.error);
+ }
+
+ XmlElement element = filterElement.getOnlyChildElement();
+ DataSchemaNode schemaNode = getSchemaNodeFromNamespace(element);
+
+ return getReadPointFromNode(YangInstanceIdentifier.builder().build(), filterToNormalizedNode(element, schemaNode));
+ }
+
+ private YangInstanceIdentifier getReadPointFromNode(final YangInstanceIdentifier pathArg, final NormalizedNode nNode) {
+ final YangInstanceIdentifier path = pathArg.node(nNode.getIdentifier());
+ if (nNode instanceof DataContainerNode) {
+ DataContainerNode node = (DataContainerNode) nNode;
+ if (node.getValue().size() == 1) {
+ return getReadPointFromNode(path, (NormalizedNode) Lists.newArrayList(node.getValue()).get(0));
+ }
+ }
+ return path;
+ }
+
+ private NormalizedNode filterToNormalizedNode(XmlElement element, DataSchemaNode schemaNode) throws DocumentedException {
+ DomToNormalizedNodeParserFactory parserFactory = DomToNormalizedNodeParserFactory
+ .getInstance(DomUtils.defaultValueCodecProvider(), schemaContext.getCurrentContext());
+
+ final NormalizedNode parsedNode;
+
+ if (schemaNode instanceof ContainerSchemaNode) {
+ parsedNode = parserFactory.getContainerNodeParser().parse(Collections.singletonList(element.getDomElement()), (ContainerSchemaNode) schemaNode);
+ } else if (schemaNode instanceof ListSchemaNode) {
+ parsedNode = parserFactory.getMapNodeParser().parse(Collections.singletonList(element.getDomElement()), (ListSchemaNode) schemaNode);
+ } else {
+ throw new DocumentedException("Schema node of the top level element is not an instance of container or list",
+ ErrorType.application, ErrorTag.unknown_element, ErrorSeverity.error);
+ }
+ return parsedNode;
+ }
+