blob: 7a0295e1ac2f65ca7d53e009a846e32b6580296c [file] [log] [blame]
Thomas Vachuska7a8de842016-03-07 20:56:35 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska7a8de842016-03-07 20:56:35 -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
17package org.onosproject.cluster.impl;
18
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.runtime.ServiceComponentRuntime;
20import org.osgi.service.component.annotations.Activate;
21import org.osgi.service.component.annotations.Component;
22import org.osgi.service.component.annotations.Deactivate;
23import org.osgi.service.component.annotations.Reference;
24import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska7a8de842016-03-07 20:56:35 -080025import org.apache.karaf.features.Feature;
26import org.apache.karaf.features.FeaturesService;
27import org.onlab.util.SharedScheduledExecutors;
28import org.onosproject.cluster.ClusterAdminService;
29import org.osgi.framework.Bundle;
30import org.osgi.framework.BundleContext;
31import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070032import org.osgi.service.component.runtime.dto.ComponentDescriptionDTO;
Thomas Vachuska7a8de842016-03-07 20:56:35 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.concurrent.ScheduledFuture;
37import java.util.concurrent.TimeUnit;
38
39/**
40 * Monitors the system to make sure that all bundles and their components
41 * are properly activated and keeps the cluster node service appropriately
42 * updated.
43 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044@Component(immediate = true)
Thomas Vachuska7a8de842016-03-07 20:56:35 -080045public class ComponentsMonitor {
46
47 private Logger log = LoggerFactory.getLogger(getClass());
48
49 private static final long PERIOD = 2500;
50
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska7a8de842016-03-07 20:56:35 -080052 protected FeaturesService featuresService;
53
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY)
55 protected ServiceComponentRuntime scrService;
Thomas Vachuska7a8de842016-03-07 20:56:35 -080056
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska7a8de842016-03-07 20:56:35 -080058 protected ClusterAdminService clusterAdminService;
59
60 private BundleContext bundleContext;
61 private ScheduledFuture<?> poller;
62
63 @Activate
64 protected void activate(ComponentContext context) {
65 bundleContext = context.getBundleContext();
66 poller = SharedScheduledExecutors.getSingleThreadExecutor()
67 .scheduleAtFixedRate(this::checkStartedState, PERIOD,
68 PERIOD, TimeUnit.MILLISECONDS);
69 log.info("Started");
70 }
71
72 @Deactivate
73 protected void deactivate() {
74 poller.cancel(false);
75 log.info("Stopped");
76 }
77
78 private void checkStartedState() {
79 clusterAdminService.markFullyStarted(isFullyStarted());
80 }
81
82 /**
83 * Scans the system to make sure that all bundles and their components
84 * are fully started.
85 *
86 * @return true if all bundles and their components are active
87 */
88 private boolean isFullyStarted() {
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 try {
90 for (Feature feature : featuresService.listInstalledFeatures()) {
91 if (!isFullyStarted(feature)) {
92 return false;
93 }
Thomas Vachuska7a8de842016-03-07 20:56:35 -080094 }
Ray Milkeyd84f89b2018-08-17 14:54:17 -070095 return true;
96 } catch (Exception ex) {
97 return false;
Thomas Vachuska7a8de842016-03-07 20:56:35 -080098 }
Thomas Vachuska7a8de842016-03-07 20:56:35 -080099 }
100
101 private boolean isFullyStarted(Feature feature) {
Ray Milkeyf13feb82016-08-30 10:38:59 -0700102 try {
103 return feature.getBundles().stream()
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700104 .map(info -> bundleContext.getBundle())
Ray Milkeyf13feb82016-08-30 10:38:59 -0700105 .allMatch(this::isFullyStarted);
106 } catch (NullPointerException npe) {
107 // FIXME: Remove this catch block when Felix fixes the bug
108 // Due to a bug in the Felix implementation, this can throw an NPE.
109 // Catch the error and do something sensible with it.
110 return false;
111 }
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800112 }
113
114 private boolean isFullyStarted(Bundle bundle) {
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700115 for (ComponentDescriptionDTO component : scrService.getComponentDescriptionDTOs(bundle)) {
116 if (!scrService.isComponentEnabled(component)) {
117 return false;
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800118 }
119 }
120 return true;
121 }
122
Thomas Vachuska7a8de842016-03-07 20:56:35 -0800123}