Merge "Edit todo comments"
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / ops / get / AbstractGet.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.mdsal.connector.ops.get;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Function;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Throwables;
15 import com.google.common.collect.Iterables;
16 import java.io.IOException;
17 import javax.xml.stream.XMLOutputFactory;
18 import javax.xml.stream.XMLStreamException;
19 import javax.xml.stream.XMLStreamWriter;
20 import javax.xml.transform.dom.DOMResult;
21 import org.opendaylight.controller.config.util.xml.DocumentedException;
22 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
23 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
24 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
25 import org.opendaylight.controller.config.util.xml.XmlElement;
26 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
27 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
28 import org.opendaylight.netconf.mdsal.connector.ops.Datastore;
29 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
38 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47
48 public abstract class AbstractGet extends AbstractSingletonNetconfOperation {
49
50     private static final Logger LOG = LoggerFactory.getLogger(AbstractGet.class);
51
52     protected static final String FILTER = "filter";
53     static final YangInstanceIdentifier ROOT = YangInstanceIdentifier.builder().build();
54     protected final CurrentSchemaContext schemaContext;
55     private final FilterContentValidator validator;
56
57     public AbstractGet(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext) {
58         super(netconfSessionIdForReporting);
59         this.schemaContext = schemaContext;
60         this.validator = new FilterContentValidator(schemaContext);
61     }
62
63     private static final XMLOutputFactory XML_OUTPUT_FACTORY;
64
65     static {
66         XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
67         XML_OUTPUT_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
68     }
69
70     protected Node transformNormalizedNode(final Document document, final NormalizedNode<?, ?> data, final YangInstanceIdentifier dataRoot) {
71
72         final DOMResult result = new DOMResult(document.createElement(XmlNetconfConstants.DATA_KEY));
73
74         final XMLStreamWriter xmlWriter = getXmlStreamWriter(result);
75
76         final NormalizedNodeStreamWriter nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
77                 schemaContext.getCurrentContext(), getSchemaPath(dataRoot));
78
79         final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter, true);
80
81         writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
82         return result.getNode();
83     }
84
85
86     private XMLStreamWriter getXmlStreamWriter(final DOMResult result) {
87         try {
88             return XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
89         } catch (final XMLStreamException e) {
90             throw new RuntimeException(e);
91         }
92     }
93
94     private static final Function<PathArgument, QName> PATH_ARG_TO_QNAME = new Function<YangInstanceIdentifier.PathArgument, QName>() {
95         @Override
96         public QName apply(final YangInstanceIdentifier.PathArgument input) {
97             return input.getNodeType();
98         }
99     };
100
101     private SchemaPath getSchemaPath(final YangInstanceIdentifier dataRoot) {
102         return SchemaPath.create(Iterables.transform(dataRoot.getPathArguments(), PATH_ARG_TO_QNAME), dataRoot.equals(ROOT));
103     }
104
105     private void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data) {
106         try {
107             if (data.getNodeType().equals(SchemaContext.NAME)) {
108                 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
109                     nnWriter.write(child);
110                 }
111             } else {
112                 nnWriter.write(data);
113             }
114             nnWriter.flush();
115             xmlWriter.flush();
116         } catch (XMLStreamException | IOException e) {
117             Throwables.propagate(e);
118         }
119     }
120
121     protected Element serializeNodeWithParentStructure(Document document, YangInstanceIdentifier dataRoot, NormalizedNode node) {
122         if (!dataRoot.equals(ROOT)) {
123             return (Element) transformNormalizedNode(document,
124                     ImmutableNodes.fromInstanceId(schemaContext.getCurrentContext(), dataRoot, node),
125                     ROOT);
126         }
127         return  (Element) transformNormalizedNode(document, node, ROOT);
128     }
129
130     /**
131      *
132      * @param operationElement operation element
133      * @return if Filter is present and not empty returns Optional of the InstanceIdentifier to the read location in datastore.
134      *          empty filter returns Optional.absent() which should equal an empty &lt;data/&gt; container in the response.
135      *         if filter is not present we want to read the entire datastore - return ROOT.
136      * @throws DocumentedException
137      */
138     protected Optional<YangInstanceIdentifier> getDataRootFromFilter(XmlElement operationElement) throws DocumentedException {
139         Optional<XmlElement> filterElement = operationElement.getOnlyChildElementOptionally(FILTER);
140         if (filterElement.isPresent()) {
141             if (filterElement.get().getChildElements().size() == 0) {
142                 return Optional.absent();
143             }
144             return Optional.of(getInstanceIdentifierFromFilter(filterElement.get()));
145         } else {
146             return Optional.of(ROOT);
147         }
148     }
149
150     @VisibleForTesting
151     protected YangInstanceIdentifier getInstanceIdentifierFromFilter(XmlElement filterElement) throws DocumentedException {
152
153         if (filterElement.getChildElements().size() != 1) {
154             throw new DocumentedException("Multiple filter roots not supported yet",
155                     ErrorType.application, ErrorTag.operation_not_supported, ErrorSeverity.error);
156         }
157
158         XmlElement element = filterElement.getOnlyChildElement();
159         return validator.validate(element);
160     }
161
162     protected static final class GetConfigExecution {
163
164         private final Optional<Datastore> datastore;
165         public GetConfigExecution(final Optional<Datastore> datastore) {
166             this.datastore = datastore;
167         }
168
169         public Optional<Datastore> getDatastore() {
170             return datastore;
171         }
172
173         static GetConfigExecution fromXml(final XmlElement xml, final String operationName) throws DocumentedException {
174             try {
175                 validateInputRpc(xml, operationName);
176             } catch (final DocumentedException e) {
177                 throw new DocumentedException("Incorrect RPC: " + e.getMessage(), e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
178             }
179
180             final Optional<Datastore> sourceDatastore;
181             try {
182                 sourceDatastore = parseSource(xml);
183             } catch (final DocumentedException e) {
184                 throw new DocumentedException("Get-config source attribute error: " + e.getMessage(), e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
185             }
186
187             return new GetConfigExecution(sourceDatastore);
188         }
189
190         private static Optional<Datastore> parseSource(final XmlElement xml) throws DocumentedException {
191             final Optional<XmlElement> sourceElement = xml.getOnlyChildElementOptionally(XmlNetconfConstants.SOURCE_KEY,
192                     XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
193
194             return  sourceElement.isPresent() ?
195                     Optional.of(Datastore.valueOf(sourceElement.get().getOnlyChildElement().getName())) : Optional.<Datastore>absent();
196         }
197
198         private static void validateInputRpc(final XmlElement xml, String operationName) throws DocumentedException{
199             xml.checkName(operationName);
200             xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
201         }
202
203     }
204
205 }