blob: 20e8929585c891dc98bda91a90b2f6ce39c04d54 [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 com.google.common.collect.ImmutableList;
20import org.onosproject.core.ApplicationId;
21import org.onosproject.net.flow.TrafficTreatment;
22
23import java.util.Collection;
24import java.util.List;
alshabib2a441c62015-04-13 18:39:38 -070025import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070026
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Default implementation of a next objective.
32 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070033@Beta
alshabibfaa1e362015-04-02 15:01:54 -070034public final class DefaultNextObjective implements NextObjective {
35
36 private final List<TrafficTreatment> treatments;
37 private final ApplicationId appId;
38 private final Type type;
39 private final Integer id;
alshabib2a441c62015-04-13 18:39:38 -070040 private final Operation op;
41 private final Optional<ObjectiveContext> context;
alshabibfaa1e362015-04-02 15:01:54 -070042
Ray Milkey810d6e72015-08-14 10:35:06 -070043 private DefaultNextObjective(Builder builder) {
44 this.treatments = builder.treatments;
45 this.appId = builder.appId;
46 this.type = builder.type;
47 this.id = builder.id;
48 this.op = builder.op;
49 this.context = Optional.ofNullable(builder.context);
alshabibfaa1e362015-04-02 15:01:54 -070050 }
51
52 @Override
53 public Collection<TrafficTreatment> next() {
54 return treatments;
55 }
56
57 @Override
58 public Type type() {
59 return type;
60 }
61
62 @Override
63 public int id() {
64 return id;
65 }
66
67 @Override
68 public int priority() {
69 return 0;
70 }
71
72 @Override
73 public ApplicationId appId() {
74 return appId;
75 }
76
77 @Override
78 public int timeout() {
79 return 0;
80 }
81
82 @Override
83 public boolean permanent() {
84 return false;
85 }
86
87 @Override
88 public Operation op() {
alshabib2a441c62015-04-13 18:39:38 -070089 return op;
90 }
91
92 @Override
93 public Optional<ObjectiveContext> context() {
94 return context;
alshabibfaa1e362015-04-02 15:01:54 -070095 }
96
97 /**
98 * Returns a new builder.
99 *
100 * @return new builder
101 */
102 public static Builder builder() {
103 return new Builder();
104 }
105
106 public static final class Builder implements NextObjective.Builder {
107
108 private ApplicationId appId;
109 private Type type;
110 private Integer id;
Ray Milkey810d6e72015-08-14 10:35:06 -0700111 private List<TrafficTreatment> treatments;
112 private Operation op;
113 private ObjectiveContext context;
alshabibfaa1e362015-04-02 15:01:54 -0700114
115 private final ImmutableList.Builder<TrafficTreatment> listBuilder
116 = ImmutableList.builder();
117
alshabibfaa1e362015-04-02 15:01:54 -0700118 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700119 public Builder withId(int nextId) {
alshabibfaa1e362015-04-02 15:01:54 -0700120 this.id = nextId;
121 return this;
122 }
123
124 @Override
125 public Builder withType(Type type) {
126 this.type = type;
127 return this;
128 }
129
130 @Override
131 public Builder addTreatment(TrafficTreatment treatment) {
132 listBuilder.add(treatment);
133 return this;
134 }
135
136 /**
137 * Noop. This method has no effect.
138 *
139 * @param timeout a timeout
140 * @return a next objective builder
141 */
142 @Override
143 public Builder makeTemporary(int timeout) {
144 return this;
145 }
146
147 /**
148 * Noop. This method has no effect.
149 *
150 * @return a next objective builder
151 */
152 @Override
153 public Builder makePermanent() {
154 return this;
155 }
156
157 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700158 public Builder fromApp(ApplicationId appId) {
alshabibfaa1e362015-04-02 15:01:54 -0700159 this.appId = appId;
160 return this;
161 }
162
163 /**
164 * Noop. This method has no effect.
165 *
166 * @param priority an integer
167 * @return a next objective builder
168 */
169 @Override
170 public Builder withPriority(int priority) {
171 return this;
172 }
173
174 @Override
alshabib2a441c62015-04-13 18:39:38 -0700175 public NextObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700176 treatments = listBuilder.build();
177 op = Operation.ADD;
alshabibfaa1e362015-04-02 15:01:54 -0700178 checkNotNull(appId, "Must supply an application id");
179 checkNotNull(id, "id cannot be null");
180 checkNotNull(type, "The type cannot be null");
181 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
182
Ray Milkey810d6e72015-08-14 10:35:06 -0700183 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700184 }
185
186 @Override
187 public NextObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700188 treatments = listBuilder.build();
189 op = Operation.REMOVE;
alshabib2a441c62015-04-13 18:39:38 -0700190 checkNotNull(appId, "Must supply an application id");
191 checkNotNull(id, "id cannot be null");
192 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700193
Ray Milkey810d6e72015-08-14 10:35:06 -0700194 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700195 }
196
197 @Override
198 public NextObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700199 treatments = listBuilder.build();
200 op = Operation.ADD;
201 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700202 checkNotNull(appId, "Must supply an application id");
203 checkNotNull(id, "id cannot be null");
204 checkNotNull(type, "The type cannot be null");
205 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
206
Ray Milkey810d6e72015-08-14 10:35:06 -0700207 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700208 }
209
210 @Override
211 public NextObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700212 treatments = listBuilder.build();
213 op = Operation.REMOVE;
214 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700215 checkNotNull(appId, "Must supply an application id");
216 checkNotNull(id, "id cannot be null");
217 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700218
Ray Milkey810d6e72015-08-14 10:35:06 -0700219 return new DefaultNextObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700220 }
221 }
222}