blob: 56486ec9e6efe3336c9443a1d4336ede258b0d73 [file] [log] [blame]
Jonathan Hartb4a42152015-12-08 17:06:30 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeye56c34d2017-08-03 13:39:30 -070017package org.onosproject.component.impl;
Jonathan Hartb4a42152015-12-08 17:06:30 -080018
Ray Milkeye56c34d2017-08-03 13:39:30 -070019import org.onosproject.component.ComponentService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.onosproject.core.ApplicationId;
21import org.osgi.service.component.annotations.Activate;
22import org.osgi.service.component.annotations.Component;
23import org.osgi.service.component.annotations.Deactivate;
24import org.osgi.service.component.annotations.Reference;
25import org.osgi.service.component.annotations.ReferenceCardinality;
26import org.osgi.service.component.runtime.ServiceComponentRuntime;
27import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO;
Jonathan Hartb4a42152015-12-08 17:06:30 -080028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Jon Hall4b0eda82017-03-24 10:49:38 -070031import java.util.Collections;
Jonathan Hartb4a42152015-12-08 17:06:30 -080032import java.util.Set;
33import java.util.concurrent.ConcurrentHashMap;
Jonathan Hartb4a42152015-12-08 17:06:30 -080034import java.util.concurrent.ScheduledExecutorService;
35import java.util.concurrent.TimeUnit;
36
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037import static java.util.concurrent.Executors.newScheduledThreadPool;
Jonathan Hartb4a42152015-12-08 17:06:30 -080038import static org.onlab.util.Tools.groupedThreads;
39
40/**
41 * Manages OSGi components.
42 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043@Component(immediate = true, service = ComponentService.class)
Jonathan Hartb4a42152015-12-08 17:06:30 -080044public class ComponentManager implements ComponentService {
45
46 private final Logger log = LoggerFactory.getLogger(getClass());
47
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048 private static final long TIMEOUT = 3000;
Jonathan Hartb4a42152015-12-08 17:06:30 -080049 private static final int POLLING_PERIOD_MS = 500;
Jonathan Hartb4a42152015-12-08 17:06:30 -080050 private static final int NUM_THREADS = 1;
51
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
53 protected ServiceComponentRuntime scrService;
Jonathan Hartb4a42152015-12-08 17:06:30 -080054
55 private Set<String> components;
56
57 private ScheduledExecutorService executor;
58
59 @Activate
60 private void activate() {
Jon Hall4b0eda82017-03-24 10:49:38 -070061 components = Collections.newSetFromMap(new ConcurrentHashMap<>());
Jonathan Hartb4a42152015-12-08 17:06:30 -080062
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063 executor = newScheduledThreadPool(NUM_THREADS, groupedThreads("onos/component", "%d", log));
Jonathan Hartb4a42152015-12-08 17:06:30 -080064 executor.scheduleAtFixedRate(() -> components.forEach(this::enableComponent),
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 0, POLLING_PERIOD_MS, TimeUnit.MILLISECONDS);
Jonathan Hartb4a42152015-12-08 17:06:30 -080066 log.info("Started");
67 }
68
69 @Deactivate
70 private void deactivate() {
71 executor.shutdownNow();
Jonathan Hartb4a42152015-12-08 17:06:30 -080072 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 Milkeyd84f89b2018-08-17 14:54:17 -070087 private ComponentDescriptionDTO getComponent(String name) {
88 for (ComponentDescriptionDTO component : scrService.getComponentDescriptionDTOs()) {
89 if (component.name.equals(name)) {
90 return component;
91 }
Jonathan Hartb4a42152015-12-08 17:06:30 -080092 }
Ray Milkeyd84f89b2018-08-17 14:54:17 -070093 return null;
94 }
Jonathan Hartb4a42152015-12-08 17:06:30 -080095
Ray Milkeyd84f89b2018-08-17 14:54:17 -070096 private void enableComponent(String name) {
97 ComponentDescriptionDTO component = getComponent(name);
98 if (component != null && !scrService.isComponentEnabled(component)) {
Jonathan Hartb4a42152015-12-08 17:06:30 -080099 log.info("Enabling component {}", name);
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700100 try {
101 scrService.enableComponent(component).timeout(TIMEOUT);
102 } catch (Exception e) {
103 throw new IllegalStateException("Unable to start component " + name, e);
104 }
Jonathan Hartb4a42152015-12-08 17:06:30 -0800105 }
106 }
107
108 private void disableComponent(String name) {
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700109 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 Hartb4a42152015-12-08 17:06:30 -0800117 }
Jonathan Hartb4a42152015-12-08 17:06:30 -0800118 }
119}