use of org.opendaylight.netconf.api.xml.XmlElement 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.xml.XmlElement in project netconf by opendaylight.
the class SchemalessRpcStructureTransformer method checkDataValidForPath.
private static void checkDataValidForPath(final YangInstanceIdentifier dataPath, final Element dataNode) {
// if datapath is empty, consider dataNode to be a root node
if (dataPath.isEmpty()) {
return;
}
final XmlElement dataElement = XmlElement.fromDomElement(dataNode);
final PathArgument lastPathArgument = dataPath.getLastPathArgument();
final QName nodeType = lastPathArgument.getNodeType();
if (!nodeType.getNamespace().toString().equals(dataNode.getNamespaceURI()) || !nodeType.getLocalName().equals(dataElement.getName())) {
throw new IllegalStateException(String.format("Can't write data '%s' to path %s", dataNode.getTagName(), dataPath));
}
if (lastPathArgument instanceof NodeIdentifierWithPredicates) {
checkKeyValuesValidForPath(dataElement, lastPathArgument);
}
}
use of org.opendaylight.netconf.api.xml.XmlElement in project lighty-netconf-simulator by PANTHEONtech.
the class RpcHandlerImpl method getResponse.
@Override
public Optional<Document> getResponse(final XmlElement rpcElement) {
final Element element = rpcElement.getDomElement();
final String formattedRequest = RPCUtil.formatXml(element);
LOG.debug("Received get request with payload:\n{} ", formattedRequest);
final Optional<RequestProcessor> processorForRequestOpt = getProcessorForRequest(element);
if (processorForRequestOpt.isPresent()) {
return Optional.ofNullable(processorForRequestOpt.get().processRequest(element));
}
return Optional.empty();
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class SchemalessMessageTransformer method toNotification.
@Override
public DOMNotification toNotification(final NetconfMessage message) {
final Map.Entry<Instant, XmlElement> stripped = NetconfMessageTransformUtil.stripNotification(message);
final QName notificationNoRev;
try {
notificationNoRev = QName.create(stripped.getValue().getNamespace(), stripped.getValue().getName()).withoutRevision();
} catch (final MissingNameSpaceException e) {
throw new IllegalArgumentException("Unable to parse notification " + message + ", cannot find namespace", e);
}
final DOMSourceAnyxmlNode notificationPayload = Builders.anyXmlBuilder().withNodeIdentifier(new NodeIdentifier(notificationNoRev)).withValue(new DOMSource(stripped.getValue().getDomElement())).build();
final ContainerNode notificationBody = Builders.containerBuilder().withNodeIdentifier(SCHEMALESS_NOTIFICATION_PAYLOAD).withChild(notificationPayload).build();
return new NetconfMessageTransformer.NetconfDeviceNotification(notificationBody, stripped.getKey());
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class SchemalessRpcStructureTransformer method checkKeyValuesValidForPath.
private static void checkKeyValuesValidForPath(final XmlElement dataElement, final PathArgument lastPathArgument) {
final NodeIdentifierWithPredicates keyedId = (NodeIdentifierWithPredicates) lastPathArgument;
for (Entry<QName, Object> entry : keyedId.entrySet()) {
QName qualifiedName = entry.getKey();
final List<XmlElement> key = dataElement.getChildElementsWithinNamespace(qualifiedName.getLocalName(), qualifiedName.getNamespace().toString());
if (key.isEmpty()) {
throw new IllegalStateException("No key present in xml");
}
if (key.size() > 1) {
throw new IllegalStateException("Multiple values for same key present");
}
final String textContent;
try {
textContent = key.get(0).getTextContent();
} catch (DocumentedException e) {
throw new IllegalStateException("Key value not present in key element", e);
}
if (!entry.getValue().equals(textContent)) {
throw new IllegalStateException("Key value in path not equal to key value in xml");
}
}
}
Aggregations