Jonathan Hart
Committed by Gerrit Code Review

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

Change-Id: I1e5eceef65c16040b75cf35ac88a497ba9a05412
...@@ -104,6 +104,7 @@ ...@@ -104,6 +104,7 @@
104 description="ONOS core incubator components"> 104 description="ONOS core incubator components">
105 <feature>onos-core</feature> 105 <feature>onos-core</feature>
106 <bundle>mvn:org.onosproject/onos-incubator-net/@ONOS-VERSION</bundle> 106 <bundle>mvn:org.onosproject/onos-incubator-net/@ONOS-VERSION</bundle>
107 + <bundle>mvn:org.onosproject/onos-incubator-core/@ONOS-VERSION</bundle>
107 <bundle>mvn:org.onosproject/onos-incubator-store/@ONOS-VERSION</bundle> 108 <bundle>mvn:org.onosproject/onos-incubator-store/@ONOS-VERSION</bundle>
108 <bundle>mvn:org.onosproject/onos-incubator-rpc/@ONOS-VERSION</bundle> 109 <bundle>mvn:org.onosproject/onos-incubator-rpc/@ONOS-VERSION</bundle>
109 </feature> 110 </feature>
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.incubator.component;
18 +
19 +import org.onosproject.core.ApplicationId;
20 +
21 +/**
22 + * Service for managing the components in the system.
23 + */
24 +public interface ComponentService {
25 +
26 + /**
27 + * Activates the component identified by the given name. If the component
28 + * is not currently available, it will be activated when it becomes
29 + * available.
30 + *
31 + * @param appId application ID of the requesting application
32 + * @param name fully-qualified name of the component to activate
33 + */
34 + void activate(ApplicationId appId, String name);
35 +
36 + /**
37 + * Deactivates the component identified by the given name.
38 + *
39 + * @param appId application ID of the requesting application
40 + * @param name fully-qualified name of the component to deactivate
41 + */
42 + void deactivate(ApplicationId appId, String name);
43 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Component management system.
19 + */
20 +package org.onosproject.incubator.component;
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +
3 +<!--
4 + ~ Copyright 2015 Open Networking Laboratory
5 + ~
6 + ~ Licensed under the Apache License, Version 2.0 (the "License");
7 + ~ you may not use this file except in compliance with the License.
8 + ~ You may obtain a copy of the License at
9 + ~
10 + ~ http://www.apache.org/licenses/LICENSE-2.0
11 + ~
12 + ~ Unless required by applicable law or agreed to in writing, software
13 + ~ distributed under the License is distributed on an "AS IS" BASIS,
14 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 + ~ See the License for the specific language governing permissions and
16 + ~ limitations under the License.
17 + -->
18 +
19 +<project xmlns="http://maven.apache.org/POM/4.0.0"
20 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
22 + <modelVersion>4.0.0</modelVersion>
23 +
24 + <parent>
25 + <groupId>org.onosproject</groupId>
26 + <artifactId>onos-incubator</artifactId>
27 + <version>1.5.0-SNAPSHOT</version>
28 + <relativePath>../pom.xml</relativePath>
29 + </parent>
30 +
31 + <artifactId>onos-incubator-core</artifactId>
32 + <packaging>bundle</packaging>
33 +
34 + <description>ONOS incubating core</description>
35 +
36 + <dependencies>
37 + <dependency>
38 + <groupId>org.onosproject</groupId>
39 + <artifactId>onos-incubator-api</artifactId>
40 + </dependency>
41 + <dependency>
42 + <groupId>com.google.guava</groupId>
43 + <artifactId>guava-testlib</artifactId>
44 + <scope>test</scope>
45 + </dependency>
46 + <dependency>
47 + <groupId>org.easymock</groupId>
48 + <artifactId>easymock</artifactId>
49 + <scope>test</scope>
50 + </dependency>
51 + <dependency>
52 + <groupId>org.apache.felix</groupId>
53 + <artifactId>org.apache.felix.scr.annotations</artifactId>
54 + <version>1.9.8</version>
55 + </dependency>
56 + <dependency>
57 + <groupId>org.apache.felix</groupId>
58 + <artifactId>org.apache.felix.scr</artifactId>
59 + <version>1.8.2</version>
60 + </dependency>
61 + <dependency>
62 + <groupId>org.osgi</groupId>
63 + <artifactId>org.osgi.core</artifactId>
64 + </dependency>
65 + </dependencies>
66 +
67 + <build>
68 + <plugins>
69 + <plugin>
70 + <groupId>org.apache.felix</groupId>
71 + <artifactId>maven-scr-plugin</artifactId>
72 + </plugin>
73 + </plugins>
74 + </build>
75 +
76 +</project>
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.incubator.component.impl;
18 +
19 +import com.google.common.collect.Sets;
20 +import org.apache.felix.scr.ScrService;
21 +import org.apache.felix.scr.annotations.Activate;
22 +import org.apache.felix.scr.annotations.Component;
23 +import org.apache.felix.scr.annotations.Deactivate;
24 +import org.apache.felix.scr.annotations.Reference;
25 +import org.apache.felix.scr.annotations.ReferenceCardinality;
26 +import org.apache.felix.scr.annotations.Service;
27 +import org.onosproject.core.ApplicationId;
28 +import org.onosproject.incubator.component.ComponentService;
29 +import org.slf4j.Logger;
30 +import org.slf4j.LoggerFactory;
31 +
32 +import java.util.Set;
33 +import java.util.concurrent.ConcurrentHashMap;
34 +import java.util.concurrent.Executors;
35 +import java.util.concurrent.ScheduledExecutorService;
36 +import java.util.concurrent.TimeUnit;
37 +
38 +import static org.onlab.util.Tools.groupedThreads;
39 +
40 +/**
41 + * Manages OSGi components.
42 + */
43 +@Service
44 +@Component(immediate = true)
45 +public class ComponentManager implements ComponentService {
46 +
47 + private final Logger log = LoggerFactory.getLogger(getClass());
48 +
49 + private static final int POLLING_PERIOD_MS = 500;
50 +
51 + private static final int NUM_THREADS = 1;
52 +
53 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 + protected ScrService scrService;
55 +
56 + private Set<String> components;
57 +
58 + private ScheduledExecutorService executor;
59 +
60 + @Activate
61 + private void activate() {
62 + components = Sets.newSetFromMap(new ConcurrentHashMap<>());
63 +
64 + executor = Executors.newScheduledThreadPool(NUM_THREADS,
65 + groupedThreads("onos/component", "%d"));
66 +
67 + executor.scheduleAtFixedRate(() -> components.forEach(this::enableComponent),
68 + 0, POLLING_PERIOD_MS, TimeUnit.MILLISECONDS);
69 +
70 + log.info("Started");
71 + }
72 +
73 + @Deactivate
74 + private void deactivate() {
75 + executor.shutdownNow();
76 +
77 + log.info("Stopped");
78 + }
79 +
80 + @Override
81 + public void activate(ApplicationId appId, String name) {
82 + components.add(name);
83 + enableComponent(name);
84 + }
85 +
86 + @Override
87 + public void deactivate(ApplicationId appId, String name) {
88 + components.remove(name);
89 + disableComponent(name);
90 + }
91 +
92 + private void enableComponent(String name) {
93 + org.apache.felix.scr.Component[] components = scrService.getComponents(name);
94 +
95 + if (components == null || components.length == 0) {
96 + return;
97 + }
98 +
99 + org.apache.felix.scr.Component component = components[0];
100 +
101 + if (component.getState() != org.apache.felix.scr.Component.STATE_ACTIVE) {
102 + log.info("Enabling component {}", name);
103 + component.enable();
104 + }
105 + }
106 +
107 + private void disableComponent(String name) {
108 + org.apache.felix.scr.Component[] components = scrService.getComponents(name);
109 +
110 + if (components == null || components.length == 0) {
111 + return;
112 + }
113 +
114 + log.info("Disabling component {}", name);
115 +
116 + components[0].disable();
117 + }
118 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Implementation of component management system.
19 + */
20 +package org.onosproject.incubator.component.impl;
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
33 33
34 <modules> 34 <modules>
35 <module>api</module> 35 <module>api</module>
36 + <module>core</module>
36 <module>net</module> 37 <module>net</module>
37 <module>store</module> 38 <module>store</module>
38 <module>rpc</module> 39 <module>rpc</module>
......