Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.net.group; |
| 17 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 18 | import org.onosproject.core.GroupId; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 19 | import org.onosproject.net.PortNumber; |
| 20 | import org.onosproject.net.flow.TrafficTreatment; |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 21 | import org.onosproject.net.flow.instructions.Instruction; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 29 | |
Thomas Vachuska | 0cd4de8 | 2015-02-03 09:17:08 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Group bucket implementation. A group bucket is collection of |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 32 | * instructions that can be performed on a traffic flow. A select |
| 33 | * Group can have one or more Buckets where traffic will be |
| 34 | * processed by a single bucket in the group, based on device |
| 35 | * specific selection algorithm (e.g. hash on some fields of the |
| 36 | * incoming traffic flows or round robin) and hence can contains |
| 37 | * optional weight field to define the weights among the buckets |
| 38 | * in the group. A failover group bucket is associated with a |
| 39 | * specific port or group that controls its liveness. |
| 40 | */ |
| 41 | public final class DefaultGroupBucket implements GroupBucket { |
| 42 | private final GroupDescription.Type type; |
| 43 | private final TrafficTreatment treatment; |
| 44 | private final short weight; |
| 45 | private final PortNumber watchPort; |
| 46 | private final GroupId watchGroup; |
| 47 | |
| 48 | /** |
| 49 | * Group bucket constructor with the parameters. |
| 50 | * |
| 51 | * @param type group bucket type |
| 52 | * @param treatment traffic treatment associated with group bucket |
| 53 | * @param weight optional weight associated with group bucket |
| 54 | * @param watchPort port that determines the liveness of group bucket |
Thomas Vachuska | 0cd4de8 | 2015-02-03 09:17:08 -0800 | [diff] [blame] | 55 | * @param watchGroup group that determines the liveness of group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 56 | */ |
| 57 | private DefaultGroupBucket(GroupDescription.Type type, |
| 58 | TrafficTreatment treatment, |
| 59 | short weight, |
| 60 | PortNumber watchPort, |
| 61 | GroupId watchGroup) { |
| 62 | this.type = type; |
| 63 | this.treatment = checkNotNull(treatment); |
| 64 | this.weight = weight; |
| 65 | this.watchPort = watchPort; |
| 66 | this.watchGroup = watchGroup; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates indirect group bucket. |
| 71 | * |
| 72 | * @param treatment traffic treatment associated with group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 73 | * @return indirect group bucket object |
| 74 | */ |
| 75 | public static GroupBucket createIndirectGroupBucket( |
| 76 | TrafficTreatment treatment) { |
| 77 | return new DefaultGroupBucket(GroupDescription.Type.INDIRECT, |
| 78 | treatment, |
| 79 | (short) -1, |
| 80 | null, |
| 81 | null); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Creates select group bucket with weight as 1. |
| 86 | * |
| 87 | * @param treatment traffic treatment associated with group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 88 | * @return select group bucket object |
| 89 | */ |
| 90 | public static GroupBucket createSelectGroupBucket( |
| 91 | TrafficTreatment treatment) { |
| 92 | return new DefaultGroupBucket(GroupDescription.Type.SELECT, |
| 93 | treatment, |
| 94 | (short) 1, |
| 95 | null, |
| 96 | null); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates select group bucket with specified weight. |
| 101 | * |
| 102 | * @param treatment traffic treatment associated with group bucket |
| 103 | * @param weight weight associated with group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 104 | * @return select group bucket object |
| 105 | */ |
| 106 | public static GroupBucket createSelectGroupBucket( |
| 107 | TrafficTreatment treatment, |
| 108 | short weight) { |
| 109 | if (weight == 0) { |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | return new DefaultGroupBucket(GroupDescription.Type.SELECT, |
| 114 | treatment, |
| 115 | weight, |
| 116 | null, |
| 117 | null); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Creates failover group bucket with watchport or watchgroup. |
| 122 | * |
| 123 | * @param treatment traffic treatment associated with group bucket |
| 124 | * @param watchPort port that determines the liveness of group bucket |
Thomas Vachuska | 0cd4de8 | 2015-02-03 09:17:08 -0800 | [diff] [blame] | 125 | * @param watchGroup group that determines the liveness of group bucket |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 126 | * @return failover group bucket object |
| 127 | */ |
| 128 | public static GroupBucket createFailoverGroupBucket( |
| 129 | TrafficTreatment treatment, |
| 130 | PortNumber watchPort, |
| 131 | GroupId watchGroup) { |
| 132 | checkArgument(((watchPort != null) || (watchGroup != null))); |
| 133 | return new DefaultGroupBucket(GroupDescription.Type.FAILOVER, |
| 134 | treatment, |
| 135 | (short) -1, |
| 136 | watchPort, |
| 137 | watchGroup); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public GroupDescription.Type type() { |
| 142 | return this.type; |
| 143 | } |
| 144 | |
| 145 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 146 | * Returns list of Traffic instructions that are part of the bucket. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 147 | * |
| 148 | * @return TrafficTreatment Traffic instruction list |
| 149 | */ |
| 150 | @Override |
| 151 | public TrafficTreatment treatment() { |
| 152 | return treatment; |
| 153 | } |
| 154 | |
| 155 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 156 | * Returns weight of select group bucket. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 157 | * |
| 158 | * @return short weight associated with a bucket |
| 159 | */ |
| 160 | @Override |
| 161 | public short weight() { |
| 162 | return weight; |
| 163 | } |
| 164 | |
| 165 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 166 | * Returns port number used for liveness detection for a |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 167 | * failover bucket. |
| 168 | * |
| 169 | * @return PortNumber port number used for liveness detection |
| 170 | */ |
| 171 | @Override |
| 172 | public PortNumber watchPort() { |
| 173 | return watchPort; |
| 174 | } |
| 175 | |
| 176 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 177 | * Returns group identifier used for liveness detection for a |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 178 | * failover bucket. |
| 179 | * |
| 180 | * @return GroupId group identifier to be used for liveness detection |
| 181 | */ |
| 182 | @Override |
| 183 | public GroupId watchGroup() { |
| 184 | return watchGroup; |
| 185 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * The type and treatment can change on a given bucket |
| 189 | * |
| 190 | * (non-Javadoc) |
| 191 | * @see java.lang.Object#equals(java.lang.Object) |
| 192 | */ |
| 193 | @Override |
| 194 | public int hashCode() { |
| 195 | return Objects.hash(type, treatment); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * The priority and statistics can change on a given treatment and selector |
| 200 | * |
| 201 | * (non-Javadoc) |
| 202 | * @see java.lang.Object#equals(java.lang.Object) |
| 203 | */ |
| 204 | @Override |
| 205 | public boolean equals(Object obj) { |
| 206 | if (this == obj) { |
| 207 | return true; |
| 208 | } |
| 209 | if (obj instanceof DefaultGroupBucket) { |
| 210 | DefaultGroupBucket that = (DefaultGroupBucket) obj; |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 211 | List<Instruction> myInstructions = this.treatment.allInstructions(); |
| 212 | List<Instruction> theirInstructions = that.treatment.allInstructions(); |
| 213 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 214 | return Objects.equals(type, that.type) && |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 215 | myInstructions.containsAll(theirInstructions) && |
| 216 | theirInstructions.containsAll(myInstructions); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 221 | @Override |
| 222 | public String toString() { |
| 223 | return toStringHelper(this) |
| 224 | .add("type", type) |
| 225 | .add("treatment", treatment) |
| 226 | .toString(); |
| 227 | } |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 228 | } |