Ensure CSS modules are closed before blueprint containers on shutdown
[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.Event;
53 import org.osgi.service.event.EventHandler;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.w3c.dom.Document;
57 import org.w3c.dom.Element;
58
59 /**
60  * Implementation of the BlueprintContainerRestartService.
61  *
62  * @author Thomas Pantelis
63  */
64 class BlueprintContainerRestartServiceImpl implements AutoCloseable, BlueprintContainerRestartService {
65     private static final Logger LOG = LoggerFactory.getLogger(BlueprintContainerRestartServiceImpl.class);
66     private static final String CONFIG_MODULE_NAMESPACE_PROP = "config-module-namespace";
67     private static final String CONFIG_MODULE_NAME_PROP = "config-module-name";
68     private static final String CONFIG_INSTANCE_NAME_PROP = "config-instance-name";
69
70     private final ExecutorService restartExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().
71             setDaemon(true).setNameFormat("BlueprintContainerRestartService").build());
72     private final BlueprintExtenderService blueprintExtenderService;
73
74     BlueprintContainerRestartServiceImpl(BlueprintExtenderService blueprintExtenderService) {
75         this.blueprintExtenderService = blueprintExtenderService;
76     }
77
78     @Override
79     public void restartContainerAndDependents(final Bundle bundle) {
80         if(restartExecutor.isShutdown()) {
81             return;
82         }
83
84         LOG.debug("restartContainerAndDependents for bundle {}", bundle);
85
86         restartExecutor.execute(new Runnable() {
87             @Override
88             public void run() {
89                 restartContainerAndDependentsInternal(bundle);
90
91             }
92         });
93     }
94
95     private void restartContainerAndDependentsInternal(Bundle forBundle) {
96         // We use a LinkedHashSet to preserve insertion order as we walk the service usage hierarchy.
97         Set<Bundle> containerBundlesSet = new LinkedHashSet<>();
98         List<Entry<String, ModuleIdentifier>> configModules = new ArrayList<>();
99         findDependentContainersRecursively(forBundle, containerBundlesSet, configModules);
100
101         List<Bundle> containerBundles = new ArrayList<>(containerBundlesSet);
102
103         LOG.info("Restarting blueprint containers for bundle {} and its dependent bundles {}", forBundle,
104                 containerBundles.subList(1, containerBundles.size()));
105
106         // Destroy the containers in reverse order with 'forBundle' last, ie bottom-up in the service tree.
107         for(Bundle bundle: Lists.reverse(containerBundles)) {
108             blueprintExtenderService.destroyContainer(bundle, blueprintExtenderService.getContainer(bundle));
109         }
110
111         // The blueprint containers are created asynchronously so we register a handler for blueprint events
112         // that are sent when a container is complete, successful or not. The CountDownLatch tells when all
113         // containers are complete. This is done to ensure all blueprint containers are finished before we
114         // restart config modules.
115         final CountDownLatch containerCreationComplete = new CountDownLatch(containerBundles.size());
116         ServiceRegistration<?> eventHandlerReg = registerEventHandler(forBundle.getBundleContext(), new EventHandler() {
117             @Override
118             public void handleEvent(Event event) {
119                 LOG.debug("handleEvent {} for bundle {}", event.getTopic(), event.getProperty(EventConstants.BUNDLE));
120                 if(containerBundles.contains(event.getProperty(EventConstants.BUNDLE))) {
121                     containerCreationComplete.countDown();
122                 }
123             }
124         });
125
126         // Restart the containers top-down starting with 'forBundle'.
127         for(Bundle bundle: containerBundles) {
128             List<Object> paths = BlueprintBundleTracker.findBlueprintPaths(bundle);
129
130             LOG.info("Restarting blueprint container for bundle {} with paths {}", bundle, paths);
131
132             blueprintExtenderService.createContainer(bundle, paths);
133         }
134
135         try {
136             containerCreationComplete.await(5, TimeUnit.MINUTES);
137         } catch(InterruptedException e) {
138             LOG.debug("CountDownLatch await was interrupted - returning");
139             return;
140         }
141
142         AriesFrameworkUtil.safeUnregisterService(eventHandlerReg);
143
144         // Now restart any associated config system Modules.
145         restartConfigModules(forBundle.getBundleContext(), configModules);
146     }
147
148     private void restartConfigModules(BundleContext bundleContext, List<Entry<String, ModuleIdentifier>> configModules) {
149         if(configModules.isEmpty()) {
150             return;
151         }
152
153         ServiceReference<ConfigSubsystemFacadeFactory> configFacadeFactoryRef =
154                 bundleContext.getServiceReference(ConfigSubsystemFacadeFactory.class);
155         if(configFacadeFactoryRef == null) {
156             LOG.debug("ConfigSubsystemFacadeFactory service reference not found");
157             return;
158         }
159
160         ConfigSubsystemFacadeFactory configFacadeFactory = bundleContext.getService(configFacadeFactoryRef);
161         if(configFacadeFactory == null) {
162             LOG.debug("ConfigSubsystemFacadeFactory service not found");
163             return;
164         }
165
166         ConfigSubsystemFacade configFacade = configFacadeFactory.createFacade("BlueprintContainerRestartService");
167         try {
168             restartConfigModules(configModules, configFacade);
169         } catch(Exception e) {
170             LOG.error("Error restarting config modules", e);
171         } finally {
172             configFacade.close();
173             bundleContext.ungetService(configFacadeFactoryRef);
174         }
175
176     }
177
178     private void restartConfigModules(List<Entry<String, ModuleIdentifier>> configModules,
179             ConfigSubsystemFacade configFacade) throws ParserConfigurationException, DocumentedException,
180                     ValidationException, ConflictingVersionException {
181
182         Document document = XmlUtil.newDocument();
183         Element dataElement = XmlUtil.createElement(document, XmlMappingConstants.DATA_KEY, Optional.<String>absent());
184         Element modulesElement = XmlUtil.createElement(document, XmlMappingConstants.MODULES_KEY,
185                 Optional.of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
186         dataElement.appendChild(modulesElement);
187
188         Config configMapping = configFacade.getConfigMapping();
189
190         ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory.getPlatformMBeanServer());
191         for(Entry<String, ModuleIdentifier> entry: configModules) {
192             String moduleNamespace = entry.getKey();
193             ModuleIdentifier moduleId = entry.getValue();
194             try {
195                 ObjectName instanceON = configRegistryClient.lookupConfigBean(moduleId.getFactoryName(),
196                         moduleId.getInstanceName());
197
198                 LOG.debug("Found config module instance ObjectName: {}", instanceON);
199
200                 Element moduleElement = configMapping.moduleToXml(moduleNamespace, moduleId.getFactoryName(),
201                         moduleId.getInstanceName(), instanceON, document);
202                 modulesElement.appendChild(moduleElement);
203             } catch(InstanceNotFoundException e) {
204                 LOG.warn("Error looking up config module: namespace {}, module name {}, instance {}",
205                         moduleNamespace, moduleId.getFactoryName(), moduleId.getInstanceName(), e);
206             }
207         }
208
209         if(LOG.isDebugEnabled()) {
210             LOG.debug("Pushing config xml: {}", XmlUtil.toString(dataElement));
211         }
212
213         ConfigExecution execution = new ConfigExecution(configMapping, XmlElement.fromDomElement(dataElement),
214                 TestOption.testThenSet, EditStrategyType.recreate);
215         configFacade.executeConfigExecution(execution);
216         configFacade.commitSilentTransaction();
217     }
218
219     /**
220      * Recursively finds the services registered by the given bundle and the bundles using those services.
221      * User bundles that have an associated blueprint container are added to containerBundles. In addition,
222      * if a registered service has an associated config system Module, as determined via the presence of
223      * certain service properties, the ModuleIdentifier is added to the configModules list.
224      *
225      * @param bundle the bundle to traverse
226      * @param containerBundles the current set of bundles containing blueprint containers
227      */
228     private void findDependentContainersRecursively(Bundle bundle, Set<Bundle> containerBundles,
229             List<Entry<String, ModuleIdentifier>> configModules) {
230         if(!containerBundles.add(bundle)) {
231             // Already seen this bundle...
232             return;
233         }
234
235         ServiceReference<?>[] references = bundle.getRegisteredServices();
236         if (references != null) {
237             for (ServiceReference<?> reference : references) {
238                 possiblyAddConfigModuleIdentifier(reference, configModules);
239
240                 Bundle[] usingBundles = reference.getUsingBundles();
241                 if(usingBundles != null) {
242                     for(Bundle usingBundle: usingBundles) {
243                         if(blueprintExtenderService.getContainer(usingBundle) != null) {
244                             findDependentContainersRecursively(usingBundle, containerBundles, configModules);
245                         }
246                     }
247                 }
248             }
249         }
250     }
251
252     private void possiblyAddConfigModuleIdentifier(ServiceReference<?> reference,
253             List<Entry<String, ModuleIdentifier>> configModules) {
254         Object moduleNamespace = reference.getProperty(CONFIG_MODULE_NAMESPACE_PROP);
255         if(moduleNamespace == null) {
256             return;
257         }
258
259         String moduleName = getRequiredConfigModuleProperty(CONFIG_MODULE_NAME_PROP, moduleNamespace,
260                 reference);
261         String instanceName = getRequiredConfigModuleProperty(CONFIG_INSTANCE_NAME_PROP, moduleNamespace,
262                 reference);
263         if(moduleName == null || instanceName == null) {
264             return;
265         }
266
267         LOG.debug("Found service with config module: namespace {}, module name {}, instance {}",
268                 moduleNamespace, moduleName, instanceName);
269
270         configModules.add(new SimpleEntry<>(moduleNamespace.toString(),
271                 new ModuleIdentifier(moduleName, instanceName)));
272     }
273
274     @Nullable
275     private String getRequiredConfigModuleProperty(String propName, Object moduleNamespace,
276             ServiceReference<?> reference) {
277         Object value = reference.getProperty(propName);
278         if(value == null) {
279             LOG.warn("OSGi service with {} property is missing property {} therefore the config module can't be restarted",
280                     CONFIG_MODULE_NAMESPACE_PROP, propName);
281             return null;
282         }
283
284         return value.toString();
285     }
286
287     private ServiceRegistration<?> registerEventHandler(BundleContext bundleContext, EventHandler handler) {
288         Dictionary<String, Object> props = new Hashtable<>();
289         props.put(org.osgi.service.event.EventConstants.EVENT_TOPIC,
290                 new String[]{EventConstants.TOPIC_CREATED, EventConstants.TOPIC_FAILURE});
291         return bundleContext.registerService(EventHandler.class.getName(), handler, props);
292     }
293
294     @Override
295     public void close() {
296         restartExecutor.shutdownNow();
297     }
298 }