0abf5abe4f68f0cafb36cd188a4a48a1e08b6fbb
[onosfw.git] /
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 com.google.common.annotations.Beta;
19 import org.onosproject.core.ApplicationId;
20 import org.onosproject.net.flow.TrafficSelector;
21 import org.onosproject.net.flow.TrafficTreatment;
22
23 import java.util.Objects;
24 import java.util.Optional;
25
26 import static com.google.common.base.Preconditions.checkArgument;
27 import static com.google.common.base.Preconditions.checkNotNull;
28
29 /**
30  * Default implementation of a forwarding objective.
31  */
32 @Beta
33 public 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;
41     private final Integer nextId;
42     private final TrafficTreatment treatment;
43     private final Operation op;
44     private final Optional<ObjectiveContext> context;
45
46     private final int id;
47
48     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);
59
60         this.id = Objects.hash(selector, flag, permanent,
61                 timeout, appId, priority, nextId,
62                 treatment, op);
63     }
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
117     @Override
118     public Optional<ObjectiveContext> context() {
119         return context;
120     }
121
122     /**
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;
141         private Operation op;
142         private ObjectiveContext context;
143
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");
197             checkArgument(nextId != null || treatment != null, "Must supply at " +
198                     "least a treatment and/or a nextId");
199             checkNotNull(appId, "Must supply an application id");
200             op = Operation.ADD;
201             return new DefaultForwardingObjective(this);
202         }
203
204         @Override
205         public ForwardingObjective remove() {
206             checkNotNull(selector, "Must have a selector");
207             checkNotNull(flag, "A flag must be set");
208             checkArgument(nextId != null || treatment != null, "Must supply at " +
209                     "least a treatment and/or a nextId");
210             checkNotNull(appId, "Must supply an application id");
211             op = Operation.REMOVE;
212             return new DefaultForwardingObjective(this);
213         }
214
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");
222             op = Operation.ADD;
223             this.context = context;
224
225             return new DefaultForwardingObjective(this);
226         }
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");
235             op = Operation.REMOVE;
236             this.context = context;
237
238             return new DefaultForwardingObjective(this);
239         }
240     }
241 }