Search in sources :

Example 1 with ResponseData

use of io.lighty.netconf.device.response.ResponseData 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 ResponseData

use of io.lighty.netconf.device.response.ResponseData 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 ResponseData

use of io.lighty.netconf.device.response.ResponseData in project lighty-netconf-simulator by PANTHEONtech.

the class NetworkTopologyServiceAbstractProcessor method execute.

@Override
protected CompletableFuture<Response> execute(final Element requestXmlElement) {
    try (Reader readerFromElement = RPCUtil.createReaderFromElement(requestXmlElement)) {
        final XmlNodeConverter xmlNodeConverter = getNetconfDeviceServices().getXmlNodeConverter();
        // 1. convert XML input into NormalizedNode
        final NormalizedNode deserializedNode = xmlNodeConverter.deserialize(Absolute.of(getRpcDefinition().getQName(), getRpcDefinition().getInput().getQName()), readerFromElement);
        // 2. convert NormalizedNode into RPC input
        final T input = convertToBindingAwareRpc(getRpcDefInputAbsolutePath(), (ContainerNode) deserializedNode);
        // 3. invoke RPC
        final RpcResult<O> rpcResult = execMethod(input);
        final DataContainer result = rpcResult.getResult();
        // 4. convert RPC output to ContainerNode
        final ContainerNode containerNode = this.adapterSerializer.toNormalizedNodeRpcData(result);
        // 5. create response
        final ResponseData responseData;
        if (containerNode.body().isEmpty()) {
            responseData = new ResponseData(Collections.emptyList());
        } else {
            responseData = new ResponseData(Collections.singletonList(containerNode));
        }
        return CompletableFuture.completedFuture(responseData);
    } catch (final ExecutionException | DeserializationException | TransformerException | TimeoutException | IOException e) {
        LOG.error("Error while executing RPC", e);
        return CompletableFuture.failedFuture(e);
    } catch (final InterruptedException e) {
        LOG.error("Interrupted while executing RPC", e);
        Thread.currentThread().interrupt();
        return CompletableFuture.failedFuture(e);
    }
}
Also used : ResponseData(io.lighty.netconf.device.response.ResponseData) Reader(java.io.Reader) IOException(java.io.IOException) DeserializationException(io.lighty.codecs.util.exception.DeserializationException) DataContainer(org.opendaylight.yangtools.yang.binding.DataContainer) XmlNodeConverter(io.lighty.codecs.util.XmlNodeConverter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ExecutionException(java.util.concurrent.ExecutionException) TransformerException(javax.xml.transform.TransformerException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with ResponseData

use of io.lighty.netconf.device.response.ResponseData in project lighty-netconf-simulator by PANTHEONtech.

the class ToasterServiceAbstractProcessor method execute.

@Override
protected CompletableFuture<Response> execute(final Element requestXmlElement) {
    // 1. convert XML input into NormalizedNode
    try (Reader readerFromElement = RPCUtil.createReaderFromElement(requestXmlElement)) {
        final XmlNodeConverter xmlNodeConverter = getNetconfDeviceServices().getXmlNodeConverter();
        final NormalizedNode deserializedNode = xmlNodeConverter.deserialize(Absolute.of(getRpcDefinition().getQName(), getRpcDefinition().getInput().getQName()), readerFromElement);
        // 2. convert NormalizedNode into RPC input
        final I input = convertToBindingAwareRpc(getRpcDefInputAbsolutePath(), (ContainerNode) deserializedNode);
        // 3. invoke RPC and wait for completion
        final Future<RpcResult<O>> invokeRpc = execMethod(input);
        final RpcResult<O> rpcResult = invokeRpc.get(TimeoutUtil.TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        // 4. convert RPC output to ContainerNode
        final ContainerNode data = this.adapterSerializer.toNormalizedNodeRpcData(rpcResult.getResult());
        // 5. create response
        return CompletableFuture.completedFuture(new ResponseData(Collections.singletonList(data)));
    } catch (final ExecutionException | DeserializationException | TransformerException | TimeoutException | IOException e) {
        LOG.error("Error while executing RPC", e);
        return CompletableFuture.failedFuture(e);
    } catch (final InterruptedException e) {
        LOG.error("Interrupted while executing RPC", e);
        Thread.currentThread().interrupt();
        return CompletableFuture.failedFuture(e);
    }
}
Also used : ResponseData(io.lighty.netconf.device.response.ResponseData) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) Reader(java.io.Reader) IOException(java.io.IOException) DeserializationException(io.lighty.codecs.util.exception.DeserializationException) XmlNodeConverter(io.lighty.codecs.util.XmlNodeConverter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ExecutionException(java.util.concurrent.ExecutionException) TransformerException(javax.xml.transform.TransformerException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with ResponseData

use of io.lighty.netconf.device.response.ResponseData in project lighty-netconf-simulator by PANTHEONtech.

the class CreateSubscriptionRequestProcessor method execute.

@Override
protected CompletableFuture<Response> execute(Element requestXmlElement) {
    messageId = requestXmlElement.getAttribute("message-id");
    final CompletableFuture<Response> responseFuture = new CompletableFuture<>();
    responseFuture.complete(new ResponseData(Collections.emptyList()));
    return responseFuture;
}
Also used : Response(io.lighty.netconf.device.response.Response) CompletableFuture(java.util.concurrent.CompletableFuture) ResponseData(io.lighty.netconf.device.response.ResponseData)

Aggregations

ResponseData (io.lighty.netconf.device.response.ResponseData)12 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)10 Response (io.lighty.netconf.device.response.Response)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 DeserializationException (io.lighty.codecs.util.exception.DeserializationException)8 XmlNodeConverter (io.lighty.codecs.util.XmlNodeConverter)7 Reader (java.io.Reader)7 TransformerException (javax.xml.transform.TransformerException)7 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)6 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)5 Element (org.w3c.dom.Element)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 DocumentedException (org.opendaylight.netconf.api.DocumentedException)4 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)4 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Device (org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.Device)2 Server (org.opendaylight.yang.gen.v1.urn.example.data.center.rev180807.Server)2