BUG-2453 (De)Serialize enum values as defined in yang
[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.api.util.NetconfConstants;
16 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
17 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
19 import org.osgi.framework.BundleActivator;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceReference;
22 import org.osgi.framework.ServiceRegistration;
23 import org.osgi.util.tracker.ServiceTracker;
24 import org.osgi.util.tracker.ServiceTrackerCustomizer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class Activator implements BundleActivator {
29
30     private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
31
32     private BundleContext context;
33     private ServiceRegistration<?> osgiRegistration;
34     private ConfigRegistryLookupThread configRegistryLookup = null;
35
36     @Override
37     public void start(final BundleContext context) throws Exception {
38         this.context = context;
39
40         ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread> customizer = new ServiceTrackerCustomizer<SchemaContextProvider, ConfigRegistryLookupThread>() {
41             @Override
42             public ConfigRegistryLookupThread addingService(ServiceReference<SchemaContextProvider> reference) {
43                 LOG.debug("Got addingService(SchemaContextProvider) event, starting ConfigRegistryLookupThread");
44                 checkState(configRegistryLookup == null, "More than one onYangStoreAdded received");
45
46                 SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
47
48                 YangStoreService yangStoreService = new YangStoreService(schemaContextProvider, context);
49                 configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
50                 configRegistryLookup.start();
51                 return configRegistryLookup;
52             }
53
54             @Override
55             public void modifiedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
56                 LOG.debug("Got modifiedService(SchemaContextProvider) event");
57                 final BindingRuntimeContext runtimeContext = (BindingRuntimeContext) reference.getProperty(BindingRuntimeContext.class.getName());
58                 LOG.debug("BindingRuntimeContext retrieved as {}", runtimeContext);
59                 configRegistryLookup.yangStoreService.refresh(runtimeContext);
60
61             }
62
63             @Override
64             public void removedService(ServiceReference<SchemaContextProvider> reference, ConfigRegistryLookupThread configRegistryLookup) {
65                 configRegistryLookup.interrupt();
66                 if (osgiRegistration != null) {
67                     osgiRegistration.unregister();
68                 }
69                 osgiRegistration = null;
70                 Activator.this.configRegistryLookup = null;
71             }
72         };
73
74         ServiceTracker<SchemaContextProvider, ConfigRegistryLookupThread> listenerTracker = new ServiceTracker<>(context, SchemaContextProvider.class, customizer);
75         listenerTracker.open();
76     }
77
78     @Override
79     public void stop(BundleContext context) {
80         if (configRegistryLookup != null) {
81             configRegistryLookup.interrupt();
82         }
83     }
84
85     private class ConfigRegistryLookupThread extends Thread {
86         private final YangStoreService yangStoreService;
87
88         private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
89             super("config-registry-lookup");
90             this.yangStoreService = yangStoreService;
91         }
92
93         @Override
94         public void run() {
95             NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
96             LOG.debug("Registering into OSGi");
97             Dictionary<String, String> properties = new Hashtable<>();
98             properties.put(NetconfConstants.SERVICE_NAME, NetconfConstants.CONFIG_NETCONF_CONNECTOR);
99             osgiRegistration = context.registerService(NetconfOperationServiceFactory.class, factory, properties);
100         }
101     }
102 }