Enabling notification from netconf client 57/2657/3
authorRobert Gallas <rgallas@cisco.com>
Tue, 12 Nov 2013 11:07:18 +0000 (12:07 +0100)
committerRobert Gallas <rgallas@cisco.com>
Tue, 12 Nov 2013 12:46:28 +0000 (13:46 +0100)
Extending NetconfClientSessionListener to enable notifications

Change-Id: I6190afef686cb51aea9961fbd5cb43ebf1669a0d
Signed-off-by: Robert Gallas <rgallas@cisco.com>
opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/AbstractNetconfClientNotifySessionListener.java [new file with mode: 0644]
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlNetconfConstants.java

diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/AbstractNetconfClientNotifySessionListener.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/AbstractNetconfClientNotifySessionListener.java
new file mode 100644 (file)
index 0000000..48109d1
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.netconf.client;
+
+import org.opendaylight.controller.netconf.api.NetconfMessage;
+import org.opendaylight.controller.netconf.util.xml.XmlElement;
+import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
+
+/**
+ * Class extending {@link NetconfClientSessionListener} to provide notification capability.
+ */
+public abstract class AbstractNetconfClientNotifySessionListener extends NetconfClientSessionListener {
+    /*
+     * Maybe some capabilities could be expressed as internal NetconfClientSessionListener handlers.
+     * It would enable NetconfClient functionality to be extended by using namespace handlers.
+     * So far let just enable notification capability by extending and let parent class intact.
+     */
+
+    /**
+     * As class purpose is to provide notification capability to session listener
+     * onMessage method is not allowed to be further overridden.
+     * {@see #onNotification(NetconfClientSession, NetconfMessage)}
+     *
+     * @param session {@see NetconfClientSessionListener#onMessage(NetconfClientSession, NetconfMessage)}
+     * @param message {@see NetconfClientSessionListener#onMessage(NetconfClientSession, NetconfMessage)}
+     */
+    @Override
+    public final synchronized void onMessage(NetconfClientSession session, NetconfMessage message) {
+        if (isNotification(message)) {
+            onNotification(session, message);
+        } else {
+            super.onMessage(session, message);
+        }
+    }
+
+    /**
+     * Method intended to customize notification processing.
+     *
+     * @param session {@see NetconfClientSessionListener#onMessage(NetconfClientSession, NetconfMessage)}
+     * @param message {@see NetconfClientSessionListener#onMessage(NetconfClientSession, NetconfMessage)}
+     */
+    public abstract void onNotification(NetconfClientSession session, NetconfMessage message);
+
+    private boolean isNotification(NetconfMessage message) {
+        XmlElement xmle = XmlElement.fromDomDocument(message.getDocument());
+        return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(xmle.getName()) ;
+    }
+}
index 3e862faa7b34c96378002d17d79350fb0d4f45d7..2a900e052bcf9ba378c5e7a39c1de8f8bfd0dbbf 100644 (file)
@@ -31,6 +31,8 @@ public class XmlNetconfConstants {
     public static final String RPC_REPLY_KEY = "rpc-reply";
     public static final String RPC_ERROR = "rpc-error";
     public static final String NAME_KEY = "name";
+    public static final String NOTIFICATION_ELEMENT_NAME = "notification";
+
     //
     //
     public static final String RFC4741_TARGET_NAMESPACE = "urn:ietf:params:xml:ns:netconf:base:1.0";