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 | |
Ray Milkey | e56c34d | 2017-08-03 13:39:30 -0700 | [diff] [blame] | 19 | import org.onosproject.component.ComponentService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.osgi.service.component.annotations.Activate; |
| 22 | import org.osgi.service.component.annotations.Component; |
| 23 | import org.osgi.service.component.annotations.Deactivate; |
| 24 | import org.osgi.service.component.annotations.Reference; |
| 25 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 26 | import org.osgi.service.component.runtime.ServiceComponentRuntime; |
| 27 | import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO; |
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; |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 34 | import java.util.concurrent.ScheduledExecutorService; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 38 | import static org.onlab.util.Tools.groupedThreads; |
| 39 | |
| 40 | /** |
| 41 | * Manages OSGi components. |
| 42 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 43 | @Component(immediate = true, service = ComponentService.class) |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 44 | public class ComponentManager implements ComponentService { |
| 45 | |
| 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 47 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | private static final long TIMEOUT = 3000; |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 49 | private static final int POLLING_PERIOD_MS = 500; |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 50 | private static final int NUM_THREADS = 1; |
| 51 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 53 | protected ServiceComponentRuntime scrService; |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 54 | |
| 55 | private Set<String> components; |
| 56 | |
| 57 | private ScheduledExecutorService executor; |
| 58 | |
| 59 | @Activate |
| 60 | private void activate() { |
Jon Hall | 4b0eda8 | 2017-03-24 10:49:38 -0700 | [diff] [blame] | 61 | components = Collections.newSetFromMap(new ConcurrentHashMap<>()); |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 62 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | executor = newScheduledThreadPool(NUM_THREADS, groupedThreads("onos/component", "%d", log)); |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 64 | executor.scheduleAtFixedRate(() -> components.forEach(this::enableComponent), |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 65 | 0, POLLING_PERIOD_MS, TimeUnit.MILLISECONDS); |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 66 | log.info("Started"); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | private void deactivate() { |
| 71 | executor.shutdownNow(); |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 72 | log.info("Stopped"); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void activate(ApplicationId appId, String name) { |
| 77 | components.add(name); |
| 78 | enableComponent(name); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void deactivate(ApplicationId appId, String name) { |
| 83 | components.remove(name); |
| 84 | disableComponent(name); |
| 85 | } |
| 86 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 87 | private ComponentDescriptionDTO getComponent(String name) { |
| 88 | for (ComponentDescriptionDTO component : scrService.getComponentDescriptionDTOs()) { |
| 89 | if (component.name.equals(name)) { |
| 90 | return component; |
| 91 | } |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 92 | } |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 93 | return null; |
| 94 | } |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 95 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 96 | private void enableComponent(String name) { |
| 97 | ComponentDescriptionDTO component = getComponent(name); |
| 98 | if (component != null && !scrService.isComponentEnabled(component)) { |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 99 | log.info("Enabling component {}", name); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 100 | try { |
| 101 | scrService.enableComponent(component).timeout(TIMEOUT); |
| 102 | } catch (Exception e) { |
| 103 | throw new IllegalStateException("Unable to start component " + name, e); |
| 104 | } |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | private void disableComponent(String name) { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 109 | ComponentDescriptionDTO component = getComponent(name); |
| 110 | if (component != null && scrService.isComponentEnabled(component)) { |
| 111 | log.info("Disabling component {}", name); |
| 112 | try { |
| 113 | scrService.disableComponent(component).timeout(TIMEOUT); |
| 114 | } catch (Exception e) { |
| 115 | throw new IllegalStateException("Unable to start component " + name, e); |
| 116 | } |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 117 | } |
Jonathan Hart | b4a4215 | 2015-12-08 17:06:30 -0800 | [diff] [blame] | 118 | } |
| 119 | } |