Carmelo Cascone | 9db4d5c | 2019-04-16 17:36:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | |
| 20 | import com.google.common.annotations.Beta; |
| 21 | import com.google.common.base.MoreObjects; |
| 22 | import com.google.common.base.Objects; |
| 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkArgument; |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Representation of a clone session entry of a protocol-independent packet |
| 34 | * replication engine (PRE). |
| 35 | */ |
| 36 | @Beta |
| 37 | public final class PiCloneSessionEntry implements PiPreEntry { |
| 38 | |
| 39 | public static final int DEFAULT_CLASS_OF_SERVICE = 0; |
| 40 | public static final int DO_NOT_TRUNCATE = 0; |
| 41 | |
| 42 | private final int sessionId; |
| 43 | private final Set<PiPreReplica> replicas; |
| 44 | private final int classOfService; |
| 45 | private final int maxPacketLengthBytes; |
| 46 | |
| 47 | private PiCloneSessionEntry(int sessionId, Set<PiPreReplica> replicas, |
| 48 | int classOfService, int maxPacketBytes) { |
| 49 | this.sessionId = sessionId; |
| 50 | this.replicas = replicas; |
| 51 | this.classOfService = classOfService; |
| 52 | this.maxPacketLengthBytes = maxPacketBytes; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the identifier of this clone session, unique in the scope of a |
| 57 | * PRE instance. |
| 58 | * |
| 59 | * @return clone session ID |
| 60 | */ |
| 61 | public int sessionId() { |
| 62 | return sessionId; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the packet replicas provided by this clone session. |
| 67 | * |
| 68 | * @return packet replicas |
| 69 | */ |
| 70 | public Set<PiPreReplica> replicas() { |
| 71 | return replicas; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the class of service associated to the replicas produced by this |
| 76 | * clone session. |
| 77 | * |
| 78 | * @return class of service |
| 79 | */ |
| 80 | public int classOfService() { |
| 81 | return classOfService; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the maximum length in bytes of cloned packets. If a larger packet |
| 86 | * is cloned, then the PRE is expected to truncate clones to the given size. |
| 87 | * 0 means that no truncation on the clone(s) will be performed. |
| 88 | * |
| 89 | * @return maximum length in bytes of clones packets |
| 90 | */ |
| 91 | public int maxPacketLengthBytes() { |
| 92 | return maxPacketLengthBytes; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public PiEntityType piEntityType() { |
| 97 | return PiEntityType.PRE_ENTRY; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public PiPreEntryType preEntryType() { |
| 102 | return PiPreEntryType.CLONE_SESSION; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public PiCloneSessionEntryHandle handle(DeviceId deviceId) { |
| 107 | return PiCloneSessionEntryHandle.of(deviceId, this); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int hashCode() { |
| 112 | return Objects.hashCode(sessionId, replicas, classOfService, |
| 113 | maxPacketLengthBytes); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean equals(Object obj) { |
| 118 | if (this == obj) { |
| 119 | return true; |
| 120 | } |
| 121 | if (obj == null || getClass() != obj.getClass()) { |
| 122 | return false; |
| 123 | } |
| 124 | final PiCloneSessionEntry other = (PiCloneSessionEntry) obj; |
| 125 | return Objects.equal(this.sessionId, other.sessionId) |
| 126 | && Objects.equal(this.replicas, other.replicas) |
| 127 | && Objects.equal(this.classOfService, other.classOfService) |
| 128 | && Objects.equal(this.maxPacketLengthBytes, other.maxPacketLengthBytes); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public String toString() { |
| 133 | return MoreObjects.toStringHelper(this) |
| 134 | .add("sessionId", sessionId) |
| 135 | .add("replicas", replicas) |
| 136 | .add("classOfService", classOfService) |
| 137 | .add("maxPacketLengthBytes", maxPacketLengthBytes) |
| 138 | .toString(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns a new builder of clone session entries. |
| 143 | * |
| 144 | * @return builder |
| 145 | */ |
| 146 | public static PiCloneSessionEntry.Builder builder() { |
| 147 | return new PiCloneSessionEntry.Builder(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Builder of PI clone session entries. |
| 152 | */ |
| 153 | public static final class Builder { |
| 154 | |
| 155 | private Integer sessionId; |
| 156 | private ImmutableSet.Builder<PiPreReplica> replicaSetBuilder = ImmutableSet.builder(); |
| 157 | private int classOfService = DEFAULT_CLASS_OF_SERVICE; |
| 158 | private int maxPacketLengthBytes = DO_NOT_TRUNCATE; |
| 159 | |
| 160 | private Builder() { |
| 161 | // Hide constructor. |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Sets the identifier of this clone session. |
| 166 | * |
| 167 | * @param sessionId session ID |
| 168 | * @return this |
| 169 | */ |
| 170 | public PiCloneSessionEntry.Builder withSessionId(int sessionId) { |
| 171 | this.sessionId = sessionId; |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Adds the given packet replica to this clone session. |
| 177 | * |
| 178 | * @param replica packet replica |
| 179 | * @return this |
| 180 | */ |
| 181 | public PiCloneSessionEntry.Builder addReplica(PiPreReplica replica) { |
| 182 | checkNotNull(replica); |
| 183 | replicaSetBuilder.add(replica); |
| 184 | return this; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Adds the given packet replicas to this clone session. |
| 189 | * |
| 190 | * @param replicas packet replicas |
| 191 | * @return this |
| 192 | */ |
| 193 | public PiCloneSessionEntry.Builder addReplicas(Collection<PiPreReplica> replicas) { |
| 194 | checkNotNull(replicas); |
| 195 | replicaSetBuilder.addAll(replicas); |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Sets the class of service of this clone session. If not set, the |
| 201 | * default value {@link PiCloneSessionEntry#DEFAULT_CLASS_OF_SERVICE} |
| 202 | * will be used. |
| 203 | * |
| 204 | * @param classOfService class of service value |
| 205 | * @return this |
| 206 | */ |
| 207 | public PiCloneSessionEntry.Builder withClassOfService( |
| 208 | int classOfService) { |
| 209 | this.classOfService = classOfService; |
| 210 | return this; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Sets the maximum length in bytes of cloned packets. If not set, the |
| 215 | * default value {@link PiCloneSessionEntry#DO_NOT_TRUNCATE} will be |
| 216 | * used. |
| 217 | * |
| 218 | * @param maxPacketLengthBytes max length in bytes of cloned packets |
| 219 | * @return this |
| 220 | */ |
| 221 | public PiCloneSessionEntry.Builder withMaxPacketLengthBytes( |
| 222 | int maxPacketLengthBytes) { |
| 223 | checkArgument(maxPacketLengthBytes >= 0, |
| 224 | "maxPacketLengthBytes must be a positive integer"); |
| 225 | this.maxPacketLengthBytes = maxPacketLengthBytes; |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Returns a new clone session entry. |
| 231 | * |
| 232 | * @return clone session entry |
| 233 | */ |
| 234 | public PiCloneSessionEntry build() { |
| 235 | checkNotNull(sessionId, "Clone session ID must be set"); |
| 236 | final ImmutableSet<PiPreReplica> replicas = replicaSetBuilder.build(); |
| 237 | return new PiCloneSessionEntry( |
| 238 | sessionId, replicas, classOfService, maxPacketLengthBytes); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |