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;
12 import java.net.URISyntaxException;
13 import java.util.Iterator;
15 import java.util.Optional;
16 import javax.xml.transform.dom.DOMSource;
17 import org.opendaylight.netconf.api.DocumentedException;
18 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
19 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
20 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
21 import org.opendaylight.netconf.api.NetconfDocumentedException;
22 import org.opendaylight.netconf.api.xml.XmlElement;
23 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
26 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.NodeList;
36 abstract class AbstractEdit extends AbstractConfigOperation {
37 private static final Logger LOG = LoggerFactory.getLogger(AbstractEdit.class);
38 private static final String TARGET_KEY = "target";
40 protected final CurrentSchemaContext schemaContext;
42 protected AbstractEdit(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext) {
43 super(netconfSessionIdForReporting);
44 this.schemaContext = schemaContext;
47 @SuppressWarnings("checkstyle:IllegalCatch")
48 protected void parseIntoNormalizedNode(final DataSchemaNode schemaNode, final XmlElement element,
49 final NormalizedNodeStreamWriter writer) throws DocumentedException {
50 if (!(schemaNode instanceof ContainerSchemaNode) && !(schemaNode instanceof ListSchemaNode)) {
51 // This should never happen since any edit operation on any other node type
52 // should not be possible nor makes sense
53 LOG.debug("DataNode from module is not ContainerSchemaNode nor ListSchemaNode, aborting..");
54 throw new UnsupportedOperationException("implement exception if parse fails");
57 final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext.getCurrentContext(), schemaNode);
59 xmlParser.traverse(new DOMSource(element.getDomElement()));
60 } catch (final Exception ex) {
61 throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex, ErrorType.PROTOCOL,
62 ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
66 protected DataSchemaNode getSchemaNodeFromNamespace(final String namespace, final XmlElement element)
67 throws DocumentedException {
68 final Iterator<? extends Module> it;
70 // Returns module with newest revision since findModuleByNamespace returns a set of modules and we only
71 // need the newest one
72 it = schemaContext.getCurrentContext().findModules(new URI(namespace)).iterator();
73 } catch (final URISyntaxException e) {
74 throw new NetconfDocumentedException("Unable to create URI for namespace : " + namespace, e,
75 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
79 // No module is present with this namespace
80 throw new NetconfDocumentedException("Unable to find module by namespace: " + namespace,
81 ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
84 final Module module = it.next();
85 final String elementName = element.getName();
86 final Optional<DataSchemaNode> schemaNode = module.findDataChildByName(QName.create(module.getQNameModule(),
88 if (!schemaNode.isPresent()) {
89 throw new DocumentedException(
90 "Unable to find node " + elementName + " with namespace: " + namespace + "in module: "
92 ErrorType.APPLICATION,
93 ErrorTag.UNKNOWN_NAMESPACE,
97 return schemaNode.get();
100 protected static XmlElement extractTargetElement(final XmlElement operationElement, final String operationName)
101 throws DocumentedException {
102 final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
103 // Direct lookup instead of using XmlElement class due to performance
104 if (elementsByTagName.getLength() == 0) {
105 final Map<String, String> errorInfo = ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element",
107 throw new DocumentedException("Missing target element", ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE,
108 ErrorSeverity.ERROR, errorInfo);
109 } else if (elementsByTagName.getLength() > 1) {
110 throw new DocumentedException("Multiple target elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
111 ErrorSeverity.ERROR);
113 return XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();