* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.mdsal.connector.ops.get;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
-import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import java.io.IOException;
+import java.util.Optional;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.dom.DOMResult;
-import org.opendaylight.controller.config.util.xml.DocumentedException;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
-import org.opendaylight.controller.config.util.xml.XmlElement;
+import org.opendaylight.netconf.api.DocumentedException;
+import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
+import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
+import org.opendaylight.netconf.api.DocumentedException.ErrorType;
+import org.opendaylight.netconf.api.xml.XmlElement;
import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
import org.opendaylight.netconf.mdsal.connector.ops.Datastore;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
protected Node transformNormalizedNode(final Document document, final NormalizedNode<?, ?> data,
final YangInstanceIdentifier dataRoot) {
+
final DOMResult result = new DOMResult(document.createElement(XmlNetconfConstants.DATA_KEY));
final XMLStreamWriter xmlWriter = getXmlStreamWriter(result);
final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter, true);
- writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
+ if (data instanceof ContainerNode) {
+ writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
+ } else if (data instanceof MapNode) {
+ writeRootElement(xmlWriter, nnWriter, (MapNode) data);
+ } else {
+ throw new IllegalArgumentException("Unable to transform node of type: " + data.getClass().toString()
+ + " offending node: " + data.toString());
+ }
+
return result.getNode();
}
- private XMLStreamWriter getXmlStreamWriter(final DOMResult result) {
+ private static XMLStreamWriter getXmlStreamWriter(final DOMResult result) {
try {
return XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
} catch (final XMLStreamException e) {
}
}
- private SchemaPath getSchemaPath(final YangInstanceIdentifier dataRoot) {
+ private static SchemaPath getSchemaPath(final YangInstanceIdentifier dataRoot) {
return SchemaPath.create(
Iterables.transform(dataRoot.getPathArguments(), PathArgument::getNodeType), dataRoot.equals(ROOT));
}
- private void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter,
+ private static void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter,
final ContainerNode data) {
try {
if (data.getNodeType().equals(SchemaContext.NAME)) {
nnWriter.flush();
xmlWriter.flush();
} catch (XMLStreamException | IOException e) {
- Throwables.propagate(e);
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter,
+ final MapNode data) {
+ try {
+ if (data.getNodeType().equals(SchemaContext.NAME)) {
+ for (final MapEntryNode child : data.getValue()) {
+ nnWriter.write(child);
+ }
+ } else {
+ nnWriter.write(data);
+ }
+ nnWriter.flush();
+ xmlWriter.flush();
+ } catch (XMLStreamException | IOException e) {
+ throw new RuntimeException(e);
}
}
protected Element serializeNodeWithParentStructure(final Document document, final YangInstanceIdentifier dataRoot,
- final NormalizedNode node) {
+ final NormalizedNode<?, ?> node) {
if (!dataRoot.equals(ROOT)) {
return (Element) transformNormalizedNode(document,
ImmutableNodes.fromInstanceId(schemaContext.getCurrentContext(), dataRoot, node),
final Optional<XmlElement> filterElement = operationElement.getOnlyChildElementOptionally(FILTER);
if (filterElement.isPresent()) {
if (filterElement.get().getChildElements().size() == 0) {
- return Optional.absent();
+ return Optional.empty();
}
return Optional.of(getInstanceIdentifierFromFilter(filterElement.get()));
}
try {
validateInputRpc(xml, operationName);
} catch (final DocumentedException e) {
- throw new DocumentedException("Incorrect RPC: " + e.getMessage(), e.getErrorType(), e.getErrorTag(),
+ throw new DocumentedException("Incorrect RPC: " + e.getMessage(), e, e.getErrorType(), e.getErrorTag(),
e.getErrorSeverity(), e.getErrorInfo());
}
try {
sourceDatastore = parseSource(xml);
} catch (final DocumentedException e) {
- throw new DocumentedException("Get-config source attribute error: " + e.getMessage(), e.getErrorType(),
- e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
+ throw new DocumentedException("Get-config source attribute error: " + e.getMessage(), e,
+ e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
}
return new GetConfigExecution(sourceDatastore);
private static Optional<Datastore> parseSource(final XmlElement xml) throws DocumentedException {
final Optional<XmlElement> sourceElement = xml.getOnlyChildElementOptionally(XmlNetconfConstants.SOURCE_KEY,
XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
-
return sourceElement.isPresent()
? Optional.of(Datastore.valueOf(sourceElement.get().getOnlyChildElement().getName()))
- : Optional.<Datastore>absent();
+ : Optional.empty();
}
private static void validateInputRpc(final XmlElement xml, final String operationName) throws