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