Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 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 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import org.onlab.util.ImmutableByteSequence; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.pi.model.PiControlMetadataId; |
| 26 | import org.onosproject.net.pi.model.PiPacketOperationType; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 27 | |
| 28 | import java.util.Collection; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.Map; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 36 | * Instance of a packet I/O operation, and its control metadatas, for a protocol-independent pipeline. |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 37 | */ |
| 38 | @Beta |
| 39 | public final class PiPacketOperation { |
| 40 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 41 | private final DeviceId deviceId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 42 | private final ImmutableByteSequence data; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 43 | private final Set<PiControlMetadata> packetMetadatas; |
| 44 | private final PiPacketOperationType type; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 45 | |
| 46 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 47 | * Creates a new packet I/O operation for the given device ID, data, control metadatas and operation type. |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 48 | * |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 49 | * @param deviceId device ID |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 50 | * @param data the packet raw data |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 51 | * @param packetMetadatas collection of control metadata |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 52 | * @param type type of this packet operation |
| 53 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 54 | private PiPacketOperation(DeviceId deviceId, ImmutableByteSequence data, |
| 55 | Collection<PiControlMetadata> packetMetadatas, |
| 56 | PiPacketOperationType type) { |
| 57 | this.deviceId = deviceId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 58 | this.data = data; |
| 59 | this.packetMetadatas = ImmutableSet.copyOf(packetMetadatas); |
| 60 | this.type = type; |
| 61 | } |
| 62 | |
| 63 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 64 | * Returns the device ID of this packet operation. |
| 65 | * |
| 66 | * @return device ID |
| 67 | */ |
| 68 | public DeviceId deviceId() { |
| 69 | return deviceId; |
| 70 | } |
| 71 | |
| 72 | /** |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 73 | * Return the type of this packet. |
| 74 | * |
| 75 | * @return packet type |
| 76 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 77 | public PiPacketOperationType type() { |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 78 | return type; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the data of this packet. |
| 83 | * |
| 84 | * @return packet data |
| 85 | */ |
| 86 | public ImmutableByteSequence data() { |
| 87 | return data; |
| 88 | } |
| 89 | |
| 90 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 91 | * Returns all metadatas of this packet. Returns an empty collection if the packet doesn't have any metadata. |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 92 | * |
| 93 | * @return collection of metadatas |
| 94 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 95 | public Collection<PiControlMetadata> metadatas() { |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 96 | return packetMetadatas; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object o) { |
| 101 | if (this == o) { |
| 102 | return true; |
| 103 | } |
| 104 | if (o == null || getClass() != o.getClass()) { |
| 105 | return false; |
| 106 | } |
| 107 | PiPacketOperation that = (PiPacketOperation) o; |
| 108 | return Objects.equal(packetMetadatas, that.packetMetadatas) && |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 109 | Objects.equal(deviceId, that.deviceId) && |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 110 | Objects.equal(data, that.data()) && |
| 111 | Objects.equal(type, that.type()); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 116 | return Objects.hashCode(deviceId, data, packetMetadatas, type); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public String toString() { |
| 121 | return MoreObjects.toStringHelper(this) |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 122 | .add("deviceId", deviceId) |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 123 | .addValue(type.toString()) |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 124 | .addValue(packetMetadatas) |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 125 | .toString(); |
| 126 | } |
| 127 | |
| 128 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 129 | * Returns a new builder of packet operations. |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 130 | * |
| 131 | * @return a new builder |
| 132 | */ |
| 133 | public static Builder builder() { |
| 134 | return new Builder(); |
| 135 | } |
| 136 | |
| 137 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 138 | * Builder of packet operations. |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 139 | */ |
| 140 | public static final class Builder { |
| 141 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 142 | private DeviceId deviceId; |
| 143 | private Map<PiControlMetadataId, PiControlMetadata> packetMetadatas = new HashMap<>(); |
| 144 | private PiPacketOperationType type; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 145 | private ImmutableByteSequence data; |
| 146 | |
| 147 | private Builder() { |
| 148 | // hides constructor. |
| 149 | } |
| 150 | |
| 151 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 152 | * Sets the device ID. |
| 153 | * |
| 154 | * @param deviceId device ID |
| 155 | * @return this |
| 156 | */ |
| 157 | public Builder forDevice(DeviceId deviceId) { |
| 158 | checkNotNull(deviceId); |
| 159 | this.deviceId = deviceId; |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Sets the raw packet data. |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 165 | * |
| 166 | * @param data the packet raw data |
| 167 | * @return this |
| 168 | */ |
| 169 | public Builder withData(ImmutableByteSequence data) { |
| 170 | checkNotNull(data); |
| 171 | this.data = data; |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 176 | * Adds a control metadata. Only one metadata is allowed for a given metadata id. If a metadata with same id |
| 177 | * already exists it will be replaced by the given one. |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 178 | * |
| 179 | * @param metadata packet metadata |
| 180 | * @return this |
| 181 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 182 | public Builder withMetadata(PiControlMetadata metadata) { |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 183 | checkNotNull(metadata); |
| 184 | packetMetadatas.put(metadata.id(), metadata); |
| 185 | |
| 186 | return this; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Adds many packet metadatas. |
| 191 | * |
| 192 | * @param metadatas collection of metadata |
| 193 | * @return this |
| 194 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 195 | public Builder withMetadatas(Collection<PiControlMetadata> metadatas) { |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 196 | checkNotNull(metadatas); |
| 197 | metadatas.forEach(this::withMetadata); |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Sets the type of this packet. |
| 203 | * |
| 204 | * @param type type of the packet |
| 205 | * @return this |
| 206 | */ |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 207 | public Builder withType(PiPacketOperationType type) { |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 208 | this.type = type; |
| 209 | return this; |
| 210 | } |
| 211 | |
| 212 | /** |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 213 | * Builds a new instance of a packet operation. |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 214 | * |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 215 | * @return packet operation |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 216 | */ |
| 217 | public PiPacketOperation build() { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 218 | checkNotNull(deviceId); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 219 | checkNotNull(data); |
| 220 | checkNotNull(packetMetadatas); |
| 221 | checkNotNull(type); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 222 | return new PiPacketOperation(deviceId, data, packetMetadatas.values(), type); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | } |