BUG-7697: add defences against nulls 50/51150/2
authorRobert Varga <rovarga@cisco.com>
Fri, 27 Jan 2017 19:40:49 +0000 (20:40 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 27 Jan 2017 20:09:50 +0000 (21:09 +0100)
Null listener is invalid, also make sure we do not ever set
prevRpcs to null.

Change-Id: If8cd16e93a2a07c77a26569c8ecacdc35696cea1
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMRpcRouter.java

index daf0d06..3c28d30 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<>();