2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.mdsal.connector.ops.get;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.annotations.VisibleForTesting;
13 import java.io.IOException;
14 import java.util.Optional;
15 import javax.xml.stream.XMLOutputFactory;
16 import javax.xml.stream.XMLStreamException;
17 import javax.xml.stream.XMLStreamWriter;
18 import javax.xml.transform.dom.DOMResult;
19 import org.opendaylight.netconf.api.DocumentedException;
20 import org.opendaylight.netconf.api.xml.XmlElement;
21 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
22 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
23 import org.opendaylight.netconf.mdsal.connector.ops.Datastore;
24 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
25 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
26 import org.opendaylight.yangtools.yang.common.ErrorTag;
27 import org.opendaylight.yangtools.yang.common.ErrorType;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.YangInstanceIdentifierWriter;
34 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.Node;
40 // FIXME: seal when we have JDK17+
41 public abstract class AbstractGet extends AbstractSingletonNetconfOperation {
42 private static final XMLOutputFactory XML_OUTPUT_FACTORY;
43 private static final String FILTER = "filter";
46 XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
47 XML_OUTPUT_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
50 // FIXME: hide this field
51 protected final CurrentSchemaContext schemaContext;
52 private final FilterContentValidator validator;
54 // FIXME: package-private
55 protected AbstractGet(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext) {
56 super(netconfSessionIdForReporting);
57 this.schemaContext = schemaContext;
58 validator = new FilterContentValidator(schemaContext);
61 // FIXME: hide this method
62 // FIXME: throw a DocumentedException
63 protected Node transformNormalizedNode(final Document document, final NormalizedNode data,
64 final YangInstanceIdentifier dataRoot) {
65 final DOMResult result = new DOMResult(document.createElement(XmlNetconfConstants.DATA_KEY));
66 final XMLStreamWriter xmlWriter = getXmlStreamWriter(result);
67 final EffectiveModelContext currentContext = schemaContext.getCurrentContext();
69 final NormalizedNodeStreamWriter nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
73 if (dataRoot.isEmpty()) {
74 writeRoot(nnStreamWriter, data);
76 write(nnStreamWriter, currentContext, dataRoot.coerceParent(), data);
78 } catch (IOException e) {
79 throw new RuntimeException(e);
82 return result.getNode();
85 private static void write(final NormalizedNodeStreamWriter nnStreamWriter,
86 final EffectiveModelContext currentContext, final YangInstanceIdentifier parent, final NormalizedNode data)
88 try (var yiidWriter = YangInstanceIdentifierWriter.open(nnStreamWriter, currentContext, parent)) {
89 try (var nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter, true)) {
95 private static void writeRoot(final NormalizedNodeStreamWriter nnStreamWriter, final NormalizedNode data)
97 checkArgument(data instanceof ContainerNode, "Unexpected root data %s", data);
99 try (var nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter, true)) {
100 for (var child : ((ContainerNode) data).body()) {
101 nnWriter.write(child);
106 private static XMLStreamWriter getXmlStreamWriter(final DOMResult result) {
108 return XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
109 } catch (final XMLStreamException e) {
110 throw new RuntimeException(e);
114 protected Element serializeNodeWithParentStructure(final Document document, final YangInstanceIdentifier dataRoot,
115 final NormalizedNode node) {
116 return (Element) transformNormalizedNode(document, node, dataRoot);
120 * Obtain data root according to filter from operation element.
122 * @param operationElement operation element
123 * @return if filter is present and not empty returns Optional of the InstanceIdentifier to the read location
124 * in datastore. Empty filter returns Optional.absent() which should equal an empty <data/>
125 * container in the response. If filter is not present we want to read the entire datastore - return ROOT.
126 * @throws DocumentedException if not possible to get identifier from filter
128 protected Optional<YangInstanceIdentifier> getDataRootFromFilter(final XmlElement operationElement)
129 throws DocumentedException {
130 final Optional<XmlElement> filterElement = operationElement.getOnlyChildElementOptionally(FILTER);
131 if (filterElement.isPresent()) {
132 if (filterElement.get().getChildElements().size() == 0) {
133 return Optional.empty();
135 return Optional.of(getInstanceIdentifierFromFilter(filterElement.get()));
138 return Optional.of(YangInstanceIdentifier.empty());
142 protected YangInstanceIdentifier getInstanceIdentifierFromFilter(final XmlElement filterElement)
143 throws DocumentedException {
145 if (filterElement.getChildElements().size() != 1) {
146 throw new DocumentedException("Multiple filter roots not supported yet",
147 ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
150 final XmlElement element = filterElement.getOnlyChildElement();
151 return validator.validate(element);
154 protected static final class GetConfigExecution {
155 private final Optional<Datastore> datastore;
157 GetConfigExecution(final Optional<Datastore> datastore) {
158 this.datastore = datastore;
161 static GetConfigExecution fromXml(final XmlElement xml, final String operationName) throws DocumentedException {
163 validateInputRpc(xml, operationName);
164 } catch (final DocumentedException e) {
165 throw new DocumentedException("Incorrect RPC: " + e.getMessage(), e, e.getErrorType(), e.getErrorTag(),
166 e.getErrorSeverity(), e.getErrorInfo());
169 final Optional<Datastore> sourceDatastore;
171 sourceDatastore = parseSource(xml);
172 } catch (final DocumentedException e) {
173 throw new DocumentedException("Get-config source attribute error: " + e.getMessage(), e,
174 e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
177 return new GetConfigExecution(sourceDatastore);
180 private static Optional<Datastore> parseSource(final XmlElement xml) throws DocumentedException {
181 final Optional<XmlElement> sourceElement = xml.getOnlyChildElementOptionally(XmlNetconfConstants.SOURCE_KEY,
182 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
183 return sourceElement.isPresent()
184 ? Optional.of(Datastore.valueOf(sourceElement.get().getOnlyChildElement().getName()))
188 private static void validateInputRpc(final XmlElement xml, final String operationName) throws
189 DocumentedException {
190 xml.checkName(operationName);
191 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
194 public Optional<Datastore> getDatastore() {