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