2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.blueprint;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Lists;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import java.lang.management.ManagementFactory;
14 import java.util.AbstractMap.SimpleEntry;
15 import java.util.ArrayList;
16 import java.util.Dictionary;
17 import java.util.Hashtable;
18 import java.util.LinkedHashSet;
19 import java.util.List;
20 import java.util.Map.Entry;
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.TimeUnit;
26 import javax.annotation.Nullable;
27 import javax.management.InstanceNotFoundException;
28 import javax.management.ObjectName;
29 import javax.xml.parsers.ParserConfigurationException;
30 import org.apache.aries.blueprint.services.BlueprintExtenderService;
31 import org.apache.aries.util.AriesFrameworkUtil;
32 import org.opendaylight.controller.config.api.ConfigRegistry;
33 import org.opendaylight.controller.config.api.ConflictingVersionException;
34 import org.opendaylight.controller.config.api.ModuleIdentifier;
35 import org.opendaylight.controller.config.api.ValidationException;
36 import org.opendaylight.controller.config.facade.xml.ConfigExecution;
37 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
38 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
39 import org.opendaylight.controller.config.facade.xml.TestOption;
40 import org.opendaylight.controller.config.facade.xml.mapping.config.Config;
41 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
42 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
43 import org.opendaylight.controller.config.util.xml.DocumentedException;
44 import org.opendaylight.controller.config.util.xml.XmlElement;
45 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
46 import org.opendaylight.controller.config.util.xml.XmlUtil;
47 import org.osgi.framework.Bundle;
48 import org.osgi.framework.BundleContext;
49 import org.osgi.framework.ServiceReference;
50 import org.osgi.framework.ServiceRegistration;
51 import org.osgi.service.blueprint.container.EventConstants;
52 import org.osgi.service.event.EventHandler;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.w3c.dom.Document;
56 import org.w3c.dom.Element;
59 * Implementation of the BlueprintContainerRestartService.
61 * @author Thomas Pantelis
63 class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintContainerRestartService {
64 private static final Logger LOG = LoggerFactory.getLogger(BlueprintContainerRestartServiceImpl.class);
65 private static final String CONFIG_MODULE_NAMESPACE_PROP = "config-module-namespace";
66 private static final String CONFIG_MODULE_NAME_PROP = "config-module-name";
67 private static final String CONFIG_INSTANCE_NAME_PROP = "config-instance-name";
69 private final ExecutorService restartExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
70 .setDaemon(true).setNameFormat("BlueprintContainerRestartService").build());
71 private final BlueprintExtenderService blueprintExtenderService;
73 BlueprintContainerRestartServiceImpl(BlueprintExtenderService blueprintExtenderService) {
74 this.blueprintExtenderService = blueprintExtenderService;
77 public void restartContainer(final Bundle bundle, final List<Object> paths) {
78 if (restartExecutor.isShutdown()) {
82 LOG.debug("restartContainer for bundle {}", bundle);
84 restartExecutor.execute(() -> {
85 blueprintExtenderService.destroyContainer(bundle, blueprintExtenderService.getContainer(bundle));
86 blueprintExtenderService.createContainer(bundle, paths);
91 public void restartContainerAndDependents(final Bundle bundle) {
92 if (restartExecutor.isShutdown()) {
96 LOG.debug("restartContainerAndDependents for bundle {}", bundle);
98 restartExecutor.execute(() -> restartContainerAndDependentsInternal(bundle));
101 private void restartContainerAndDependentsInternal(Bundle forBundle) {
102 // We use a LinkedHashSet to preserve insertion order as we walk the service usage hierarchy.
103 Set<Bundle> containerBundlesSet = new LinkedHashSet<>();
104 List<Entry<String, ModuleIdentifier>> configModules = new ArrayList<>();
105 findDependentContainersRecursively(forBundle, containerBundlesSet, configModules);
107 List<Bundle> containerBundles = new ArrayList<>(containerBundlesSet);
109 LOG.info("Restarting blueprint containers for bundle {} and its dependent bundles {}", forBundle,
110 containerBundles.subList(1, containerBundles.size()));
112 // Destroy the containers in reverse order with 'forBundle' last, ie bottom-up in the service tree.
113 for (Bundle bundle: Lists.reverse(containerBundles)) {
114 blueprintExtenderService.destroyContainer(bundle, blueprintExtenderService.getContainer(bundle));
117 // The blueprint containers are created asynchronously so we register a handler for blueprint events
118 // that are sent when a container is complete, successful or not. The CountDownLatch tells when all
119 // containers are complete. This is done to ensure all blueprint containers are finished before we
120 // restart config modules.
121 final CountDownLatch containerCreationComplete = new CountDownLatch(containerBundles.size());
122 ServiceRegistration<?> eventHandlerReg = registerEventHandler(forBundle.getBundleContext(), event -> {
123 LOG.debug("handleEvent {} for bundle {}", event.getTopic(), event.getProperty(EventConstants.BUNDLE));
124 if (containerBundles.contains(event.getProperty(EventConstants.BUNDLE))) {
125 containerCreationComplete.countDown();
129 // Restart the containers top-down starting with 'forBundle'.
130 for (Bundle bundle: containerBundles) {
131 List<Object> paths = BlueprintBundleTracker.findBlueprintPaths(bundle);
133 LOG.info("Restarting blueprint container for bundle {} with paths {}", bundle, paths);
135 blueprintExtenderService.createContainer(bundle, paths);
139 containerCreationComplete.await(5, TimeUnit.MINUTES);
140 } catch (InterruptedException e) {
141 LOG.debug("CountDownLatch await was interrupted - returning");
145 AriesFrameworkUtil.safeUnregisterService(eventHandlerReg);
147 // Now restart any associated config system Modules.
148 restartConfigModules(forBundle.getBundleContext(), configModules);
151 private void restartConfigModules(BundleContext bundleContext, List<Entry<String,
152 ModuleIdentifier>> configModules) {
153 if (configModules.isEmpty()) {
157 ServiceReference<ConfigSubsystemFacadeFactory> configFacadeFactoryRef = bundleContext
158 .getServiceReference(ConfigSubsystemFacadeFactory.class);
159 if (configFacadeFactoryRef == null) {
160 LOG.debug("ConfigSubsystemFacadeFactory service reference not found");
164 ConfigSubsystemFacadeFactory configFacadeFactory = bundleContext.getService(configFacadeFactoryRef);
165 if (configFacadeFactory == null) {
166 LOG.debug("ConfigSubsystemFacadeFactory service not found");
170 ConfigSubsystemFacade configFacade = configFacadeFactory.createFacade("BlueprintContainerRestartService");
172 restartConfigModules(configModules, configFacade);
173 } catch (ParserConfigurationException | DocumentedException | ValidationException
174 | ConflictingVersionException e) {
175 LOG.error("Error restarting config modules", e);
177 configFacade.close();
178 bundleContext.ungetService(configFacadeFactoryRef);
183 private void restartConfigModules(List<Entry<String, ModuleIdentifier>> configModules,
184 ConfigSubsystemFacade configFacade) throws ParserConfigurationException, DocumentedException,
185 ValidationException, ConflictingVersionException {
187 Document document = XmlUtil.newDocument();
188 Element dataElement = XmlUtil.createElement(document, XmlMappingConstants.DATA_KEY, Optional.<String>absent());
189 Element modulesElement = XmlUtil.createElement(document, XmlMappingConstants.MODULES_KEY,
190 Optional.of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
191 dataElement.appendChild(modulesElement);
193 Config configMapping = configFacade.getConfigMapping();
195 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory.getPlatformMBeanServer());
196 for (Entry<String, ModuleIdentifier> entry: configModules) {
197 String moduleNamespace = entry.getKey();
198 ModuleIdentifier moduleId = entry.getValue();
200 ObjectName instanceON = configRegistryClient.lookupConfigBean(moduleId.getFactoryName(),
201 moduleId.getInstanceName());
203 LOG.debug("Found config module instance ObjectName: {}", instanceON);
205 Element moduleElement = configMapping.moduleToXml(moduleNamespace, moduleId.getFactoryName(),
206 moduleId.getInstanceName(), instanceON, document);
207 modulesElement.appendChild(moduleElement);
208 } catch (InstanceNotFoundException e) {
209 LOG.warn("Error looking up config module: namespace {}, module name {}, instance {}",
210 moduleNamespace, moduleId.getFactoryName(), moduleId.getInstanceName(), e);
214 if (LOG.isDebugEnabled()) {
215 LOG.debug("Pushing config xml: {}", XmlUtil.toString(dataElement));
218 ConfigExecution execution = new ConfigExecution(configMapping, XmlElement.fromDomElement(dataElement),
219 TestOption.testThenSet, EditStrategyType.recreate);
220 configFacade.executeConfigExecution(execution);
221 configFacade.commitSilentTransaction();
225 * Recursively finds the services registered by the given bundle and the bundles using those services.
226 * User bundles that have an associated blueprint container are added to containerBundles. In addition,
227 * if a registered service has an associated config system Module, as determined via the presence of
228 * certain service properties, the ModuleIdentifier is added to the configModules list.
230 * @param bundle the bundle to traverse
231 * @param containerBundles the current set of bundles containing blueprint containers
233 private void findDependentContainersRecursively(Bundle bundle, Set<Bundle> containerBundles,
234 List<Entry<String, ModuleIdentifier>> configModules) {
235 if (!containerBundles.add(bundle)) {
236 // Already seen this bundle...
240 ServiceReference<?>[] references = bundle.getRegisteredServices();
241 if (references != null) {
242 for (ServiceReference<?> reference : references) {
243 possiblyAddConfigModuleIdentifier(reference, configModules);
245 Bundle[] usingBundles = reference.getUsingBundles();
246 if (usingBundles != null) {
247 for (Bundle usingBundle : usingBundles) {
248 if (blueprintExtenderService.getContainer(usingBundle) != null) {
249 findDependentContainersRecursively(usingBundle, containerBundles, configModules);
257 private void possiblyAddConfigModuleIdentifier(ServiceReference<?> reference,
258 List<Entry<String, ModuleIdentifier>> configModules) {
259 Object moduleNamespace = reference.getProperty(CONFIG_MODULE_NAMESPACE_PROP);
260 if (moduleNamespace == null) {
264 String moduleName = getRequiredConfigModuleProperty(CONFIG_MODULE_NAME_PROP, moduleNamespace,
266 String instanceName = getRequiredConfigModuleProperty(CONFIG_INSTANCE_NAME_PROP, moduleNamespace,
268 if (moduleName == null || instanceName == null) {
272 LOG.debug("Found service with config module: namespace {}, module name {}, instance {}",
273 moduleNamespace, moduleName, instanceName);
275 configModules.add(new SimpleEntry<>(moduleNamespace.toString(),
276 new ModuleIdentifier(moduleName, instanceName)));
280 private String getRequiredConfigModuleProperty(String propName, Object moduleNamespace,
281 ServiceReference<?> reference) {
282 Object value = reference.getProperty(propName);
285 "OSGi service with {} property is missing property {} therefore the config module can't be restarted",
286 CONFIG_MODULE_NAMESPACE_PROP, propName);
290 return value.toString();
293 private ServiceRegistration<?> registerEventHandler(BundleContext bundleContext, EventHandler handler) {
294 Dictionary<String, Object> props = new Hashtable<>();
295 props.put(org.osgi.service.event.EventConstants.EVENT_TOPIC,
296 new String[]{EventConstants.TOPIC_CREATED, EventConstants.TOPIC_FAILURE});
297 return bundleContext.registerService(EventHandler.class.getName(), handler, props);
301 public void close() {
302 restartExecutor.shutdownNow();