alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [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.flowobjective; |
| 17 | |
| 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.net.flow.TrafficSelector; |
| 20 | import org.onosproject.net.flow.TrafficTreatment; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Default implementation of a forwarding objective. |
| 29 | */ |
| 30 | public final class DefaultForwardingObjective implements ForwardingObjective { |
| 31 | |
| 32 | private final TrafficSelector selector; |
| 33 | private final Flag flag; |
| 34 | private final boolean permanent; |
| 35 | private final int timeout; |
| 36 | private final ApplicationId appId; |
| 37 | private final int priority; |
| 38 | private final int nextId; |
| 39 | private final TrafficTreatment treatment; |
| 40 | private final Operation op; |
| 41 | |
| 42 | private final int id; |
| 43 | |
| 44 | private DefaultForwardingObjective(TrafficSelector selector, |
| 45 | Flag flag, boolean permanent, |
| 46 | int timeout, ApplicationId appId, |
| 47 | int priority, int nextId, |
| 48 | TrafficTreatment treatment, Operation op) { |
| 49 | this.selector = selector; |
| 50 | this.flag = flag; |
| 51 | this.permanent = permanent; |
| 52 | this.timeout = timeout; |
| 53 | this.appId = appId; |
| 54 | this.priority = priority; |
| 55 | this.nextId = nextId; |
| 56 | this.treatment = treatment; |
| 57 | this.op = op; |
| 58 | |
| 59 | this.id = Objects.hash(selector, flag, permanent, |
| 60 | timeout, appId, priority, nextId, |
| 61 | treatment, op); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | @Override |
| 66 | public TrafficSelector selector() { |
| 67 | return selector; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Integer nextId() { |
| 72 | return nextId; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public TrafficTreatment treatment() { |
| 77 | return treatment; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | @Override |
| 82 | public Flag flag() { |
| 83 | return flag; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int id() { |
| 88 | return id; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public int priority() { |
| 93 | return priority; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public ApplicationId appId() { |
| 98 | return appId; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int timeout() { |
| 103 | return timeout; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean permanent() { |
| 108 | return permanent; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public Operation op() { |
| 113 | return op; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns a new builder. |
| 118 | * |
| 119 | * @return new builder |
| 120 | */ |
| 121 | public static Builder builder() { |
| 122 | return new Builder(); |
| 123 | } |
| 124 | |
| 125 | public static final class Builder implements ForwardingObjective.Builder { |
| 126 | |
| 127 | private TrafficSelector selector; |
| 128 | private Flag flag; |
| 129 | private boolean permanent = DEFAULT_PERMANENT; |
| 130 | private int timeout = DEFAULT_TIMEOUT; |
| 131 | private int priority = DEFAULT_PRIORITY; |
| 132 | private ApplicationId appId; |
| 133 | private Integer nextId; |
| 134 | private TrafficTreatment treatment; |
| 135 | |
| 136 | @Override |
| 137 | public Builder withSelector(TrafficSelector selector) { |
| 138 | this.selector = selector; |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Builder nextStep(int nextId) { |
| 144 | this.nextId = nextId; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Builder withTreatment(TrafficTreatment treatment) { |
| 150 | this.treatment = treatment; |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Builder withFlag(Flag flag) { |
| 156 | this.flag = flag; |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Builder makeTemporary(int timeout) { |
| 162 | this.timeout = timeout; |
| 163 | this.permanent = false; |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Builder makePermanent() { |
| 169 | this.permanent = true; |
| 170 | return this; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Builder fromApp(ApplicationId appId) { |
| 175 | this.appId = appId; |
| 176 | return this; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Builder withPriority(int priority) { |
| 181 | this.priority = priority; |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public ForwardingObjective add() { |
| 187 | checkNotNull(selector, "Must have a selector"); |
| 188 | checkNotNull(flag, "A flag must be set"); |
| 189 | checkArgument(nextId != null && treatment != null, "Must supply at " + |
| 190 | "least a treatment and/or a nextId"); |
| 191 | checkNotNull(appId, "Must supply an application id"); |
| 192 | return new DefaultForwardingObjective(selector, flag, permanent, |
| 193 | timeout, appId, priority, |
| 194 | nextId, treatment, Operation.ADD); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public ForwardingObjective remove() { |
| 199 | checkNotNull(selector, "Must have a selector"); |
| 200 | checkNotNull(flag, "A flag must be set"); |
| 201 | checkArgument(nextId != null && treatment != null, "Must supply at " + |
| 202 | "least a treatment and/or a nextId"); |
| 203 | checkNotNull(appId, "Must supply an application id"); |
| 204 | return new DefaultForwardingObjective(selector, flag, permanent, |
| 205 | timeout, appId, priority, |
| 206 | nextId, treatment, Operation.REMOVE); |
| 207 | } |
| 208 | } |
| 209 | } |