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