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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
}
Aggregations