* 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.net.URISyntaxException;
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;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
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.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
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.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
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);
}
//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,
return Optional.of(rpcDef);
}
}
- return Optional.absent();
+ return Optional.empty();
}
@Override
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 : "
}
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(rpcDefinition.getQName(), 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
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()) {
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);
nnWriter.flush();
xmlWriter.flush();
} catch (XMLStreamException | IOException e) {
- Throwables.propagate(e);
+ throw new RuntimeException(e);
}
}
* @param input input container schema node, or null if rpc does not take any input
* @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 InputSchemaNode input) throws DocumentedException {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext.getCurrentContext(), input);
try {
xmlParser.traverse(new DOMSource(element.getDomElement()));
- } catch (final Exception ex) {
+ } catch (final XMLStreamException | URISyntaxException | IOException | SAXException ex) {
throw new NetconfDocumentedException("Error parsing input: " + ex.getMessage(), ex, ErrorType.PROTOCOL,
ErrorTag.MALFORMED_MESSAGE, ErrorSeverity.ERROR);
}
- return resultHolder.getResult();
+ return (ContainerNode) resultHolder.getResult();
}
}