Sho SHIMIZU | cdc5013 | 2014-07-22 09:21:01 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.newintent; |
| 2 | |
| 3 | import com.google.common.base.Objects; |
| 4 | import net.onrc.onos.api.intent.Intent; |
| 5 | import net.onrc.onos.api.intent.IntentId; |
| 6 | import net.onrc.onos.core.matchaction.match.IMatch; |
| 7 | import net.onrc.onos.core.util.Pair; |
| 8 | import net.onrc.onos.core.util.SwitchPort; |
| 9 | |
| 10 | import java.util.concurrent.TimeUnit; |
| 11 | |
| 12 | import static com.google.common.base.Preconditions.checkArgument; |
| 13 | import static com.google.common.base.Preconditions.checkNotNull; |
| 14 | |
| 15 | /** |
| 16 | * This class represents point-to-point connectivity. |
| 17 | */ |
| 18 | public class PointToPointIntent extends Intent { |
| 19 | private final SwitchPort ingressPort; |
| 20 | private final SwitchPort egressPort; |
| 21 | private final IMatch match; |
| 22 | private final long idleTimeout; |
| 23 | private final TimeUnit unit; |
| 24 | |
| 25 | /** |
| 26 | * Constructs an intent representing ingress port to egress port |
| 27 | * connectivity with a given match condition. |
| 28 | * |
| 29 | * @param id ID of this Intent object. |
| 30 | * @param ingressPort the ingress port where a path is originated. |
| 31 | * @param egressPort the egress port where a path is terminated. |
| 32 | * @param match the match condition of a path established by this intent. |
| 33 | */ |
| 34 | public PointToPointIntent(IntentId id, |
| 35 | SwitchPort ingressPort, SwitchPort egressPort, |
| 36 | IMatch match) { |
| 37 | this(id, ingressPort, egressPort, match, 0, TimeUnit.SECONDS); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Constructs an intent representing ingress port to egress port |
| 42 | * connectivity with a given match condition |
| 43 | * and idle timeout. |
| 44 | * |
| 45 | * @param id ID of this Intent object. |
| 46 | * @param ingressPort the ingress port where a path is originated. |
| 47 | * @param egressPort the egress port where a path is terminated. |
| 48 | * @param match the match condition of a path established by this intent. |
| 49 | * @param idleTimeout the maximum time to be idle. |
| 50 | * @param unit the unit of the idleTimeout argument. |
| 51 | */ |
| 52 | public PointToPointIntent(IntentId id, |
| 53 | SwitchPort ingressPort, SwitchPort egressPort, |
| 54 | IMatch match, long idleTimeout, TimeUnit unit) { |
| 55 | super(id); |
| 56 | |
| 57 | checkArgument(idleTimeout >= 0, "idleTimeout should not be negative"); |
| 58 | |
| 59 | this.ingressPort = checkNotNull(ingressPort); |
| 60 | this.egressPort = checkNotNull(egressPort); |
| 61 | this.match = checkNotNull(match); |
| 62 | this.idleTimeout = idleTimeout; |
| 63 | this.unit = checkNotNull(unit); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the ingress port. |
| 68 | * |
| 69 | * @return the ingress port. |
| 70 | */ |
| 71 | public SwitchPort getIngressPort() { |
| 72 | return ingressPort; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the egress port. |
| 77 | * |
| 78 | * @return the egress port. |
| 79 | */ |
| 80 | public SwitchPort getEgressPort() { |
| 81 | return egressPort; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the match condition. |
| 86 | * |
| 87 | * @return the match condition. |
| 88 | */ |
| 89 | public IMatch getMatch() { |
| 90 | return match; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the idle timeout. |
| 95 | * |
| 96 | * @return the idle timeout. |
| 97 | */ |
| 98 | public Pair<Long, TimeUnit> getIdleTimeout() { |
| 99 | return new Pair<>(idleTimeout, unit); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int hashCode() { |
| 104 | return Objects.hashCode(getId(), ingressPort, egressPort, match, idleTimeout, unit); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Compares the specified object with this intent for equality. |
| 109 | * |
| 110 | * Note: Comparison of idleTimeout value is done in micro-second precision. |
| 111 | * Then the value less than a micro second is truncated. In addition, the comparison |
| 112 | * is done between long values. It causes overflow if the idleTimeout is large. |
| 113 | * |
| 114 | * @param obj the object to be compared with this intent for equality. |
| 115 | * @return true if the specified object is equal to this intent. |
| 116 | */ |
| 117 | @Override |
| 118 | public boolean equals(Object obj) { |
| 119 | if (obj == this) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | if (!(obj instanceof PointToPointIntent)) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | PointToPointIntent that = (PointToPointIntent) obj; |
| 128 | long thisIdleTimeoutInMicro = this.unit.toMicros(this.idleTimeout); |
| 129 | long thatIdleTimeoutInMicro = that.unit.toMicros(that.idleTimeout); |
| 130 | |
| 131 | return Objects.equal(this.getId(), that.getId()) |
| 132 | && Objects.equal(this.ingressPort, that.ingressPort) |
| 133 | && Objects.equal(this.egressPort, that.egressPort) |
| 134 | && Objects.equal(this.match, that.match) |
| 135 | && Objects.equal(thisIdleTimeoutInMicro, thatIdleTimeoutInMicro); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String toString() { |
| 140 | return Objects.toStringHelper(this) |
| 141 | .add("id", getId()) |
| 142 | .add("ingressPort", ingressPort) |
| 143 | .add("egressPort", egressPort) |
| 144 | .add("match", match) |
| 145 | .add("idleTimeout", idleTimeout) |
| 146 | .add("unit", unit) |
| 147 | .toString(); |
| 148 | } |
| 149 | } |