[ONOS-5264] [ONOS-5242] Intents w/ FilteredConnectPoint

Change-Id: Ibe9062c904ad9a6c3ba001fe57be7cec49eb8a4d
diff --git a/core/api/src/main/java/org/onosproject/net/intent/SinglePointToMultiPointIntent.java b/core/api/src/main/java/org/onosproject/net/intent/SinglePointToMultiPointIntent.java
index 4b17c66..8431a0a 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/SinglePointToMultiPointIntent.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/SinglePointToMultiPointIntent.java
@@ -18,35 +18,31 @@
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 
 import com.google.common.collect.Sets;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.FilteredConnectPoint;
 import org.onosproject.net.flow.TrafficSelector;
 import org.onosproject.net.flow.TrafficTreatment;
+import org.slf4j.Logger;
 
-import java.util.Map;
 import java.util.Set;
 import java.util.List;
+import java.util.stream.Collectors;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Abstraction of single source, multiple destination connectivity intent.
  */
 @Beta
 public final class SinglePointToMultiPointIntent extends ConnectivityIntent {
-
-    private final ConnectPoint ingressPoint;
-    private final Set<ConnectPoint> egressPoints;
-    /**
-     * To manage multiple treatments use case.
-     */
-    private final Map<ConnectPoint, TrafficTreatment> egressTreatments;
+    private final FilteredConnectPoint ingressPoint;
+    private final Set<FilteredConnectPoint> egressPoints;
 
     /**
      * Creates a new single-to-multi point connectivity intent.
@@ -59,7 +55,6 @@
      * @param egressPoints set of ports on which traffic will egress
      * @param constraints constraints to apply to the intent
      * @param priority priority to use for flows generated by this intent
-     * @param egressTreatments map to store the association egress to treatment
      * @throws NullPointerException if {@code ingressPoint} or
      *             {@code egressPoints} is null
      * @throws IllegalArgumentException if the size of {@code egressPoints} is
@@ -69,22 +64,20 @@
                                           Key key,
                                           TrafficSelector selector,
                                           TrafficTreatment treatment,
-                                          ConnectPoint ingressPoint,
-                                          Set<ConnectPoint> egressPoints,
+                                          FilteredConnectPoint ingressPoint,
+                                          Set<FilteredConnectPoint> egressPoints,
                                           List<Constraint> constraints,
-                                          int priority,
-                                          Map<ConnectPoint, TrafficTreatment> egressTreatments) {
+                                          int priority) {
         super(appId, key, ImmutableList.of(), selector, treatment, constraints,
               priority);
         checkNotNull(egressPoints);
         checkNotNull(ingressPoint);
         checkArgument(!egressPoints.isEmpty(), "Egress point set cannot be empty");
         checkArgument(!egressPoints.contains(ingressPoint),
-                "Set of egresses should not contain ingress (ingress: %s)", ingressPoint);
+                      "Set of egresses should not contain ingress (ingress: %s)", ingressPoint);
 
         this.ingressPoint = ingressPoint;
         this.egressPoints = Sets.newHashSet(egressPoints);
-        this.egressTreatments = egressTreatments;
     }
 
     /**
@@ -100,17 +93,42 @@
     }
 
     /**
+     * Creates a new builder pre-populated with the information in the given
+     * intent.
+     *
+     * @param intent initial intent
+     * @return intent builder
+     */
+    public static Builder builder(SinglePointToMultiPointIntent intent) {
+        return new Builder(intent);
+    }
+
+    /**
      * Builder of a single point to multi point intent.
      */
     public static final class Builder extends ConnectivityIntent.Builder {
-        ConnectPoint ingressPoint;
-        Set<ConnectPoint> egressPoints;
-        Map<ConnectPoint, TrafficTreatment> egressTreatments = ImmutableMap.of();
+        private final Logger log = getLogger(getClass());
+        private FilteredConnectPoint ingressPoint;
+        private Set<FilteredConnectPoint> egressPoints;
 
         private Builder() {
             // Hide constructor
         }
 
+        /**
+         * Creates a new builder pre-populated with information from the given
+         * intent.
+         *
+         * @param intent initial intent
+         */
+        protected Builder(SinglePointToMultiPointIntent intent) {
+            super(intent);
+
+            this.filteredEgressPoints(intent.filteredEgressPoints())
+                    .filteredIngressPoint(intent.filteredIngressPoint());
+
+        }
+
         @Override
         public Builder appId(ApplicationId appId) {
             return (Builder) super.appId(appId);
@@ -148,8 +166,13 @@
          * @param ingressPoint ingress connect point
          * @return this builder
          */
+        @Deprecated
         public Builder ingressPoint(ConnectPoint ingressPoint) {
-            this.ingressPoint = ingressPoint;
+            if (this.ingressPoint != null) {
+                log.warn("Ingress point is already set, " +
+                "this will override original ingress point.");
+            }
+            this.ingressPoint = new FilteredConnectPoint(ingressPoint);
             return this;
         }
 
@@ -160,24 +183,46 @@
          * @param egressPoints egress connect points
          * @return this builder
          */
+        @Deprecated
         public Builder egressPoints(Set<ConnectPoint> egressPoints) {
-            this.egressPoints = ImmutableSet.copyOf(egressPoints);
+            if (this.egressPoints != null) {
+                log.warn("Egress points are already set, " +
+                                 "this will override original egress points.");
+            }
+            Set<FilteredConnectPoint> filteredConnectPoints =
+                    egressPoints.stream()
+                            .map(FilteredConnectPoint::new)
+                            .collect(Collectors.toSet());
+            this.egressPoints = ImmutableSet.copyOf(filteredConnectPoints);
             return this;
         }
 
         /**
-         * Sets the treatments of the single point to multi point intent
-         * that will be built.
+         * Sets the filtered ingress point of the single point to
+         * multi point intent that will be built.
          *
-         * @param egressTreatments the multiple treatments
+         * @param ingressPoint ingress connect point
          * @return this builder
          */
-        public Builder treatments(Map<ConnectPoint, TrafficTreatment> egressTreatments) {
-            this.egressTreatments = ImmutableMap.copyOf(egressTreatments);
+        public Builder filteredIngressPoint(FilteredConnectPoint ingressPoint) {
+            this.ingressPoint = ingressPoint;
             return this;
         }
 
         /**
+         * Sets the filtered egress points of the single point to
+         * multi point intent that will be built.
+         *
+         * @param egressPoints egress connect points
+         * @return this builder
+         */
+        public Builder filteredEgressPoints(Set<FilteredConnectPoint> egressPoints) {
+            this.egressPoints = ImmutableSet.copyOf(egressPoints);
+            return this;
+        }
+
+
+        /**
          * Builds a single point to multi point intent from the
          * accumulated parameters.
          *
@@ -185,12 +230,6 @@
          */
         public SinglePointToMultiPointIntent build() {
 
-            if (treatment != null && !treatment.allInstructions().isEmpty() &&
-                    !treatment.equals(DefaultTrafficTreatment.emptyTreatment()) &&
-                    egressTreatments != null && !egressTreatments.isEmpty()) {
-                throw new IllegalArgumentException("Treatment and Multiple Treatments are both set");
-            }
-
             return new SinglePointToMultiPointIntent(
                     appId,
                     key,
@@ -199,8 +238,7 @@
                     ingressPoint,
                     egressPoints,
                     constraints,
-                    priority,
-                    egressTreatments
+                    priority
             );
         }
     }
@@ -212,7 +250,6 @@
         super();
         this.ingressPoint = null;
         this.egressPoints = null;
-        this.egressTreatments = null;
     }
 
     /**
@@ -222,7 +259,7 @@
      * @return ingress port
      */
     public ConnectPoint ingressPoint() {
-        return ingressPoint;
+        return ingressPoint.connectPoint();
     }
 
     /**
@@ -231,17 +268,31 @@
      * @return set of egress ports
      */
     public Set<ConnectPoint> egressPoints() {
-        return egressPoints;
+        return egressPoints.stream()
+                .map(FilteredConnectPoint::connectPoint)
+                .collect(Collectors.toSet());
     }
 
     /**
-     * Returns the multiple treatments jointly with their connection points.
-     * @return multiple treatments
+     * Returns the filtered port on which the ingress traffic should be connected to the
+     * egress.
+     *
+     * @return ingress port
      */
-    public Map<ConnectPoint, TrafficTreatment> egressTreatments() {
-        return egressTreatments;
+    public FilteredConnectPoint filteredIngressPoint() {
+        return ingressPoint;
     }
 
+    /**
+     * Returns the set of filtered ports on which the traffic should egress.
+     *
+     * @return set of egress ports
+     */
+    public Set<FilteredConnectPoint> filteredEgressPoints() {
+        return egressPoints;
+    }
+
+
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(getClass())
@@ -254,7 +305,8 @@
                 .add("treatment", treatment())
                 .add("ingress", ingressPoint)
                 .add("egress", egressPoints)
-                .add("treatments", egressTreatments)
+                .add("filteredIngressCPs", filteredIngressPoint())
+                .add("filteredEgressCP", filteredEgressPoints())
                 .add("constraints", constraints())
                 .toString();
     }