6a464b75d03533eea7a992e45439d8771af2df92
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / BlueprintContainerRestartServiceImpl.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.blueprint;
9
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;
21 import java.util.Set;
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;
57
58 /**
59  * Implementation of the BlueprintContainerRestartService.
60  *
61  * @author Thomas Pantelis
62  */
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";
68
69     private final ExecutorService restartExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
70             .setDaemon(true).setNameFormat("BlueprintContainerRestartService").build());
71     private final BlueprintExtenderService blueprintExtenderService;
72
73     BlueprintContainerRestartServiceImpl(BlueprintExtenderService blueprintExtenderService) {
74         this.blueprintExtenderService = blueprintExtenderService;
75     }
76
77     public void restartContainer(final Bundle bundle, final List<Object> paths) {
78         if (restartExecutor.isShutdown()) {
79             return;
80         }
81
82         LOG.debug("restartContainer for bundle {}", bundle);
83
84         restartExecutor.execute(() -> {
85             blueprintExtenderService.destroyContainer(bundle, blueprintExtenderService.getContainer(bundle));
86             blueprintExtenderService.createContainer(bundle, paths);
87         });
88     }
89
90     @Override
91     public void restartContainerAndDependents(final Bundle bundle) {
92         if (restartExecutor.isShutdown()) {
93             return;
94         }
95
96         LOG.debug("restartContainerAndDependents for bundle {}", bundle);
97
98         restartExecutor.execute(() -> restartContainerAndDependentsInternal(bundle));
99     }
100
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);
106
107         List<Bundle> containerBundles = new ArrayList<>(containerBundlesSet);
108
109         LOG.info("Restarting blueprint containers for bundle {} and its dependent bundles {}", forBundle,
110                 containerBundles.subList(1, containerBundles.size()));
111
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));
115         }
116
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();
126             }
127         });
128
129         // Restart the containers top-down starting with 'forBundle'.
130         for (Bundle bundle: containerBundles) {
131             List<Object> paths = BlueprintBundleTracker.findBlueprintPaths(bundle);
132
133             LOG.info("Restarting blueprint container for bundle {} with paths {}", bundle, paths);
134
135             blueprintExtenderService.createContainer(bundle, paths);
136         }
137
138         try {
139             containerCreationComplete.await(5, TimeUnit.MINUTES);
140         } catch (InterruptedException e) {
141             LOG.debug("CountDownLatch await was interrupted - returning");
142             return;
143         }
144
145         AriesFrameworkUtil.safeUnregisterService(eventHandlerReg);
146
147         // Now restart any associated config system Modules.
148         restartConfigModules(forBundle.getBundleContext(), configModules);
149     }
150
151     private void restartConfigModules(BundleContext bundleContext, List<Entry<String,
152             ModuleIdentifier>> configModules) {
153         if (configModules.isEmpty()) {
154             return;
155         }
156
157         ServiceReference<ConfigSubsystemFacadeFactory> configFacadeFactoryRef = bundleContext
158                 .getServiceReference(ConfigSubsystemFacadeFactory.class);
159         if (configFacadeFactoryRef == null) {
160             LOG.debug("ConfigSubsystemFacadeFactory service reference not found");
161             return;
162         }
163
164         ConfigSubsystemFacadeFactory configFacadeFactory = bundleContext.getService(configFacadeFactoryRef);
165         if (configFacadeFactory == null) {
166             LOG.debug("ConfigSubsystemFacadeFactory service not found");
167             return;
168         }
169
170         ConfigSubsystemFacade configFacade = configFacadeFactory.createFacade("BlueprintContainerRestartService");
171         try {
172             restartConfigModules(configModules, configFacade);
173         } catch (ParserConfigurationException | DocumentedException | ValidationException
174                 | ConflictingVersionException e) {
175             LOG.error("Error restarting config modules", e);
176         } finally {
177             configFacade.close();
178             bundleContext.ungetService(configFacadeFactoryRef);
179         }
180
181     }
182
183     private void restartConfigModules(List<Entry<String, ModuleIdentifier>> configModules,
184             ConfigSubsystemFacade configFacade) throws ParserConfigurationException, DocumentedException,
185                     ValidationException, ConflictingVersionException {
186
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);
192
193         Config configMapping = configFacade.getConfigMapping();
194
195         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory.getPlatformMBeanServer());
196         for (Entry<String, ModuleIdentifier> entry: configModules) {
197             String moduleNamespace = entry.getKey();
198             ModuleIdentifier moduleId = entry.getValue();
199             try {
200                 ObjectName instanceON = configRegistryClient.lookupConfigBean(moduleId.getFactoryName(),
201                         moduleId.getInstanceName());
202
203                 LOG.debug("Found config module instance ObjectName: {}", instanceON);
204
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);
211             }
212         }
213
214         if (LOG.isDebugEnabled()) {
215             LOG.debug("Pushing config xml: {}", XmlUtil.toString(dataElement));
216         }
217
218         ConfigExecution execution = new ConfigExecution(configMapping, XmlElement.fromDomElement(dataElement),
219                 TestOption.testThenSet, EditStrategyType.recreate);
220         configFacade.executeConfigExecution(execution);
221         configFacade.commitSilentTransaction();
222     }
223
224     /**
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.
229      *
230      * @param bundle the bundle to traverse
231      * @param containerBundles the current set of bundles containing blueprint containers
232      */
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...
237             return;
238         }
239
240         ServiceReference<?>[] references = bundle.getRegisteredServices();
241         if (references != null) {
242             for (ServiceReference<?> reference : references) {
243                 possiblyAddConfigModuleIdentifier(reference, configModules);
244
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);
250                         }
251                     }
252                 }
253             }
254         }
255     }
256
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) {
261             return;
262         }
263
264         String moduleName = getRequiredConfigModuleProperty(CONFIG_MODULE_NAME_PROP, moduleNamespace,
265                 reference);
266         String instanceName = getRequiredConfigModuleProperty(CONFIG_INSTANCE_NAME_PROP, moduleNamespace,
267                 reference);
268         if (moduleName == null || instanceName == null) {
269             return;
270         }
271
272         LOG.debug("Found service with config module: namespace {}, module name {}, instance {}",
273                 moduleNamespace, moduleName, instanceName);
274
275         configModules.add(new SimpleEntry<>(moduleNamespace.toString(),
276                 new ModuleIdentifier(moduleName, instanceName)));
277     }
278
279     @Nullable
280     private String getRequiredConfigModuleProperty(String propName, Object moduleNamespace,
281             ServiceReference<?> reference) {
282         Object value = reference.getProperty(propName);
283         if (value == null) {
284             LOG.warn(
285                 "OSGi service with {} property is missing property {} therefore the config module can't be restarted",
286                 CONFIG_MODULE_NAMESPACE_PROP, propName);
287             return null;
288         }
289
290         return value.toString();
291     }
292
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);
298     }
299
300     @Override
301     public void close() {
302         restartExecutor.shutdownNow();
303     }
304 }