Merge "Fixed discard-changes for mdsal netconf, mapping code cleanup."
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.netconf.mdsal.connector.ops.get;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Throwables;
14 import com.google.common.collect.Iterables;
15 import java.io.IOException;
16 import javax.xml.stream.XMLOutputFactory;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.stream.XMLStreamWriter;
19 import javax.xml.transform.dom.DOMResult;
20 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
21 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
22 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
23 import org.opendaylight.controller.netconf.mdsal.connector.ops.Datastore;
24 import org.opendaylight.controller.netconf.util.mapping.AbstractLastNetconfOperation;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
34 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Node;
38
39 public abstract class AbstractGet extends AbstractLastNetconfOperation {
40
41     protected static final YangInstanceIdentifier ROOT = YangInstanceIdentifier.builder().build();
42
43     protected final CurrentSchemaContext schemaContext;
44
45
46     public AbstractGet(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext) {
47         super(netconfSessionIdForReporting);
48         this.schemaContext = schemaContext;
49     }
50
51     private static final XMLOutputFactory XML_OUTPUT_FACTORY;
52
53     static {
54         XML_OUTPUT_FACTORY = XMLOutputFactory.newFactory();
55         XML_OUTPUT_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
56     }
57
58     protected Node transformNormalizedNode(final Document document, final NormalizedNode<?, ?> data, final YangInstanceIdentifier dataRoot) {
59
60         final DOMResult result = new DOMResult(document.createElement(XmlNetconfConstants.DATA_KEY));
61
62         final XMLStreamWriter xmlWriter = getXmlStreamWriter(result);
63
64         final NormalizedNodeStreamWriter nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
65                 schemaContext.getCurrentContext(), getSchemaPath(dataRoot));
66
67         final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter);
68
69         writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
70         return result.getNode();
71     }
72
73     private XMLStreamWriter getXmlStreamWriter(final DOMResult result) {
74         try {
75             return XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
76         } catch (final XMLStreamException e) {
77             throw new RuntimeException(e);
78         }
79     }
80
81     private static final Function<PathArgument, QName> PATH_ARG_TO_QNAME = new Function<YangInstanceIdentifier.PathArgument, QName>() {
82         @Override
83         public QName apply(final YangInstanceIdentifier.PathArgument input) {
84             return input.getNodeType();
85         }
86     };
87
88     private SchemaPath getSchemaPath(final YangInstanceIdentifier dataRoot) {
89         return SchemaPath.create(Iterables.transform(dataRoot.getPathArguments(), PATH_ARG_TO_QNAME), dataRoot.equals(ROOT));
90     }
91
92     // TODO this code is located in Restconf already
93     private void writeRootElement(final XMLStreamWriter xmlWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data) {
94         try {
95             for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
96                 nnWriter.write(child);
97             }
98             nnWriter.flush();
99             xmlWriter.flush();
100         } catch (XMLStreamException | IOException e) {
101             Throwables.propagate(e);
102         }
103     }
104
105     protected static final class GetConfigExecution {
106         private final Optional<Datastore> datastore;
107
108         public GetConfigExecution(final Optional<Datastore> datastore) {
109             this.datastore = datastore;
110         }
111
112         public Optional<Datastore> getDatastore() {
113             return datastore;
114         }
115
116         static GetConfigExecution fromXml(final XmlElement xml, final String operationName) throws NetconfDocumentedException {
117             try {
118                 validateInputRpc(xml, operationName);
119             } catch (final NetconfDocumentedException e) {
120                 throw new NetconfDocumentedException("Incorrect RPC: " + e.getMessage(), e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
121             }
122
123             final Optional<Datastore> sourceDatastore;
124             try {
125                 sourceDatastore = parseSource(xml);
126             } catch (final NetconfDocumentedException e) {
127                 throw new NetconfDocumentedException("Get-config source attribute error: " + e.getMessage(), e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
128             }
129
130             // Add filter
131
132             return new GetConfigExecution(sourceDatastore);
133         }
134
135         private static Optional<Datastore> parseSource(final XmlElement xml) throws NetconfDocumentedException {
136             final Optional<XmlElement> sourceElement = xml.getOnlyChildElementOptionally(XmlNetconfConstants.SOURCE_KEY,
137                     XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
138
139             return  sourceElement.isPresent() ?
140                     Optional.of(Datastore.valueOf(sourceElement.get().getOnlyChildElement().getName())) : Optional.<Datastore>absent();
141         }
142
143         private static void validateInputRpc(final XmlElement xml, String operationName) throws NetconfDocumentedException{
144             xml.checkName(operationName);
145             xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
146         }
147     }
148
149 }