Merge "(Bug 2035) - Increasing default Akka config for auto-down of unreachable nodes...
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / Activator.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
10
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.Dictionary;
14 import java.util.Hashtable;
15 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
17 import org.osgi.framework.BundleActivator;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20 import org.osgi.framework.ServiceRegistration;
21 import org.osgi.util.tracker.ServiceTracker;
22 import org.osgi.util.tracker.ServiceTrackerCustomizer;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class Activator implements BundleActivator {
27
28     private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
29
30     private BundleContext context;
31     private ServiceRegistration<?> osgiRegistration;
32     private ConfigRegistryLookupThread configRegistryLookup = null;
33
34     @Override
35     public void start(final BundleContext context) throws Exception {
36         this.context = context;
37
38         ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread> customizer = new ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread>() {
39             @Override
40             public ConfigRegistryLookupThread addingService(ServiceReference<SchemaContextProvider> reference) {
41                 LOG.debug("Got addingService(SchemaContextProvider) event, starting ConfigRegistryLookupThread");
42                 checkState(configRegistryLookup == null, "More than one onYangStoreAdded received");
43
44                 SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
45
46                 YangStoreService yangStoreService = new YangStoreService(schemaContextProvider, context);
47                 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
48                 configRegistryLookup.start();
49                 return configRegistryLookup;
50             }
51
52             @Override
53             public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
54                 LOG.debug("Got modifiedService(SchemaContextProvider) event");
55                 configRegistryLookup.yangStoreService.refresh();
56
57             }
58
59             @Override
60             public void removedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
61                 configRegistryLookup.interrupt();
62                 if (osgiRegistration != null) {
63                     osgiRegistration.unregister();
64                 }
65                 osgiRegistration = null;
66                 Activator.this.configRegistryLookup = null;
67             }
68         };
69
70         ServiceTracker<SchemaContextProvider, ConfigRegistryLookupThread> listenerTracker = new ServiceTracker<>(context, SchemaContextProvider.class, customizer);
71         listenerTracker.open();
72     }
73
74     @Override
75     public void stop(BundleContext context) {
76         if (configRegistryLookup != null) {
77             configRegistryLookup.interrupt();
78         }
79     }
80
81     private class ConfigRegistryLookupThread extends Thread {
82         private final YangStoreService yangStoreService;
83
84         private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
85             super("config-registry-lookup");
86             this.yangStoreService = yangStoreService;
87         }
88
89         @Override
90         public void run() {
91             NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
92             LOG.debug("Registering into OSGi");
93             Dictionary<String, String> properties = new Hashtable<>();
94             properties.put("name", "config-netconf-connector");
95             osgiRegistration = context.registerService(NetconfOperationServiceFactory.class, factory, properties);
96         }
97     }
98 }