Add blueprint extension to register notification listener 92/36492/19
authorTom Pantelis <tpanteli@brocade.com>
Mon, 21 Mar 2016 11:27:36 +0000 (07:27 -0400)
committerAnil Vishnoi <vishnoianil@gmail.com>
Mon, 11 Apr 2016 17:15:25 +0000 (17:15 +0000)
Added a blueprint extension element <notification-listener> that
registers a NotificationListener implementation with the MD-SAL
NotificationService.

Change-Id: I5ba9b1124ccba4d5c0f8d2e323eb3dd0e918cd94
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/NotificationListenerBean.java [new file with mode: 0644]
opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/OpendaylightNamespaceHandler.java
opendaylight/blueprint/src/main/resources/opendaylight-blueprint-ext-1.0.0.xsd

diff --git a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/NotificationListenerBean.java b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/NotificationListenerBean.java
new file mode 100644 (file)
index 0000000..bdef13f
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 Brocade Communications 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.blueprint.ext;
+
+import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.NotificationListener;
+import org.osgi.framework.Bundle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Blueprint bean corresponding to the "notification-listener" element that registers a NotificationListener
+ * with the NotificationService.
+ *
+ * @author Thomas Pantelis
+ */
+public class NotificationListenerBean {
+    private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerBean.class);
+    static final String NOTIFICATION_LISTENER = "notification-listener";
+
+    private Bundle bundle;
+    private NotificationService notificationService;
+    private NotificationListener notificationListener;
+    private ListenerRegistration<?> registration;
+
+    public void setNotificationService(NotificationService notificationService) {
+        this.notificationService = notificationService;
+    }
+    public void setNotificationListener(NotificationListener notificationListener) {
+        this.notificationListener = notificationListener;
+    }
+
+    public void setBundle(Bundle bundle) {
+        this.bundle = bundle;
+    }
+
+    @SuppressWarnings({ })
+    public void init() {
+        LOG.debug("{}: init - registering NotificationListener {}", bundle.getSymbolicName(), notificationListener);
+
+        registration = notificationService.registerNotificationListener(notificationListener);
+    }
+
+    public void destroy() {
+        if(registration != null) {
+            registration.close();
+        }
+    }
+}
index bafc1f7a7347ec6165de5bad3932dd48921dec94..297c3d448466478ec948587807ff8e4d3976687f 100644 (file)
@@ -22,6 +22,7 @@ import org.apache.aries.blueprint.mutable.MutableServiceMetadata;
 import org.apache.aries.blueprint.mutable.MutableServiceReferenceMetadata;
 import org.apache.aries.blueprint.mutable.MutableValueMetadata;
 import org.opendaylight.controller.blueprint.BlueprintContainerRestartService;
+import org.opendaylight.controller.md.sal.binding.api.NotificationService;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
 import org.osgi.service.blueprint.container.ComponentDefinitionException;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
@@ -47,6 +48,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
     public static final String NAMESPACE_1_0_0 = "http://opendaylight.org/xmlns/blueprint/v1.0.0";
     static final String ROUTED_RPC_REG_CONVERTER_NAME = "org.opendaylight.blueprint.RoutedRpcRegConverter";
     static final String RPC_REGISTRY_NAME = "org.opendaylight.blueprint.RpcRegistry";
+    static final String NOTIFICATION_SERVICE_NAME = "org.opendaylight.blueprint.NotificationService";
 
     private static final Logger LOG = LoggerFactory.getLogger(OpendaylightNamespaceHandler.class);
     private static final String COMPONENT_PROCESSOR_NAME = ComponentProcessor.class.getName();
@@ -85,6 +87,8 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
             return parseRoutedRpcImplementation(element, context);
         } else if (nodeNameEquals(element, RPC_SERVICE)) {
             return parseRpcService(element, context);
+        } else if (nodeNameEquals(element, NotificationListenerBean.NOTIFICATION_LISTENER)) {
+            return parseNotificationListener(element, context);
         }
 
         throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName());
@@ -277,6 +281,34 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
         }
     }
 
+    private Metadata parseNotificationListener(Element element, ParserContext context) {
+        registerNotificationServiceRefBean(context);
+
+        MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
+        metadata.setId(context.generateId());
+        metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
+        metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
+        metadata.setRuntimeClass(NotificationListenerBean.class);
+        metadata.setInitMethod("init");
+        metadata.setDestroyMethod("destroy");
+        metadata.addProperty("bundle", createRef(context, "blueprintBundle"));
+        metadata.addProperty("notificationService", createRef(context, NOTIFICATION_SERVICE_NAME));
+        metadata.addProperty("notificationListener", createRef(context, element.getAttribute(REF_ATTR)));
+
+        LOG.debug("parseNotificationListener returning {}", metadata);
+
+        return metadata;
+    }
+
+     private void registerNotificationServiceRefBean(ParserContext context) {
+        ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
+        if(registry.getComponentDefinition(NOTIFICATION_SERVICE_NAME) == null) {
+            MutableReferenceMetadata metadata = createServiceRef(context, NotificationService.class, null);
+            metadata.setId(NOTIFICATION_SERVICE_NAME);
+            registry.registerComponentDefinition(metadata);
+        }
+    }
+
     private static ValueMetadata createValue(ParserContext context, String value) {
         MutableValueMetadata m = context.createMetadata(MutableValueMetadata.class);
         m.setStringValue(value);
index 3632ccb3a48020c944f61b9021838184066e4444..0c6eaaa2f066d420623a5da07d641f0352b2a5a2 100644 (file)
@@ -28,4 +28,9 @@
   </xsd:complexType>
   <xsd:element name="rpc-service" type="TrpcService"/>
 
+  <xsd:complexType name="TnotificationListener">
+    <xsd:attribute name="ref" type="bp:Tidref" use="required"/>
+  </xsd:complexType>
+  <xsd:element name="notification-listener" type="TnotificationListener"/>
+
 </xsd:schema>
\ No newline at end of file