use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class SettableRpc method handle.
@Override
public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
final XmlElement requestElement = XmlElement.fromDomDocument(requestMessage);
final XmlElement rpcElement = requestElement.getOnlyChildElement();
final String msgId = requestElement.getAttribute(XmlNetconfConstants.MESSAGE_ID);
final Optional<Document> response = rpcHandler.getResponse(rpcElement);
if (response.isPresent()) {
final Document document = response.get();
checkForError(document);
document.getDocumentElement().setAttribute(XmlNetconfConstants.MESSAGE_ID, msgId);
return document;
} else if (subsequentOperation.isExecutionTermination()) {
throw new DocumentedException("Mapping not found " + XmlUtil.toString(requestMessage), ErrorType.APPLICATION, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
} else {
return subsequentOperation.execute(requestMessage);
}
}
use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class Get method handle.
@SuppressWarnings("checkstyle:IllegalCatch")
@Override
public Document handle(final Document requestMessage, final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
if (subsequentOperation.isExecutionTermination()) {
throw new DocumentedException(String.format("Subsequent netconf operation expected by %s", this), ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
}
try {
final Document innerResult = subsequentOperation.execute(requestMessage);
final NetconfState netconfMonitoring = new NetconfState(netconfMonitor);
Element monitoringXmlElement = new JaxBSerializer().toXml(netconfMonitoring);
monitoringXmlElement = (Element) innerResult.importNode(monitoringXmlElement, true);
final Element monitoringXmlElementPlaceholder = getPlaceholder(innerResult);
monitoringXmlElementPlaceholder.appendChild(monitoringXmlElement);
return innerResult;
} catch (final RuntimeException e) {
final String errorMessage = "Get operation for netconf-state subtree failed";
LOG.warn(errorMessage, e);
throw new DocumentedException(errorMessage, e, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR, // FIXME: i.e. <error>e.getMessage()</error> ?
Map.of(ErrorSeverity.ERROR.elementBody(), e.getMessage()));
}
}
use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class XmlElementTest method testUnrecognisedElements.
@Test
public void testUnrecognisedElements() throws Exception {
xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"), xmlElement.getOnlyChildElement("innerPrefixed"), xmlElement.getOnlyChildElement("innerNamespace"));
final DocumentedException e = assertThrows(DocumentedException.class, () -> xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner")));
assertThat(e.getMessage(), // FIXME: this looks very suspect
both(containsString("innerNamespace")).and(containsString("innerNamespace")));
}
use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class NetconfMessageTransformUtil method stripNotification.
public static Map.Entry<Instant, XmlElement> stripNotification(final NetconfMessage message) {
final XmlElement xmlElement = XmlElement.fromDomDocument(message.getDocument());
final List<XmlElement> childElements = xmlElement.getChildElements();
Preconditions.checkArgument(childElements.size() == 2, "Unable to parse notification %s, unexpected format." + "\nExpected 2 childElements, actual childElements size is %s", message, childElements.size());
final XmlElement eventTimeElement;
final XmlElement notificationElement;
if (childElements.get(0).getName().equals(EVENT_TIME)) {
eventTimeElement = childElements.get(0);
notificationElement = childElements.get(1);
} else if (childElements.get(1).getName().equals(EVENT_TIME)) {
eventTimeElement = childElements.get(1);
notificationElement = childElements.get(0);
} else {
throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message);
}
try {
return new SimpleEntry<>(NetconfNotification.RFC3339_DATE_PARSER.apply(eventTimeElement.getTextContent()).toInstant(), notificationElement);
} catch (final DocumentedException e) {
throw new IllegalArgumentException("Notification payload does not contain " + EVENT_TIME + " " + message, e);
} catch (final DateTimeParseException e) {
LOG.warn("Unable to parse event time from {}. Setting time to {}", eventTimeElement, NetconfNotification.UNKNOWN_EVENT_TIME, e);
return new SimpleEntry<>(NetconfNotification.UNKNOWN_EVENT_TIME.toInstant(), notificationElement);
}
}
use of org.opendaylight.netconf.api.DocumentedException in project netconf by opendaylight.
the class XmlElement method extractNamespaces.
private Map<String, String> extractNamespaces() throws DocumentedException {
Map<String, String> namespaces = new HashMap<>();
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
String attribKey = attribute.getNodeName();
if (attribKey.startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
String prefix;
if (attribKey.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
prefix = DEFAULT_NAMESPACE_PREFIX;
} else {
if (!attribKey.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":")) {
throw new DocumentedException("Attribute doesn't start with :", ErrorType.APPLICATION, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
}
prefix = attribKey.substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1);
}
namespaces.put(prefix, attribute.getNodeValue());
}
}
// namespace does not have to be defined on this element but inherited
if (!namespaces.containsKey(DEFAULT_NAMESPACE_PREFIX)) {
Optional<String> namespaceOptionally = getNamespaceOptionally();
if (namespaceOptionally.isPresent()) {
namespaces.put(DEFAULT_NAMESPACE_PREFIX, namespaceOptionally.get());
}
}
return namespaces;
}
Aggregations