2 * Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.mdsal.connector.ops;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Strings;
13 import com.google.common.collect.ImmutableMap;
15 import java.net.URISyntaxException;
16 import java.util.Iterator;
18 import javax.xml.transform.dom.DOMSource;
19 import org.opendaylight.netconf.api.DocumentedException;
20 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
21 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
22 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
23 import org.opendaylight.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.netconf.api.xml.XmlElement;
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;
39 abstract class AbstractEdit extends AbstractSingletonNetconfOperation {
40 private static final Logger LOG = LoggerFactory.getLogger(AbstractEdit.class);
41 private static final String TARGET_KEY = "target";
43 protected final CurrentSchemaContext schemaContext;
45 protected AbstractEdit(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext) {
46 super(netconfSessionIdForReporting);
47 this.schemaContext = schemaContext;
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");
60 final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext.getCurrentContext(), schemaNode);
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);
69 protected DataSchemaNode getSchemaNodeFromNamespace(final String namespace, final XmlElement element)
70 throws DocumentedException {
71 final Iterator<Module> it;
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);
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);
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,
98 return schemaNode.get();
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",
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);
114 final XmlElement targetChildNode =
115 XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();
116 return Datastore.valueOf(targetChildNode.getName());
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",
126 ErrorTag.MISSING_ELEMENT,
127 ErrorSeverity.ERROR);
130 return childNode.get();
133 protected static NodeList getElementsByTagName(final XmlElement parent, final String key) throws
134 DocumentedException {
135 final Element domParent = parent.getDomElement();
136 final NodeList elementsByTagName;
138 if (Strings.isNullOrEmpty(domParent.getPrefix())) {
139 elementsByTagName = domParent.getElementsByTagName(key);
141 elementsByTagName = domParent.getElementsByTagNameNS(parent.getNamespace(), key);
144 return elementsByTagName;