Merge "[SEBA-532] Removing dependencies on aaa, olt and dhcpl2realy from kafka"
diff --git a/app.xml b/app.xml
index 07b66d2..4038c44 100644
--- a/app.xml
+++ b/app.xml
@@ -17,7 +17,7 @@
 <app name="org.opencord.kafka" origin="ONF" version="${project.version}"
      category="Integration" url="http://opencord.org" title="Kafka integration app"
      featuresRepo="mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features"
-     features="${project.artifactId}" apps="org.opencord.olt,org.opencord.dhcpl2relay,org.opencord.aaa">
+     features="${project.artifactId}" apps="">
     <description>${project.description}</description>
     <artifact>mvn:${project.groupId}/${project.artifactId}/${project.version}</artifact>
     <artifact>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.kafka-clients/1.1.1_1</artifact>
diff --git a/pom.xml b/pom.xml
index fa831b2..6704176 100644
--- a/pom.xml
+++ b/pom.xml
@@ -76,18 +76,21 @@
             <groupId>org.opencord</groupId>
             <artifactId>aaa</artifactId>
             <version>${aaa.api.version}</version>
+            <optional>true</optional>
         </dependency>
 
         <dependency>
             <groupId>org.opencord</groupId>
             <artifactId>olt-api</artifactId>
             <version>${olt.api.version}</version>
+            <optional>true</optional>
         </dependency>
 
         <dependency>
             <groupId>org.opencord</groupId>
             <artifactId>dhcpl2relay</artifactId>
             <version>${dhcpl2relay.api.version}</version>
+            <optional>true</optional>
         </dependency>
 
 
diff --git a/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java
index e68e307..6db44cb 100644
--- a/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java
+++ b/src/main/java/org/opencord/kafka/integrations/AaaKafkaIntegration.java
@@ -44,7 +44,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected EventBusService eventBusService;
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
+            bind = "bindAuthenticationService",
+            unbind = "unbindAuthenticationService")
     protected AuthenticationService authenticationService;
 
     private final AuthenticationEventListener listener = new InternalAuthenticationListener();
@@ -56,15 +58,35 @@
     private static final String PORT_NUMBER = "portNumber";
     private static final String AUTHENTICATION_STATE = "authenticationState";
 
+    protected void bindAuthenticationService(AuthenticationService authenticationService) {
+        if (this.authenticationService == null) {
+            log.info("Binding AuthenticationService");
+            this.authenticationService = authenticationService;
+            log.info("Adding listener on AuthenticationService");
+            authenticationService.addListener(listener);
+        } else {
+            log.warn("Trying to bind AuthenticationService but it is already bound");
+        }
+    }
+
+    protected void unbindAuthenticationService(AuthenticationService authenticationService) {
+        if (this.authenticationService == authenticationService) {
+            log.info("Unbinding AuthenticationService");
+            this.authenticationService = null;
+            log.info("Removing listener on AuthenticationService");
+            authenticationService.removeListener(listener);
+        } else {
+            log.warn("Trying to unbind AuthenticationService but it is already unbound");
+        }
+    }
+
     @Activate
     public void activate() {
-        authenticationService.addListener(listener);
         log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
-        authenticationService.removeListener(listener);
         log.info("Stopped");
     }
 
diff --git a/src/main/java/org/opencord/kafka/integrations/AccessDeviceKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/AccessDeviceKafkaIntegration.java
index 15f317f..8dddb78 100644
--- a/src/main/java/org/opencord/kafka/integrations/AccessDeviceKafkaIntegration.java
+++ b/src/main/java/org/opencord/kafka/integrations/AccessDeviceKafkaIntegration.java
@@ -45,7 +45,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected EventBusService eventBusService;
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
+            bind = "bindAccessDeviceService",
+            unbind = "unbindAccessDeviceService")
     protected AccessDeviceService accessDeviceService;
 
     private final AccessDeviceListener listener = new InternalAccessDeviceListener();
@@ -63,15 +65,35 @@
     private static final String ACTIVATED = "activated";
     private static final String DISABLED = "disabled";
 
+    protected void bindAccessDeviceService(AccessDeviceService accessDeviceService) {
+        if (this.accessDeviceService == null) {
+            log.info("Binding AccessDeviceService");
+            this.accessDeviceService = accessDeviceService;
+            log.info("Adding listener on AccessDeviceService");
+            accessDeviceService.addListener(listener);
+        } else {
+            log.warn("Trying to bind AccessDeviceService but it is already bound");
+        }
+    }
+
+    protected void unbindAccessDeviceService(AccessDeviceService accessDeviceService) {
+        if (this.accessDeviceService == accessDeviceService) {
+            log.info("Unbinding AccessDeviceService");
+            this.accessDeviceService = null;
+            log.info("Removing listener on AccessDeviceService");
+            accessDeviceService.removeListener(listener);
+        } else {
+            log.warn("Trying to unbind AccessDeviceService but it is already unbound");
+        }
+    }
+
     @Activate
     public void activate() {
-        accessDeviceService.addListener(listener);
         log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
-        accessDeviceService.removeListener(listener);
         log.info("Stopped");
     }
 
diff --git a/src/main/java/org/opencord/kafka/integrations/DhcpL2RelayKafkaIntegration.java b/src/main/java/org/opencord/kafka/integrations/DhcpL2RelayKafkaIntegration.java
index 786be87..3eca723 100644
--- a/src/main/java/org/opencord/kafka/integrations/DhcpL2RelayKafkaIntegration.java
+++ b/src/main/java/org/opencord/kafka/integrations/DhcpL2RelayKafkaIntegration.java
@@ -45,8 +45,10 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected EventBusService eventBusService;
 
-    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected DhcpL2RelayService authenticationService;
+    @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
+            bind = "bindDhcpL2RelayService",
+            unbind = "unbindDhcpL2RelayService")
+    protected DhcpL2RelayService dhcpL2RelayService;
 
     private final DhcpL2RelayListener listener = new InternalDhcpL2RelayListener();
 
@@ -60,15 +62,35 @@
     private static final String MAC_ADDRESS = "macAddress";
     private static final String IP_ADDRESS = "ipAddress";
 
+    protected void bindDhcpL2RelayService(DhcpL2RelayService dhcpL2RelayService) {
+        if (this.dhcpL2RelayService == null) {
+            log.info("Binding DhcpL2RelayService");
+            this.dhcpL2RelayService = dhcpL2RelayService;
+            log.info("Adding listener on DhcpL2RelayService");
+            dhcpL2RelayService.addListener(listener);
+        } else {
+            log.warn("Trying to bind DhcpL2RelayService but it is already bound");
+        }
+    }
+
+    protected void unbindDhcpL2RelayService(DhcpL2RelayService dhcpL2RelayService) {
+        if (this.dhcpL2RelayService == dhcpL2RelayService) {
+            log.info("Unbinding DhcpL2RelayService");
+            this.dhcpL2RelayService = null;
+            log.info("Removing listener on DhcpL2RelayService");
+            dhcpL2RelayService.removeListener(listener);
+        } else {
+            log.warn("Trying to unbind DhcpL2RelayService but it is already unbound");
+        }
+    }
+
     @Activate
     public void activate() {
-        authenticationService.addListener(listener);
         log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
-        authenticationService.removeListener(listener);
         log.info("Stopped");
     }