Search in sources :

Example 1 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project lighty-netconf-simulator by PANTHEONtech.

the class ResetActionProcessor method execute.

@SuppressWarnings({ "rawtypes", "unchecked", "checkstyle:IllegalCatch" })
@Override
protected CompletableFuture<Response> execute(final Element requestXmlElement, final ActionDefinition paramActionDefinition) {
    this.actionDefinition = paramActionDefinition;
    final XmlNodeConverter xmlNodeConverter = getNetconfDeviceServices().getXmlNodeConverter();
    try {
        final XmlElement xmlElement = XmlElement.fromDomElement(requestXmlElement);
        final Element actionElement = findInputElement(xmlElement, this.actionDefinition.getQName());
        final Reader readerFromElement = RPCUtil.createReaderFromElement(actionElement);
        final ContainerNode deserializedNode = (ContainerNode) xmlNodeConverter.deserialize(this.actionDefinition.getInput(), readerFromElement);
        final Input input = this.adapterSerializer.fromNormalizedNodeActionInput(Reset.class, deserializedNode);
        final String key = findNameElement(xmlElement);
        Preconditions.checkNotNull(key);
        final Class listItem = Server.class;
        final Identifier listKey = new ServerKey(key);
        final KeyedInstanceIdentifier<Server, ServerKey> keydIID = (KeyedInstanceIdentifier<Server, ServerKey>) InstanceIdentifier.create(Collections.singletonList(IdentifiableItem.of(listItem, listKey)));
        final ListenableFuture<RpcResult<Output>> outputFuture = this.resetAction.invoke(keydIID, input);
        final CompletableFuture<Response> completableFuture = new CompletableFuture<>();
        Futures.addCallback(outputFuture, new FutureCallback<RpcResult<Output>>() {

            @Override
            public void onSuccess(final RpcResult<Output> result) {
                final NormalizedNode domOutput = ResetActionProcessor.this.adapterSerializer.toNormalizedNodeActionOutput(Reset.class, result.getResult());
                final List<NormalizedNode> list = new ArrayList<>();
                list.add(domOutput);
                completableFuture.complete(new ResponseData(list));
            }

            @Override
            public void onFailure(final Throwable throwable) {
            }
        }, Executors.newSingleThreadExecutor());
        return completableFuture;
    } catch (final TransformerException | DocumentedException | DeserializationException e) {
        throw new RuntimeException(e);
    }
}
Also used : Server(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.Server) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) Reader(java.io.Reader) DeserializationException(io.lighty.codecs.util.exception.DeserializationException) ServerKey(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.ServerKey) Input(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.server.reset.Input) CompletableFuture(java.util.concurrent.CompletableFuture) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) KeyedInstanceIdentifier(org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier) Identifier(org.opendaylight.yangtools.yang.binding.Identifier) DocumentedException(org.opendaylight.netconf.api.DocumentedException) Output(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.server.reset.Output) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) Reset(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.server.Reset) TransformerException(javax.xml.transform.TransformerException) ResponseData(io.lighty.netconf.device.response.ResponseData) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) Response(io.lighty.netconf.device.response.Response) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) XmlNodeConverter(io.lighty.codecs.util.XmlNodeConverter) KeyedInstanceIdentifier(org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier)

Example 2 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project lighty-netconf-simulator by PANTHEONtech.

the class StartActionProcessor method execute.

@SuppressWarnings("checkstyle:IllegalCatch")
@Override
protected CompletableFuture<Response> execute(final Element requestXmlElement, final ActionDefinition paramActionDefinition) {
    this.actionDefinition = paramActionDefinition;
    final XmlNodeConverter xmlNodeConverter = getNetconfDeviceServices().getXmlNodeConverter();
    try {
        final XmlElement xmlElement = XmlElement.fromDomElement(requestXmlElement);
        final Element actionElement = findInputElement(xmlElement, this.actionDefinition.getQName());
        final Reader readerFromElement = RPCUtil.createReaderFromElement(actionElement);
        final ContainerNode deserializedNode = (ContainerNode) xmlNodeConverter.deserialize(this.actionDefinition.getInput(), readerFromElement);
        final Input input = this.adapterSerializer.fromNormalizedNodeActionInput(Start.class, deserializedNode);
        final ListenableFuture<RpcResult<Output>> outputFuture = this.startAction.invoke(InstanceIdentifier.create(Device.class), input);
        final CompletableFuture<Response> completableFuture = new CompletableFuture<>();
        Futures.addCallback(outputFuture, new FutureCallback<RpcResult<Output>>() {

            @Override
            public void onSuccess(final RpcResult<Output> result) {
                final NormalizedNode domOutput = StartActionProcessor.this.adapterSerializer.toNormalizedNodeActionOutput(Start.class, result.getResult());
                final List<NormalizedNode> list = new ArrayList<>();
                list.add(domOutput);
                completableFuture.complete(new ResponseData(list));
            }

            @Override
            public void onFailure(final Throwable throwable) {
            }
        }, Executors.newSingleThreadExecutor());
        return completableFuture;
    } catch (final TransformerException | DocumentedException | DeserializationException e) {
        throw new RuntimeException(e);
    }
}
Also used : Start(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.device.Start) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) Element(org.w3c.dom.Element) Reader(java.io.Reader) DeserializationException(io.lighty.codecs.util.exception.DeserializationException) Input(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.device.start.Input) CompletableFuture(java.util.concurrent.CompletableFuture) DocumentedException(org.opendaylight.netconf.api.DocumentedException) Output(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.device.start.Output) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) ArrayList(java.util.ArrayList) List(java.util.List) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) TransformerException(javax.xml.transform.TransformerException) Device(org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.Device) ResponseData(io.lighty.netconf.device.response.ResponseData) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) Response(io.lighty.netconf.device.response.Response) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) XmlNodeConverter(io.lighty.codecs.util.XmlNodeConverter)

