a5d77eabe014bc60ca5ecf162f41ac7e8ea4dbe2
[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, bindingContextProvider, context);
62
63             final ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(context, moduleInfoRegistryWrapper);
64
65             // start config registry
66             final BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
67                     context);
68             this.configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, this.configMBeanServer,
69                     bindingContextProvider);
70
71             // track bundles containing factories
72             final BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
73                     this.configRegistry);
74             final ModuleFactoryBundleTracker moduleFactoryTracker = new ModuleFactoryBundleTracker(
75                     blankTransactionServiceTracker);
76
77             BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> moduleInfoResolvedBundleTracker =
78                     new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE,
79                             moduleInfoBundleTracker);
80             ExtensibleBundleTracker<?> moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context,
81                     moduleFactoryTracker);
82             moduleInfoBundleTracker.open(moduleInfoResolvedBundleTracker);
83
84             // start extensible tracker
85             moduleFactoryBundleTracker.open();
86
87             // Wrap config registry with JMX notification publishing adapter
88             final JMXNotifierConfigRegistry notifyingConfigRegistry =
89                     new JMXNotifierConfigRegistry(this.configRegistry, this.configMBeanServer);
90
91             // register config registry to OSGi
92             final AutoCloseable clsReg = OsgiRegistrationUtil.registerService(context, moduleInfoBackedContext, ClassLoadingStrategy.class);
93             final AutoCloseable configRegReg = OsgiRegistrationUtil.registerService(context, notifyingConfigRegistry, ConfigRegistry.class);
94
95             // register config registry to jmx
96             final ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(this.configMBeanServer);
97             try {
98                 configRegistryJMXRegistrator.registerToJMXNoNotifications(this.configRegistry);
99             } catch (final InstanceAlreadyExistsException e) {
100                 configRegistryJMXRegistrator.close();
101                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
102             }
103
104             // register config registry to jmx
105             final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications = new ConfigRegistryJMXRegistrator(this.configMBeanServer);
106             try {
107                 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
108             } catch (final InstanceAlreadyExistsException e) {
109                 configRegistryJMXRegistrator.close();
110                 configRegistryJMXRegistratorWithNotifications.close();
111                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
112             }
113
114             // TODO wire directly via moduleInfoBundleTracker
115             final ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
116                     blankTransactionServiceTracker);
117             serviceTracker.open();
118
119             final AutoCloseable configMgrReg = OsgiRegistrationUtil.registerService(context, this, ConfigSystemService.class);
120
121             final List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg,
122                     OsgiRegistrationUtil.wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker,
123                     configRegReg, configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications,
124                     OsgiRegistrationUtil.wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry, configMgrReg);
125             this.autoCloseable = OsgiRegistrationUtil.aggregate(list);
126
127             context.addBundleListener(this);
128         } catch(final Exception e) {
129             LOG.warn("Error starting config manager", e);
130         } catch(final Error e) {
131             // Log JVM Error and re-throw. The OSGi container may silently fail the bundle and not always log
132             // the exception. This has been seen on initial feature install.
133             LOG.error("Error starting config manager", e);
134             throw e;
135         }
136     }
137
138     @Override
139     public void stop(final BundleContext context) throws Exception {
140         context.removeBundleListener(this);
141         this.autoCloseable.close();
142     }
143
144     @Override
145     public void bundleChanged(final BundleEvent event) {
146         if(this.configRegistry == null) {
147             return;
148         }
149
150         // If the system bundle (id 0) is stopping close the ConfigRegistry so it destroys all modules. On
151         // shutdown the system bundle is stopped first.
152         if(event.getBundle().getBundleId() == SYSTEM_BUNDLE_ID && event.getType() == BundleEvent.STOPPING) {
153             this.configRegistry.close();
154         }
155     }
156
157     @Override
158     public void closeAllConfigModules() {
159         if(this.configRegistry != null) {
160             this.configRegistry.close();
161         }
162     }
163 }