Fix modules Restconf call for mounted devices
[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
13 import java.lang.management.ManagementFactory;
14 import java.util.Arrays;
15 import java.util.List;
16 import javax.management.InstanceAlreadyExistsException;
17 import javax.management.MBeanServer;
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.osgi.mapping.CodecRegistryProvider;
21 import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
22 import org.opendaylight.controller.config.manager.impl.osgi.mapping.RefreshingSCPModuleInfoRegistry;
23 import org.opendaylight.controller.config.manager.impl.util.OsgiRegistrationUtil;
24 import org.opendaylight.controller.config.spi.ModuleFactory;
25 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
26 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
27 import org.osgi.framework.BundleActivator;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.util.tracker.ServiceTracker;
30
31 public class ConfigManagerActivator implements BundleActivator {
32     private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
33
34     private AutoCloseable autoCloseable;
35
36     @Override
37     public void start(final BundleContext context) {
38
39         ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();// the inner strategy is backed by thread context cl?
40
41         RefreshingSCPModuleInfoRegistry moduleInfoRegistryWrapper = new RefreshingSCPModuleInfoRegistry(
42                 moduleInfoBackedContext, moduleInfoBackedContext, context);
43
44         ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker(moduleInfoRegistryWrapper);
45
46         CodecRegistryProvider codecRegistryProvider = new CodecRegistryProvider(moduleInfoBackedContext, context);
47
48         // start config registry
49         BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
50                 context);
51         ConfigRegistryImpl configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
52                 codecRegistryProvider.getCodecRegistry());
53
54         // track bundles containing factories
55         BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
56                 configRegistry);
57         ModuleFactoryBundleTracker primaryModuleFactoryBundleTracker = new ModuleFactoryBundleTracker(
58                 blankTransactionServiceTracker);
59
60         // start extensible tracker
61         ExtensibleBundleTracker<?> bundleTracker = new ExtensibleBundleTracker<>(context,
62                 primaryModuleFactoryBundleTracker, moduleInfoBundleTracker);
63         bundleTracker.open();
64
65         // register config registry to OSGi
66         AutoCloseable clsReg = registerService(context, moduleInfoBackedContext, GeneratedClassLoadingStrategy.class);
67         AutoCloseable configRegReg = registerService(context, configRegistry, ConfigRegistryImpl.class);
68
69         // register config registry to jmx
70         ConfigRegistryJMXRegistrator configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(configMBeanServer);
71         try {
72             configRegistryJMXRegistrator.registerToJMX(configRegistry);
73         } catch (InstanceAlreadyExistsException e) {
74             throw new IllegalStateException("Config Registry was already registered to JMX", e);
75         }
76
77         // TODO wire directly via moduleInfoBundleTracker
78         ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
79                 blankTransactionServiceTracker);
80         serviceTracker.open();
81
82         List<AutoCloseable> list = Arrays.asList(
83                 codecRegistryProvider, clsReg,configRegistry, wrap(bundleTracker), configRegReg, configRegistryJMXRegistrator, wrap(serviceTracker));
84         autoCloseable = OsgiRegistrationUtil.aggregate(list);
85     }
86
87     @Override
88     public void stop(final BundleContext context) throws Exception {
89         autoCloseable.close();
90     }
91 }