Introducing EVC and FC Builders

Change-Id: Idecc63150e2f5724a9f5d23c98cb735e5b7d3310
diff --git a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetForwardingConstruct.java b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetForwardingConstruct.java
index c055837..6b20e4b 100644
--- a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetForwardingConstruct.java
+++ b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetForwardingConstruct.java
@@ -26,6 +26,8 @@
 import java.util.stream.Collectors;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * Representation of a CE Forwarding Construct.
@@ -35,13 +37,13 @@
     private Set<CarrierEthernetLogicalTerminationPoint> ltpSet;
     private VlanId vlanId;
     private CarrierEthernetMetroConnectivity metroConnectivity;
-    private boolean congruentPaths;
+    private boolean congruentPaths = true;
     protected AtomicInteger refCount;
 
-    // Set to true if both directions should use the same path
-    private static final boolean CONGRUENT_PATHS = true;
-
-    // Note: fcId should be provided only when updating an existing FC
+    // TODO: Remove id from constructor - currently used only when updating FC
+    // TODO: Add congruentPaths flag to constructor and Builder
+    // TODO: Make constructor private when SCA/NRP API apps are migrated
+    @Deprecated
     public CarrierEthernetForwardingConstruct(String id, String cfgId, Type type,
                                               Set<CarrierEthernetLogicalTerminationPoint> ltpSet,
                                               Duration maxLatency) {
@@ -49,7 +51,6 @@
         this.ltpSet = new HashSet<>(ltpSet);
         this.vlanId = null;
         this.metroConnectivity = new CarrierEthernetMetroConnectivity(null, OpticalPathEvent.Type.PATH_REMOVED);
-        this.congruentPaths = CONGRUENT_PATHS;
         this.refCount = new AtomicInteger();
     }
 
@@ -94,9 +95,9 @@
     }
 
     /**
-     * Returns true if FC requires congruent paths.
+     * Returns true if FC requires that both directions should use the same path.
      *
-     * @return true if congruent paths required
+     * @return true if both directions should use the same path
      */
     public boolean congruentPaths() {
         return congruentPaths;
@@ -158,4 +159,95 @@
                 .add("refCount", refCount)
                 .add("LTPs", ltpSet).toString();
     }
+
+    /**
+     * Returns a new builder.
+     *
+     * @return new builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Builder of CarrierEthernetForwardingConstruct entities.
+     */
+    public static final class Builder {
+
+        private String id;
+        private String cfgId;
+        private Type type;
+        private Duration maxLatency;
+        private Set<CarrierEthernetLogicalTerminationPoint> ltpSet;
+        private boolean congruentPaths;
+
+        /**
+         * Sets the id of this builder.
+         *
+         * @param id the builder id to set
+         * @return this builder instance
+         */
+        public Builder id(String id) {
+            this.id = id;
+            return this;
+        }
+
+        /**
+         * Sets the cfgId of this builder.
+         *
+         * @param cfgId the builder cfgId to set
+         * @return this builder instance
+         */
+        public Builder cfgId(String cfgId) {
+            this.cfgId = cfgId;
+            return this;
+        }
+
+        /**
+         * Sets the type of this builder.
+         *
+         * @param type the builder type to set
+         * @return this builder instance
+         */
+        public Builder type(Type type) {
+            this.type = type;
+            return this;
+        }
+
+        /**
+         * Sets the maxLatency of this builder.
+         *
+         * @param maxLatency the builder maxLatency to set
+         * @return this builder instance
+         */
+        public Builder maxLatency(Duration maxLatency) {
+            this.maxLatency = maxLatency;
+            return this;
+        }
+
+        /**
+         * Sets the ltpSet of this builder.
+         *
+         * @param ltpSet the builder ltpSet to set
+         * @return this builder instance
+         */
+        public Builder ltpSet(Set<CarrierEthernetLogicalTerminationPoint> ltpSet) {
+            this.ltpSet = ltpSet;
+            return this;
+        }
+
+        /**
+         * Builds a new CarrierEthernetForwardingConstruct instance.
+         * based on this builder's parameters
+         *
+         * @return a new CarrierEthernetForwardingConstruct instance
+         */
+        public CarrierEthernetForwardingConstruct build() {
+            checkNotNull(type, "FC must have a type");
+            checkArgument(ltpSet != null && ltpSet.size() > 1,
+                          "FC must include at least two LTPs");
+            return new CarrierEthernetForwardingConstruct(id, cfgId, type,
+                                                          ltpSet, maxLatency);
+        }
+    }
 }
