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

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