blob: 134828b3d4a8bcaebdb9cd9712f124d252619ab6 [file] [log] [blame]
sangho1e575652015-05-14 00:39:53 -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 */
16
17package org.onosproject.segmentrouting;
18
19import org.onosproject.net.flow.TrafficSelector;
20import org.onosproject.net.flowobjective.DefaultForwardingObjective;
21import org.onosproject.net.flowobjective.ForwardingObjective;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Tunnel Policy.
27 */
28public final class TunnelPolicy implements Policy {
29
Thomas Vachuska5d410a22015-05-19 17:52:37 -070030 // FIXME: We should avoid passing around references to implementation objects
31 // Instead, if some operational context is required, we should abstract it to
32 // a bare minimum.
sangho1e575652015-05-14 00:39:53 -070033 private final SegmentRoutingManager srManager;
34 private final Type type;
35 private final String id;
36 private final TrafficSelector selector;
37 private final int priority;
38 private final String tunnelId;
39
40 private TunnelPolicy(SegmentRoutingManager srm, String policyId, Type type,
41 TrafficSelector selector, int priority, String tunnelId) {
42 this.srManager = srm;
43 this.id = checkNotNull(policyId);
44 this.type = type;
45 this.tunnelId = tunnelId;
46 this.priority = priority;
47 this.selector = selector;
48 }
49
50 /**
51 * Creates a TunnelPolicy reference.
52 *
53 * @param p TunnelPolicy reference
54 */
55 public TunnelPolicy(TunnelPolicy p) {
56 this.srManager = p.srManager;
57 this.id = p.id;
58 this.type = p.type;
59 this.tunnelId = p.tunnelId;
60 this.priority = p.priority;
61 this.selector = p.selector;
62 }
63
64 /**
65 * Creates a TunnelPolicy reference.
66 *
Thomas Vachuska5d410a22015-05-19 17:52:37 -070067 * @param srm reference to the segment routing component
sangho1e575652015-05-14 00:39:53 -070068 * @param p TunnelPolicy reference
69 */
70 public TunnelPolicy(SegmentRoutingManager srm, TunnelPolicy p) {
71 this.srManager = srm;
72 this.id = p.id;
73 this.type = p.type;
74 this.tunnelId = p.tunnelId;
75 this.priority = p.priority;
76 this.selector = p.selector;
77 }
78
79 /**
80 * Returns the TunnelPolicy builder reference.
81 *
82 * @return TunnelPolicy builder
83 */
84 public static TunnelPolicy.Builder builder() {
85 return new Builder();
86 }
87
88 @Override
89 public String id() {
90 return this.id;
91 }
92
93 @Override
94 public TrafficSelector selector() {
95 return selector;
96 }
97
98 @Override
99 public int priority() {
100 return priority;
101 }
102
103 @Override
104 public Type type() {
105 return type;
106 }
107
108 @Override
109 public boolean create() {
110
111 Tunnel tunnel = srManager.getTunnel(tunnelId);
112
113 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
114 .builder()
115 .fromApp(srManager.appId)
116 .makePermanent()
117 .nextStep(tunnel.groupId())
118 .withPriority(priority)
119 .withSelector(selector)
120 .withFlag(ForwardingObjective.Flag.VERSATILE);
121
122 srManager.flowObjectiveService.forward(tunnel.source(), fwdBuilder.add());
123
124 return true;
125 }
126
127 @Override
128 public boolean remove() {
129
130 Tunnel tunnel = srManager.getTunnel(tunnelId);
131
132 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
133 .builder()
134 .fromApp(srManager.appId)
135 .makePermanent()
136 .withSelector(selector)
137 .withPriority(priority)
138 .nextStep(tunnel.groupId())
139 .withFlag(ForwardingObjective.Flag.VERSATILE);
140
141 srManager.flowObjectiveService.forward(tunnel.source(), fwdBuilder.remove());
142
143 return true;
144 }
145
146 /**
147 * Returns the tunnel ID of the policy.
148 *
149 * @return Tunnel ID
150 */
151 public String tunnelId() {
152 return this.tunnelId;
153 }
154
155 /**
156 * Tunnel Policy Builder.
157 */
158 public static final class Builder {
159
160 private SegmentRoutingManager srManager;
161 private String id;
162 private Type type;
163 private TrafficSelector selector;
164 private int priority;
165 private String tunnelId;
166
167 /**
168 * Sets the policy Id.
169 *
170 * @param id policy Id
171 * @return Builder object
172 */
173 public Builder setPolicyId(String id) {
174 this.id = id;
175
176 return this;
177 }
178
179 /**
180 * Sets the policy type.
181 *
182 * @param type policy type
183 * @return Builder object
184 */
185 public Builder setType(Type type) {
186 this.type = type;
187
188 return this;
189 }
190
191 /**
192 * Sets the TrafficSelector.
193 *
194 * @param selector TrafficSelector
195 * @return Builder object
196 */
197 public Builder setSelector(TrafficSelector selector) {
198 this.selector = selector;
199
200 return this;
201 }
202
203 /**
204 * Sets the priority of the policy.
205 *
206 * @param p priority
207 * @return Builder object
208 */
209 public Builder setPriority(int p) {
210 this.priority = p;
211
212 return this;
213 }
214
215 /**
216 * Sets the tunnel Id.
217 *
218 * @param tunnelId tunnel Id
219 * @return Builder object
220 */
221 public Builder setTunnelId(String tunnelId) {
222 this.tunnelId = tunnelId;
223
224 return this;
225 }
226
227 /**
228 * Sets the Segment Routing Manager reference.
229 *
230 * @param srm Segment Routing Manager reference
231 * @return Builder object
232 */
233 public Builder setManager(SegmentRoutingManager srm) {
234 this.srManager = srm;
235
236 return this;
237 }
238
239 /**
240 * Builds the policy.
241 *
242 * @return Tunnel Policy reference
243 */
244 public Policy build() {
245 return new TunnelPolicy(srManager, id, type, selector, priority, tunnelId);
246 }
247 }
248}