Fix to update gateway group only if new gateway is added
Also implemented equals, hashCode, and toString for GatewayNode.
Change-Id: I4c487cafa263833701c5790b2057c1d9c23e33bf
diff --git a/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/api/GatewayNode.java b/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/api/GatewayNode.java
index c02c1ec..b2f0af0 100644
--- a/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/api/GatewayNode.java
+++ b/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/api/GatewayNode.java
@@ -15,9 +15,12 @@
*/
package org.onosproject.scalablegateway.api;
+import com.google.common.base.MoreObjects;
import org.onlab.packet.Ip4Address;
import org.onosproject.net.DeviceId;
+import java.util.Objects;
+
import static com.google.common.base.Preconditions.checkNotNull;
/**
@@ -62,6 +65,40 @@
return dataIpAddress;
}
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (obj instanceof GatewayNode) {
+ GatewayNode that = (GatewayNode) obj;
+ if (Objects.equals(gatewayDeviceId, that.gatewayDeviceId) &&
+ Objects.equals(gatewayExternalInterfaceName,
+ that.gatewayExternalInterfaceName) &&
+ Objects.equals(dataIpAddress, that.dataIpAddress)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(gatewayDeviceId,
+ gatewayExternalInterfaceName,
+ dataIpAddress);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("deviceId", gatewayDeviceId)
+ .add("externalPort", gatewayExternalInterfaceName)
+ .add("dataIp", dataIpAddress)
+ .toString();
+ }
+
/**
* Returns GatewayNode builder object.
*
diff --git a/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/ScalableGatewayManager.java b/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/ScalableGatewayManager.java
index adf354e..37e601c 100644
--- a/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/ScalableGatewayManager.java
+++ b/apps/scalablegateway/src/main/java/org/onosproject/scalablegateway/impl/ScalableGatewayManager.java
@@ -203,9 +203,15 @@
@Override
public boolean addGatewayNode(GatewayNode gatewayNode) {
- gatewayNodeMap.putIfAbsent(gatewayNode.getGatewayDeviceId(), gatewayNode);
- updateGatewayLoadBalance(gatewayNode, true);
- return true;
+ Versioned<GatewayNode> existingNode = gatewayNodeMap.putIfAbsent(
+ gatewayNode.getGatewayDeviceId(), gatewayNode);
+ if (existingNode == null) {
+ updateGatewayLoadBalance(gatewayNode, true);
+ log.info("Added {} to gateway pool", gatewayNode);
+ return true;
+ } else {
+ return false;
+ }
}
@Override