*/
package org.opendaylight.netconf.messagebus.eventsources.netconf;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
import java.util.Set;
import javax.xml.transform.dom.DOMSource;
import org.opendaylight.mdsal.dom.api.DOMNotification;
import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
+import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatus;
import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotification;
.create(true, QName.create(EventSourceStatusNotification.QNAME, "event-source-status"));
private static final NodeIdentifier EVENT_SOURCE_STATUS_ARG = NodeIdentifier.create(
EventSourceStatusNotification.QNAME);
- private static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
- private static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
private final DOMNotificationListener domNotificationListener;
private static AnyXmlNode encapsulate(final EventSourceStatusNotification notification) {
Document doc = UntrustedXML.newDocumentBuilder().newDocument();
- final Optional<String> namespace = Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString());
- final Element rootElement = createElement(doc, "EventSourceStatusNotification", namespace);
+ final Element rootElement = XmlUtil.createElement(doc, "EventSourceStatusNotification",
+ Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString()));
final Element sourceElement = doc.createElement("status");
sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
.withValue(new DOMSource(rootElement)).build();
}
-
- // Helper to create root XML element with correct namespace and attribute
- private static Element createElement(final Document document, final String qualifiedName,
- final Optional<String> namespaceURI) {
- if (namespaceURI.isPresent()) {
- final Element element = document.createElementNS(namespaceURI.get(), qualifiedName);
- String name = XMLNS_ATTRIBUTE_KEY;
- if (element.getPrefix() != null) {
- name += ":" + element.getPrefix();
- }
- element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
- return element;
- }
- return document.createElement(qualifiedName);
- }
}
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import javax.xml.XMLConstants;
import org.opendaylight.netconf.api.DocumentedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
String attribKey = attribute.getNodeName();
- if (attribKey.startsWith(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
+ if (attribKey.startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
String prefix;
- if (attribKey.equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
+ if (attribKey.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
prefix = DEFAULT_NAMESPACE_PREFIX;
} else {
- if (!attribKey.startsWith(XmlUtil.XMLNS_ATTRIBUTE_KEY + ":")) {
+ if (!attribKey.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":")) {
throw new DocumentedException("Attribute doesn't start with :",
DocumentedException.ErrorType.APPLICATION,
DocumentedException.ErrorTag.INVALID_VALUE,
DocumentedException.ErrorSeverity.ERROR);
}
- prefix = attribKey.substring(XmlUtil.XMLNS_ATTRIBUTE_KEY.length() + 1);
+ prefix = attribKey.substring(XMLConstants.XMLNS_ATTRIBUTE.length() + 1);
}
namespaces.put(prefix, attribute.getNodeValue());
}
}
public String getNamespaceAttribute() throws MissingNameSpaceException {
- String attribute = element.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY);
+ String attribute = element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE);
if (attribute.isEmpty() || attribute.equals(DEFAULT_NAMESPACE_PREFIX)) {
throw new MissingNameSpaceException(String.format("Element %s must specify namespace",
toString()),
}
public Optional<String> getNamespaceAttributeOptionally() {
- String attribute = element.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY);
+ String attribute = element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE);
if (attribute.isEmpty() || attribute.equals(DEFAULT_NAMESPACE_PREFIX)) {
return Optional.empty();
}
*/
package org.opendaylight.netconf.api.xml;
+import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE;
+import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
public final class XmlUtil {
- public static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
- public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
private static final DocumentBuilderFactory BUILDER_FACTORY;
private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Optional<String> namespaceURI) {
if (namespaceURI.isPresent()) {
final Element element = document.createElementNS(namespaceURI.get(), qname);
- String name = XMLNS_ATTRIBUTE_KEY;
+ String name = XMLNS_ATTRIBUTE;
if (element.getPrefix() != null) {
name += ":" + element.getPrefix();
}
- element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
+ element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, name, namespaceURI.get());
return element;
}
return document.createElement(qname);
String content = createPrefixedValue(XmlNetconfConstants.PREFIX, contentWithoutPrefix);
Element element = createTextElement(document, qname, content, namespaceURI);
- String prefixedNamespaceAttr = createPrefixedValue(XMLNS_ATTRIBUTE_KEY, prefix);
- element.setAttributeNS(XMLNS_URI, prefixedNamespaceAttr, namespace);
+ String prefixedNamespaceAttr = createPrefixedValue(XMLNS_ATTRIBUTE, prefix);
+ element.setAttributeNS(XMLNS_ATTRIBUTE_NS_URI, prefixedNamespaceAttr, namespace);
return element;
}
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
+import javax.xml.XMLConstants;
import org.opendaylight.netconf.api.DocumentedException;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.api.NetconfSession;
for (int i = 0; i < incomingAttributes.getLength(); i++) {
final Attr attr = (Attr) incomingAttributes.item(i);
// skip namespace
- if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
+ if (attr.getNodeName().equals(XMLConstants.XMLNS_ATTRIBUTE)) {
continue;
}
rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
import java.util.Map;
import java.util.Optional;
+import javax.xml.XMLConstants;
import org.opendaylight.netconf.api.DocumentedException;
import org.opendaylight.netconf.api.xml.XmlElement;
import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
}
- public static Document applyRpcSubtreeFilter(Document requestDocument,
- Document rpcReply) throws DocumentedException {
+ public static Document applyRpcSubtreeFilter(final Document requestDocument,
+ final Document rpcReply) throws DocumentedException {
OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(requestDocument);
if (XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(operationNameAndNamespace.getNamespace())
&& XmlNetconfConstants.GET.equals(operationNameAndNamespace.getOperationName())
* @return document containing filtered notification content
* @throws DocumentedException if operation fails
*/
- public static Optional<Document> applySubtreeNotificationFilter(XmlElement filter,
- Document notification) throws DocumentedException {
+ public static Optional<Document> applySubtreeNotificationFilter(final XmlElement filter,
+ final Document notification) throws DocumentedException {
removeEventTimeNode(notification);
if (isSupported(filter)) {
return Optional.ofNullable(filteredNotification(filter, notification));
return Optional.of(extractNotificationContent(notification));
}
- private static void removeEventTimeNode(Document document) {
+ private static void removeEventTimeNode(final Document document) {
final Node eventTimeNode = document.getDocumentElement().getElementsByTagNameNS(XmlNetconfConstants
.URN_IETF_PARAMS_NETCONF_CAPABILITY_NOTIFICATION_1_0, XmlNetconfConstants.EVENT_TIME).item(0);
document.getDocumentElement().removeChild(eventTimeNode);
}
- private static boolean isSupported(XmlElement filter) {
+ private static boolean isSupported(final XmlElement filter) {
return "subtree".equals(filter.getAttribute("type"))
|| "subtree".equals(filter.getAttribute("type",
XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
}
- private static Document extractNotificationContent(Document notification) throws DocumentedException {
+ private static Document extractNotificationContent(final Document notification) throws DocumentedException {
XmlElement root = XmlElement.fromDomElement(notification.getDocumentElement());
XmlElement content = root.getOnlyChildElement();
notification.removeChild(root.getDomElement());
return notification;
}
- private static Document filteredNotification(XmlElement filter,
- Document originalNotification) throws DocumentedException {
+ private static Document filteredNotification(final XmlElement filter,
+ final Document originalNotification) throws DocumentedException {
Document result = XmlUtil.newDocument();
XmlElement dataSrc = XmlElement.fromDomDocument(originalNotification);
Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false);
if (dataDst.getFirstChild() != null) {
result.appendChild(dataDst.getFirstChild());
return result;
- } else {
- return null;
}
+ return null;
}
- private static Document filtered(XmlElement filter, Document originalReplyDocument) throws DocumentedException {
+ 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();
return result;
}
- private static void addSubtree(XmlElement filter, XmlElement src, XmlElement dst) throws DocumentedException {
+ private static void addSubtree(final XmlElement filter, final XmlElement src, final XmlElement dst)
+ throws DocumentedException {
for (XmlElement srcChild : src.getChildElements()) {
for (XmlElement filterChild : filter.getChildElements()) {
addSubtree2(filterChild, srcChild, dst);
}
}
- private static MatchingResult addSubtree2(XmlElement filter, XmlElement src,
- XmlElement dstParent) throws DocumentedException {
+ 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) {
* Shallow compare src node to filter: tag name and namespace must match.
* If filter node has no children and has text content, it also must match.
*/
- private static MatchingResult matches(XmlElement src, XmlElement filter) throws DocumentedException {
+ private static MatchingResult matches(final XmlElement src, final XmlElement filter) throws DocumentedException {
boolean tagMatch = src.getName().equals(filter.getName())
&& src.getNamespaceOptionally().equals(filter.getNamespaceOptionally());
MatchingResult result = null;
if (result == null) {
for (Attr attr : filter.getAttributes().values()) {
// ignore namespace declarations
- if (!XmlUtil.XMLNS_URI.equals(attr.getNamespaceURI())) {
+ if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.getNamespaceURI())) {
// find attr with matching localName(), namespaceURI(), == value() in src
String found = src.getAttribute(attr.getLocalName(), attr.getNamespaceURI());
if (attr.getValue().equals(found) && result != MatchingResult.NO_MATCH) {