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.collect.ImmutableMap;
13 import java.net.URISyntaxException;
14 import java.util.Iterator;
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<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 java.util.Optional<DataSchemaNode> schemaNode =
86 module.findDataChildByName(QName.create(module.getQNameModule(), element.getName()));
87 if (!schemaNode.isPresent()) {
88 throw new DocumentedException(
89 "Unable to find node with namespace: " + namespace + "in module: " + module.toString(),
90 ErrorType.APPLICATION,
91 ErrorTag.UNKNOWN_NAMESPACE,
95 return schemaNode.get();
98 protected static XmlElement extractTargetElement(final XmlElement operationElement, final String operationName)
99 throws DocumentedException {
100 final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
101 // Direct lookup instead of using XmlElement class due to performance
102 if (elementsByTagName.getLength() == 0) {
103 final Map<String, String> errorInfo = ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element",
105 throw new DocumentedException("Missing target element", ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE,
106 ErrorSeverity.ERROR, errorInfo);
107 } else if (elementsByTagName.getLength() > 1) {
108 throw new DocumentedException("Multiple target elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
109 ErrorSeverity.ERROR);
111 return XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();