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