blob: a341bb111cc8c2c76b3a115c35567bc142aa605a [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;
Charles Chan319d1a22015-11-03 10:42:14 -080030import org.onosproject.segmentrouting.config.DeviceConfiguration;
sangho4a5c42a2015-05-20 22:16:38 -070031import org.onosproject.store.service.EventuallyConsistentMap;
sangho27462c62015-05-14 00:39:53 -070032import org.slf4j.Logger;
33
sangho27462c62015-05-14 00:39:53 -070034import java.util.List;
Sho SHIMIZU594c2672015-09-02 18:47:40 -070035import java.util.stream.Collectors;
sangho27462c62015-05-14 00:39:53 -070036
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Segment Routing Policy Handler.
41 */
42public class PolicyHandler {
43
44 protected final Logger log = getLogger(getClass());
45
sanghobd812f82015-06-29 14:58:47 -070046 private ApplicationId appId;
47 private DeviceConfiguration deviceConfiguration;
48 private FlowObjectiveService flowObjectiveService;
49 private TunnelHandler tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070050 private final EventuallyConsistentMap<String, Policy> policyStore;
Charles Chanb7f75ac2016-01-11 18:28:54 -080051 /**
52 * Result of policy creation.
53 */
sanghobd812f82015-06-29 14:58:47 -070054 public enum Result {
Charles Chanb7f75ac2016-01-11 18:28:54 -080055 /**
56 * Success.
57 */
sanghobd812f82015-06-29 14:58:47 -070058 SUCCESS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080059
60 /**
61 * The same policy exists already.
62 */
sanghobd812f82015-06-29 14:58:47 -070063 POLICY_EXISTS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080064
65 /**
66 * The policy ID exists already.
67 */
sanghobd812f82015-06-29 14:58:47 -070068 ID_EXISTS,
Charles Chanb7f75ac2016-01-11 18:28:54 -080069
70 /**
71 * Cannot find associated tunnel.
72 */
sanghobd812f82015-06-29 14:58:47 -070073 TUNNEL_NOT_FOUND,
Charles Chanb7f75ac2016-01-11 18:28:54 -080074
75 /**
76 * Policy was not found.
77 */
sanghobd812f82015-06-29 14:58:47 -070078 POLICY_NOT_FOUND,
Charles Chanb7f75ac2016-01-11 18:28:54 -080079
80 /**
81 * Policy type {} is not supported yet.
82 */
sanghobd812f82015-06-29 14:58:47 -070083 UNSUPPORTED_TYPE
84 }
85
sangho27462c62015-05-14 00:39:53 -070086 /**
Charles Chanb7f75ac2016-01-11 18:28:54 -080087 * Constructs policy handler.
Thomas Vachuskad62989f2015-05-21 16:41:41 -070088 *
Thomas Vachuska68154242015-07-30 11:59:07 -070089 * @param appId segment routing application ID
90 * @param deviceConfiguration DeviceConfiguration reference
sanghobd812f82015-06-29 14:58:47 -070091 * @param flowObjectiveService FlowObjectiveService reference
Thomas Vachuska68154242015-07-30 11:59:07 -070092 * @param tunnelHandler tunnel handler reference
93 * @param policyStore policy store
sangho27462c62015-05-14 00:39:53 -070094 */
sanghobd812f82015-06-29 14:58:47 -070095 public PolicyHandler(ApplicationId appId,
96 DeviceConfiguration deviceConfiguration,
97 FlowObjectiveService flowObjectiveService,
98 TunnelHandler tunnelHandler,
sangho4a5c42a2015-05-20 22:16:38 -070099 EventuallyConsistentMap<String, Policy> policyStore) {
sanghobd812f82015-06-29 14:58:47 -0700100 this.appId = appId;
101 this.deviceConfiguration = deviceConfiguration;
102 this.flowObjectiveService = flowObjectiveService;
103 this.tunnelHandler = tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -0700104 this.policyStore = policyStore;
sangho27462c62015-05-14 00:39:53 -0700105 }
106
107 /**
108 * Returns the policies.
109 *
110 * @return policy list
111 */
112 public List<Policy> getPolicies() {
Sho SHIMIZU594c2672015-09-02 18:47:40 -0700113 return policyStore.values()
114 .stream()
Sho SHIMIZU0b454632015-09-02 18:49:53 -0700115 .filter(policy -> policy instanceof TunnelPolicy)
Sho SHIMIZU594c2672015-09-02 18:47:40 -0700116 .map(policy -> new TunnelPolicy((TunnelPolicy) policy))
117 .collect(Collectors.toList());
sangho27462c62015-05-14 00:39:53 -0700118 }
119
120 /**
121 * Creates a policy using the policy information given.
sanghobd812f82015-06-29 14:58:47 -0700122 * @param policy policy reference to create
123 * @return ID_EXISTS if the same policy ID exists,
124 * POLICY_EXISTS if the same policy exists, TUNNEL_NOT_FOUND if the tunnel
125 * does not exists, UNSUPPORTED_TYPE if the policy type is not supported,
126 * SUCCESS if the policy is created successfully
sangho27462c62015-05-14 00:39:53 -0700127 */
sanghobd812f82015-06-29 14:58:47 -0700128 public Result createPolicy(Policy policy) {
sangho4a5c42a2015-05-20 22:16:38 -0700129
130 if (policyStore.containsKey(policy.id())) {
131 log.warn("The policy id {} exists already", policy.id());
sanghobd812f82015-06-29 14:58:47 -0700132 return Result.ID_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700133 }
134
135 if (policyStore.containsValue(policy)) {
136 log.warn("The same policy exists already");
sanghobd812f82015-06-29 14:58:47 -0700137 return Result.POLICY_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700138 }
139
140 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
141
142 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700143 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700144 if (tunnel == null) {
sanghobd812f82015-06-29 14:58:47 -0700145 return Result.TUNNEL_NOT_FOUND;
sangho4a5c42a2015-05-20 22:16:38 -0700146 }
147
148 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
149 .builder()
sanghobd812f82015-06-29 14:58:47 -0700150 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700151 .makePermanent()
152 .nextStep(tunnel.groupId())
153 .withPriority(tunnelPolicy.priority())
154 .withSelector(buildSelector(policy))
155 .withFlag(ForwardingObjective.Flag.VERSATILE);
156
sanghobd812f82015-06-29 14:58:47 -0700157 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
158 flowObjectiveService.forward(source, fwdBuilder.add());
sangho4a5c42a2015-05-20 22:16:38 -0700159
160 } else {
161 log.warn("Policy type {} is not supported yet.", policy.type());
sanghobd812f82015-06-29 14:58:47 -0700162 return Result.UNSUPPORTED_TYPE;
sangho4a5c42a2015-05-20 22:16:38 -0700163 }
164
165 policyStore.put(policy.id(), policy);
sanghobd812f82015-06-29 14:58:47 -0700166
167 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700168 }
169
170 /**
171 * Removes the policy given.
172 *
173 * @param policyInfo policy information to remove
sanghobd812f82015-06-29 14:58:47 -0700174 * @return POLICY_NOT_FOUND if the policy to remove does not exists,
175 * SUCCESS if it is removed successfully
sangho27462c62015-05-14 00:39:53 -0700176 */
sanghobd812f82015-06-29 14:58:47 -0700177 public Result removePolicy(Policy policyInfo) {
sangho4a5c42a2015-05-20 22:16:38 -0700178
179 if (policyStore.get(policyInfo.id()) != null) {
180 Policy policy = policyStore.get(policyInfo.id());
181 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
182 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700183 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700184
185 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
186 .builder()
sanghobd812f82015-06-29 14:58:47 -0700187 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700188 .makePermanent()
189 .withSelector(buildSelector(policy))
190 .withPriority(tunnelPolicy.priority())
191 .nextStep(tunnel.groupId())
192 .withFlag(ForwardingObjective.Flag.VERSATILE);
193
sanghobd812f82015-06-29 14:58:47 -0700194 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
195 flowObjectiveService.forward(source, fwdBuilder.remove());
sangho4a5c42a2015-05-20 22:16:38 -0700196
197 policyStore.remove(policyInfo.id());
sangho27462c62015-05-14 00:39:53 -0700198 }
199 } else {
200 log.warn("Policy {} was not found", policyInfo.id());
sanghobd812f82015-06-29 14:58:47 -0700201 return Result.POLICY_NOT_FOUND;
sangho27462c62015-05-14 00:39:53 -0700202 }
sanghobd812f82015-06-29 14:58:47 -0700203
204 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700205 }
206
sangho4a5c42a2015-05-20 22:16:38 -0700207
208 private TrafficSelector buildSelector(Policy policy) {
209
210 TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
211 tsb.matchEthType(Ethernet.TYPE_IPV4);
212 if (policy.dstIp() != null && !policy.dstIp().isEmpty()) {
213 tsb.matchIPDst(IpPrefix.valueOf(policy.dstIp()));
214 }
215 if (policy.srcIp() != null && !policy.srcIp().isEmpty()) {
216 tsb.matchIPSrc(IpPrefix.valueOf(policy.srcIp()));
217 }
218 if (policy.ipProto() != null && !policy.ipProto().isEmpty()) {
Sho SHIMIZU43d842e2015-09-02 20:37:26 -0700219 Short ipProto = IpProtocol.valueOf(policy.ipProto()).value();
sangho4a5c42a2015-05-20 22:16:38 -0700220 tsb.matchIPProtocol(ipProto.byteValue());
221 if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) {
222 if (policy.srcPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700223 tsb.matchTcpSrc(TpPort.tpPort(policy.srcPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700224 }
225 if (policy.dstPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700226 tsb.matchTcpDst(TpPort.tpPort(policy.dstPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700227 }
228 } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) {
229 if (policy.srcPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700230 tsb.matchUdpSrc(TpPort.tpPort(policy.srcPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700231 }
232 if (policy.dstPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700233 tsb.matchUdpDst(TpPort.tpPort(policy.dstPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700234 }
235 }
236 }
237
238 return tsb.build();
239 }
240
sangho27462c62015-05-14 00:39:53 -0700241}