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;
12 import java.net.URISyntaxException;
13 import java.util.Iterator;
14 import java.util.Optional;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.transform.dom.DOMSource;
17 import org.opendaylight.netconf.api.DocumentedException;
18 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
19 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
20 import org.opendaylight.netconf.api.NetconfDocumentedException;
21 import org.opendaylight.netconf.api.xml.XmlElement;
22 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
23 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
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, SchemaInferenceStack.ofInstantiatedPath(
60 schemaContext.getCurrentContext(), schemaNode.getPath()).toInference());
62 xmlParser.traverse(new DOMSource(element.getDomElement()));
63 } catch (final XMLStreamException | URISyntaxException | IOException | SAXException 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<? extends 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(XMLNamespace.of(namespace)).iterator();
76 } catch (final IllegalArgumentException 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 String elementName = element.getName();
89 final Optional<DataSchemaNode> schemaNode = module.findDataChildByName(QName.create(module.getQNameModule(),
91 if (schemaNode.isEmpty()) {
92 throw new DocumentedException(
93 "Unable to find node " + elementName + " with namespace: " + namespace + "in module: "
95 ErrorType.APPLICATION,
96 ErrorTag.UNKNOWN_NAMESPACE,
100 return schemaNode.get();
103 protected static XmlElement extractTargetElement(final XmlElement operationElement, final String operationName)
104 throws DocumentedException {
105 final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
106 // Direct lookup instead of using XmlElement class due to performance
107 if (elementsByTagName.getLength() == 0) {
108 throw new DocumentedException("Missing target element", ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE,
109 ErrorSeverity.ERROR, ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element", operationName));
110 } else if (elementsByTagName.getLength() > 1) {
111 throw new DocumentedException("Multiple target elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
112 ErrorSeverity.ERROR);
114 return XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();