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.collect.ImmutableMap;
14 import java.net.URISyntaxException;
15 import java.util.Iterator;
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.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.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.NodeList;
37 abstract class AbstractEdit extends AbstractConfigOperation {
38 private static final Logger LOG = LoggerFactory.getLogger(AbstractEdit.class);
39 private static final String TARGET_KEY = "target";
41 protected final CurrentSchemaContext schemaContext;
43 protected AbstractEdit(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext) {
44 super(netconfSessionIdForReporting);
45 this.schemaContext = schemaContext;
48 @SuppressWarnings("checkstyle:IllegalCatch")
49 protected void parseIntoNormalizedNode(final DataSchemaNode schemaNode, final XmlElement element,
50 final NormalizedNodeStreamWriter writer) throws DocumentedException {
51 if (!(schemaNode instanceof ContainerSchemaNode) && !(schemaNode instanceof ListSchemaNode)) {
52 // This should never happen since any edit operation on any other node type
53 // should not be possible nor makes sense
54 LOG.debug("DataNode from module is not ContainerSchemaNode nor ListSchemaNode, aborting..");
55 throw new UnsupportedOperationException("implement exception if parse fails");
58 final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext.getCurrentContext(), schemaNode);
60 xmlParser.traverse(new DOMSource(element.getDomElement()));
61 } catch (final Exception ex) {
62 throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex, ErrorType.PROTOCOL,
63 ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
67 protected DataSchemaNode getSchemaNodeFromNamespace(final String namespace, final XmlElement element)
68 throws DocumentedException {
69 final Iterator<Module> it;
71 // Returns module with newest revision since findModuleByNamespace returns a set of modules and we only
72 // need the newest one
73 it = schemaContext.getCurrentContext().findModules(new URI(namespace)).iterator();
74 } catch (final URISyntaxException e) {
75 throw new NetconfDocumentedException("Unable to create URI for namespace : " + namespace, e,
76 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
80 // No module is present with this namespace
81 throw new NetconfDocumentedException("Unable to find module by namespace: " + namespace,
82 ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
85 final Module module = it.next();
86 final java.util.Optional<DataSchemaNode> schemaNode =
87 module.findDataChildByName(QName.create(module.getQNameModule(), element.getName()));
88 if (!schemaNode.isPresent()) {
89 throw new DocumentedException(
90 "Unable to find node with namespace: " + namespace + "in module: " + module.toString(),
91 ErrorType.APPLICATION,
92 ErrorTag.UNKNOWN_NAMESPACE,
96 return schemaNode.get();
99 protected static Datastore extractTargetParameter(final XmlElement operationElement, final String operationName)
100 throws DocumentedException {
101 final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
102 // Direct lookup instead of using XmlElement class due to performance
103 if (elementsByTagName.getLength() == 0) {
104 final Map<String, String> errorInfo = ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element",
106 throw new DocumentedException("Missing target element", ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE,
107 ErrorSeverity.ERROR, errorInfo);
108 } else if (elementsByTagName.getLength() > 1) {
109 throw new DocumentedException("Multiple target elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
110 ErrorSeverity.ERROR);
112 final XmlElement targetChildNode =
113 XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();
114 return Datastore.valueOf(targetChildNode.getName());
118 protected static XmlElement getElement(final XmlElement parent, final String elementName)
119 throws DocumentedException {
120 final Optional<XmlElement> childNode = parent.getOnlyChildElementOptionally(elementName);
121 if (!childNode.isPresent()) {
122 throw new DocumentedException(elementName + " element is missing",
124 ErrorTag.MISSING_ELEMENT,
125 ErrorSeverity.ERROR);
128 return childNode.get();