Add support to CordMcast for sending multicast sink ports to remote cluster.

Change-Id: Ib915c68218033e1dcfa6f738a629c2d1d8442261
diff --git a/src/main/java/org/onosproject/cordconfig/access/AccessAgentConfig.java b/src/main/java/org/onosproject/cordconfig/access/AccessAgentConfig.java
index 704c6a1..6dc5c9d 100644
--- a/src/main/java/org/onosproject/cordconfig/access/AccessAgentConfig.java
+++ b/src/main/java/org/onosproject/cordconfig/access/AccessAgentConfig.java
@@ -20,11 +20,9 @@
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.Iterators;
 import com.google.common.collect.Maps;
-import org.apache.commons.lang.StringUtils;
 import org.onlab.packet.MacAddress;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
-import org.onosproject.net.PortNumber;
 import org.onosproject.net.config.Config;
 
 import java.util.Map;
@@ -49,7 +47,7 @@
         return hasOnlyFields(OLTS, AGENT_MAC, VTN_LOCATION) &&
                 isMacAddress(AGENT_MAC, MANDATORY) &&
                 isConnectPoint(VTN_LOCATION, OPTIONAL) &&
-                isValidOlts();
+                areOltsValid();
     }
 
     /**
@@ -61,7 +59,7 @@
         JsonNode olts = node.get(OLTS);
         Map<ConnectPoint, MacAddress> oltMacInfo = Maps.newHashMap();
         olts.fields().forEachRemaining(item -> oltMacInfo.put(
-                new ConnectPoint(subject(), PortNumber.fromString(item.getKey())),
+                ConnectPoint.deviceConnectPoint(item.getKey()),
                 MacAddress.valueOf(item.getValue().asText())));
 
         MacAddress agentMac = MacAddress.valueOf(node.path(AGENT_MAC).asText());
@@ -77,12 +75,13 @@
         return new AccessAgentData(subject(), oltMacInfo, agentMac, vtnLocation);
     }
 
-    private boolean isValidOlts() {
+    private boolean areOltsValid() {
         JsonNode olts = node.get(OLTS);
         if (!olts.isObject()) {
             return false;
         }
-        return !Iterators.any(olts.fields(), item -> !StringUtils.isNumeric(item.getKey()) ||
-                        !isMacAddress((ObjectNode) olts, item.getKey(), MANDATORY));
+        return Iterators.all(olts.fields(),
+                item -> ConnectPoint.deviceConnectPoint(item.getKey()) != null &&
+                        isMacAddress((ObjectNode) olts, item.getKey(), MANDATORY));
     }
 }
diff --git a/src/main/java/org/onosproject/cordconfig/access/AccessAgentData.java b/src/main/java/org/onosproject/cordconfig/access/AccessAgentData.java
index 8d14dde..de7a342 100644
--- a/src/main/java/org/onosproject/cordconfig/access/AccessAgentData.java
+++ b/src/main/java/org/onosproject/cordconfig/access/AccessAgentData.java
@@ -17,12 +17,15 @@
 package org.onosproject.cordconfig.access;
 
 import com.google.common.collect.ImmutableMap;
+import org.apache.commons.lang3.tuple.Pair;
 import org.onlab.packet.MacAddress;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 
+import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
@@ -30,32 +33,41 @@
  * Information about an access agent.
  */
 public class AccessAgentData {
+
     private static final String DEVICE_ID_MISSING = "Device ID cannot be null";
     private static final String OLT_INFO_MISSING = "OLT information cannot be null";
     private static final String AGENT_MAC_MISSING = "Agent mac cannot be null";
     private static final String VTN_MISSING = "VTN location cannot be null";
 
+    private static final int CHIP_PORT_RANGE_SIZE = 130;
 
     private final Map<ConnectPoint, MacAddress> oltMacInfo;
     private final MacAddress agentMac;
     private final Optional<ConnectPoint> vtnLocation;
     private final DeviceId deviceId;
 
+    // OLT chip information sorted by ascending MAC address
+    private final List<Pair<ConnectPoint, MacAddress>> sortedOltChips;
 
     /**
-     * Constucts an agent configuration for a given device.
+     * Constructs an agent configuration for a given device.
      *
-     * @param deviceId    access device id
+     * @param deviceId    access device ID
      * @param oltMacInfo  a map of olt chips and their mac address
-     * @param agentMac    the mac address of the agent
+     * @param agentMac    the MAC address of the agent
      * @param vtnLocation the location of the agent
      */
     public AccessAgentData(DeviceId deviceId, Map<ConnectPoint, MacAddress> oltMacInfo,
                            MacAddress agentMac, Optional<ConnectPoint> vtnLocation) {
         this.deviceId = checkNotNull(deviceId, DEVICE_ID_MISSING);
-        this.oltMacInfo = checkNotNull(oltMacInfo, OLT_INFO_MISSING);
+        this.oltMacInfo = ImmutableMap.copyOf(checkNotNull(oltMacInfo, OLT_INFO_MISSING));
         this.agentMac = checkNotNull(agentMac, AGENT_MAC_MISSING);
         this.vtnLocation = checkNotNull(vtnLocation, VTN_MISSING);
+
+        this.sortedOltChips = oltMacInfo.entrySet().stream()
+                .sorted((e1, e2) -> Long.compare(e1.getValue().toLong(), e2.getValue().toLong()))
+                .map(e -> Pair.of(e.getKey(), e.getValue()))
+                .collect(Collectors.toList());
     }
 
     /**
@@ -68,17 +80,17 @@
     }
 
     /**
-     * Returns the mapping of olt chips to mac addresses. Each chip is
+     * Returns the mapping of OLT chips to MAC addresses. Each chip is
      * symbolized by a connect point.
      *
-     * @return a mapping of chips (as connect points) to mac addresses
+     * @return a mapping of chips (as connect points) to MAC addresses
      */
     public Map<ConnectPoint, MacAddress> getOltMacInfo() {
-        return ImmutableMap.copyOf(oltMacInfo);
+        return oltMacInfo;
     }
 
     /**
-     * Reuturns the agents mac address.
+     * Returns the agent's MAC address.
      *
      * @return a mac address
      */
@@ -94,4 +106,21 @@
     public Optional<ConnectPoint> getVtnLocation() {
         return vtnLocation;
     }
+
+    /**
+     * Returns the point where the OLT is connected to the fabric given a
+     * connect point on the agent device.
+     *
+     * @param agentConnectPoint connect point on the agent device
+     * @return point were OLT is connected to fabric
+     */
+    public Optional<ConnectPoint> getOltConnectPoint(ConnectPoint agentConnectPoint) {
+        int index = ((int) agentConnectPoint.port().toLong()) / CHIP_PORT_RANGE_SIZE;
+
+        if (index >= sortedOltChips.size()) {
+            return Optional.empty();
+        }
+
+        return Optional.of(sortedOltChips.get(index).getKey());
+    }
 }