Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 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 | |
Ray Milkey | e56c34d | 2017-08-03 13:39:30 -0700 | [diff] [blame] | 17 | package org.onosproject.component.impl; |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 18 | |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.ScrService; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onosproject.core.ApplicationId; |
Ray Milkey | e56c34d | 2017-08-03 13:39:30 -0700 | [diff] [blame] | 27 | import org.onosproject.component.ComponentService; |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Jon Hall | 4b0eda8 | 2017-03-24 10:49:38 -0700 | [diff] [blame] | 31 | import java.util.Collections; |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 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() { |
Jon Hall | 4b0eda8 | 2017-03-24 10:49:38 -0700 | [diff] [blame] | 62 | components = Collections.newSetFromMap(new ConcurrentHashMap<>()); |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 63 | |
| 64 | executor = Executors.newScheduledThreadPool(NUM_THREADS, |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 65 | groupedThreads("onos/component", "%d", log)); |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 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 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 101 | if (component.getState() == org.apache.felix.scr.Component.STATE_DISABLED) { |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 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 | } |