Add "static-reference" blueprint extension 21/40421/5
authorTom Pantelis <tpanteli@brocade.com>
Wed, 22 Jun 2016 17:11:50 +0000 (13:11 -0400)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 23 Jun 2016 07:19:46 +0000 (03:19 -0400)
Added a blueprint extension, "static-reference", that obtains an OSGi
service and returns the actual instance. This differs from the standard
"reference" element that returns a dynamic proxy whose underlying
service instance can come and go. This is useful especially in cases
where the service exists for the life of the karaf container and you
don't need/want the overhead of the proxy.

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

index a84b8b6..8001c3e 100644 (file)
@@ -96,8 +96,12 @@ abstract class AbstractDependentComponentFactoryMetadata implements DependentCom
     }
 
     protected void retrieveService(String name, Class<?> interfaceClass, Consumer<Object> onServiceRetrieved) {
+        retrieveService(name, interfaceClass.getName(), onServiceRetrieved);
+    }
+
+    protected void retrieveService(String name, String interfaceName, Consumer<Object> onServiceRetrieved) {
         StaticServiceReferenceRecipe recipe = new StaticServiceReferenceRecipe(getId() + "-" + name,
-                container, interfaceClass.getName());
+                container, interfaceName);
         setDependendencyDesc(recipe.getOsgiFilter());
         serviceRecipes.add(recipe);
 
index ab9e216..14ca14b 100644 (file)
@@ -65,6 +65,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
     private static final String ID_ATTR = "id";
     private static final String RPC_SERVICE = "rpc-service";
     private static final String SPECIFIC_SERVICE_REF_LIST = "specific-reference-list";
+    private static final String STATIC_REFERENCE = "static-reference";
 
     @SuppressWarnings("rawtypes")
     @Override
@@ -99,6 +100,8 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
             return parseClusteredAppConfig(element, context);
         } else if (nodeNameEquals(element, SPECIFIC_SERVICE_REF_LIST)) {
             return parseSpecificReferenceList(element, context);
+        } else if (nodeNameEquals(element, STATIC_REFERENCE)) {
+            return parseStaticReference(element, context);
         }
 
         throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName());
@@ -358,6 +361,15 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
         return metadata;
     }
 
+    private Metadata parseStaticReference(Element element, ParserContext context) {
+        ComponentFactoryMetadata metadata = new StaticReferenceMetadata(getId(context, element),
+                element.getAttribute(INTERFACE));
+
+        LOG.debug("parseStaticReference returning {}", metadata);
+
+        return metadata;
+    }
+
     private Element parseXML(String name, String xml) {
         DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
         builderFactory.setNamespaceAware(true);
diff --git a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/StaticReferenceMetadata.java b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/StaticReferenceMetadata.java
new file mode 100644 (file)
index 0000000..693f518
--- /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.osgi.service.blueprint.container.ComponentDefinitionException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Factory metadata corresponding to the "static-reference" element that obtains an OSGi service and
+ * returns the actual instance. This differs from the standard "reference" element that returns a dynamic
+ * proxy whose underlying service instance can come and go.
+ *
+ * @author Thomas Pantelis
+ */
+class StaticReferenceMetadata extends AbstractDependentComponentFactoryMetadata {
+    private static final Logger LOG = LoggerFactory.getLogger(StaticReferenceMetadata.class);
+
+    private final String interfaceName;
+    private volatile Object retrievedService;
+
+    StaticReferenceMetadata(String id, String interfaceName) {
+        super(id);
+        this.interfaceName = interfaceName;
+    }
+
+    @Override
+    protected void startTracking() {
+        retrieveService(interfaceName, interfaceName, service -> {
+            retrievedService = service;
+            setSatisfied();
+        });
+    }
+
+    @Override
+    public Object create() throws ComponentDefinitionException {
+        super.onCreate();
+
+        LOG.debug("{}: create returning service {}", logName(), retrievedService);
+
+        return retrievedService;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder builder = new StringBuilder();
+        builder.append("StaticReferenceMetadata [interfaceName=").append(interfaceName).append("]");
+        return builder.toString();
+    }
+}
index 2a2189d..b3a7646 100644 (file)
     <xsd:attribute name="id" type="xsd:ID"/>
   </xsd:complexType>
   <xsd:element name="specific-reference-list" type="TspecificReferenceList"/>
+
+  <xsd:complexType name="TstaticReference">
+    <xsd:attribute name="interface" type="bp:Tclass" use="required"/>
+    <xsd:attribute name="id" type="xsd:ID"/>
+  </xsd:complexType>
+  <xsd:element name="static-reference" type="TstaticReference"/>
 </xsd:schema>
\ No newline at end of file