Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -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 | |
| 17 | package org.onosproject.cluster.impl; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.runtime.ServiceComponentRuntime; |
| 20 | import org.osgi.service.component.annotations.Activate; |
| 21 | import org.osgi.service.component.annotations.Component; |
| 22 | import org.osgi.service.component.annotations.Deactivate; |
| 23 | import org.osgi.service.component.annotations.Reference; |
| 24 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 25 | import org.apache.karaf.features.Feature; |
| 26 | import org.apache.karaf.features.FeaturesService; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.ClusterAdminService; |
| 28 | import org.osgi.framework.Bundle; |
| 29 | import org.osgi.framework.BundleContext; |
| 30 | import org.osgi.service.component.ComponentContext; |
Jordan Halterman | dfc4855 | 2018-11-02 11:08:30 -0700 | [diff] [blame] | 31 | import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame^] | 36 | import java.util.concurrent.Executors; |
| 37 | import java.util.concurrent.ScheduledExecutorService; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 38 | import java.util.concurrent.ScheduledFuture; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame^] | 41 | import static org.onlab.util.Tools.groupedThreads; |
| 42 | |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Monitors the system to make sure that all bundles and their components |
| 45 | * are properly activated and keeps the cluster node service appropriately |
| 46 | * updated. |
| 47 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | @Component(immediate = true) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 49 | public class ComponentsMonitor { |
| 50 | |
| 51 | private Logger log = LoggerFactory.getLogger(getClass()); |
| 52 | |
| 53 | private static final long PERIOD = 2500; |
| 54 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 56 | protected FeaturesService featuresService; |
| 57 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 59 | protected ServiceComponentRuntime scrService; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 60 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 62 | protected ClusterAdminService clusterAdminService; |
| 63 | |
| 64 | private BundleContext bundleContext; |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame^] | 65 | |
| 66 | private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( |
| 67 | groupedThreads("components-monitor", "%d", log)); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 68 | private ScheduledFuture<?> poller; |
| 69 | |
| 70 | @Activate |
| 71 | protected void activate(ComponentContext context) { |
| 72 | bundleContext = context.getBundleContext(); |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame^] | 73 | poller = executor.scheduleAtFixedRate(this::checkStartedState, PERIOD, PERIOD, TimeUnit.MILLISECONDS); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 74 | log.info("Started"); |
| 75 | } |
| 76 | |
| 77 | @Deactivate |
| 78 | protected void deactivate() { |
| 79 | poller.cancel(false); |
Jordan Halterman | af3461c | 2019-01-07 10:03:46 -0800 | [diff] [blame^] | 80 | executor.shutdownNow(); |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 81 | log.info("Stopped"); |
| 82 | } |
| 83 | |
| 84 | private void checkStartedState() { |
| 85 | clusterAdminService.markFullyStarted(isFullyStarted()); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Scans the system to make sure that all bundles and their components |
| 90 | * are fully started. |
| 91 | * |
| 92 | * @return true if all bundles and their components are active |
| 93 | */ |
| 94 | private boolean isFullyStarted() { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 95 | try { |
| 96 | for (Feature feature : featuresService.listInstalledFeatures()) { |
| 97 | if (!isFullyStarted(feature)) { |
| 98 | return false; |
| 99 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 100 | } |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 101 | return true; |
| 102 | } catch (Exception ex) { |
| 103 | return false; |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 104 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private boolean isFullyStarted(Feature feature) { |
Ray Milkey | f13feb8 | 2016-08-30 10:38:59 -0700 | [diff] [blame] | 108 | try { |
| 109 | return feature.getBundles().stream() |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 110 | .map(info -> bundleContext.getBundle()) |
Ray Milkey | f13feb8 | 2016-08-30 10:38:59 -0700 | [diff] [blame] | 111 | .allMatch(this::isFullyStarted); |
| 112 | } catch (NullPointerException npe) { |
| 113 | // FIXME: Remove this catch block when Felix fixes the bug |
| 114 | // Due to a bug in the Felix implementation, this can throw an NPE. |
| 115 | // Catch the error and do something sensible with it. |
| 116 | return false; |
| 117 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private boolean isFullyStarted(Bundle bundle) { |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 121 | for (ComponentDescriptionDTO component : scrService.getComponentDescriptionDTOs(bundle)) { |
Jordan Halterman | dfc4855 | 2018-11-02 11:08:30 -0700 | [diff] [blame] | 122 | if (scrService.isComponentEnabled(component)) { |
| 123 | for (ComponentConfigurationDTO config : scrService.getComponentConfigurationDTOs(component)) { |
| 124 | if (config.state != ComponentConfigurationDTO.ACTIVE) { |
| 125 | return false; |
| 126 | } |
| 127 | } |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
Thomas Vachuska | 7a8de84 | 2016-03-07 20:56:35 -0800 | [diff] [blame] | 133 | } |