Added builder and more logs for VtnService and VtnPort

Change-Id: I52f672caebf1946b9bb01c9de0ca2ad2cee23f69
diff --git a/apps/xosclient/src/main/java/org/onosproject/xosclient/api/VtnService.java b/apps/xosclient/src/main/java/org/onosproject/xosclient/api/VtnService.java
index ec33b9f..3e7b100 100644
--- a/apps/xosclient/src/main/java/org/onosproject/xosclient/api/VtnService.java
+++ b/apps/xosclient/src/main/java/org/onosproject/xosclient/api/VtnService.java
@@ -16,7 +16,8 @@
 package org.onosproject.xosclient.api;
 
 import com.google.common.base.MoreObjects;
-import com.google.common.collect.Sets;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
 import org.onosproject.xosclient.api.VtnServiceApi.NetworkType;
@@ -25,6 +26,7 @@
 import java.util.Objects;
 import java.util.Set;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
@@ -42,37 +44,24 @@
     private final Set<VtnServiceId> providerServices;
     private final Set<VtnServiceId> tenantServices;
 
-    /**
-     * Creates a new VTN service with the specified entities.
-     *
-     * @param id service id
-     * @param name user friendly name
-     * @param serviceType service type
-     * @param networkType network type
-     * @param vni vni of this service network
-     * @param subnet service network subnet range
-     * @param serviceIp service ip for indirect service access
-     * @param providerServices provider services
-     * @param tenantServices tenant services
-     */
-    public VtnService(VtnServiceId id,
-                      String name,
-                      ServiceType serviceType,
-                      NetworkType networkType,
-                      long vni,
-                      IpPrefix subnet,
-                      IpAddress serviceIp,
-                      Set<VtnServiceId> providerServices,
-                      Set<VtnServiceId> tenantServices) {
-        this.id = checkNotNull(id);
+    private VtnService(VtnServiceId id,
+                       String name,
+                       ServiceType serviceType,
+                       NetworkType networkType,
+                       long vni,
+                       IpPrefix subnet,
+                       IpAddress serviceIp,
+                       Set<VtnServiceId> providerServices,
+                       Set<VtnServiceId> tenantServices) {
+        this.id = id;
         this.name = name;
         this.serviceType = serviceType;
         this.networkType = networkType;
         this.vni = vni;
-        this.subnet = checkNotNull(subnet);
-        this.serviceIp = checkNotNull(serviceIp);
-        this.providerServices = providerServices == null ? Sets.newHashSet() : providerServices;
-        this.tenantServices = tenantServices == null ? Sets.newHashSet() : tenantServices;
+        this.subnet = subnet;
+        this.serviceIp = serviceIp;
+        this.providerServices = providerServices;
+        this.tenantServices = tenantServices;
     }
 
     /**
@@ -187,4 +176,231 @@
                 .add("tenantServices", tenantServices)
                 .toString();
     }
+
+    /**
+     * Returns a new builder instance.
+     *
+     * @return new builder
+     */
+    public static final Builder build() {
+        return new Builder();
+    }
+
+    /**
+     * Builder of VTN service entities.
+     */
+    public static final class Builder {
+
+        private VtnServiceId id;
+        private String name;
+        private ServiceType serviceType;
+        private NetworkType networkType;
+        private long vni = -1;
+        private IpPrefix subnet;
+        private IpAddress serviceIp;
+        private Set<VtnServiceId> providerServices;
+        private Set<VtnServiceId> tenantServices;
+
+        private Builder() {
+        }
+
+        /**
+         * Builds an immutable VTN service.
+         *
+         * @return vtn service instance
+         */
+        public VtnService build() {
+            checkNotNull(id, "VTN service ID cannot be null");
+            checkArgument(!Strings.isNullOrEmpty(name), "VTN service name cannot be null");
+            checkNotNull(serviceType, "VTN service type cannot be null");
+            checkNotNull(networkType, "VTN network type cannot be null");
+            checkArgument(vni > 0, "VTN network VNI is not set");
+            checkNotNull(subnet, "VTN subnet cannot be null");
+            checkNotNull(serviceIp, "VTN service IP cannot be null");
+
+            providerServices = providerServices == null ? ImmutableSet.of() : providerServices;
+            tenantServices = tenantServices == null ? ImmutableSet.of() : tenantServices;
+
+            return new VtnService(id,
+                                  name,
+                                  serviceType,
+                                  networkType,
+                                  vni,
+                                  subnet,
+                                  serviceIp,
+                                  providerServices,
+                                  tenantServices);
+        }
+
+        /**
+         * Returns VTN service builder with the supplied service ID.
+         *
+         * @param id service identifier
+         * @return vtn service builder
+         */
+        public Builder id(VtnServiceId id) {
+            this.id = id;
+            return this;
+        }
+
+        /**
+         * Returns VTN service builder with the supplied service name.
+         *
+         * @param name service name
+         * @return vtn service builder
+         */
+        public Builder name(String name) {
+            if (Strings.isNullOrEmpty(name)) {
+                final String msg = "VTN service name cannot be null";
+                throw new IllegalArgumentException(msg);
+            }
+            this.name = name;
+            return this;
+        }
+
+        /**
+         * Returns VTN service builder with the supplied service type.
+         *
+         * @param serviceType service type
+         * @return vtn service builder
+         */
+        public Builder serviceType(ServiceType serviceType) {
+            this.serviceType = serviceType;
+            return this;
+        }
+
+        /**
+         * Returns VTN service builder with the supplied network type.
+         *
+         * @param networkType network type
+         * @return vtn service builder
+         */
+        public Builder networkType(NetworkType networkType) {
+            this.networkType = networkType;
+            return this;
+        }
+
+        /**
+         * Returns VTN service builder with the supplied VNI.
+         *
+         * @param vni vni of the service network
+         * @return vtn service builder
+         */
+        public Builder vni(long vni) {
+            if (vni < 0 || vni > 16777215) {
+                final String msg = "VNI " + vni + " is out of range";
+                throw new IllegalArgumentException(msg);
+            }
+            this.vni = vni;
+            return this;
+        }
+
+        /**
+         * Returns VTN service builder with the supplied VNI.
+         *
+         * @param vni vni of the service network as a string
+         * @return vtn service builder
+         */
+        public Builder vni(String vni) {
+            try {
+                return vni(Long.parseLong(vni));
+            } catch (NumberFormatException | NullPointerException e) {
+                final String msg = "Malformed number string " + vni +
+                        " for VTN network VNI";
+                throw new IllegalArgumentException(msg);
+            }
+        }
+
+        /**
+         * Returns VTN service builder with the supplied subnet.
+         *
+         * @param subnet subnet of the service network
+         * @return vtn service builder
+         */
+        public Builder subnet(IpPrefix subnet) {
+            if (subnet == null) {
+                final String msg = "VTN service subnet is null";
+                throw new IllegalArgumentException(msg);
+            }
+            this.subnet = subnet;
+            return this;
+        }
+
+        /**
+         * Returns VTN service builder with the supplied subnet.
+         *
+         * @param subnet subnet of the service network as a string
+         * @return vtn service builder
+         */
+        public Builder subnet(String subnet) {
+            try {
+                return subnet(IpPrefix.valueOf(subnet));
+            } catch (IllegalArgumentException | NullPointerException e) {
+                final String msg = "Malformed IP prefix string " + subnet +
+                        " for VTN service subnet";
+                throw new IllegalArgumentException(msg);
+            }
+        }
+
+        /**
+         * Returns VTN service builder with the supplied service IP address.
+         *
+         * @param serviceIp service ip address
+         * @return vtn service builder
+         */
+        public Builder serviceIp(IpAddress serviceIp) {
+            if (serviceIp == null) {
+                final String msg = "VTN service IP cannot be null";
+                throw new IllegalArgumentException(msg);
+            }
+            this.serviceIp = serviceIp;
+            return this;
+        }
+
+        /**
+         * Returns VTN service builder with the supplied service IP address.
+         *
+         * @param serviceIp service ip address as a string
+         * @return vtn service builder
+         */
+        public Builder serviceIp(String serviceIp) {
+            try {
+                return serviceIp(IpAddress.valueOf(serviceIp));
+            } catch (IllegalArgumentException | NullPointerException e) {
+                final String msg = "Malformed IP address string " + serviceIp +
+                        " for VTN service IP address";
+                throw new IllegalArgumentException(msg);
+            }
+        }
+
+        /**
+         * Returns VTN service builder with the supplied provider services.
+         *
+         * @param pServices provider services
+         * @return vtn service builder
+         */
+        public Builder providerServices(Set<VtnServiceId> pServices) {
+            if (pServices == null) {
+                final String msg = "Provider services cannot be null";
+                throw new IllegalArgumentException(msg);
+            }
+            this.providerServices = pServices;
+            return this;
+        }
+
+        /**
+         * Returns VTN service builder with the supplied tenant services.
+         *
+         * @param tServices tenant services
+         * @return vtn service builder
+         */
+        public Builder tenantServices(Set<VtnServiceId> tServices) {
+            if (tServices == null) {
+                final String msg = "Tenant services cannot be null";
+                throw new IllegalArgumentException(msg);
+            }
+            this.tenantServices = tServices;
+            return this;
+        }
+    }
 }