Remove yang-test
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / ConfigManagerActivator.java
1 /*
2  * Copyright (c) 2013, 2017 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(
66                     moduleInfoRegistryWrapper);
67
68             // start config registry
69             final BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver =
70                     new BundleContextBackedModuleFactoriesResolver(context);
71             this.configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver,
72                     this.configMBeanServer, bindingContextProvider);
73
74             // track bundles containing factories
75             final BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
76                     this.configRegistry);
77             final ModuleFactoryBundleTracker moduleFactoryTracker = new ModuleFactoryBundleTracker(
78                     blankTransactionServiceTracker);
79
80             BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> moduleInfoResolvedBundleTracker =
81                     new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING | Bundle.STOPPING | Bundle.ACTIVE,
82                     moduleInfoBundleTracker);
83             ExtensibleBundleTracker<?> moduleFactoryBundleTracker = new ExtensibleBundleTracker<>(context,
84                     moduleFactoryTracker);
85             moduleInfoBundleTracker.open(moduleInfoResolvedBundleTracker);
86
87             // start extensible tracker
88             moduleFactoryBundleTracker.open();
89
90             // Wrap config registry with JMX notification publishing adapter
91             final JMXNotifierConfigRegistry notifyingConfigRegistry = new JMXNotifierConfigRegistry(this.configRegistry,
92                     this.configMBeanServer);
93
94             // register config registry to OSGi
95             final AutoCloseable clsReg = OsgiRegistrationUtil.registerService(context, moduleInfoBackedContext,
96                     ClassLoadingStrategy.class);
97             final AutoCloseable configRegReg = OsgiRegistrationUtil.registerService(context, notifyingConfigRegistry,
98                     ConfigRegistry.class);
99
100             // register config registry to jmx
101             final ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(
102                     this.configMBeanServer);
103             try {
104                 configRegistryJMXRegistrator.registerToJMXNoNotifications(this.configRegistry);
105             } catch (final InstanceAlreadyExistsException e) {
106                 configRegistryJMXRegistrator.close();
107                 throw new IllegalStateException("Config Registry was already registered to JMX", e);
108             }
109
110             // register config registry to jmx
111             final ConfigRegistryJMXRegistrator configRegistryJMXRegistratorWithNotifications =
112                     new ConfigRegistryJMXRegistrator(this.configMBeanServer);
113             try {
114                 configRegistryJMXRegistrator.registerToJMX(notifyingConfigRegistry);
115             } catch (final 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             final ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context,
123                     ModuleFactory.class, blankTransactionServiceTracker);
124             serviceTracker.open();
125
126             final AutoCloseable configMgrReg = OsgiRegistrationUtil.registerService(context, this,
127                     ConfigSystemService.class);
128
129             final List<AutoCloseable> list = Arrays.asList(bindingContextProvider, clsReg,
130                     OsgiRegistrationUtil.wrap(moduleFactoryBundleTracker), moduleInfoBundleTracker, configRegReg,
131                     configRegistryJMXRegistrator, configRegistryJMXRegistratorWithNotifications,
132                     OsgiRegistrationUtil.wrap(serviceTracker), moduleInfoRegistryWrapper, notifyingConfigRegistry,
133                     configMgrReg);
134             this.autoCloseable = OsgiRegistrationUtil.aggregate(list);
135
136             context.addBundleListener(this);
137         } catch (final IllegalStateException e) {
138             LOG.error("Error starting config manager", e);
139         }
140
141         LOG.info("Config manager start complete");
142     }
143
144     @Override
145     public void stop(final BundleContext context) throws Exception {
146         LOG.info("Config manager stopping");
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
158         // destroys all modules. On
159         // shutdown the system bundle is stopped first.
160         if (event.getBundle().getBundleId() == SYSTEM_BUNDLE_ID && event.getType() == BundleEvent.STOPPING) {
161             this.configRegistry.close();
162         }
163     }
164
165     @Override
166     public void closeAllConfigModules() {
167         if (this.configRegistry != null) {
168             this.configRegistry.close();
169         }
170     }
171 }