Example 3 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class NetconfServerSessionListener method onMessage.

@SuppressWarnings("checkstyle:IllegalCatch")
@Override
public void onMessage(final NetconfServerSession session, final NetconfMessage netconfMessage) {
    try {
        Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
        // there is no validation since the document may contain yang schemas
        final NetconfMessage message = processDocument(netconfMessage, session);
        LOG.debug("Responding with message {}", message);
        session.sendMessage(message);
        monitoringSessionListener.onSessionEvent(SessionEvent.inRpcSuccess(session));
    } catch (final RuntimeException e) {
        // TODO: should send generic error or close session?
        LOG.error("Unexpected exception", e);
        session.onIncommingRpcFail();
        monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
        throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
    } catch (final DocumentedException e) {
        LOG.trace("Error occurred while processing message", e);
        session.onOutgoingRpcError();
        session.onIncommingRpcFail();
        monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
        monitoringSessionListener.onSessionEvent(SessionEvent.outRpcError(session));
        SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
    }
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) DocumentedException(org.opendaylight.netconf.api.DocumentedException)

Example 4 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class NetconfServerSessionListener method processDocument.

private NetconfMessage processDocument(final NetconfMessage netconfMessage, final NetconfServerSession session) throws DocumentedException {
    final Document incomingDocument = netconfMessage.getDocument();
    final Node rootNode = incomingDocument.getDocumentElement();
    if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
        final Document responseDocument = XmlUtil.newDocument();
        checkMessageId(rootNode);
        Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
        rpcReply = SubtreeFilter.applyRpcSubtreeFilter(incomingDocument, rpcReply);
        session.onIncommingRpcSuccess();
        responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
        return new NetconfMessage(responseDocument);
    } else {
        /*
             * Tag: unknown-element Error-type: rpc, protocol, application
             * Severity: error Error-info: <bad-element> : name of the
             * unexpected element Description: An unexpected element is present.
             */
        throw new DocumentedException("Unknown tag " + rootNode.getNodeName() + " in message:\n" + netconfMessage, ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT, ErrorSeverity.ERROR, ImmutableMap.of("bad-element", rootNode.getNodeName()));
    }
}
Also used : NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) DocumentedException(org.opendaylight.netconf.api.DocumentedException) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 5 with DocumentedException

use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.

the class SendErrorExceptionUtil method tryToCopyAttributes.

@SuppressWarnings("checkstyle:IllegalCatch")
private static void tryToCopyAttributes(final Document incommingDocument, final Document errorDocument, final DocumentedException sendErrorException) {
    try {
        final Element incommingRpc = incommingDocument.getDocumentElement();
        Preconditions.checkState(XmlNetconfConstants.RPC_KEY.equals(incommingRpc.getLocalName()) && XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(incommingRpc.getNamespaceURI()), "Missing %s element", XmlNetconfConstants.RPC_KEY);
        final Element rpcReply = errorDocument.getDocumentElement();
        Preconditions.checkState(rpcReply.getTagName().equals(XmlNetconfConstants.RPC_REPLY_KEY), "Missing %s element", XmlNetconfConstants.RPC_REPLY_KEY);
        final NamedNodeMap incomingAttributes = incommingRpc.getAttributes();
        for (int i = 0; i < incomingAttributes.getLength(); i++) {
            final Attr attr = (Attr) incomingAttributes.item(i);
            // skip namespace
            if (attr.getNodeName().equals(XMLConstants.XMLNS_ATTRIBUTE)) {
                continue;
            }
            rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
        }
    } catch (final Exception e) {
        LOG.warn("Unable to copy incomming attributes to {}, returned rpc-error might be invalid for client", sendErrorException, e);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr) DocumentedException(org.opendaylight.netconf.api.DocumentedException)

Aggregations

DocumentedException (org.opendaylight.netconf.api.DocumentedException)54 Test (org.junit.Test)25 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)14 Document (org.w3c.dom.Document)9 NetconfDocumentedException (org.opendaylight.netconf.api.NetconfDocumentedException)6 ExecutionException (java.util.concurrent.ExecutionException)5 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)5 Element (org.w3c.dom.Element)5 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)4 List (java.util.List)3 XMLNamespace (org.opendaylight.yangtools.yang.common.XMLNamespace)3 XmlNodeConverter (io.lighty.codecs.util.XmlNodeConverter)2 DeserializationException (io.lighty.codecs.util.exception.DeserializationException)2 Response (io.lighty.netconf.device.response.Response)2 ResponseData (io.lighty.netconf.device.response.ResponseData)2 Reader (java.io.Reader)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CompletableFuture (java.util.concurrent.CompletableFuture)2