Integrate netconf-mapping-api into netconf-server
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / ops / RuntimeRpc.java
index 8159382c36b04eaeae6fc1718906e76727e608f1..ec8951e257f50e34c6ba7b3699cc5aae0c3d6484 100644 (file)
@@ -25,10 +25,10 @@ import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.xml.XmlElement;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.netconf.api.xml.XmlUtil;
-import org.opendaylight.netconf.mapping.api.HandlingPriority;
-import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
-import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
+import org.opendaylight.netconf.server.api.operations.AbstractSingletonNetconfOperation;
+import org.opendaylight.netconf.server.api.operations.HandlingPriority;
+import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
@@ -43,7 +43,6 @@ import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
 import org.slf4j.Logger;
@@ -55,9 +54,7 @@ import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
 public class RuntimeRpc extends AbstractSingletonNetconfOperation {
-
     private static final Logger LOG = LoggerFactory.getLogger(RuntimeRpc.class);
-
     private static final XMLOutputFactory XML_OUTPUT_FACTORY;
 
     static {
@@ -85,9 +82,8 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
             return HandlingPriority.CANNOT_HANDLE;
         }
 
-        getRpcDefinitionFromModule(module.get(), namespaceURI, netconfOperationName);
+        getRpcDefinitionFromModule(module.orElseThrow(), namespaceURI, netconfOperationName);
         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
-
     }
 
     @Override
@@ -138,7 +134,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
                     ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
         }
 
-        final Optional<RpcDefinition> rpcDefinitionOptional = getRpcDefinitionFromModule(moduleOptional.get(),
+        final Optional<RpcDefinition> rpcDefinitionOptional = getRpcDefinitionFromModule(moduleOptional.orElseThrow(),
                 namespaceURI, netconfOperationName);
 
         if (rpcDefinitionOptional.isEmpty()) {
@@ -148,7 +144,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
                     ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
         }
 
-        final RpcDefinition rpcDefinition = rpcDefinitionOptional.get();
+        final RpcDefinition rpcDefinition = rpcDefinitionOptional.orElseThrow();
         final ContainerNode inputNode = rpcToNNode(operationElement, rpcDefinition);
 
         final DOMRpcResult result;
@@ -157,21 +153,19 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
         } catch (final InterruptedException | ExecutionException e) {
             throw DocumentedException.wrap(e);
         }
-        if (result.getResult() == null) {
+        if (result.value() == null) {
             return XmlUtil.createElement(document, XmlNetconfConstants.OK,
                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
         }
-        return transformNormalizedNode(document, result.getResult(), rpcDefinition.getOutput().getPath());
+        return transformNormalizedNode(document, result.value(),
+                Absolute.of(rpcDefinition.getQName(), rpcDefinition.getOutput().getQName()));
     }
 
     @Override
-    public Document handle(final Document requestMessage,
-                           final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
-
+    public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation)
+            throws DocumentedException {
         final XmlElement requestElement = getRequestElementWithCheck(requestMessage);
-
         final Document document = XmlUtil.newDocument();
-
         final XmlElement operationElement = requestElement.getOnlyChildElement();
         final Map<String, Attr> attributes = requestElement.getAttributes();
 
@@ -200,7 +194,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
     }
 
     private Element transformNormalizedNode(final Document document, final NormalizedNode data,
-                                            final SchemaPath rpcOutputPath) {
+                                            final Absolute rpcOutputPath) {
         final DOMResult result = new DOMResult(document.createElement(XmlNetconfConstants.RPC_REPLY_KEY));
 
         final XMLStreamWriter xmlWriter = getXmlStreamWriter(result);
@@ -208,14 +202,15 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
         final NormalizedNodeStreamWriter nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
                 schemaContext.getCurrentContext(), rpcOutputPath);
 
-        final SchemaOrderedNormalizedNodeWriter nnWriter =
-                new SchemaOrderedNormalizedNodeWriter(nnStreamWriter, schemaContext.getCurrentContext(), rpcOutputPath);
+        final SchemaOrderedNormalizedNodeWriter nnWriter = new SchemaOrderedNormalizedNodeWriter(nnStreamWriter,
+                schemaContext.getCurrentContext(), rpcOutputPath);
 
         writeRootElement(xmlWriter, nnWriter, (ContainerNode) data);
         try {
             nnStreamWriter.close();
             xmlWriter.close();
         } catch (IOException | XMLStreamException e) {
+            // FIXME: throw DocumentedException
             LOG.warn("Error while closing streams", e);
         }
 
@@ -226,7 +221,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
         try {
             return XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
         } catch (final XMLStreamException e) {
-            throw new RuntimeException(e);
+            throw new IllegalStateException(e);
         }
     }
 
@@ -237,7 +232,8 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
             nnWriter.flush();
             xmlWriter.flush();
         } catch (XMLStreamException | IOException e) {
-            throw new RuntimeException(e);
+            // FIXME: throw DocumentedException
+            throw new IllegalStateException(e);
         }
     }
 
@@ -259,8 +255,8 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
         try {
             xmlParser.traverse(new DOMSource(element.getDomElement()));
         } catch (final XMLStreamException | URISyntaxException | IOException | SAXException ex) {
-            throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex, ErrorType.PROTOCOL,
-                    DocumentedException.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
+            throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex,
+                ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
         }
 
         return (ContainerNode) resultHolder.getResult();