Add a builder for DefaultLink to hide multiplying constructors

Change-Id: Iaf073d07989c398a8f44541ffdb8dd93e5715162
diff --git a/core/api/src/main/java/org/onosproject/net/DefaultLink.java b/core/api/src/main/java/org/onosproject/net/DefaultLink.java
index e1e48b9..1876db6 100644
--- a/core/api/src/main/java/org/onosproject/net/DefaultLink.java
+++ b/core/api/src/main/java/org/onosproject/net/DefaultLink.java
@@ -21,6 +21,9 @@
 
 import static com.google.common.base.MoreObjects.toStringHelper;
 import static org.onosproject.net.Link.State.ACTIVE;
+import static org.onosproject.net.DefaultAnnotations.EMPTY;
+import static com.google.common.base.Preconditions.checkNotNull;
+
 
 /**
  * Default infrastructure link model implementation.
@@ -42,7 +45,7 @@
      * @param type        link type
      * @param annotations optional key/value annotations
      */
-    public DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
+    protected DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
                        Type type, Annotations... annotations) {
         this(providerId, src, dst, type, ACTIVE, false, annotations);
     }
@@ -60,7 +63,7 @@
      * @param isExpected   indicates if the link is preconfigured
      * @param annotations optional key/value annotations
      */
-    public DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
+    private DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
                            Type type, State state,
                            boolean isExpected, Annotations... annotations) {
         super(providerId, annotations);
@@ -129,8 +132,130 @@
                 .add("dst", dst)
                 .add("type", type)
                 .add("state", state)
-                .add("configured", isExpected)
+                .add("expected", isExpected)
                 .toString();
     }
 
+    /**
+     * Creates a new default link builder.
+     *
+     * @return default link builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Builder for DefaultLink objects.
+     */
+    public static final class Builder {
+        private ProviderId providerId;
+        private Annotations annotations = EMPTY;
+        private ConnectPoint src;
+        private ConnectPoint dst;
+        private Type type;
+        private State state = ACTIVE;
+        private boolean isExpected = false;
+
+        private Builder() {
+            // Hide constructor
+        }
+
+        /**
+         * Sets the providerId to be used by the builder.
+         *
+         * @param providerId new provider id
+         * @return self
+         */
+        public Builder providerId(ProviderId providerId) {
+            this.providerId = providerId;
+            return this;
+        }
+
+        /**
+         * Sets the annotations to be used by the builder.
+         *
+         * @param annotations new annotations
+         * @return self
+         */
+        public Builder annotations(Annotations annotations) {
+            this.annotations = annotations;
+            return this;
+        }
+
+        /**
+         * Sets the source connect point to be used by the builder.
+         *
+         * @param src source connect point
+         * @return self
+         */
+        public Builder src(ConnectPoint src) {
+            this.src = src;
+            return this;
+        }
+
+        /**
+         * Sets the destination connect point to be used by the builder.
+         *
+         * @param dst new destination connect point
+         * @return self
+         */
+        public Builder dst(ConnectPoint dst) {
+            this.dst = dst;
+            return this;
+        }
+
+        /**
+         * Sets the link type to be used by the builder.
+         *
+         * @param type new link type
+         * @return self
+         */
+        public Builder type(Type type) {
+            this.type = type;
+            return this;
+        }
+
+        /**
+         * Sets the link state to be used by the builder.
+         *
+         * @param state new link state
+         * @return self
+         */
+        public Builder state(State state) {
+            this.state = state;
+            return this;
+        }
+
+        /**
+         * Sets the expected flag to be used by the builder.
+         *
+         * @param isExpected new expected flag
+         * @return self
+         */
+        public Builder isExpected(boolean isExpected) {
+            this.isExpected = isExpected;
+            return this;
+        }
+
+        /**
+         * Builds a default link object from the accumulated parameters.
+         *
+         * @return default link object
+         */
+        public DefaultLink build() {
+            checkNotNull(src, "Source connect point cannot be null");
+            checkNotNull(dst, "Destination connect point cannot be null");
+            checkNotNull(type, "Type cannot be null");
+            checkNotNull(providerId, "Provider Id cannot be null");
+
+            return new DefaultLink(providerId, src, dst,
+                                   type, state,
+                                   isExpected, annotations);
+        }
+
+    }
+
+
+
 }
