blob: 0abf5abe4f68f0cafb36cd188a4a48a1e08b6fbb [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
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 */
16package org.onosproject.net.flowobjective;
17
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070018import com.google.common.annotations.Beta;
alshabibfaa1e362015-04-02 15:01:54 -070019import org.onosproject.core.ApplicationId;
20import org.onosproject.net.flow.TrafficSelector;
21import org.onosproject.net.flow.TrafficTreatment;
22
23import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070024import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070025
26import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Default implementation of a forwarding objective.
31 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070032@Beta
alshabibfaa1e362015-04-02 15:01:54 -070033public final class DefaultForwardingObjective implements ForwardingObjective {
34
35 private final TrafficSelector selector;
36 private final Flag flag;
37 private final boolean permanent;
38 private final int timeout;
39 private final ApplicationId appId;
40 private final int priority;
Jonathan Hart17d00452015-04-21 17:10:00 -070041 private final Integer nextId;
alshabibfaa1e362015-04-02 15:01:54 -070042 private final TrafficTreatment treatment;
43 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070044 private final Optional<ObjectiveContext> context;
alshabibfaa1e362015-04-02 15:01:54 -070045
46 private final int id;
47
Ray Milkey810d6e72015-08-14 10:35:06 -070048 private DefaultForwardingObjective(Builder builder) {
49 this.selector = builder.selector;
50 this.flag = builder.flag;
51 this.permanent = builder.permanent;
52 this.timeout = builder.timeout;
53 this.appId = builder.appId;
54 this.priority = builder.priority;
55 this.nextId = builder.nextId;
56 this.treatment = builder.treatment;
57 this.op = builder.op;
58 this.context = Optional.ofNullable(builder.context);
alshabib2a441c62015-04-13 18:39:38 -070059
60 this.id = Objects.hash(selector, flag, permanent,
Ray Milkey810d6e72015-08-14 10:35:06 -070061 timeout, appId, priority, nextId,
62 treatment, op);
alshabibfaa1e362015-04-02 15:01:54 -070063 }
64
65
66 @Override
67 public TrafficSelector selector() {
68 return selector;
69 }
70
71 @Override
72 public Integer nextId() {
73 return nextId;
74 }
75
76 @Override
77 public TrafficTreatment treatment() {
78 return treatment;
79 }
80
81
82 @Override
83 public Flag flag() {
84 return flag;
85 }
86
87 @Override
88 public int id() {
89 return id;
90 }
91
92 @Override
93 public int priority() {
94 return priority;
95 }
96
97 @Override
98 public ApplicationId appId() {
99 return appId;
100 }
101
102 @Override
103 public int timeout() {
104 return timeout;
105 }
106
107 @Override
108 public boolean permanent() {
109 return permanent;
110 }
111
112 @Override
113 public Operation op() {
114 return op;
115 }
116
alshabib2a441c62015-04-13 18:39:38 -0700117 @Override
118 public Optional<ObjectiveContext> context() {
119 return context;
120 }
121
alshabibfaa1e362015-04-02 15:01:54 -0700122 /**
123 * Returns a new builder.
124 *
125 * @return new builder
126 */
127 public static Builder builder() {
128 return new Builder();
129 }
130
131 public static final class Builder implements ForwardingObjective.Builder {
132
133 private TrafficSelector selector;
134 private Flag flag;
135 private boolean permanent = DEFAULT_PERMANENT;
136 private int timeout = DEFAULT_TIMEOUT;
137 private int priority = DEFAULT_PRIORITY;
138 private ApplicationId appId;
139 private Integer nextId;
140 private TrafficTreatment treatment;
Ray Milkey810d6e72015-08-14 10:35:06 -0700141 private Operation op;
142 private ObjectiveContext context;
alshabibfaa1e362015-04-02 15:01:54 -0700143
144 @Override
145 public Builder withSelector(TrafficSelector selector) {
146 this.selector = selector;
147 return this;
148 }
149
150 @Override
151 public Builder nextStep(int nextId) {
152 this.nextId = nextId;
153 return this;
154 }
155
156 @Override
157 public Builder withTreatment(TrafficTreatment treatment) {
158 this.treatment = treatment;
159 return this;
160 }
161
162 @Override
163 public Builder withFlag(Flag flag) {
164 this.flag = flag;
165 return this;
166 }
167
168 @Override
169 public Builder makeTemporary(int timeout) {
170 this.timeout = timeout;
171 this.permanent = false;
172 return this;
173 }
174
175 @Override
176 public Builder makePermanent() {
177 this.permanent = true;
178 return this;
179 }
180
181 @Override
182 public Builder fromApp(ApplicationId appId) {
183 this.appId = appId;
184 return this;
185 }
186
187 @Override
188 public Builder withPriority(int priority) {
189 this.priority = priority;
190 return this;
191 }
192
193 @Override
194 public ForwardingObjective add() {
195 checkNotNull(selector, "Must have a selector");
196 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700197 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700198 "least a treatment and/or a nextId");
199 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700200 op = Operation.ADD;
201 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700202 }
203
204 @Override
205 public ForwardingObjective remove() {
206 checkNotNull(selector, "Must have a selector");
207 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700208 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700209 "least a treatment and/or a nextId");
210 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700211 op = Operation.REMOVE;
212 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700213 }
alshabib2a441c62015-04-13 18:39:38 -0700214
215 @Override
216 public ForwardingObjective add(ObjectiveContext context) {
217 checkNotNull(selector, "Must have a selector");
218 checkNotNull(flag, "A flag must be set");
219 checkArgument(nextId != null || treatment != null, "Must supply at " +
220 "least a treatment and/or a nextId");
221 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700222 op = Operation.ADD;
223 this.context = context;
224
225 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700226 }
227
228 @Override
229 public ForwardingObjective remove(ObjectiveContext context) {
230 checkNotNull(selector, "Must have a selector");
231 checkNotNull(flag, "A flag must be set");
232 checkArgument(nextId != null || treatment != null, "Must supply at " +
233 "least a treatment and/or a nextId");
234 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700235 op = Operation.REMOVE;
236 this.context = context;
237
238 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700239 }
alshabibfaa1e362015-04-02 15:01:54 -0700240 }
241}