Fix performance issue in DistributedCastorStore.

The CastorArpManager
runs slowly when it contains many ARP records. The issue is caused by
the copy operation every time the `addressMap` is queried.

Change-Id: Ice80a1230dabebe3f1ed426a6bcecefac126d1c8
diff --git a/apps/castor/src/main/java/org/onosproject/castor/CastorArpManager.java b/apps/castor/src/main/java/org/onosproject/castor/CastorArpManager.java
index ff7ec6b..d471ff2 100644
--- a/apps/castor/src/main/java/org/onosproject/castor/CastorArpManager.java
+++ b/apps/castor/src/main/java/org/onosproject/castor/CastorArpManager.java
@@ -244,7 +244,7 @@
      */
     private void updateMac(MessageContext context) {
 
-        if ((castorStore.getAddressMap()).containsKey(context.sender())) {
+        if (castorStore.getMacAddress(context.sender) != null) {
             return;
         }
         Ethernet eth = context.packet();
diff --git a/apps/castor/src/main/java/org/onosproject/castor/CastorStore.java b/apps/castor/src/main/java/org/onosproject/castor/CastorStore.java
index 03bba7b..846ebb5 100644
--- a/apps/castor/src/main/java/org/onosproject/castor/CastorStore.java
+++ b/apps/castor/src/main/java/org/onosproject/castor/CastorStore.java
@@ -78,6 +78,17 @@
      */
     Map<IpAddress, MacAddress> getAddressMap();
 
+
+    /**
+     * Get MacAddress from addressMap. Returns null if `ip` is
+     * not found.
+     *
+     * @param ip IP Address
+     *
+     * @return Corresponding MacAddress or null
+     */
+    MacAddress getMacAddress(IpAddress ip);
+
     /**
      * Sets the mapping from IP address to Mac.
      *
diff --git a/apps/castor/src/main/java/org/onosproject/castor/CastorWebResource.java b/apps/castor/src/main/java/org/onosproject/castor/CastorWebResource.java
index 37630cf..79b5adc 100644
--- a/apps/castor/src/main/java/org/onosproject/castor/CastorWebResource.java
+++ b/apps/castor/src/main/java/org/onosproject/castor/CastorWebResource.java
@@ -82,8 +82,8 @@
             ObjectMapper mapper = new ObjectMapper();
             Peer peer = mapper.readValue(incomingData, Peer.class);
             get(ConnectivityManagerService.class).setUpConnectivity(peer);
-            if ((get(CastorStore.class)).getAddressMap()
-                    .get(IpAddress.valueOf(peer.getIpAddress())) != null) {
+            if ((get(CastorStore.class))
+                    .getMacAddress(IpAddress.valueOf(peer.getIpAddress())) != null) {
                 get(ConnectivityManagerService.class).setUpL2(peer);
             } else {
                 get(ArpService.class).createArp(peer);
diff --git a/apps/castor/src/main/java/org/onosproject/castor/ConnectivityManager.java b/apps/castor/src/main/java/org/onosproject/castor/ConnectivityManager.java
index 5125ce3..1e067fc 100644
--- a/apps/castor/src/main/java/org/onosproject/castor/ConnectivityManager.java
+++ b/apps/castor/src/main/java/org/onosproject/castor/ConnectivityManager.java
@@ -330,7 +330,7 @@
             }
         }
         TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
-        MacAddress macAddress = castorStore.getAddressMap().get(IpAddress.valueOf(peer.getIpAddress()));
+        MacAddress macAddress = castorStore.getMacAddress(IpAddress.valueOf(peer.getIpAddress()));
         selector.matchEthDst(macAddress);
         TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
         Key key = Key.of(peer.getIpAddress(), appId);
diff --git a/apps/castor/src/main/java/org/onosproject/castor/DistributedCastorStore.java b/apps/castor/src/main/java/org/onosproject/castor/DistributedCastorStore.java
index 3a1a98c..b38c23f 100644
--- a/apps/castor/src/main/java/org/onosproject/castor/DistributedCastorStore.java
+++ b/apps/castor/src/main/java/org/onosproject/castor/DistributedCastorStore.java
@@ -157,6 +157,11 @@
     }
 
     @Override
+    public MacAddress getMacAddress(IpAddress ip) {
+        return Versioned.valueOrNull(addressMap.get(ip));
+    }
+
+    @Override
     public void setAddressMap(IpAddress ip, MacAddress mac) {
         addressMap.put(ip, mac);
     }