BUG-7697: add defences against nulls
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMRpcRouter.java
index daf0d065f8b5435ed025465d4e3a5918d38c844a..3c28d3078664bed7481373e164655ee499dec24f 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.controller.md.sal.dom.broker.impl;
 
+import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableList.Builder;
@@ -119,13 +121,13 @@ public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcP
     @Override
     public synchronized <T extends DOMRpcAvailabilityListener> ListenerRegistration<T> registerRpcListener(
             final T listener) {
-        final Registration<T> ret = new Registration<>(this, listener);
+        final Registration<T> ret = new Registration<>(this, listener, routingTable.getRpcs(listener));
         final Builder<Registration<?>> b = ImmutableList.builder();
         b.addAll(listeners);
         b.add(ret);
         listeners = b.build();
 
-        listenerNotifier.execute(() -> ret.initialTable(routingTable));
+        listenerNotifier.execute(() -> ret.initialTable());
         return ret;
     }
 
@@ -148,9 +150,11 @@ public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcP
 
         private Map<SchemaPath, Set<YangInstanceIdentifier>> prevRpcs;
 
-        Registration(final DOMRpcRouter router, final T listener) {
-            super(listener);
-            this.router = router;
+        Registration(final DOMRpcRouter router, final T listener,
+                final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs) {
+            super(Preconditions.checkNotNull(listener));
+            this.router = Preconditions.checkNotNull(router);
+            this.prevRpcs = Preconditions.checkNotNull(rpcs);
         }
 
         @Override
@@ -158,30 +162,25 @@ public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcP
             router.removeListener(this);
         }
 
-        void initialTable(final DOMRpcRoutingTable newTable) {
-            final T l = getInstance();
-            if (l == null) {
-                return;
-            }
-
-            final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
+        void initialTable() {
             final Collection<DOMRpcIdentifier> added = new ArrayList<>();
-            for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : rpcs.entrySet()) {
+            for (Entry<SchemaPath, Set<YangInstanceIdentifier>> e : prevRpcs.entrySet()) {
                 added.addAll(Collections2.transform(e.getValue(), i -> DOMRpcIdentifier.create(e.getKey(), i)));
             }
-            prevRpcs = rpcs;
+
             if (!added.isEmpty()) {
+                final T l = getInstance();
                 l.onRpcAvailable(added);
             }
         }
 
         void addRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
             final T l = getInstance();
-            if (l == null || !l.acceptsImplementation(impl)) {
+            if (!l.acceptsImplementation(impl)) {
                 return;
             }
 
-            final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
+            final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = Verify.verifyNotNull(newTable.getRpcs(l));
             final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
 
             final Collection<DOMRpcIdentifier> added = new ArrayList<>();
@@ -202,11 +201,11 @@ public final class DOMRpcRouter implements AutoCloseable, DOMRpcService, DOMRpcP
 
         void removeRpc(final DOMRpcRoutingTable newTable, final DOMRpcImplementation impl) {
             final T l = getInstance();
-            if (l == null || !l.acceptsImplementation(impl)) {
+            if (!l.acceptsImplementation(impl)) {
                 return;
             }
 
-            final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = newTable.getRpcs(l);
+            final Map<SchemaPath, Set<YangInstanceIdentifier>> rpcs = Verify.verifyNotNull(newTable.getRpcs(l));
             final MapDifference<SchemaPath, Set<YangInstanceIdentifier>> diff = Maps.difference(prevRpcs, rpcs);
 
             final Collection<DOMRpcIdentifier> removed = new ArrayList<>();