Update MRI projects for Aluminium
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / ops / RuntimeRpc.java
index 00d9b81e4a729870c9c54efc24500e1df0e6d1a6..f29adff83a65e67463c3af912ef0689b290e2a4c 100644 (file)
@@ -5,35 +5,31 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.mdsal.connector.ops;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Throwables;
-import com.google.common.util.concurrent.CheckedFuture;
 import java.io.IOException;
 import java.net.URI;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Map;
-import javax.annotation.Nullable;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.dom.DOMSource;
-import org.opendaylight.controller.config.util.xml.DocumentedException;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
-import org.opendaylight.controller.config.util.xml.XmlElement;
-import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
+import org.opendaylight.mdsal.dom.api.DOMRpcService;
+import org.opendaylight.netconf.api.DocumentedException;
+import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
+import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
+import org.opendaylight.netconf.api.DocumentedException.ErrorType;
 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;
@@ -83,7 +79,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
     @Override
     protected HandlingPriority canHandle(final String netconfOperationName, final String namespace) {
         final URI namespaceURI = createNsUri(namespace);
-        final Optional<Module> module = getModule(namespaceURI);
+        final Optional<? extends Module> module = getModule(namespaceURI);
 
         if (!module.isPresent()) {
             LOG.debug("Cannot handle rpc: {}, {}", netconfOperationName, namespace);
@@ -106,9 +102,8 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
     }
 
     //this returns module with the newest revision if more then 1 module with same namespace is found
-    private Optional<Module> getModule(final URI namespaceURI) {
-        return Optional.fromNullable(
-                schemaContext.getCurrentContext().findModuleByNamespaceAndRevision(namespaceURI, null));
+    private Optional<? extends Module> getModule(final URI namespaceURI) {
+        return schemaContext.getCurrentContext().findModules(namespaceURI).stream().findFirst();
     }
 
     private static Optional<RpcDefinition> getRpcDefinitionFromModule(final Module module, final URI namespaceURI,
@@ -119,7 +114,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
                 return Optional.of(rpcDef);
             }
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     @Override
@@ -132,12 +127,12 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
             netconfOperationNamespace = operationElement.getNamespace();
         } catch (final DocumentedException e) {
             LOG.debug("Cannot retrieve netconf operation namespace from message due to ", e);
-            throw new DocumentedException("Cannot retrieve netconf operation namespace from message",
+            throw new DocumentedException("Cannot retrieve netconf operation namespace from message", e,
                     ErrorType.PROTOCOL, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
         }
 
         final URI namespaceURI = createNsUri(netconfOperationNamespace);
-        final Optional<Module> moduleOptional = getModule(namespaceURI);
+        final Optional<? extends Module> moduleOptional = getModule(namespaceURI);
 
         if (!moduleOptional.isPresent()) {
             throw new DocumentedException("Unable to find module in Schema Context with namespace and name : "
@@ -157,19 +152,19 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
 
         final RpcDefinition rpcDefinition = rpcDefinitionOptional.get();
         final SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(rpcDefinition.getQName()), true);
-        final NormalizedNode<?, ?> inputNode = rpcToNNode(operationElement, rpcDefinition.getInput());
+        final ContainerNode inputNode = rpcToNNode(operationElement, rpcDefinition.getInput());
 
-        final CheckedFuture<DOMRpcResult, DOMRpcException> rpcFuture = rpcService.invokeRpc(schemaPath, inputNode);
+        final DOMRpcResult result;
         try {
-            final DOMRpcResult result = rpcFuture.checkedGet();
-            if (result.getResult() == null) {
-                return XmlUtil.createElement(document, XmlNetconfConstants.OK,
-                        Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
-            }
-            return (Element) transformNormalizedNode(document, result.getResult(), rpcDefinition.getOutput().getPath());
-        } catch (final DOMRpcException e) {
+            result = rpcService.invokeRpc(schemaPath, inputNode).get();
+        } catch (final InterruptedException | ExecutionException e) {
             throw DocumentedException.wrap(e);
         }
+        if (result.getResult() == null) {
+            return XmlUtil.createElement(document, XmlNetconfConstants.OK,
+                Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
+        }
+        return (Element) transformNormalizedNode(document, result.getResult(), rpcDefinition.getOutput().getPath());
     }
 
     @Override
@@ -184,7 +179,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
         final Map<String, Attr> attributes = requestElement.getAttributes();
 
         final Element response = handle(document, operationElement, subsequentOperation);
-        final Element rpcReply = XmlUtil.createElement(document, XmlMappingConstants.RPC_REPLY_KEY,
+        final Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY,
                 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
 
         if (XmlElement.fromDomElement(response).hasNamespace()) {
@@ -209,7 +204,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
 
     private Node transformNormalizedNode(final Document document, final NormalizedNode<?, ?> data,
                                          final SchemaPath rpcOutputPath) {
-        final DOMResult result = new DOMResult(document.createElement(XmlMappingConstants.RPC_REPLY_KEY));
+        final DOMResult result = new DOMResult(document.createElement(XmlNetconfConstants.RPC_REPLY_KEY));
 
         final XMLStreamWriter xmlWriter = getXmlStreamWriter(result);
 
@@ -246,7 +241,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
             nnWriter.flush();
             xmlWriter.flush();
         } catch (XMLStreamException | IOException e) {
-            Throwables.propagate(e);
+            throw new RuntimeException(e);
         }
     }
 
@@ -258,13 +253,8 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
      * @return parsed rpc into normalized node, or null if input schema is null
      */
     @SuppressWarnings("checkstyle:IllegalCatch")
-    @Nullable
-    private NormalizedNode<?, ?> rpcToNNode(final XmlElement element, @Nullable final ContainerSchemaNode input)
-            throws DocumentedException {
-        if (input.getChildNodes().isEmpty()) {
-            return null;
-        }
-
+    private @Nullable ContainerNode rpcToNNode(final XmlElement element,
+            final @Nullable ContainerSchemaNode input) throws DocumentedException {
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
         final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext.getCurrentContext(), input);
@@ -276,7 +266,7 @@ public class RuntimeRpc extends AbstractSingletonNetconfOperation {
                     ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
         }
 
-        return resultHolder.getResult();
+        return (ContainerNode) resultHolder.getResult();
     }
 
 }