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
8 package org.opendaylight.netconf.mdsal.connector.ops;
10 import com.google.common.collect.ImmutableMap;
11 import java.io.IOException;
13 import java.net.URISyntaxException;
14 import java.util.Iterator;
16 import java.util.Optional;
17 import javax.xml.stream.XMLStreamException;
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.yangtools.yang.common.QName;
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.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;
39 abstract class AbstractEdit extends AbstractConfigOperation {
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 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");
59 final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext.getCurrentContext(), schemaNode);
61 xmlParser.traverse(new DOMSource(element.getDomElement()));
62 } catch (final XMLStreamException | URISyntaxException | IOException | SAXException ex) {
63 throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex, ErrorType.PROTOCOL,
64 ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
68 protected DataSchemaNode getSchemaNodeFromNamespace(final String namespace, final XmlElement element)
69 throws DocumentedException {
70 final Iterator<? extends Module> it;
72 // Returns module with newest revision since findModuleByNamespace returns a set of modules and we only
73 // need the newest one
74 it = schemaContext.getCurrentContext().findModules(new URI(namespace)).iterator();
75 } catch (final URISyntaxException e) {
76 throw new NetconfDocumentedException("Unable to create URI for namespace : " + namespace, e,
77 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
81 // No module is present with this namespace
82 throw new NetconfDocumentedException("Unable to find module by namespace: " + namespace,
83 ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
86 final Module module = it.next();
87 final String elementName = element.getName();
88 final Optional<DataSchemaNode> schemaNode = module.findDataChildByName(QName.create(module.getQNameModule(),
90 if (!schemaNode.isPresent()) {
91 throw new DocumentedException(
92 "Unable to find node " + elementName + " with namespace: " + namespace + "in module: "
94 ErrorType.APPLICATION,
95 ErrorTag.UNKNOWN_NAMESPACE,
99 return schemaNode.get();
102 protected static XmlElement extractTargetElement(final XmlElement operationElement, final String operationName)
103 throws DocumentedException {
104 final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
105 // Direct lookup instead of using XmlElement class due to performance
106 if (elementsByTagName.getLength() == 0) {
107 final Map<String, String> errorInfo = ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element",
109 throw new DocumentedException("Missing target element", ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE,
110 ErrorSeverity.ERROR, errorInfo);
111 } else if (elementsByTagName.getLength() > 1) {
112 throw new DocumentedException("Multiple target elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
113 ErrorSeverity.ERROR);
115 return XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();