blob: 98b476b12f1252a2d192ccf863a02077189d22c4 [file] [log] [blame]
sangho27462c62015-05-14 00:39:53 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho27462c62015-05-14 00:39:53 -07003 *
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
sangho4a5c42a2015-05-20 22:16:38 -070019import org.onlab.packet.Ethernet;
20import org.onlab.packet.IpPrefix;
Hyunsun Moone11cdb92015-08-22 21:04:23 -070021import org.onlab.packet.TpPort;
sangho4a5c42a2015-05-20 22:16:38 -070022import org.onosproject.cli.net.IpProtocol;
sanghobd812f82015-06-29 14:58:47 -070023import org.onosproject.core.ApplicationId;
sangho4a5c42a2015-05-20 22:16:38 -070024import org.onosproject.net.DeviceId;
25import org.onosproject.net.flow.DefaultTrafficSelector;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flowobjective.DefaultForwardingObjective;
sanghobd812f82015-06-29 14:58:47 -070028import org.onosproject.net.flowobjective.FlowObjectiveService;
sangho4a5c42a2015-05-20 22:16:38 -070029import org.onosproject.net.flowobjective.ForwardingObjective;
30import org.onosproject.store.service.EventuallyConsistentMap;
sangho27462c62015-05-14 00:39:53 -070031import org.slf4j.Logger;
32
sangho27462c62015-05-14 00:39:53 -070033import java.util.List;
Sho SHIMIZU594c2672015-09-02 18:47:40 -070034import java.util.stream.Collectors;
sangho27462c62015-05-14 00:39:53 -070035
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Segment Routing Policy Handler.
40 */
41public class PolicyHandler {
42
43 protected final Logger log = getLogger(getClass());
44
sanghobd812f82015-06-29 14:58:47 -070045 private ApplicationId appId;
46 private DeviceConfiguration deviceConfiguration;
47 private FlowObjectiveService flowObjectiveService;
48 private TunnelHandler tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070049 private final EventuallyConsistentMap<String, Policy> policyStore;
Charles Chanb7f75ac2016-01-11 18:28:54 -080050 /**
51 * Result of policy creation.
52 */
sanghobd812f82015-06-29 14:58:47 -070053 public enum Result {
Charles Chanb7f75ac2016-01-11 18:28:54 -080054 /**
55 * Success.
56 */
sanghobd812f82015-06-29 14:58:47 -070057 SUCCESS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080058
59 /**
60 * The same policy exists already.
61 */
sanghobd812f82015-06-29 14:58:47 -070062 POLICY_EXISTS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080063
64 /**
65 * The policy ID exists already.
66 */
sanghobd812f82015-06-29 14:58:47 -070067 ID_EXISTS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080068
69 /**
70 * Cannot find associated tunnel.
71 */
sanghobd812f82015-06-29 14:58:47 -070072 TUNNEL_NOT_FOUND,
Charles Chanb7f75ac2016-01-11 18:28:54 -080073
74 /**
75 * Policy was not found.
76 */
sanghobd812f82015-06-29 14:58:47 -070077 POLICY_NOT_FOUND,
Charles Chanb7f75ac2016-01-11 18:28:54 -080078
79 /**
80 * Policy type {} is not supported yet.
81 */
sanghobd812f82015-06-29 14:58:47 -070082 UNSUPPORTED_TYPE
83 }
84
sangho27462c62015-05-14 00:39:53 -070085 /**
Charles Chanb7f75ac2016-01-11 18:28:54 -080086 * Constructs policy handler.
Thomas Vachuskad62989f2015-05-21 16:41:41 -070087 *
Thomas Vachuska68154242015-07-30 11:59:07 -070088 * @param appId segment routing application ID
89 * @param deviceConfiguration DeviceConfiguration reference
sanghobd812f82015-06-29 14:58:47 -070090 * @param flowObjectiveService FlowObjectiveService reference
Thomas Vachuska68154242015-07-30 11:59:07 -070091 * @param tunnelHandler tunnel handler reference
92 * @param policyStore policy store
sangho27462c62015-05-14 00:39:53 -070093 */
sanghobd812f82015-06-29 14:58:47 -070094 public PolicyHandler(ApplicationId appId,
95 DeviceConfiguration deviceConfiguration,
96 FlowObjectiveService flowObjectiveService,
97 TunnelHandler tunnelHandler,
sangho4a5c42a2015-05-20 22:16:38 -070098 EventuallyConsistentMap<String, Policy> policyStore) {
sanghobd812f82015-06-29 14:58:47 -070099 this.appId = appId;
100 this.deviceConfiguration = deviceConfiguration;
101 this.flowObjectiveService = flowObjectiveService;
102 this.tunnelHandler = tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -0700103 this.policyStore = policyStore;
sangho27462c62015-05-14 00:39:53 -0700104 }
105
106 /**
107 * Returns the policies.
108 *
109 * @return policy list
110 */
111 public List<Policy> getPolicies() {
Sho SHIMIZU594c2672015-09-02 18:47:40 -0700112 return policyStore.values()
113 .stream()
Sho SHIMIZU0b454632015-09-02 18:49:53 -0700114 .filter(policy -> policy instanceof TunnelPolicy)
Sho SHIMIZU594c2672015-09-02 18:47:40 -0700115 .map(policy -> new TunnelPolicy((TunnelPolicy) policy))
116 .collect(Collectors.toList());
sangho27462c62015-05-14 00:39:53 -0700117 }
118
119 /**
120 * Creates a policy using the policy information given.
sanghobd812f82015-06-29 14:58:47 -0700121 * @param policy policy reference to create
122 * @return ID_EXISTS if the same policy ID exists,
123 * POLICY_EXISTS if the same policy exists, TUNNEL_NOT_FOUND if the tunnel
124 * does not exists, UNSUPPORTED_TYPE if the policy type is not supported,
125 * SUCCESS if the policy is created successfully
sangho27462c62015-05-14 00:39:53 -0700126 */
sanghobd812f82015-06-29 14:58:47 -0700127 public Result createPolicy(Policy policy) {
sangho4a5c42a2015-05-20 22:16:38 -0700128
129 if (policyStore.containsKey(policy.id())) {
130 log.warn("The policy id {} exists already", policy.id());
sanghobd812f82015-06-29 14:58:47 -0700131 return Result.ID_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700132 }
133
134 if (policyStore.containsValue(policy)) {
135 log.warn("The same policy exists already");
sanghobd812f82015-06-29 14:58:47 -0700136 return Result.POLICY_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700137 }
138
139 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
140
141 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700142 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700143 if (tunnel == null) {
sanghobd812f82015-06-29 14:58:47 -0700144 return Result.TUNNEL_NOT_FOUND;
sangho4a5c42a2015-05-20 22:16:38 -0700145 }
146
147 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
148 .builder()
sanghobd812f82015-06-29 14:58:47 -0700149 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700150 .makePermanent()
151 .nextStep(tunnel.groupId())
152 .withPriority(tunnelPolicy.priority())
153 .withSelector(buildSelector(policy))
154 .withFlag(ForwardingObjective.Flag.VERSATILE);
155
sanghobd812f82015-06-29 14:58:47 -0700156 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
157 flowObjectiveService.forward(source, fwdBuilder.add());
sangho4a5c42a2015-05-20 22:16:38 -0700158
159 } else {
160 log.warn("Policy type {} is not supported yet.", policy.type());
sanghobd812f82015-06-29 14:58:47 -0700161 return Result.UNSUPPORTED_TYPE;
sangho4a5c42a2015-05-20 22:16:38 -0700162 }
163
164 policyStore.put(policy.id(), policy);
sanghobd812f82015-06-29 14:58:47 -0700165
166 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700167 }
168
169 /**
170 * Removes the policy given.
171 *
172 * @param policyInfo policy information to remove
sanghobd812f82015-06-29 14:58:47 -0700173 * @return POLICY_NOT_FOUND if the policy to remove does not exists,
174 * SUCCESS if it is removed successfully
sangho27462c62015-05-14 00:39:53 -0700175 */
sanghobd812f82015-06-29 14:58:47 -0700176 public Result removePolicy(Policy policyInfo) {
sangho4a5c42a2015-05-20 22:16:38 -0700177
178 if (policyStore.get(policyInfo.id()) != null) {
179 Policy policy = policyStore.get(policyInfo.id());
180 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
181 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700182 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700183
184 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
185 .builder()
sanghobd812f82015-06-29 14:58:47 -0700186 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700187 .makePermanent()
188 .withSelector(buildSelector(policy))
189 .withPriority(tunnelPolicy.priority())
190 .nextStep(tunnel.groupId())
191 .withFlag(ForwardingObjective.Flag.VERSATILE);
192
sanghobd812f82015-06-29 14:58:47 -0700193 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
194 flowObjectiveService.forward(source, fwdBuilder.remove());
sangho4a5c42a2015-05-20 22:16:38 -0700195
196 policyStore.remove(policyInfo.id());
sangho27462c62015-05-14 00:39:53 -0700197 }
198 } else {
199 log.warn("Policy {} was not found", policyInfo.id());
sanghobd812f82015-06-29 14:58:47 -0700200 return Result.POLICY_NOT_FOUND;
sangho27462c62015-05-14 00:39:53 -0700201 }
sanghobd812f82015-06-29 14:58:47 -0700202
203 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700204 }
205
sangho4a5c42a2015-05-20 22:16:38 -0700206
207 private TrafficSelector buildSelector(Policy policy) {
208
209 TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
210 tsb.matchEthType(Ethernet.TYPE_IPV4);
211 if (policy.dstIp() != null && !policy.dstIp().isEmpty()) {
212 tsb.matchIPDst(IpPrefix.valueOf(policy.dstIp()));
213 }
214 if (policy.srcIp() != null && !policy.srcIp().isEmpty()) {
215 tsb.matchIPSrc(IpPrefix.valueOf(policy.srcIp()));
216 }
217 if (policy.ipProto() != null && !policy.ipProto().isEmpty()) {
Sho SHIMIZU43d842e2015-09-02 20:37:26 -0700218 Short ipProto = IpProtocol.valueOf(policy.ipProto()).value();
sangho4a5c42a2015-05-20 22:16:38 -0700219 tsb.matchIPProtocol(ipProto.byteValue());
220 if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) {
221 if (policy.srcPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700222 tsb.matchTcpSrc(TpPort.tpPort(policy.srcPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700223 }
224 if (policy.dstPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700225 tsb.matchTcpDst(TpPort.tpPort(policy.dstPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700226 }
227 } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) {
228 if (policy.srcPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700229 tsb.matchUdpSrc(TpPort.tpPort(policy.srcPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700230 }
231 if (policy.dstPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700232 tsb.matchUdpDst(TpPort.tpPort(policy.dstPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700233 }
234 }
235 }
236
237 return tsb.build();
238 }
239
sangho27462c62015-05-14 00:39:53 -0700240}