Rename netconf-server-mdsal packages
[netconf.git] / plugins / netconf-server-mdsal / src / main / java / org / opendaylight / netconf / server / mdsal / operations / AbstractEdit.java
1 /*
2  * Copyright (c) 2018 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.server.mdsal.operations;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.io.IOException;
12 import java.net.URISyntaxException;
13 import java.util.Iterator;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.transform.dom.DOMSource;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.netconf.api.DocumentedException;
18 import org.opendaylight.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.netconf.api.xml.XmlElement;
20 import org.opendaylight.netconf.server.mdsal.CurrentSchemaContext;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
22 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
23 import org.opendaylight.yangtools.yang.common.ErrorTag;
24 import org.opendaylight.yangtools.yang.common.ErrorType;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaTreeInference;
34 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.NodeList;
39 import org.xml.sax.SAXException;
40
41 abstract class AbstractEdit extends AbstractConfigOperation {
42     private static final Logger LOG = LoggerFactory.getLogger(AbstractEdit.class);
43     private static final String TARGET_KEY = "target";
44
45     final CurrentSchemaContext schemaContext;
46
47     AbstractEdit(final SessionIdType sessionId, final CurrentSchemaContext schemaContext) {
48         super(sessionId);
49         this.schemaContext = schemaContext;
50     }
51
52     static final void parseIntoNormalizedNode(final SchemaTreeInference inference, final XmlElement element,
53                                               final NormalizedNodeStreamWriter writer) throws DocumentedException {
54         final var path = inference.statementPath();
55         final var schemaNode = path.get(path.size() - 1);
56         if (!(schemaNode instanceof ContainerSchemaNode) && !(schemaNode instanceof ListSchemaNode)) {
57             // This should never happen since any edit operation on any other node type
58             // should not be possible nor makes sense
59             LOG.debug("DataNode from module is not ContainerSchemaNode nor ListSchemaNode, aborting..");
60             throw new UnsupportedOperationException("implement exception if parse fails");
61         }
62
63         final XmlParserStream xmlParser = XmlParserStream.create(writer, inference);
64         try {
65             xmlParser.traverse(new DOMSource(element.getDomElement()));
66         } catch (final XMLStreamException | URISyntaxException | IOException | SAXException ex) {
67             throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex,
68                 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
69         }
70     }
71
72     final SchemaTreeInference getSchemaNodeFromNamespace(final String namespace, final XmlElement element)
73             throws DocumentedException {
74         final XMLNamespace ns;
75         try {
76             ns = XMLNamespace.of(namespace);
77         } catch (final IllegalArgumentException e) {
78             throw new NetconfDocumentedException("Unable to create URI for namespace : " + namespace, e,
79                 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
80         }
81
82         // Returns module with newest revision since findModuleByNamespace returns a set of modules and we only
83         // need the newest one
84         final EffectiveModelContext ctx = schemaContext.getCurrentContext();
85         final Iterator<? extends @NonNull Module> it = ctx.findModules(ns).iterator();
86         if (!it.hasNext()) {
87             // No module is present with this namespace
88             throw new NetconfDocumentedException("Unable to find module by namespace: " + namespace,
89                 ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
90         }
91
92         final Module module = it.next();
93         final SchemaInferenceStack stack = SchemaInferenceStack.of(ctx);
94         final String elementName = element.getName();
95         try {
96             // FIXME: This is a bit suspect. The element is formed using XML encoding, hence it corresponds to
97             //        enterDataTree(). But then we use the result of this method to create a NormalizedNode tree,
98             //        which contains ChoiceNode. This needs to be tested with something like to following:
99             //
100             //        module mod {
101             //          choice foo {
102             //            case bar {
103             //              leaf baz {
104             //                type string;
105             //              }
106             //            }
107             //          }
108             //        }
109             stack.enterSchemaTree(QName.create(module.getQNameModule(), elementName));
110         } catch (IllegalArgumentException e) {
111             throw new DocumentedException(
112                 "Unable to find node " + elementName + " with namespace: " + namespace + " in module: " + module, e,
113                 ErrorType.APPLICATION, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
114         }
115
116         return stack.toSchemaTreeInference();
117     }
118
119     static final XmlElement extractTargetElement(final XmlElement operationElement, final String operationName)
120         throws DocumentedException {
121         final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
122         // Direct lookup instead of using XmlElement class due to performance
123         if (elementsByTagName.getLength() == 0) {
124             throw new DocumentedException("Missing target element", ErrorType.PROTOCOL, ErrorTag.MISSING_ATTRIBUTE,
125                 ErrorSeverity.ERROR, ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element", operationName));
126         } else if (elementsByTagName.getLength() > 1) {
127             throw new DocumentedException("Multiple target elements", ErrorType.RPC, ErrorTag.UNKNOWN_ATTRIBUTE,
128                 ErrorSeverity.ERROR);
129         } else {
130             return XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();
131         }
132     }
133 }