use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class SubtreeFilter method filtered.
private static Document filtered(final XmlElement filter, final Document originalReplyDocument) throws DocumentedException {
Document result = XmlUtil.newDocument();
// even if filter is empty, copy /rpc/data
Element rpcReply = originalReplyDocument.getDocumentElement();
Node rpcReplyDst = result.importNode(rpcReply, false);
result.appendChild(rpcReplyDst);
XmlElement dataSrc = XmlElement.fromDomElement(rpcReply).getOnlyChildElement("data", XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false);
rpcReplyDst.appendChild(dataDst);
addSubtree(filter, dataSrc, XmlElement.fromDomElement(dataDst));
return result;
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class SubtreeFilter method addSubtree2.
private static MatchingResult addSubtree2(final XmlElement filter, final XmlElement src, final XmlElement dstParent) throws DocumentedException {
Document document = dstParent.getDomElement().getOwnerDocument();
MatchingResult matches = matches(src, filter);
if (matches != MatchingResult.NO_MATCH && matches != MatchingResult.CONTENT_MISMATCH) {
// copy srcChild to dst
boolean filterHasChildren = !filter.getChildElements().isEmpty();
// copy to depth if this is leaf of filter tree
Element copied = (Element) document.importNode(src.getDomElement(), !filterHasChildren);
boolean shouldAppend = !filterHasChildren;
if (filterHasChildren) {
// this implies TAG_MATCH
// do the same recursively
int numberOfTextMatchingChildren = 0;
for (XmlElement srcChild : src.getChildElements()) {
for (XmlElement filterChild : filter.getChildElements()) {
MatchingResult childMatch = addSubtree2(filterChild, srcChild, XmlElement.fromDomElement(copied));
if (childMatch == MatchingResult.CONTENT_MISMATCH) {
return MatchingResult.NO_MATCH;
}
if (childMatch == MatchingResult.CONTENT_MATCH) {
numberOfTextMatchingChildren++;
}
shouldAppend |= childMatch != MatchingResult.NO_MATCH;
}
}
// if only text matching child filters are specified..
if (numberOfTextMatchingChildren == filter.getChildElements().size()) {
// force all children to be added (to depth). This is done by copying parent node to depth.
// implies shouldAppend == true
copied = (Element) document.importNode(src.getDomElement(), true);
}
}
if (shouldAppend) {
dstParent.getDomElement().appendChild(copied);
}
}
return matches;
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class SubtreeFilterNotificationTest method testFilterNotification.
@Test
public void testFilterNotification() throws Exception {
XmlElement filter = XmlElement.fromDomDocument(getDocument("filter.xml"));
Document preFilterDocument = getDocument("pre-filter.xml");
Document postFilterDocument = getDocument("post-filter.xml");
Optional<Document> actualPostFilterDocumentOpt = SubtreeFilter.applySubtreeNotificationFilter(filter, preFilterDocument);
if (actualPostFilterDocumentOpt.isPresent()) {
Document actualPostFilterDocument = actualPostFilterDocumentOpt.get();
LOG.info("Actual document: {}", XmlUtil.toString(actualPostFilterDocument));
Diff diff = XMLUnit.compareXML(postFilterDocument, actualPostFilterDocument);
assertTrue(diff.toString(), diff.similar());
} else {
assertEquals("empty", XmlElement.fromDomDocument(postFilterDocument).getName());
}
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class NetconfUtil method checkIsMessageOk.
public static Document checkIsMessageOk(final Document response) throws DocumentedException {
final XmlElement docElement = XmlElement.fromDomDocument(response);
// FIXME: we should throw DocumentedException here
checkState(XmlNetconfConstants.RPC_REPLY_KEY.equals(docElement.getName()));
final XmlElement element = docElement.getOnlyChildElement();
if (XmlNetconfConstants.OK.equals(element.getName())) {
return response;
}
LOG.warn("Can not load last configuration. Operation failed.");
// FIXME: we should be throwing a DocumentedException here
throw new IllegalStateException("Can not load last configuration. Operation failed: " + XmlUtil.toString(response));
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class CreateSubscription method handleWithNoSubsequentOperations.
@Override
protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
operationElement.checkName(CREATE_SUBSCRIPTION);
operationElement.checkNamespace(CreateSubscriptionInput.QNAME.getNamespace().toString());
// FIXME reimplement using CODEC_REGISTRY and parse everything into generated class instance
// Binding doesn't support anyxml nodes yet, so filter could not be retrieved
// xml -> normalized node -> CreateSubscriptionInput conversion could be slower than current approach
final Optional<XmlElement> filter = operationElement.getOnlyChildElementWithSameNamespaceOptionally("filter");
// Replay not supported
final Optional<XmlElement> startTime = operationElement.getOnlyChildElementWithSameNamespaceOptionally("startTime");
checkArgument(startTime.isEmpty(), "StartTime element not yet supported");
// Stop time not supported
final Optional<XmlElement> stopTime = operationElement.getOnlyChildElementWithSameNamespaceOptionally("stopTime");
checkArgument(stopTime.isEmpty(), "StopTime element not yet supported");
final StreamNameType streamNameType = parseStreamIfPresent(operationElement);
requireNonNull(netconfSession);
// Premature streams are allowed (meaning listener can register even if no provider is available yet)
if (!notifications.isStreamAvailable(streamNameType)) {
LOG.warn("Registering premature stream {}. No publisher available yet for session {}", streamNameType, getNetconfSessionIdForReporting());
}
final NotificationListenerRegistration notificationListenerRegistration = notifications.registerNotificationListener(streamNameType, new NotificationSubscription(netconfSession, filter));
subscriptions.add(notificationListenerRegistration);
return document.createElement(XmlNetconfConstants.OK);
}
Aggregations