Search in sources :

Example 1 with DeserializationException

use of io.lighty.codecs.util.exception.DeserializationException 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 DeserializationException

use of io.lighty.codecs.util.exception.DeserializationException 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 DeserializationException

use of io.lighty.codecs.util.exception.DeserializationException 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 DeserializationException

use of io.lighty.codecs.util.exception.DeserializationException in project lighty-netconf-simulator by PANTHEONtech.

the class NetconfDeviceImpl method initDatastore.

@SuppressWarnings("checkstyle:AvoidHidingCauseException")
private void initDatastore(LogicalDatastoreType datastoreType, InputStream initialData) {
    LOG.debug("Setting up initial state of {} datastore from XML", datastoreType);
    try (Reader reader = new InputStreamReader(initialData, Charset.defaultCharset())) {
        NormalizedNode initialDataBI = netconfDeviceServices.getXmlNodeConverter().deserialize(netconfDeviceServices.getRootInference(), reader);
        DOMDataTreeWriteTransaction writeTx = netconfDeviceServices.getDOMDataBroker().newWriteOnlyTransaction();
        writeTx.put(datastoreType, YangInstanceIdentifier.empty(), initialDataBI);
        writeTx.commit().get(TimeoutUtil.TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        final String dataTreeString = NormalizedNodes.toStringTree(initialDataBI);
        LOG.trace("Initial {} datastore data: {}", datastoreType, dataTreeString);
    } catch (DeserializationException | IOException | ExecutionException | TimeoutException e) {
        throw new IllegalStateException(String.format("Unable to set initial state of %s datastore from XML!", datastoreType), e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException(String.format("Interrupted while setting initial state of %s datastore from XML!", datastoreType), e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DOMDataTreeWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) ExecutionException(java.util.concurrent.ExecutionException) DeserializationException(io.lighty.codecs.util.exception.DeserializationException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with DeserializationException

use of io.lighty.codecs.util.exception.DeserializationException 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)

Aggregations

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