More defensive RPC handling in DOM Broker
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / SchemaServiceImpl.java
index 531f9e86c2dd76f55116a46a8d347d9b2daa1f6a..8afa1eeb5f0e475cfe65fb43936967a6f35bb0f3 100644 (file)
@@ -27,6 +27,7 @@ import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
 import org.opendaylight.controller.sal.core.api.model.SchemaService;
 import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener;
+import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,9 +42,10 @@ import com.google.common.collect.Sets;
 import static com.google.common.base.Preconditions.*;
 
 public class SchemaServiceImpl implements //
-SchemaService, //
-ServiceTrackerCustomizer<SchemaServiceListener, SchemaServiceListener>, //
-AutoCloseable {
+        SchemaContextProvider, //
+        SchemaService, //
+        ServiceTrackerCustomizer<SchemaServiceListener, SchemaServiceListener>, //
+        AutoCloseable {
     private static final Logger logger = LoggerFactory.getLogger(SchemaServiceImpl.class);
 
     private ListenerRegistry<SchemaServiceListener> listeners;
@@ -61,7 +63,7 @@ AutoCloseable {
     private BundleTracker<Object> bundleTracker;
     private final YangStoreCache cache = new YangStoreCache();
 
-    private ServiceTracker<SchemaServiceListener,SchemaServiceListener> listenerTracker;
+    private ServiceTracker<SchemaServiceListener, SchemaServiceListener> listenerTracker;
 
     public ListenerRegistry<SchemaServiceListener> getListeners() {
         return listeners;
@@ -93,13 +95,19 @@ AutoCloseable {
         if (listeners == null) {
             listeners = new ListenerRegistry<>();
         }
-        
+
         listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, this);
         bundleTracker = new BundleTracker<Object>(context, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, scanner);
         bundleTracker.open();
         listenerTracker.open();
     }
 
+    
+    @Override
+    public SchemaContext getSchemaContext() {
+        return getGlobalContext();
+    }
+    
     public SchemaContext getGlobalContext() {
         return getSchemaContextSnapshot();
     }
@@ -132,12 +140,11 @@ AutoCloseable {
         throw new UnsupportedOperationException();
     }
 
-    
     @Override
     public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(SchemaServiceListener listener) {
         return listeners.register(listener);
     }
-    
+
     @Override
     public void close() throws Exception {
         bundleTracker.close();
@@ -147,7 +154,7 @@ AutoCloseable {
 
     private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState,
             boolean adding) {
-        Preconditions.checkArgument(changedURLs.size() > 0, "No change can occur when no URLs are changed");
+        Preconditions.checkArgument(!changedURLs.isEmpty(), "No change can occur when no URLs are changed");
 
         try {
             // consistent state
@@ -166,8 +173,8 @@ AutoCloseable {
         } catch (Exception e) {
             // inconsistent state
             logger.debug(
-                    "SchemaService is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
-                    consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e.toString());
+                    "SchemaService is falling back on last consistent state containing {}, inconsistent yang files {}",
+                    consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e);
             return false;
         }
     }
@@ -196,15 +203,15 @@ AutoCloseable {
 
     private void updateCache(SchemaContext snapshot) {
         cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
-        
+
         Object[] services = listenerTracker.getServices();
-        if(services != null) {
-            for(Object rawListener : services) {
+        if (services != null) {
+            for (Object rawListener : services) {
                 SchemaServiceListener listener = (SchemaServiceListener) rawListener;
                 try {
                     listener.onGlobalContextUpdated(snapshot);
                 } catch (Exception e) {
-                    logger.error("Exception occured during invoking listener",e);
+                    logger.error("Exception occured during invoking listener", e);
                 }
             }
         }
@@ -212,7 +219,7 @@ AutoCloseable {
             try {
                 listener.getInstance().onGlobalContextUpdated(snapshot);
             } catch (Exception e) {
-                logger.error("Exception occured during invoking listener",e);
+                logger.error("Exception occured during invoking listener", e);
             }
         }
     }
@@ -225,8 +232,9 @@ AutoCloseable {
             // system bundle might have config-api on classpath &&
             // config-api contains yang files =>
             // system bundle might contain yang files from that bundle
-            if (bundle.getBundleId() == 0)
+            if (bundle.getBundleId() == 0) {
                 return bundle;
+            }
 
             Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
             if (enumeration != null && enumeration.hasMoreElements()) {
@@ -243,7 +251,7 @@ AutoCloseable {
                     proposedNewState.putAll(inconsistentBundlesToYangURLs);
                     proposedNewState.putAll(bundle, addedURLs);
                     boolean adding = true;
-                    
+
                     if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
                         inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
                     }
@@ -301,23 +309,23 @@ AutoCloseable {
             this.cachedContextSnapshot = ctx;
         }
     }
-    
+
     @Override
     public SchemaServiceListener addingService(ServiceReference<SchemaServiceListener> reference) {
-        
+
         SchemaServiceListener listener = context.getService(reference);
         SchemaContext _ctxContext = getGlobalContext();
-        if(getContext() != null) {
+        if (getContext() != null) {
             listener.onGlobalContextUpdated(_ctxContext);
         }
         return listener;
     }
-    
+
     @Override
     public void modifiedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
         // NOOP
     }
-    
+
     @Override
     public void removedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
         context.ungetService(reference);