diff --git a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetManager.java b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetManager.java
index 297deed..7aa4860 100644
--- a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetManager.java
+++ b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetManager.java
@@ -432,7 +432,10 @@
     CarrierEthernetForwardingConstruct fcFromEvc(CarrierEthernetVirtualConnection evc) {
         Set<CarrierEthernetLogicalTerminationPoint> ltpSet = new HashSet<>();
         evc.uniSet().forEach(uni -> ltpSet.add(new CarrierEthernetLogicalTerminationPoint(null, uni)));
-        return new CarrierEthernetForwardingConstruct(null, null, evc.type(), ltpSet, null);
+        return CarrierEthernetForwardingConstruct.builder()
+                .type(evc.type())
+                .ltpSet(ltpSet)
+                .build();
     }
 
     /**
@@ -582,23 +585,24 @@
         //////////////////////////////////////////////////////////////////////////////////
 
         ltpSetMap.values().stream().collect(Collectors.toSet()).forEach(ltpSet -> {
+            CarrierEthernetForwardingConstruct.Builder fcBuilder =
+                    CarrierEthernetForwardingConstruct.builder().ltpSet(ltpSet);
             // Type is determined by number and type of LTPs in each set
             CarrierEthernetVirtualConnection.Type fcType =
                     ltpSet.size() == 2 ? CarrierEthernetVirtualConnection.Type.POINT_TO_POINT
                             : CarrierEthernetConnection.Type.MULTIPOINT_TO_MULTIPOINT;
-            CarrierEthernetForwardingConstruct fc =
-                    new CarrierEthernetForwardingConstruct(null, null, null, ltpSet, null);
             // If one of the LTPs is LEAF, indicate FC as ROOT_MULTIPOINT
-            for (CarrierEthernetLogicalTerminationPoint ltp : fc.ltpSet()) {
+            for (CarrierEthernetLogicalTerminationPoint ltp : ltpSet) {
                 if (ltp.role().equals(CarrierEthernetLogicalTerminationPoint.Role.LEAF)) {
                     fcType = CarrierEthernetConnection.Type.ROOT_MULTIPOINT;
                     break;
                 }
             }
-            fc.setType(fcType);
-            fcSet.add(fc);
+            fcSet.add(fcBuilder.type(fcType).build());
             log.info("Created ForwardingConstruct comprising LogicalTerminationPoints {}",
-                    ltpSet.stream().map(ltp -> ltp.id()).collect(Collectors.toList()));
+                     ltpSet.stream()
+                             .map(CarrierEthernetLogicalTerminationPoint::id)
+                             .collect(Collectors.toList()));
         });
 
         return fcSet;
diff --git a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetVirtualConnection.java b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetVirtualConnection.java
index 0eed7f3..3b0de5f 100644
--- a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetVirtualConnection.java
+++ b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/app/CarrierEthernetVirtualConnection.java
@@ -22,6 +22,8 @@
 import java.time.Duration;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * Representation of a Carrier Ethernet EVC.
@@ -37,12 +39,16 @@
     // Maximum possible number of UNIs for non-Point-to-Point EVCs
     public static final Integer MAX_NUM_UNI = 1000;
 
-    // Note: evcId should be provided only when updating an existing service
-    public CarrierEthernetVirtualConnection(String id, String cfgId, Type type, Integer maxNumUni,
+    // TODO: Remove id from constructor - currently used only when updating EVC
+    // TODO: Make constructor private when SCA/NRP API apps are migrated
+    @Deprecated
+    public CarrierEthernetVirtualConnection(String id, String cfgId, Type type,
+                                            Integer maxNumUni,
                                             Set<CarrierEthernetUni> uniSet,
                                             Duration maxLatency) {
         super(id, cfgId, type, maxLatency);
-        this.maxNumUni = (maxNumUni != null ? maxNumUni : (type.equals(Type.POINT_TO_POINT) ? 2 : MAX_NUM_UNI));
+        this.maxNumUni = maxNumUni != null ? maxNumUni :
+                type.equals(Type.POINT_TO_POINT) ? 2 : MAX_NUM_UNI;
         this.uniSet = new HashSet<>(uniSet);
         this.fcSet = new HashSet<>();
         this.shortId = null;
@@ -139,4 +145,107 @@
                 .add("UNIs", uniSet)
                 .add("FCs", fcSet).toString();
     }
+
+    /**
+     * Returns a new builder.
+     *
+     * @return new builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Builder of CarrierEthernetVirtualConnection entities.
+     */
+    public static final class Builder {
+
+        private String id;
+        private String cfgId;
+        private Type type;
+        private Duration maxLatency;
+        private Set<CarrierEthernetUni> uniSet;
+        private Integer maxNumUni;
+
+        /**
+         * Sets the id of this builder.
+         *
+         * @param id the builder id to set
+         * @return this builder instance
+         */
+        public Builder id(String id) {
+            this.id = id;
+            return this;
+        }
+
+        /**
+         * Sets the cfgId of this builder.
+         *
+         * @param cfgId the builder cfgId to set
+         * @return this builder instance
+         */
+        public Builder cfgId(String cfgId) {
+            this.cfgId = cfgId;
+            return this;
+        }
+
+        /**
+         * Sets the type of this builder.
+         *
+         * @param type the builder type to set
+         * @return this builder instance
+         */
+        public Builder type(Type type) {
+            this.type = type;
+            return this;
+        }
+
+        /**
+         * Sets the maxLatency of this builder.
+         *
+         * @param maxLatency the builder maxLatency to set
+         * @return this builder instance
+         */
+        public Builder maxLatency(Duration maxLatency) {
+            this.maxLatency = maxLatency;
+            return this;
+        }
+
+        /**
+         * Sets the uniSet of this builder.
+         *
+         * @param uniSet the builder uniSet to set
+         * @return this builder instance
+         */
+        public Builder uniSet(Set<CarrierEthernetUni> uniSet) {
+            this.uniSet = uniSet;
+            return this;
+        }
+
+        /**
+         * Sets the maxNumUni of this builder.
+         *
+         * @param maxNumUni the builder maxNumUni to set
+         * @return this builder instance
+         */
+        public Builder maxNumUni(Integer maxNumUni) {
+            this.maxNumUni = maxNumUni;
+            return this;
+        }
+
+        /**
+         * Builds a new CarrierEthernetVirtualConnection instance.
+         * based on this builder's parameters
+         *
+         * @return a new CarrierEthernetVirtualConnection instance
+         */
+        public CarrierEthernetVirtualConnection build() {
+            checkNotNull(type, "EVC must have a type");
+            checkArgument(uniSet != null && uniSet.size() > 1,
+                          "EVC must include at least two UNIs");
+            return new CarrierEthernetVirtualConnection(id, cfgId, type,
+                                                        maxNumUni, uniSet,
+                                                        maxLatency);
+        }
+    }
 }
