Fix checkstyle reported by odlparent-3.0.0
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / osgi / YangStoreActivator.java
index 1df85391abdbaf492701304269414dc93295ac1b..016c944d80986110db5ee418a45993b4f5742c62 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,14 +8,17 @@
 
 package org.opendaylight.controller.config.facade.xml.osgi;
 
+import com.google.common.base.Preconditions;
 import java.lang.management.ManagementFactory;
 import java.util.Hashtable;
 import java.util.concurrent.atomic.AtomicBoolean;
 import javax.management.MBeanServer;
 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
-import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -26,7 +29,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Start yang store service and the XML config manager facade
+ * Start yang store service and the XML config manager facade.
  */
 public class YangStoreActivator implements BundleActivator {
 
@@ -40,70 +43,105 @@ public class YangStoreActivator implements BundleActivator {
     private ServiceRegistration<ConfigSubsystemFacadeFactory> osgiRegistrayion;
 
     @Override
+    @SuppressWarnings("checkstyle:hiddenField")
     public void start(final BundleContext context) throws Exception {
         LOG.debug("ConfigPersister starting");
         this.context = context;
 
-        ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService> schemaServiceTrackerCustomizer = new ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService>() {
+        final ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService> schemaServiceTrackerCustomizer =
+                new ServiceTrackerCustomizer<SchemaContextProvider, YangStoreService>() {
 
             private final AtomicBoolean alreadyStarted = new AtomicBoolean(false);
 
             @Override
-            public YangStoreService addingService(ServiceReference<SchemaContextProvider> reference) {
+            public YangStoreService addingService(final ServiceReference<SchemaContextProvider> reference) {
                 LOG.debug("Got addingService(SchemaContextProvider) event");
+                if (reference.getProperty(SchemaSourceProvider.class.getName()) == null
+                        && reference.getProperty(BindingRuntimeContext.class.getName()) == null) {
+                    LOG.debug("SchemaContextProvider not from config-manager. Ignoring");
+                    return null;
+                }
 
                 // Yang store service should not be registered multiple times
-                if(!alreadyStarted.compareAndSet(false, true)) {
+                if (!this.alreadyStarted.compareAndSet(false, true)) {
                     LOG.warn("Starting yang store service multiple times. Received new service {}", reference);
                     throw new RuntimeException("Starting yang store service multiple times");
                 }
-                SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext().getService(reference);
-                final YangStoreService yangStoreService = new YangStoreService(schemaContextProvider);
-                yangStoreServiceServiceRegistration = context.registerService(YangStoreService.class, yangStoreService, new Hashtable<String, Object>());
-                configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
-                configRegistryLookup.start();
+                final SchemaContextProvider schemaContextProvider = reference.getBundle().getBundleContext()
+                        .getService(reference);
+                final Object sourceProvider = Preconditions.checkNotNull(
+                        reference.getProperty(SchemaSourceProvider.class.getName()), "Source provider not found");
+                Preconditions.checkArgument(sourceProvider instanceof SchemaSourceProvider);
+
+                // TODO avoid cast
+                final YangStoreService yangStoreService = new YangStoreService(schemaContextProvider,
+                        (SchemaSourceProvider<YangTextSchemaSource>) sourceProvider);
+
+                final BindingRuntimeContext runtimeContext = (BindingRuntimeContext) reference
+                        .getProperty(BindingRuntimeContext.class.getName());
+                LOG.debug("BindingRuntimeContext retrieved as {}", runtimeContext);
+                if (runtimeContext != null) {
+                    yangStoreService.refresh(runtimeContext);
+                }
+
+                YangStoreActivator.this.yangStoreServiceServiceRegistration = context
+                        .registerService(YangStoreService.class, yangStoreService, new Hashtable<>());
+                YangStoreActivator.this.configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
+                YangStoreActivator.this.configRegistryLookup.start();
                 return yangStoreService;
             }
 
             @Override
-            public void modifiedService(ServiceReference<SchemaContextProvider> reference, YangStoreService service) {
+            public void modifiedService(final ServiceReference<SchemaContextProvider> reference,
+                    final YangStoreService service) {
+                if (service == null) {
+                    return;
+                }
+
                 LOG.debug("Got modifiedService(SchemaContextProvider) event");
-                final BindingRuntimeContext runtimeContext = (BindingRuntimeContext) reference.getProperty(BindingRuntimeContext.class.getName());
+                final BindingRuntimeContext runtimeContext = (BindingRuntimeContext) reference
+                        .getProperty(BindingRuntimeContext.class.getName());
                 LOG.debug("BindingRuntimeContext retrieved as {}", runtimeContext);
                 service.refresh(runtimeContext);
             }
 
             @Override
-            public void removedService(ServiceReference<SchemaContextProvider> reference, YangStoreService service) {
+            public void removedService(final ServiceReference<SchemaContextProvider> reference,
+                    final YangStoreService service) {
+                if (service == null) {
+                    return;
+                }
+
                 LOG.debug("Got removedService(SchemaContextProvider) event");
-                alreadyStarted.set(false);
-                configRegistryLookup.interrupt();
-                yangStoreServiceServiceRegistration.unregister();
-                yangStoreServiceServiceRegistration = null;
+                this.alreadyStarted.set(false);
+                YangStoreActivator.this.configRegistryLookup.interrupt();
+                YangStoreActivator.this.yangStoreServiceServiceRegistration.unregister();
+                YangStoreActivator.this.yangStoreServiceServiceRegistration = null;
             }
         };
 
-        ServiceTracker<SchemaContextProvider, YangStoreService> schemaContextProviderServiceTracker =
+        final ServiceTracker<SchemaContextProvider, YangStoreService> schemaContextProviderServiceTracker =
                 new ServiceTracker<>(context, SchemaContextProvider.class, schemaServiceTrackerCustomizer);
         schemaContextProviderServiceTracker.open();
     }
 
     @Override
-    public void stop(BundleContext context) throws Exception {
-        if(configRegistryLookup != null) {
-            configRegistryLookup.interrupt();
+    @SuppressWarnings("checkstyle:hiddenField")
+    public void stop(final BundleContext context) throws Exception {
+        if (this.configRegistryLookup != null) {
+            this.configRegistryLookup.interrupt();
         }
-        if(osgiRegistrayion != null) {
-            osgiRegistrayion.unregister();
+        if (this.osgiRegistrayion != null) {
+            this.osgiRegistrayion.unregister();
         }
-        if (yangStoreServiceServiceRegistration != null) {
-            yangStoreServiceServiceRegistration.unregister();
-            yangStoreServiceServiceRegistration = null;
+        if (this.yangStoreServiceServiceRegistration != null) {
+            this.yangStoreServiceServiceRegistration.unregister();
+            this.yangStoreServiceServiceRegistration = null;
         }
     }
 
     /**
-     * Find ConfigRegistry from config manager in JMX
+     * Find ConfigRegistry from config manager in JMX.
      */
     private class ConfigRegistryLookupThread extends Thread {
         public static final int ATTEMPT_TIMEOUT_MS = 1000;
@@ -111,7 +149,7 @@ public class YangStoreActivator implements BundleActivator {
 
         private final YangStoreService yangStoreService;
 
-        private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
+        ConfigRegistryLookupThread(final YangStoreService yangStoreService) {
             super("config-registry-lookup");
             this.yangStoreService = yangStoreService;
         }
@@ -121,24 +159,25 @@ public class YangStoreActivator implements BundleActivator {
 
             ConfigRegistryJMXClient configRegistryJMXClient;
             ConfigRegistryJMXClient configRegistryJMXClientNoNotifications;
-            int i = 0;
+            int index = 0;
             // Config registry might not be present yet, but will be eventually
-            while(true) {
+            while (true) {
 
                 try {
-                    configRegistryJMXClient = new ConfigRegistryJMXClient(configMBeanServer);
-                    configRegistryJMXClientNoNotifications = ConfigRegistryJMXClient.createWithoutNotifications(configMBeanServer);
+                    configRegistryJMXClient = new ConfigRegistryJMXClient(YangStoreActivator.this.configMBeanServer);
+                    configRegistryJMXClientNoNotifications = ConfigRegistryJMXClient
+                            .createWithoutNotifications(YangStoreActivator.this.configMBeanServer);
                     break;
-                } catch (IllegalStateException e) {
-                    ++i;
-                    if (i > SILENT_ATTEMPTS) {
-                        LOG.info("JMX client not created after {} attempts, still trying", i, e);
+                } catch (final IllegalStateException e) {
+                    ++index;
+                    if (index > SILENT_ATTEMPTS) {
+                        LOG.info("JMX client not created after {} attempts, still trying", index, e);
                     } else {
-                        LOG.debug("JMX client could not be created, reattempting, try {}", i, e);
+                        LOG.debug("JMX client could not be created, reattempting, try {}", index, e);
                     }
                     try {
                         Thread.sleep(ATTEMPT_TIMEOUT_MS);
-                    } catch (InterruptedException e1) {
+                    } catch (final InterruptedException e1) {
                         Thread.currentThread().interrupt();
                         throw new IllegalStateException("Interrupted while reattempting connection", e1);
                     }
@@ -147,16 +186,16 @@ public class YangStoreActivator implements BundleActivator {
 
             final ConfigRegistryJMXClient jmxClient = configRegistryJMXClient;
             final ConfigRegistryJMXClient jmxClientNoNotifications = configRegistryJMXClientNoNotifications;
-            if (i > SILENT_ATTEMPTS) {
-                LOG.info("Created JMX client after {} attempts", i);
+            if (index > SILENT_ATTEMPTS) {
+                LOG.info("Created JMX client after {} attempts", index);
             } else {
-                LOG.debug("Created JMX client after {} attempts", i);
+                LOG.debug("Created JMX client after {} attempts", index);
             }
 
-            final ConfigSubsystemFacadeFactory configSubsystemFacade =
-                    new ConfigSubsystemFacadeFactory(jmxClient, jmxClientNoNotifications, yangStoreService);
-            osgiRegistrayion = context.registerService(ConfigSubsystemFacadeFactory.class, configSubsystemFacade, new Hashtable<String, Object>());
+            final ConfigSubsystemFacadeFactory configSubsystemFacade = new ConfigSubsystemFacadeFactory(jmxClient,
+                    jmxClientNoNotifications, this.yangStoreService);
+            YangStoreActivator.this.osgiRegistrayion = YangStoreActivator.this.context
+                    .registerService(ConfigSubsystemFacadeFactory.class, configSubsystemFacade, new Hashtable<>());
         }
     }
 }
-