From 328b2bb2bdca3c26067e0ad4d57aec874f42c831 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 6 Nov 2022 22:29:33 +0100 Subject: [PATCH] Clean up namespace handling We should tie together namespaces and elements to the generated model, so that these are kept consistent. Change-Id: I2d1167e632b6695985bf93d4102794978d2811df Signed-off-by: Robert Varga --- .../RestconfInvokeOperationsServiceImpl.java | 13 ++++++----- .../listeners/AbstractNotificationsData.java | 4 ++-- .../listeners/DataTreeCandidateFormatter.java | 11 +++++---- .../listeners/NotificationFormatter.java | 23 +++++++++++-------- .../XMLDataTreeCandidateFormatter.java | 12 +++++----- .../listeners/XMLNotificationFormatter.java | 8 ------- 6 files changed, 36 insertions(+), 35 deletions(-) diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImpl.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImpl.java index ff902ddf9d..e877c74a97 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImpl.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImpl.java @@ -33,11 +33,12 @@ import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler; import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload; import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfInvokeOperationsService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateDataChangeEventSubscriptionInput; import org.opendaylight.yangtools.yang.common.ErrorTag; import org.opendaylight.yangtools.yang.common.ErrorType; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.opendaylight.yangtools.yang.common.XMLNamespace; import org.opendaylight.yangtools.yang.common.YangConstants; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -55,9 +56,8 @@ import org.slf4j.LoggerFactory; public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService { private static final Logger LOG = LoggerFactory.getLogger(RestconfInvokeOperationsServiceImpl.class); - // FIXME: at some point we do not want to have this here - private static final XMLNamespace SAL_REMOTE_NAMESPACE = - XMLNamespace.of("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote"); + // FIXME: at some point we do not want to have this here, as this is only used for dispatch + private static final QNameModule SAL_REMOTE_NAMESPACE = CreateDataChangeEventSubscriptionInput.QNAME.getModule(); private final DOMRpcService rpcService; private final SchemaContextHandler schemaContextHandler; @@ -79,8 +79,9 @@ public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperat final ListenableFuture future; if (mountPoint == null) { - // FIXME: this really should be a normal RPC invocation service which has its own interface with JAX-RS - if (SAL_REMOTE_NAMESPACE.equals(rpcName.getNamespace())) { + // FIXME: this really should be a normal RPC invocation service which has its own interface with JAX-RS, + // except ... we check 'identifier' for .contains() instead of exact RPC name! + if (SAL_REMOTE_NAMESPACE.equals(rpcName.getModule())) { if (identifier.contains("create-data-change-event-subscription")) { future = Futures.immediateFuture( CreateStreamUtil.createDataChangeNotifiStream(payload, schemaContext)); diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/AbstractNotificationsData.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/AbstractNotificationsData.java index 455d3cc248..abeaea98a3 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/AbstractNotificationsData.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/AbstractNotificationsData.java @@ -153,8 +153,8 @@ abstract class AbstractNotificationsData { * @return element of {@link Document} */ protected Element basePartDoc(final Document doc) { - final Element notificationElement = - doc.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0", "notification"); + final Element notificationElement = doc.createElementNS( + NotificationFormatter.NOTIFICATION_NAMESPACE, NotificationFormatter.NOTIFICATION_ELEMENT); doc.appendChild(notificationElement); diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/DataTreeCandidateFormatter.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/DataTreeCandidateFormatter.java index 4327671200..35ef644b17 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/DataTreeCandidateFormatter.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/DataTreeCandidateFormatter.java @@ -7,8 +7,12 @@ */ package org.opendaylight.restconf.nb.rfc8040.streams.listeners; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.DATA_CHANGED_NOTIFICATION_ELEMENT; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.DATA_CHANGE_EVENT_ELEMENT; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.NOTIFICATION_ELEMENT; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.NOTIFICATION_NAMESPACE; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.SAL_REMOTE_NAMESPACE; import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.XML_OUTPUT_FACTORY; -import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.DATA_CHANGE_EVENT_ELEMENT; import java.io.IOException; import java.time.Instant; @@ -43,14 +47,13 @@ abstract class DataTreeCandidateFormatter extends EventFormatter input) throws IOException { - final Element notificationElement = doc.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0", - "notification"); + final Element notificationElement = doc.createElementNS(NOTIFICATION_NAMESPACE, NOTIFICATION_ELEMENT); final Element eventTimeElement = doc.createElement("eventTime"); eventTimeElement.setTextContent(toRFC3339(Instant.now())); notificationElement.appendChild(eventTimeElement); final Element notificationEventElement = doc.createElementNS( - "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "data-changed-notification"); + SAL_REMOTE_NAMESPACE, DATA_CHANGED_NOTIFICATION_ELEMENT); for (DataTreeCandidate candidate : input) { final Element dataChangedElement = doc.createElement(DATA_CHANGE_EVENT_ELEMENT); diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/NotificationFormatter.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/NotificationFormatter.java index 6e70e199b8..13fef7a41e 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/NotificationFormatter.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/NotificationFormatter.java @@ -16,6 +16,8 @@ import javax.xml.transform.dom.DOMResult; import javax.xml.xpath.XPathExpressionException; import org.opendaylight.mdsal.dom.api.DOMEvent; import org.opendaylight.mdsal.dom.api.DOMNotification; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.DataChangedNotification; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.data.changed.notification.DataChangeEvent; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter; @@ -25,6 +27,13 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; abstract class NotificationFormatter extends EventFormatter { + static final String NOTIFICATION_NAMESPACE = "urn:ietf:params:xml:ns:netconf:notification:1.0"; + static final String NOTIFICATION_ELEMENT = "notification"; + + static final String SAL_REMOTE_NAMESPACE = DataChangedNotification.QNAME.getNamespace().toString(); + static final String DATA_CHANGED_NOTIFICATION_ELEMENT = DataChangedNotification.QNAME.getLocalName(); + static final String DATA_CHANGE_EVENT_ELEMENT = DataChangeEvent.QNAME.getLocalName(); + static final XMLOutputFactory XML_OUTPUT_FACTORY; static { @@ -43,18 +52,14 @@ abstract class NotificationFormatter extends EventFormatter { @Override final void fillDocument(final Document doc, final EffectiveModelContext schemaContext, final DOMNotification input) throws IOException { - final Element notificationElement = doc.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0", - "notification"); + final Element notificationElement = doc.createElementNS(NOTIFICATION_NAMESPACE, NOTIFICATION_ELEMENT); final Element eventTimeElement = doc.createElement("eventTime"); - if (input instanceof DOMEvent) { - eventTimeElement.setTextContent(toRFC3339(((DOMEvent) input).getEventInstant())); - } else { - eventTimeElement.setTextContent(toRFC3339(Instant.now())); - } + final Instant now = input instanceof DOMEvent domEvent ? domEvent.getEventInstant() : Instant.now(); + eventTimeElement.setTextContent(toRFC3339(now)); notificationElement.appendChild(eventTimeElement); - final Element notificationEventElement = doc.createElementNS( - "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "create-notification-stream"); + final Element notificationEventElement = doc.createElementNS(SAL_REMOTE_NAMESPACE, + "create-notification-stream"); final Element dataElement = doc.createElement("notification"); final DOMResult result = new DOMResult(dataElement); try { diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/XMLDataTreeCandidateFormatter.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/XMLDataTreeCandidateFormatter.java index 15433ae099..1ca5a941ea 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/XMLDataTreeCandidateFormatter.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/XMLDataTreeCandidateFormatter.java @@ -7,11 +7,11 @@ */ package org.opendaylight.restconf.nb.rfc8040.streams.listeners; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.DATA_CHANGED_NOTIFICATION_ELEMENT; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.NOTIFICATION_ELEMENT; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.NOTIFICATION_NAMESPACE; +import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.SAL_REMOTE_NAMESPACE; import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationFormatter.XML_OUTPUT_FACTORY; -import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.DATA_CHANGED_NAMESPACE; -import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.DATA_CHANGED_NOTIFICATION_ELEMENT; -import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.NOTIFICATION_ELEMENT; -import static org.opendaylight.restconf.nb.rfc8040.streams.listeners.XMLNotificationFormatter.NOTIFICATION_NAMESPACE; import java.io.IOException; import java.io.StringWriter; @@ -65,8 +65,8 @@ public final class XMLDataTreeCandidateFormatter extends DataTreeCandidateFormat xmlStreamWriter.writeCharacters(toRFC3339(now)); xmlStreamWriter.writeEndElement(); - xmlStreamWriter.setDefaultNamespace(DATA_CHANGED_NAMESPACE); - xmlStreamWriter.writeStartElement(DATA_CHANGED_NAMESPACE, DATA_CHANGED_NOTIFICATION_ELEMENT); + xmlStreamWriter.setDefaultNamespace(SAL_REMOTE_NAMESPACE); + xmlStreamWriter.writeStartElement(SAL_REMOTE_NAMESPACE, DATA_CHANGED_NOTIFICATION_ELEMENT); final XmlDataTreeCandidateSerializer serializer = new XmlDataTreeCandidateSerializer(schemaContext, xmlStreamWriter); diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/XMLNotificationFormatter.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/XMLNotificationFormatter.java index b4afe63ee7..4b0d53ea4c 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/XMLNotificationFormatter.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/listeners/XMLNotificationFormatter.java @@ -22,14 +22,6 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; final class XMLNotificationFormatter extends NotificationFormatter { private static final XMLNotificationFormatter INSTANCE = new XMLNotificationFormatter(); - static final String NOTIFICATION_NAMESPACE = "urn:ietf:params:xml:ns:netconf:notification:1.0"; - static final String NOTIFICATION_ELEMENT = "notification"; - - static final String DATA_CHANGED_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote"; - static final String DATA_CHANGED_NOTIFICATION_ELEMENT = "data-changed-notification"; - - static final String DATA_CHANGE_EVENT_ELEMENT = "data-change-event"; - static final NotificationFormatterFactory FACTORY = new NotificationFormatterFactory() { @Override public XMLNotificationFormatter getFormatter(final String xpathFilter) throws XPathExpressionException { -- 2.36.6