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