Optimize NAPT switch selection logic 98/64798/5
authorVishal Thapar <vishal.thapar@ericsson.com>
Fri, 27 Oct 2017 10:00:13 +0000 (15:30 +0530)
committerSam Hague <shague@redhat.com>
Sat, 10 Feb 2018 02:11:16 +0000 (02:11 +0000)
NaptSwitch selection logic today sorts switches in descending order of
weight and then uses iterator to find last entry. By changing the
comparator and convreting it into ascending order, we can return the first
entry without needing to iterate through list of switches.

Change-Id: I6620d9e5a92ea60d100ed2d767e73fb1bdef998e
Signed-off-by: Vishal Thapar <vishal.thapar@ericsson.com>
vpnservice/natservice/natservice-impl/src/main/java/org/opendaylight/netvirt/natservice/internal/NAPTSwitchSelector.java

index cfc83b6f0ba44c5a2a5c9a0df8ad7af795493d45..7067a1b4ec795b8d2abbf325ebc863f342a92d31 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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,
@@ -10,7 +10,6 @@ package org.opendaylight.netvirt.natservice.internal;
 import com.google.common.base.Optional;
 import java.math.BigInteger;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -64,37 +63,17 @@ public class NAPTSwitchSelector {
 
             LOG.debug("selectNewNAPTSwitch : Current switch weights for router {} - {}", routerName, switchWeights);
 
-            Iterator<SwitchWeight> it = switchWeights.iterator();
             RouterToNaptSwitchBuilder routerToNaptSwitchBuilder =
                 new RouterToNaptSwitchBuilder().setRouterName(routerName);
-            if (switchWeights.size() == 1) {
-                SwitchWeight singleSwitchWeight = null;
-                while (it.hasNext()) {
-                    singleSwitchWeight = it.next();
-                }
-                primarySwitch = singleSwitchWeight.getSwitch();
-                RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(primarySwitch).build();
-
-                MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
-                    getNaptSwitchesIdentifier(routerName), id);
+            SwitchWeight firstSwitchWeight = switchWeights.iterator().next();
+            primarySwitch = firstSwitchWeight.getSwitch();
+            RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(primarySwitch).build();
 
-                LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container "
-                    + "for single switch");
-                return primarySwitch;
-            } else {
-                SwitchWeight firstSwitchWeight = null;
-                while (it.hasNext()) {
-                    firstSwitchWeight = it.next();
-                }
-                primarySwitch = firstSwitchWeight.getSwitch();
-                RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(primarySwitch).build();
+            MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
+                getNaptSwitchesIdentifier(routerName), id);
 
-                MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
-                    getNaptSwitchesIdentifier(routerName), id);
-
-                LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container");
-                return primarySwitch;
-            }
+            LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container");
+            return primarySwitch;
         } else {
             primarySwitch = BigInteger.ZERO;
 
@@ -199,7 +178,7 @@ public class NAPTSwitchSelector {
 
         @Override
         public int compareTo(@Nonnull SwitchWeight switchWeight) {
-            return switchWeight.getWeight() - weight;
+            return weight - switchWeight.getWeight();
         }
     }
 }