diff --git a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/cli/commands/CarrierEthernetCreateEvcCommand.java b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/cli/commands/CarrierEthernetCreateEvcCommand.java
index a92fae6..f0ffff4 100644
--- a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/cli/commands/CarrierEthernetCreateEvcCommand.java
+++ b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/cli/commands/CarrierEthernetCreateEvcCommand.java
@@ -75,13 +75,14 @@
 
     @Override
     protected void execute() {
-
         CarrierEthernetManager ceManager = get(CarrierEthernetManager.class);
-
-        CarrierEthernetVirtualConnection evc = new CarrierEthernetVirtualConnection(argEvcId, argEvcCfgId,
-                generateEvcType(), generateMaxNumUni(), generateUniSet(), null);
-
-        ceManager.installEvc(evc);
+        ceManager.installEvc(CarrierEthernetVirtualConnection.builder()
+                                     .id(argEvcId)
+                                     .cfgId(argEvcCfgId)
+                                     .type(generateEvcType())
+                                     .maxNumUni(generateMaxNumUni())
+                                     .uniSet(generateUniSet())
+                                     .build());
     }
 
     /**
diff --git a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/cli/commands/CarrierEthernetCreateFcCommand.java b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/cli/commands/CarrierEthernetCreateFcCommand.java
index 0a2e6f3..2b42195 100644
--- a/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/cli/commands/CarrierEthernetCreateFcCommand.java
+++ b/ecord/carrierethernet/src/main/java/org/onosproject/ecord/carrierethernet/cli/commands/CarrierEthernetCreateFcCommand.java
@@ -72,13 +72,13 @@
 
     @Override
     protected void execute() {
-
         CarrierEthernetManager ceManager = get(CarrierEthernetManager.class);
-
-        CarrierEthernetForwardingConstruct fc = new CarrierEthernetForwardingConstruct(argFcId, argFcCfgId,
-                generateServiceType(), generateLtpSet(), null);
-
-        ceManager.installFc(fc);
+        ceManager.installFc(CarrierEthernetForwardingConstruct.builder()
+                                    .id(argFcId)
+                                    .cfgId(argFcCfgId)
+                                    .type(generateServiceType())
+                                    .ltpSet(generateLtpSet())
+                                    .build());
     }
 
     /**