Bump MRI upstreams
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / ops / AbstractEdit.java
1 /*
2  * Copyright (c) 2018 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 package org.opendaylight.netconf.mdsal.connector.ops;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.io.IOException;
12 import java.net.URISyntaxException;
13 import java.util.Iterator;
14 import java.util.Optional;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.transform.dom.DOMSource;
17 import org.opendaylight.netconf.api.DocumentedException;
18 import org.opendaylight.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.netconf.api.xml.XmlElement;
20 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
21 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.common.ErrorType;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.XMLNamespace;
26 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
27 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.NodeList;
37 import org.xml.sax.SAXException;
38
39 abstract class AbstractEdit extends AbstractConfigOperation {
40     private static final Logger LOG = LoggerFactory.getLogger(AbstractEdit.class);
41     private static final String TARGET_KEY = "target";
42
43     protected final CurrentSchemaContext schemaContext;
44
45     protected AbstractEdit(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext) {
46         super(netconfSessionIdForReporting);
47         this.schemaContext = schemaContext;
48     }
49
50     protected void parseIntoNormalizedNode(final DataSchemaNode schemaNode, final XmlElement element,
51                                            final NormalizedNodeStreamWriter writer) throws DocumentedException {
52         if (!(schemaNode instanceof ContainerSchemaNode) && !(schemaNode instanceof ListSchemaNode)) {
53             // This should never happen since any edit operation on any other node type
54             // should not be possible nor makes sense
55             LOG.debug("DataNode from module is not ContainerSchemaNode nor ListSchemaNode, aborting..");
56             throw new UnsupportedOperationException("implement exception if parse fails");
57         }
58
59         final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.ofInstantiatedPath(
60             schemaContext.getCurrentContext(), schemaNode.getPath()).toInference());
61         try {
62             xmlParser.traverse(new DOMSource(element.getDomElement()));
63         } catch (final XMLStreamException | URISyntaxException | IOException | SAXException ex) {
64             throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex,
65                 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
66         }
67     }
68
69     protected DataSchemaNode getSchemaNodeFromNamespace(final String namespace, final XmlElement element)
70         throws DocumentedException {
71         final Iterator<? extends Module> it;
72         try {
73             // Returns module with newest revision since findModuleByNamespace returns a set of modules and we only
74             // need the newest one
75             it = schemaContext.getCurrentContext().findModules(XMLNamespace.of(namespace)).iterator();
76         } catch (final IllegalArgumentException e) {
77             throw new NetconfDocumentedException("Unable to create URI for namespace : " + namespace, e,
78                 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
79         }
80
81         if (!it.hasNext()) {
82             // No module is present with this namespace
83             throw new NetconfDocumentedException("Unable to find module by namespace: " + namespace,
84                 ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
85         }
86
87         final Module module = it.next();
88         final String elementName = element.getName();
89         final Optional<DataSchemaNode> schemaNode = module.findDataChildByName(QName.create(module.getQNameModule(),
90                     element.getName()));
91         if (schemaNode.isEmpty()) {
92             throw new DocumentedException(
93                 "Unable to find node " + elementName + " with namespace: " + namespace + " in module: " + module,
94                 ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
95         }
96
97         return schemaNode.get();
98     }
99
100     protected static XmlElement extractTargetElement(final XmlElement operationElement, final String operationName)
101         throws DocumentedException {
102         final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
103         // Direct lookup instead of using XmlElement class due to performance
104         if (elementsByTagName.getLength() == 0) {
105             throw new DocumentedException("Missing target element", ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE,
106                 ErrorSeverity.ERROR, ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element", operationName));
107         } else if (elementsByTagName.getLength() > 1) {
108             throw new DocumentedException("Multiple target elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
109                 ErrorSeverity.ERROR);
110         } else {
111             return XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();
112         }
113     }
114 }