9d8694280b901cbb2f8d5375e33f76494f9e8bcd
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / ConfigManagerActivator.java
1 /*
2  * Copyright (c) 2013 Cisco 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.config.manager.impl.osgi;
9
10 import java.lang.management.ManagementFactory;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.List;
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.MBeanServer;
16 import org.opendaylight.controller.config.api.ConfigRegistry;
17 import org.opendaylight.controller.config.api.ConfigSystemService;
18 import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
19 import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
20 import org.opendaylight.controller.config.manager.impl.jmx.JMXNotifierConfigRegistry;
21 import org.opendaylight.controller.config.manager.impl.osgi.mapping.BindingContextProvider;
22 import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
23 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry;
24 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
25 import org.opendaylight.controller.config.spi.ModuleFactory;
26 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
27 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
28 import org.opendaylight.yangtools.concepts.ObjectRegistration;
29 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleActivator;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.BundleEvent;
34 import org.osgi.framework.SynchronousBundleListener;
35 import org.osgi.util.tracker.BundleTracker;
36 import org.osgi.util.tracker.ServiceTracker;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class ConfigManagerActivator implements BundleActivator, SynchronousBundleListener, ConfigSystemService {
41
42     private static final Logger LOG = LoggerFactory.getLogger(ConfigManagerActivator.class);
43
44     private static final long SYSTEM_BUNDLE_ID = 0;
45
46     private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
47
48     private AutoCloseable autoCloseable;
49
50     private ConfigRegistryImpl configRegistry;
51
52     @Override
53     public void start(final BundleContext context) {
54         try {
55             // the inner strategy is backed by thread context cl?
56             final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
57
58             final BindingContextProvider bindingContextProvider = new BindingContextProvider();
59
60             final RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
61                     moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext, moduleInfoBackedContext,
62                     bindingContextProvider, context);
63
64             final ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper);
65
66             // start config registry
67             final BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver =
68                     new BundleContextBackedModuleFactoriesResolver(context);
69             this.configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver,
70                 this.configMBeanServer, bindingContextProvider);
71
72             // track bundles containing factories
73             final BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
74                     this.configRegistry);
75             final ModuleFactoryBundleTracker moduleFactoryTracker = new ModuleFactoryBundleTracker(
76                     blankTransactionServiceTracker);
77
78             BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> moduleInfoResolvedBundleTracker =
79                     new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE,
80                             moduleInfoBundleTracker);
81             ExtensibleBundleTracker<?> moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context,
82                     moduleFactoryTracker);
83             moduleInfoBundleTracker.open(moduleInfoResolvedBundleTracker);
84
85             // start extensible tracker
86             moduleFactoryBundleTracker.open();
87
88             // Wrap config registry with JMX notification publishing adapter
89             final JMXNotifierConfigRegistry notifyingConfigRegistry =
90                     new JMXNotifierConfigRegistry(this.configRegistry, this.configMBeanServer);
91
92             // register config registry to OSGi
93             final AutoCloseable clsReg = OsgiRegistrationUtil.registerService(context, moduleInfoBackedContext,
94                 ClassLoadingStrategy.class);
95             final AutoCloseable configRegReg = OsgiRegistrationUtil.registerService(context, notifyingConfigRegistry,
96                 ConfigRegistry.class);
97
98             // register config registry to jmx
99             final ConfigRegistryJMXRegistrator configRegistryJMXRegistrator =
100                     new ConfigRegistryJMXRegistrator(this.configMBeanServer);
101             try {
102                 configRegistryJMXRegistrator.registerToJMXNoNotifications(this.configRegistry);
103             } catch (final InstanceAlreadyExistsException e) {
104                 configRegistryJMXRegistrator.close();
105                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
106             }
107
108             // register config registry to jmx
109             final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications =
110                     new ConfigRegistryJMXRegistrator(this.configMBeanServer);
111             try {
112                 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
113             } catch (final InstanceAlreadyExistsException e) {
114                 configRegistryJMXRegistrator.close();
115                 configRegistryJMXRegistratorWithNotifications.close();
116                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
117             }
118
119             // TODO wire directly via moduleInfoBundleTracker
120             final ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context,
121                     ModuleFactory.class, blankTransactionServiceTracker);
122             serviceTracker.open();
123
124             final AutoCloseable configMgrReg = OsgiRegistrationUtil.registerService(context, this,
125                 ConfigSystemService.class);
126
127             final List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg,
128                     OsgiRegistrationUtil.wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker,
129                     configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications,
130                     OsgiRegistrationUtil.wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry,
131                     configMgrReg);
132             this.autoCloseable = OsgiRegistrationUtil.aggregate(list);
133
134             context.addBundleListener(this);
135         } catch(final Exception e) {
136             LOG.warn("Error starting config manager", e);
137         } catch(final Error e) {
138             // Log JVM Error and re-throw. The OSGi container may silently fail the bundle and not always log
139             // the exception. This has been seen on initial feature install.
140             LOG.error("Error starting config manager", e);
141             throw e;
142         }
143     }
144
145     @Override
146     public void stop(final BundleContext context) throws Exception {
147         context.removeBundleListener(this);
148         this.autoCloseable.close();
149     }
150
151     @Override
152     public void bundleChanged(final BundleEvent event) {
153         if(this.configRegistry == null) {
154             return;
155         }
156
157         // If the system bundle (id 0) is stopping close the ConfigRegistry so it destroys all modules. On
158         // shutdown the system bundle is stopped first.
159         if(event.getBundle().getBundleId() == SYSTEM_BUNDLE_ID && event.getType() == BundleEvent.STOPPING) {
160             this.configRegistry.close();
161         }
162     }
163
164     @Override
165     public void closeAllConfigModules() {
166         if(this.configRegistry != null) {
167             this.configRegistry.close();
168         }
169     }
170 }

©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.