From 9f2d724c0b093009291d66a9c194c0966747dcab Mon Sep 17 00:00:00 2001 From: Yuta HIGUCHI Date: Thu, 18 May 2017 17:17:32 -0700 Subject: [PATCH] Added builder for PortDescription Change-Id: I37cf78ba7d2cebb021806a0441f2bf231a855c28 --- .../net/device/DefaultPortDescription.java | 116 +++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/core/api/src/main/java/org/onosproject/net/device/DefaultPortDescription.java b/core/api/src/main/java/org/onosproject/net/device/DefaultPortDescription.java index cc282270b4..dd767c3be6 100644 --- a/core/api/src/main/java/org/onosproject/net/device/DefaultPortDescription.java +++ b/core/api/src/main/java/org/onosproject/net/device/DefaultPortDescription.java @@ -17,9 +17,9 @@ package org.onosproject.net.device; import com.google.common.base.MoreObjects; import org.onosproject.net.AbstractDescription; +import org.onosproject.net.DefaultAnnotations; import org.onosproject.net.PortNumber; import org.onosproject.net.SparseAnnotations; - import static com.google.common.base.Preconditions.checkNotNull; import static org.onosproject.net.Port.Type; import com.google.common.base.Objects; @@ -177,4 +177,118 @@ public class DefaultPortDescription extends AbstractDescription return false; } + /** + * Creates port description builder with default parameters. + * + * @return builder + */ + public static Builder builder() { + return new Builder(); + } + + /** + * Creates port description builder inheriting with default parameters, + * from specified port description. + * + * @param desc to inherit default from + * @return builder + */ + public static Builder builder(PortDescription desc) { + return new Builder(desc); + } + + public static class Builder { + private PortNumber number; + private boolean isEnabled = true; + private boolean isRemoved = false; + private Type type = Type.COPPER; + private long portSpeed = DEFAULT_SPEED; + private SparseAnnotations annotations = DefaultAnnotations.EMPTY; + + Builder() {} + + Builder(PortDescription desc) { + this.number = desc.portNumber(); + this.isEnabled = desc.isEnabled(); + this.isRemoved = desc.isRemoved(); + this.type = desc.type(); + this.portSpeed = desc.portSpeed(); + this.annotations = desc.annotations(); + } + + /** + * Sets mandatory field PortNumber. + * + * @param number to set + * @return self + */ + public Builder withPortNumer(PortNumber number) { + this.number = checkNotNull(number); + return this; + } + + /** + * Sets enabled state. + * + * @param enabled state + * @return self + */ + public Builder isEnabled(boolean enabled) { + this.isEnabled = enabled; + return this; + } + + /** + * Sets removed state. + * + * @param removed state + * @return self + */ + public Builder isRemoved(boolean removed) { + this.isRemoved = removed; + return this; + } + + /** + * Sets port type. + * + * @param type of the port + * @return self + */ + public Builder type(Type type) { + this.type = type; + return this; + } + + /** + * Sets port speed. + * + * @param mbps port speed in Mbps + * @return self + */ + public Builder portSpeed(long mbps) { + this.portSpeed = mbps; + return this; + } + + /** + * Sets annotations. + * + * @param annotations of the port + * @return self + */ + public Builder annotations(SparseAnnotations annotations) { + this.annotations = checkNotNull(annotations); + return this; + } + + /** + * Builds the port description. + * + * @return port description + */ + public DefaultPortDescription build() { + return new DefaultPortDescription(number, isEnabled, isRemoved, type, portSpeed, annotations); + } + } }