blob: 33b8f5a713a5608ba18a6ce12279c25a7b3cdaf4 [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
18import com.google.common.collect.ImmutableList;
19import org.onosproject.core.ApplicationId;
alshabiba3a476d2015-04-10 14:35:38 -070020import org.onosproject.net.flow.criteria.Criteria;
alshabibfaa1e362015-04-02 15:01:54 -070021import org.onosproject.net.flow.criteria.Criterion;
22
23import java.util.Collection;
24import java.util.List;
25import java.util.Objects;
26
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Default implementation of a filtering objective.
32 */
33public final class DefaultFilteringObjective implements FilteringObjective {
34
35
alshabib910aff12015-04-09 16:55:57 -070036 private final Type type;
alshabibfaa1e362015-04-02 15:01:54 -070037 private final boolean permanent;
38 private final int timeout;
39 private final ApplicationId appId;
40 private final int priority;
alshabiba3a476d2015-04-10 14:35:38 -070041 private final Criterion key;
alshabibfaa1e362015-04-02 15:01:54 -070042 private final List<Criterion> conditions;
43 private final int id;
44 private final Operation op;
45
alshabib910aff12015-04-09 16:55:57 -070046 private DefaultFilteringObjective(Type type, boolean permanent, int timeout,
alshabiba3a476d2015-04-10 14:35:38 -070047 ApplicationId appId, int priority, Criterion key,
alshabibfaa1e362015-04-02 15:01:54 -070048 List<Criterion> conditions, Operation op) {
alshabiba3a476d2015-04-10 14:35:38 -070049 this.key = key;
alshabib910aff12015-04-09 16:55:57 -070050 this.type = type;
alshabibfaa1e362015-04-02 15:01:54 -070051 this.permanent = permanent;
52 this.timeout = timeout;
53 this.appId = appId;
54 this.priority = priority;
55 this.conditions = conditions;
56 this.op = op;
57
alshabiba3a476d2015-04-10 14:35:38 -070058 this.id = Objects.hash(type, key, conditions, permanent,
alshabibfaa1e362015-04-02 15:01:54 -070059 timeout, appId, priority);
60 }
61
alshabibfaa1e362015-04-02 15:01:54 -070062 @Override
alshabiba3a476d2015-04-10 14:35:38 -070063 public Criterion key() {
64 return key;
65 }
66
67 @Override
alshabib910aff12015-04-09 16:55:57 -070068 public Type type() {
69 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070070 }
71
72 @Override
73 public Collection<Criterion> conditions() {
74 return conditions;
75 }
76
77 @Override
78 public int id() {
79 return id;
80 }
81
82 @Override
83 public int priority() {
84 return priority;
85 }
86
87 @Override
88 public ApplicationId appId() {
89 return appId;
90 }
91
92 @Override
93 public int timeout() {
94 return timeout;
95 }
96
97 @Override
98 public boolean permanent() {
99 return permanent;
100 }
101
102 @Override
103 public Operation op() {
104 return op;
105 }
106
107 /**
108 * Returns a new builder.
109 *
110 * @return new builder
111 */
112 public static Builder builder() {
113 return new Builder();
114 }
115
116
117 public static final class Builder implements FilteringObjective.Builder {
118 private final ImmutableList.Builder<Criterion> listBuilder
119 = ImmutableList.builder();
120
alshabib910aff12015-04-09 16:55:57 -0700121 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700122 private boolean permanent = DEFAULT_PERMANENT;
123 private int timeout = DEFAULT_TIMEOUT;
124 private ApplicationId appId;
125 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700126 private Criterion key = Criteria.dummy();
127
128 @Override
129 public Builder withKey(Criterion key) {
130 this.key = key;
131 return this;
132 }
alshabibfaa1e362015-04-02 15:01:54 -0700133
134 @Override
135 public Builder addCondition(Criterion criterion) {
136 listBuilder.add(criterion);
137 return this;
138 }
139
140 @Override
alshabib910aff12015-04-09 16:55:57 -0700141 public Builder permit() {
142 this.type = Type.PERMIT;
143 return this;
144 }
145
146 @Override
147 public Builder deny() {
148 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700149 return this;
150 }
151
152 @Override
153 public Builder makeTemporary(int timeout) {
154 this.timeout = timeout;
155 permanent = false;
156 return this;
157 }
158
159 @Override
160 public Builder makePermanent() {
161 permanent = true;
162 return this;
163 }
164
165 @Override
166 public Builder fromApp(ApplicationId appId) {
167 this.appId = appId;
168 return this;
169 }
170
171 @Override
172 public Builder withPriority(int priority) {
173 this.priority = priority;
174 return this;
175 }
176
177 @Override
178 public FilteringObjective add() {
179 List<Criterion> conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700180 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700181 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
182 checkNotNull(appId, "Must supply an application id");
183
alshabib910aff12015-04-09 16:55:57 -0700184 return new DefaultFilteringObjective(type, permanent, timeout,
alshabiba3a476d2015-04-10 14:35:38 -0700185 appId, priority, key, conditions,
alshabibfaa1e362015-04-02 15:01:54 -0700186 Operation.ADD);
187
188 }
189
190 @Override
191 public FilteringObjective remove() {
192 List<Criterion> conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700193 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700194 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
195 checkNotNull(appId, "Must supply an application id");
196
alshabiba3a476d2015-04-10 14:35:38 -0700197
alshabib910aff12015-04-09 16:55:57 -0700198 return new DefaultFilteringObjective(type, permanent, timeout,
alshabiba3a476d2015-04-10 14:35:38 -0700199 appId, priority, key, conditions,
alshabibfaa1e362015-04-02 15:01:54 -0700200 Operation.REMOVE);
201
202 }
203
204
205 }
206
207}