Search in sources :

Example 1 with XmlNodeConverter

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

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

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

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

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

the class NotificationOperation method sendMessage.

public void sendMessage(final Notification notificationMessage, final QName quName) {
    final List<NetconfSession> sessionList = this.sessions.get(quName.getLocalName());
    if (sessionList != null && !sessionList.isEmpty()) {
        final ContainerNode containerNode = this.adapterContext.currentSerializer().toNormalizedNodeNotification(notificationMessage);
        final Optional<? extends NotificationDefinition> notificationDefinition = ConverterUtils.loadNotification(this.effectiveModelContext, quName);
        final XmlNodeConverter xmlNodeConverter = new XmlNodeConverter(this.effectiveModelContext);
        if (notificationDefinition.isEmpty()) {
            throw new UnsupportedOperationException("Cannot load definition for QName: " + quName);
        }
        final Writer writer;
        try {
            writer = xmlNodeConverter.serializeRpc(Absolute.of(notificationDefinition.get().getQName()), containerNode);
            try (InputStream is = new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8))) {
                final DocumentBuilder builder = UntrustedXML.newDocumentBuilder();
                final Document notification = builder.parse(is);
                final Element body = notification.createElementNS(RPCUtil.CREATE_SUBSCRIPTION_NAMESPACE, "notification");
                final Element notificationElement = notification.getDocumentElement();
                final Element eventTime = notification.createElement("eventTime");
                final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                eventTime.setTextContent(dateFormat.format(new Date()));
                body.appendChild(eventTime);
                body.appendChild(notificationElement);
                final Document document = builder.newDocument();
                final org.w3c.dom.Node importNode = document.importNode(body, true);
                document.appendChild(importNode);
                final NetconfMessage netconfMessage = new NetconfMessage(document);
                LOG.debug("Sending notification message: {}", netconfMessage.toString());
                sessionList.forEach(session -> session.sendMessage(netconfMessage));
            } catch (IOException | SAXException e) {
                LOG.error("Failed to send notification message", e);
            }
        } catch (final SerializationException e) {
            LOG.error("Failed to serialize notification to xml", e);
        }
    }
}
Also used : NetconfSession(org.opendaylight.netconf.api.NetconfSession) SerializationException(io.lighty.codecs.util.exception.SerializationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.util.Date) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) Node(org.w3c.dom.Node) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) XmlNodeConverter(io.lighty.codecs.util.XmlNodeConverter) SimpleDateFormat(java.text.SimpleDateFormat) Writer(java.io.Writer)

Aggregations

XmlNodeConverter (io.lighty.codecs.util.XmlNodeConverter)8 DeserializationException (io.lighty.codecs.util.exception.DeserializationException)7 ResponseData (io.lighty.netconf.device.response.ResponseData)7 Reader (java.io.Reader)7 TransformerException (javax.xml.transform.TransformerException)7 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)7 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)7 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)5 Element (org.w3c.dom.Element)5 Response (io.lighty.netconf.device.response.Response)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 DocumentedException (org.opendaylight.netconf.api.DocumentedException)4 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)4 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 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