diff --git a/core/api/src/test/java/org/onosproject/net/intent/PathIntentTest.java b/core/api/src/test/java/org/onosproject/net/intent/PathIntentTest.java
index 9e67534..918e3b3 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/PathIntentTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/PathIntentTest.java
@@ -51,8 +51,18 @@
     private final ConnectPoint cp2 = new ConnectPoint(device1, port2);
     private final ConnectPoint cp3 = new ConnectPoint(device2, port3);
     private final ConnectPoint cp4 = new ConnectPoint(device2, port4);
-    private final DefaultLink link1 = new DefaultLink(provider1, cp1, cp2, DIRECT);
-    private final DefaultLink link2 = new DefaultLink(provider1, cp1, cp2, DIRECT);
+    private final DefaultLink link1 = DefaultLink.builder()
+            .providerId(provider1)
+            .src(cp1)
+            .dst(cp2)
+            .type(DIRECT)
+            .build();
+    private final DefaultLink link2 = DefaultLink.builder()
+            .providerId(provider1)
+            .src(cp1)
+            .dst(cp2)
+            .type(DIRECT)
+            .build();
     private final double cost = 1;
 
     @Test
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
index b87dc12..299eaef 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/AnnotationConstraintTest.java
@@ -59,7 +59,12 @@
 
         DefaultAnnotations annotations = DefaultAnnotations.builder().set(KEY, String.valueOf(VALUE)).build();
 
