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