Merge "Rename restconf-binding-parent -> restconf-parent"
[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
9 package org.opendaylight.netconf.mdsal.connector.ops;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Strings;
13 import com.google.common.collect.ImmutableMap;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.util.Iterator;
17 import java.util.Map;
18 import javax.xml.transform.dom.DOMSource;
19 import org.opendaylight.controller.config.util.xml.DocumentedException;
20 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
21 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
22 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
23 import org.opendaylight.controller.config.util.xml.XmlElement;
24 import org.opendaylight.netconf.api.NetconfDocumentedException;
25 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
26 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Element;
37 import org.w3c.dom.NodeList;
38
39 abstract class AbstractEdit extends AbstractSingletonNetconfOperation {
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     @SuppressWarnings("checkstyle:IllegalCatch")
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, schemaContext.getCurrentContext(), schemaNode);
61         try {
62             xmlParser.traverse(new DOMSource(element.getDomElement()));
63         } catch (final Exception ex) {
64             throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex, ErrorType.PROTOCOL,
65                 ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
66         }
67     }
68
69     protected DataSchemaNode getSchemaNodeFromNamespace(final String namespace, final XmlElement element)
70         throws DocumentedException {
71         final Iterator<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(new URI(namespace)).iterator();
76         } catch (final URISyntaxException 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 java.util.Optional<DataSchemaNode> schemaNode =
89             module.findDataChildByName(QName.create(module.getQNameModule(), element.getName()));
90         if (!schemaNode.isPresent()) {
91             throw new DocumentedException(
92                 "Unable to find node with namespace: " + namespace + "in module: " + module.toString(),
93                 ErrorType.APPLICATION,
94                 ErrorTag.UNKNOWN_NAMESPACE,
95                 ErrorSeverity.ERROR);
96         }
97
98         return schemaNode.get();
99     }
100
101     protected static Datastore extractTargetParameter(final XmlElement operationElement, final String operationName)
102         throws DocumentedException {
103         final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
104         // Direct lookup instead of using XmlElement class due to performance
105         if (elementsByTagName.getLength() == 0) {
106             final Map<String, String> errorInfo = ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element",
107                 operationName);
108             throw new DocumentedException("Missing target element", ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE,
109                 ErrorSeverity.ERROR, errorInfo);
110         } else if (elementsByTagName.getLength() > 1) {
111             throw new DocumentedException("Multiple target elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
112                 ErrorSeverity.ERROR);
113         } else {
114             final XmlElement targetChildNode =
115                 XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();
116             return Datastore.valueOf(targetChildNode.getName());
117         }
118     }
119
120     protected static XmlElement getElement(final XmlElement parent, final String elementName)
121         throws DocumentedException {
122         final Optional<XmlElement> childNode = parent.getOnlyChildElementOptionally(elementName);
123         if (!childNode.isPresent()) {
124             throw new DocumentedException(elementName + " element is missing",
125                 ErrorType.PROTOCOL,
126                 ErrorTag.MISSING_ELEMENT,
127                 ErrorSeverity.ERROR);
128         }
129
130         return childNode.get();
131     }
132
133     protected static NodeList getElementsByTagName(final XmlElement parent, final String key) throws
134         DocumentedException {
135         final Element domParent = parent.getDomElement();
136         final NodeList elementsByTagName;
137
138         if (Strings.isNullOrEmpty(domParent.getPrefix())) {
139             elementsByTagName = domParent.getElementsByTagName(key);
140         } else {
141             elementsByTagName = domParent.getElementsByTagNameNS(parent.getNamespace(), key);
142         }
143
144         return elementsByTagName;
145     }
146 }