[Falcon] Add ComponentService that allows applications to select which components are loaded.

Change-Id: I1e5eceef65c16040b75cf35ac88a497ba9a05412
diff --git a/features/features.xml b/features/features.xml
index 0d9c2c3..8c611dc 100644
--- a/features/features.xml
+++ b/features/features.xml
@@ -104,6 +104,7 @@
              description="ONOS core incubator components">
         <feature>onos-core</feature>
         <bundle>mvn:org.onosproject/onos-incubator-net/@ONOS-VERSION</bundle>
+        <bundle>mvn:org.onosproject/onos-incubator-core/@ONOS-VERSION</bundle>
         <bundle>mvn:org.onosproject/onos-incubator-store/@ONOS-VERSION</bundle>
         <bundle>mvn:org.onosproject/onos-incubator-rpc/@ONOS-VERSION</bundle>
     </feature>
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/component/ComponentService.java b/incubator/api/src/main/java/org/onosproject/incubator/component/ComponentService.java
new file mode 100644
index 0000000..653f7df
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/component/ComponentService.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.incubator.component;
+
+import org.onosproject.core.ApplicationId;
+
+/**
+ * Service for managing the components in the system.
+ */
+public interface ComponentService {
+
+    /**
+     * Activates the component identified by the given name. If the component
+     * is not currently available, it will be activated when it becomes
+     * available.
+     *
+     * @param appId application ID of the requesting application
+     * @param name fully-qualified name of the component to activate
+     */
+    void activate(ApplicationId appId, String name);
+
+    /**
+     * Deactivates the component identified by the given name.
+     *
+     * @param appId application ID of the requesting application
+     * @param name fully-qualified name of the component to deactivate
+     */
+    void deactivate(ApplicationId appId, String name);
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/component/package-info.java b/incubator/api/src/main/java/org/onosproject/incubator/component/package-info.java
new file mode 100644
index 0000000..0354eaf
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/component/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Component management system.
+ */
+package org.onosproject.incubator.component;
diff --git a/incubator/core/pom.xml b/incubator/core/pom.xml
new file mode 100644
index 0000000..d494065
--- /dev/null
+++ b/incubator/core/pom.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  ~ Copyright 2015 Open Networking Laboratory
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-incubator</artifactId>
+        <version>1.5.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-incubator-core</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>ONOS incubating core</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-incubator-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava-testlib</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymock</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <version>1.9.8</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr</artifactId>
+            <version>1.8.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/incubator/core/src/main/java/org/onosproject/incubator/component/impl/ComponentManager.java b/incubator/core/src/main/java/org/onosproject/incubator/component/impl/ComponentManager.java
new file mode 100644
index 0000000..61af700
--- /dev/null
+++ b/incubator/core/src/main/java/org/onosproject/incubator/component/impl/ComponentManager.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.incubator.component.impl;
+
+import com.google.common.collect.Sets;
+import org.apache.felix.scr.ScrService;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.incubator.component.ComponentService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static org.onlab.util.Tools.groupedThreads;
+
+/**
+ * Manages OSGi components.
+ */
+@Service
+@Component(immediate = true)
+public class ComponentManager implements ComponentService {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private static final int POLLING_PERIOD_MS = 500;
+
+    private static final int NUM_THREADS = 1;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ScrService scrService;
+
+    private Set<String> components;
+
+    private ScheduledExecutorService executor;
+
+    @Activate
+    private void activate() {
+        components = Sets.newSetFromMap(new ConcurrentHashMap<>());
+
+        executor = Executors.newScheduledThreadPool(NUM_THREADS,
+                groupedThreads("onos/component", "%d"));
+
+        executor.scheduleAtFixedRate(() -> components.forEach(this::enableComponent),
+                0, POLLING_PERIOD_MS, TimeUnit.MILLISECONDS);
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    private void deactivate() {
+        executor.shutdownNow();
+
+        log.info("Stopped");
+    }
+
+    @Override
+    public void activate(ApplicationId appId, String name) {
+        components.add(name);
+        enableComponent(name);
+    }
+
+    @Override
+    public void deactivate(ApplicationId appId, String name) {
+        components.remove(name);
+        disableComponent(name);
+    }
+
+    private void enableComponent(String name) {
+        org.apache.felix.scr.Component[] components = scrService.getComponents(name);
+
+        if (components == null || components.length == 0) {
+            return;
+        }
+
+        org.apache.felix.scr.Component component = components[0];
+
+        if (component.getState() != org.apache.felix.scr.Component.STATE_ACTIVE) {
+            log.info("Enabling component {}", name);
+            component.enable();
+        }
+    }
+
+    private void disableComponent(String name) {
+        org.apache.felix.scr.Component[] components = scrService.getComponents(name);
+
+        if (components == null || components.length == 0) {
+            return;
+        }
+
+        log.info("Disabling component {}", name);
+
+        components[0].disable();
+    }
+}
diff --git a/incubator/core/src/main/java/org/onosproject/incubator/component/impl/package-info.java b/incubator/core/src/main/java/org/onosproject/incubator/component/impl/package-info.java
new file mode 100644
index 0000000..792488c
--- /dev/null
+++ b/incubator/core/src/main/java/org/onosproject/incubator/component/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of component management system.
+ */
+package org.onosproject.incubator.component.impl;
diff --git a/incubator/pom.xml b/incubator/pom.xml
index be6102e..acde452 100644
--- a/incubator/pom.xml
+++ b/incubator/pom.xml
@@ -33,6 +33,7 @@
 
     <modules>
         <module>api</module>
+        <module>core</module>
         <module>net</module>
         <module>store</module>
         <module>rpc</module>