PolicyHandler.java
8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright 2015-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.segmentrouting;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.TpPort;
import org.onosproject.cli.net.IpProtocol;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flowobjective.DefaultForwardingObjective;
import org.onosproject.net.flowobjective.FlowObjectiveService;
import org.onosproject.net.flowobjective.ForwardingObjective;
import org.onosproject.segmentrouting.config.DeviceConfiguration;
import org.onosproject.store.service.EventuallyConsistentMap;
import org.slf4j.Logger;
import java.util.List;
import java.util.stream.Collectors;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Segment Routing Policy Handler.
*/
public class PolicyHandler {
protected final Logger log = getLogger(getClass());
private ApplicationId appId;
private DeviceConfiguration deviceConfiguration;
private FlowObjectiveService flowObjectiveService;
private TunnelHandler tunnelHandler;
private final EventuallyConsistentMap<String, Policy> policyStore;
/**
* Result of policy creation.
*/
public enum Result {
/**
* Success.
*/
SUCCESS,
/**
* The same policy exists already.
*/
POLICY_EXISTS,
/**
* The policy ID exists already.
*/
ID_EXISTS,
/**
* Cannot find associated tunnel.
*/
TUNNEL_NOT_FOUND,
/**
* Policy was not found.
*/
POLICY_NOT_FOUND,
/**
* Policy type {} is not supported yet.
*/
UNSUPPORTED_TYPE
}
/**
* Constructs policy handler.
*
* @param appId segment routing application ID
* @param deviceConfiguration DeviceConfiguration reference
* @param flowObjectiveService FlowObjectiveService reference
* @param tunnelHandler tunnel handler reference
* @param policyStore policy store
*/
public PolicyHandler(ApplicationId appId,
DeviceConfiguration deviceConfiguration,
FlowObjectiveService flowObjectiveService,
TunnelHandler tunnelHandler,
EventuallyConsistentMap<String, Policy> policyStore) {
this.appId = appId;
this.deviceConfiguration = deviceConfiguration;
this.flowObjectiveService = flowObjectiveService;
this.tunnelHandler = tunnelHandler;
this.policyStore = policyStore;
}
/**
* Returns the policies.
*
* @return policy list
*/
public List<Policy> getPolicies() {
return policyStore.values()
.stream()
.filter(policy -> policy instanceof TunnelPolicy)
.map(policy -> new TunnelPolicy((TunnelPolicy) policy))
.collect(Collectors.toList());
}
/**
* Creates a policy using the policy information given.
* @param policy policy reference to create
* @return ID_EXISTS if the same policy ID exists,
* POLICY_EXISTS if the same policy exists, TUNNEL_NOT_FOUND if the tunnel
* does not exists, UNSUPPORTED_TYPE if the policy type is not supported,
* SUCCESS if the policy is created successfully
*/
public Result createPolicy(Policy policy) {
if (policyStore.containsKey(policy.id())) {
log.warn("The policy id {} exists already", policy.id());
return Result.ID_EXISTS;
}
if (policyStore.containsValue(policy)) {
log.warn("The same policy exists already");
return Result.POLICY_EXISTS;
}
if (policy.type() == Policy.Type.TUNNEL_FLOW) {
TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
if (tunnel == null) {
return Result.TUNNEL_NOT_FOUND;
}
ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
.builder()
.fromApp(appId)
.makePermanent()
.nextStep(tunnel.groupId())
.withPriority(tunnelPolicy.priority())
.withSelector(buildSelector(policy))
.withFlag(ForwardingObjective.Flag.VERSATILE);
DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
flowObjectiveService.forward(source, fwdBuilder.add());
} else {
log.warn("Policy type {} is not supported yet.", policy.type());
return Result.UNSUPPORTED_TYPE;
}
policyStore.put(policy.id(), policy);
return Result.SUCCESS;
}
/**
* Removes the policy given.
*
* @param policyInfo policy information to remove
* @return POLICY_NOT_FOUND if the policy to remove does not exists,
* SUCCESS if it is removed successfully
*/
public Result removePolicy(Policy policyInfo) {
if (policyStore.get(policyInfo.id()) != null) {
Policy policy = policyStore.get(policyInfo.id());
if (policy.type() == Policy.Type.TUNNEL_FLOW) {
TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
.builder()
.fromApp(appId)
.makePermanent()
.withSelector(buildSelector(policy))
.withPriority(tunnelPolicy.priority())
.nextStep(tunnel.groupId())
.withFlag(ForwardingObjective.Flag.VERSATILE);
DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
flowObjectiveService.forward(source, fwdBuilder.remove());
policyStore.remove(policyInfo.id());
}
} else {
log.warn("Policy {} was not found", policyInfo.id());
return Result.POLICY_NOT_FOUND;
}
return Result.SUCCESS;
}
private TrafficSelector buildSelector(Policy policy) {
TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
tsb.matchEthType(Ethernet.TYPE_IPV4);
if (policy.dstIp() != null && !policy.dstIp().isEmpty()) {
tsb.matchIPDst(IpPrefix.valueOf(policy.dstIp()));
}
if (policy.srcIp() != null && !policy.srcIp().isEmpty()) {
tsb.matchIPSrc(IpPrefix.valueOf(policy.srcIp()));
}
if (policy.ipProto() != null && !policy.ipProto().isEmpty()) {
Short ipProto = IpProtocol.valueOf(policy.ipProto()).value();
tsb.matchIPProtocol(ipProto.byteValue());
if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) {
if (policy.srcPort() != 0) {
tsb.matchTcpSrc(TpPort.tpPort(policy.srcPort()));
}
if (policy.dstPort() != 0) {
tsb.matchTcpDst(TpPort.tpPort(policy.dstPort()));
}
} else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) {
if (policy.srcPort() != 0) {
tsb.matchUdpSrc(TpPort.tpPort(policy.srcPort()));
}
if (policy.dstPort() != 0) {
tsb.matchUdpDst(TpPort.tpPort(policy.dstPort()));
}
}
}
return tsb.build();
}
}