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