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