-        link = new DefaultLink(PID, cp(DID1, PID1), cp(DID2, PID2), DIRECT, annotations);
+        link = DefaultLink.builder()
+                .providerId(PID)
+                .src(cp(DID1, PID1))
+                .dst(cp(DID2, PID2))
+                .type(DIRECT)
+                .annotations(annotations).build();
     }
 
     /**
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
index bab1749..a08caab 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/LatencyConstraintTest.java
@@ -69,8 +69,20 @@
         Annotations annotations1 = DefaultAnnotations.builder().set(LATENCY, LATENCY1).build();
         Annotations annotations2 = DefaultAnnotations.builder().set(LATENCY, LATENCY2).build();
 
-        link1 = new DefaultLink(PROVIDER_ID, cp(DID1, PN1), cp(DID2, PN2), DIRECT, annotations1);
-        link2 = new DefaultLink(PROVIDER_ID, cp(DID2, PN3), cp(DID3, PN4), DIRECT, annotations2);
+        link1 = DefaultLink.builder()
+                .providerId(PROVIDER_ID)
+                .src(cp(DID1, PN1))
+                .dst(cp(DID2, PN2))
+                .type(DIRECT)
+                .annotations(annotations1)
+                .build();
+        link2 = DefaultLink.builder()
+                .providerId(PROVIDER_ID)
+                .src(cp(DID2, PN3))
+                .dst(cp(DID3, PN4))
+                .type(DIRECT)
+                .annotations(annotations2)
+                .build();
         path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), 10);
     }
 
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
index f02787f..99cd338 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
@@ -62,8 +62,18 @@
     public void setUp() {
         linkResourceService = createMock(LinkResourceService.class);
 
-        link1 = new DefaultLink(PROVIDER_ID, cp(DID1, PN1), cp(DID2, PN2), DIRECT);
-        link2 = new DefaultLink(PROVIDER_ID, cp(DID2, PN3), cp(DID3, PN4), DIRECT);
+        link1 = DefaultLink.builder()
+                .providerId(PROVIDER_ID)
+                .src(cp(DID1, PN1))
+                .dst(cp(DID2, PN2))
+                .type(DIRECT)
+                .build();
+        link2 = DefaultLink.builder()
+                .providerId(PROVIDER_ID)
+                .src(cp(DID2, PN3))
+                .dst(cp(DID3, PN4))
+                .type(DIRECT)
+                .build();
         path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), 10);
     }
 
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/WaypointConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/WaypointConstraintTest.java
index f7e212a..bbd10a6 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/WaypointConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/WaypointConstraintTest.java
@@ -62,8 +62,19 @@
     public void setUp() {
         linkResourceService = createMock(LinkResourceService.class);
 
-        link1 = new DefaultLink(PROVIDER_ID, cp(DID1, PN1), cp(DID2, PN2), DIRECT);
-        link2 = new DefaultLink(PROVIDER_ID, cp(DID2, PN3), cp(DID3, PN4), DIRECT);
+        link1 = DefaultLink.builder()
+                .providerId(PROVIDER_ID)
+                .src(cp(DID1, PN1))
+                .dst(cp(DID2, PN2))
+                .type(DIRECT)
+                .build();
+        link2 = DefaultLink.builder()
+                .providerId(PROVIDER_ID)
+                .src(cp(DID2, PN3))
+                .dst(cp(DID3, PN4))
+                .type(DIRECT)
+                .build();
+
         path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), 10);
     }
 
diff --git a/core/api/src/test/java/org/onosproject/net/link/LinkEventTest.java b/core/api/src/test/java/org/onosproject/net/link/LinkEventTest.java
index 307fa69..9e30ee2 100644
--- a/core/api/src/test/java/org/onosproject/net/link/LinkEventTest.java
+++ b/core/api/src/test/java/org/onosproject/net/link/LinkEventTest.java
@@ -31,10 +31,12 @@
 public class LinkEventTest extends AbstractEventTest {
 
     private Link createLink() {
-        return new DefaultLink(new ProviderId("of", "foo"),
-                               new ConnectPoint(deviceId("of:foo"), portNumber(1)),
-                               new ConnectPoint(deviceId("of:bar"), portNumber(2)),
-                               Link.Type.INDIRECT);
+        return DefaultLink.builder()
+                .providerId(new ProviderId("of", "foo"))
+                .src(new ConnectPoint(deviceId("of:foo"), portNumber(1)))
+                .dst(new ConnectPoint(deviceId("of:bar"), portNumber(2)))
+                .type(Link.Type.INDIRECT)
+                .build();
     }
 
     @Test
diff --git a/core/api/src/test/java/org/onosproject/net/topology/DefaultTopologyEdgeTest.java b/core/api/src/test/java/org/onosproject/net/topology/DefaultTopologyEdgeTest.java
index 830e9b9f..a74882f 100644
--- a/core/api/src/test/java/org/onosproject/net/topology/DefaultTopologyEdgeTest.java
+++ b/core/api/src/test/java/org/onosproject/net/topology/DefaultTopologyEdgeTest.java
@@ -49,9 +49,19 @@
     static final ProviderId PID = new ProviderId("foo", "bar");
 
     /** D1:P1 -> D2:P1. */
-    static final Link L1 = new DefaultLink(PID, CP1, CP2, Link.Type.INDIRECT);
+    static final Link L1 = DefaultLink.builder()
+            .providerId(PID)
+            .src(CP1)
+            .dst(CP2)
+            .type(Link.Type.INDIRECT)
+            .build();
     /** D2:P1 -> D1:P2. */
-    static final Link L2 = new DefaultLink(PID, CP3, CP4, Link.Type.INDIRECT);
+    static final Link L2 = DefaultLink.builder()
+            .providerId(PID)
+            .src(CP3)
+            .dst(CP4)
+            .type(Link.Type.INDIRECT)
+            .build();
 
     @Test
     public void basics() {