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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.