blob: f8ae422642ac8a3375ff6d9f598afed5eb35b2be [file] [log] [blame]
Jonathan Hartb4a42152015-12-08 17:06:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hartb4a42152015-12-08 17:06:30 -08003 *
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
17package org.onosproject.incubator.component.impl;
18
19import com.google.common.collect.Sets;
20import org.apache.felix.scr.ScrService;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.incubator.component.ComponentService;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.Set;
33import java.util.concurrent.ConcurrentHashMap;
34import java.util.concurrent.Executors;
35import java.util.concurrent.ScheduledExecutorService;
36import java.util.concurrent.TimeUnit;
37
38import static org.onlab.util.Tools.groupedThreads;
39
40/**
41 * Manages OSGi components.
42 */
43@Service
44@Component(immediate = true)
45public 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,
Yuta HIGUCHI1624df12016-07-21 16:54:33 -070065 groupedThreads("onos/component", "%d", log));
Jonathan Hartb4a42152015-12-08 17:06:30 -080066
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
Jonathan Hart6344f572015-12-15 08:26:25 -0800101 if (component.getState() == org.apache.felix.scr.Component.STATE_DISABLED) {
Jonathan Hartb4a42152015-12-08 17:06:30 -0800102 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}