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