[ONOS-7586] ONOS leadership change does not occurs sometimes.

Change-Id: Ifa1999c7ce0601fc65cad22af753b78c43b5da0c
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorService.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorService.java
index 5afa954..e330c32 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorService.java
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/resources/impl/AtomixLeaderElectorService.java
@@ -353,16 +353,16 @@
 
     private void onSessionEnd(RaftSession session) {
         listeners.remove(session.sessionId().id());
-        Set<String> topics = elections.keySet();
+        Set<String> topics = ImmutableSet.copyOf(elections.keySet());
         List<Change<Leadership>> changes = Lists.newArrayList();
-        topics.forEach(topic -> {
+        for (String topic: topics) {
             Leadership oldLeadership = leadership(topic);
             elections.compute(topic, (k, v) -> v.cleanup(topic, session, termCounter(topic)::incrementAndGet));
             Leadership newLeadership = leadership(topic);
             if (!Objects.equal(oldLeadership, newLeadership)) {
                 changes.add(new Change<>(oldLeadership, newLeadership));
             }
-        });
+        }
         notifyLeadershipChanges(changes);
     }