blob: 4c1b71f38d296e9c03db8238bb9cbc929c09b692 [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;
20import org.onosproject.net.flow.criteria.Criterion;
21
22import java.util.Collection;
23import java.util.List;
24import java.util.Objects;
25
26import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Default implementation of a filtering objective.
31 */
32public final class DefaultFilteringObjective implements FilteringObjective {
33
34
35 private final Criterion key;
36 private final boolean permanent;
37 private final int timeout;
38 private final ApplicationId appId;
39 private final int priority;
40 private final List<Criterion> conditions;
41 private final int id;
42 private final Operation op;
43
44 private DefaultFilteringObjective(Criterion key, boolean permanent, int timeout,
45 ApplicationId appId, int priority,
46 List<Criterion> conditions, Operation op) {
47 this.key = key;
48 this.permanent = permanent;
49 this.timeout = timeout;
50 this.appId = appId;
51 this.priority = priority;
52 this.conditions = conditions;
53 this.op = op;
54
55 this.id = Objects.hash(key, conditions, permanent,
56 timeout, appId, priority);
57 }
58
59
60 @Override
61 public Criterion key() {
62 return key;
63 }
64
65 @Override
66 public Collection<Criterion> conditions() {
67 return conditions;
68 }
69
70 @Override
71 public int id() {
72 return id;
73 }
74
75 @Override
76 public int priority() {
77 return priority;
78 }
79
80 @Override
81 public ApplicationId appId() {
82 return appId;
83 }
84
85 @Override
86 public int timeout() {
87 return timeout;
88 }
89
90 @Override
91 public boolean permanent() {
92 return permanent;
93 }
94
95 @Override
96 public Operation op() {
97 return op;
98 }
99
100 /**
101 * Returns a new builder.
102 *
103 * @return new builder
104 */
105 public static Builder builder() {
106 return new Builder();
107 }
108
109
110 public static final class Builder implements FilteringObjective.Builder {
111 private final ImmutableList.Builder<Criterion> listBuilder
112 = ImmutableList.builder();
113
114 private Criterion key;
115 private boolean permanent = DEFAULT_PERMANENT;
116 private int timeout = DEFAULT_TIMEOUT;
117 private ApplicationId appId;
118 private int priority = DEFAULT_PRIORITY;
119
120 @Override
121 public Builder addCondition(Criterion criterion) {
122 listBuilder.add(criterion);
123 return this;
124 }
125
126 @Override
127 public Builder withKey(Criterion criterion) {
128 key = criterion;
129 return this;
130 }
131
132 @Override
133 public Builder makeTemporary(int timeout) {
134 this.timeout = timeout;
135 permanent = false;
136 return this;
137 }
138
139 @Override
140 public Builder makePermanent() {
141 permanent = true;
142 return this;
143 }
144
145 @Override
146 public Builder fromApp(ApplicationId appId) {
147 this.appId = appId;
148 return this;
149 }
150
151 @Override
152 public Builder withPriority(int priority) {
153 this.priority = priority;
154 return this;
155 }
156
157 @Override
158 public FilteringObjective add() {
159 List<Criterion> conditions = listBuilder.build();
160 checkNotNull(key, "Must have a key.");
161 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
162 checkNotNull(appId, "Must supply an application id");
163
164 return new DefaultFilteringObjective(key, permanent, timeout,
165 appId, priority, conditions,
166 Operation.ADD);
167
168 }
169
170 @Override
171 public FilteringObjective remove() {
172 List<Criterion> conditions = listBuilder.build();
173 checkNotNull(key, "Must have a key.");
174 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
175 checkNotNull(appId, "Must supply an application id");
176
177 return new DefaultFilteringObjective(key, permanent, timeout,
178 appId, priority, conditions,
179 Operation.REMOVE);
180
181 }
182
183
184 }
185
186}