sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Ethernet; |
| 20 | import org.onlab.packet.IpPrefix; |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 21 | import org.onlab.packet.TpPort; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.net.IpProtocol; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 26 | import org.onosproject.net.flow.TrafficSelector; |
| 27 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 28 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 30 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 31 | import org.onosproject.store.service.EventuallyConsistentMap; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 34 | import java.util.List; |
Sho SHIMIZU | 594c267 | 2015-09-02 18:47:40 -0700 | [diff] [blame] | 35 | import java.util.stream.Collectors; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Segment Routing Policy Handler. |
| 41 | */ |
| 42 | public class PolicyHandler { |
| 43 | |
| 44 | protected final Logger log = getLogger(getClass()); |
| 45 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 46 | private ApplicationId appId; |
| 47 | private DeviceConfiguration deviceConfiguration; |
| 48 | private FlowObjectiveService flowObjectiveService; |
| 49 | private TunnelHandler tunnelHandler; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 50 | private final EventuallyConsistentMap<String, Policy> policyStore; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Result of policy creation. |
| 53 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 54 | public enum Result { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Success. |
| 57 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 58 | SUCCESS, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * The same policy exists already. |
| 62 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 63 | POLICY_EXISTS, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * The policy ID exists already. |
| 67 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 68 | ID_EXISTS, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Cannot find associated tunnel. |
| 72 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 73 | TUNNEL_NOT_FOUND, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Policy was not found. |
| 77 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 78 | POLICY_NOT_FOUND, |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Policy type {} is not supported yet. |
| 82 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 83 | UNSUPPORTED_TYPE |
| 84 | } |
| 85 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 86 | /** |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 87 | * Constructs policy handler. |
Thomas Vachuska | d62989f | 2015-05-21 16:41:41 -0700 | [diff] [blame] | 88 | * |
Thomas Vachuska | 6815424 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 89 | * @param appId segment routing application ID |
| 90 | * @param deviceConfiguration DeviceConfiguration reference |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 91 | * @param flowObjectiveService FlowObjectiveService reference |
Thomas Vachuska | 6815424 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 92 | * @param tunnelHandler tunnel handler reference |
| 93 | * @param policyStore policy store |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 94 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 95 | public PolicyHandler(ApplicationId appId, |
| 96 | DeviceConfiguration deviceConfiguration, |
| 97 | FlowObjectiveService flowObjectiveService, |
| 98 | TunnelHandler tunnelHandler, |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 99 | EventuallyConsistentMap<String, Policy> policyStore) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 100 | this.appId = appId; |
| 101 | this.deviceConfiguration = deviceConfiguration; |
| 102 | this.flowObjectiveService = flowObjectiveService; |
| 103 | this.tunnelHandler = tunnelHandler; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 104 | this.policyStore = policyStore; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the policies. |
| 109 | * |
| 110 | * @return policy list |
| 111 | */ |
| 112 | public List<Policy> getPolicies() { |
Sho SHIMIZU | 594c267 | 2015-09-02 18:47:40 -0700 | [diff] [blame] | 113 | return policyStore.values() |
| 114 | .stream() |
Sho SHIMIZU | 0b45463 | 2015-09-02 18:49:53 -0700 | [diff] [blame] | 115 | .filter(policy -> policy instanceof TunnelPolicy) |
Sho SHIMIZU | 594c267 | 2015-09-02 18:47:40 -0700 | [diff] [blame] | 116 | .map(policy -> new TunnelPolicy((TunnelPolicy) policy)) |
| 117 | .collect(Collectors.toList()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Creates a policy using the policy information given. |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 122 | * @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 |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 127 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 128 | public Result createPolicy(Policy policy) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 129 | |
| 130 | if (policyStore.containsKey(policy.id())) { |
| 131 | log.warn("The policy id {} exists already", policy.id()); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 132 | return Result.ID_EXISTS; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (policyStore.containsValue(policy)) { |
| 136 | log.warn("The same policy exists already"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 137 | return Result.POLICY_EXISTS; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 141 | |
| 142 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 143 | Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId()); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 144 | if (tunnel == null) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 145 | return Result.TUNNEL_NOT_FOUND; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 149 | .builder() |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 150 | .fromApp(appId) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 151 | .makePermanent() |
| 152 | .nextStep(tunnel.groupId()) |
| 153 | .withPriority(tunnelPolicy.priority()) |
| 154 | .withSelector(buildSelector(policy)) |
| 155 | .withFlag(ForwardingObjective.Flag.VERSATILE); |
| 156 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 157 | DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0)); |
| 158 | flowObjectiveService.forward(source, fwdBuilder.add()); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 159 | |
| 160 | } else { |
| 161 | log.warn("Policy type {} is not supported yet.", policy.type()); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 162 | return Result.UNSUPPORTED_TYPE; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | policyStore.put(policy.id(), policy); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 166 | |
| 167 | return Result.SUCCESS; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Removes the policy given. |
| 172 | * |
| 173 | * @param policyInfo policy information to remove |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 174 | * @return POLICY_NOT_FOUND if the policy to remove does not exists, |
| 175 | * SUCCESS if it is removed successfully |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 176 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 177 | public Result removePolicy(Policy policyInfo) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 178 | |
| 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; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 183 | Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId()); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 184 | |
| 185 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 186 | .builder() |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 187 | .fromApp(appId) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 188 | .makePermanent() |
| 189 | .withSelector(buildSelector(policy)) |
| 190 | .withPriority(tunnelPolicy.priority()) |
| 191 | .nextStep(tunnel.groupId()) |
| 192 | .withFlag(ForwardingObjective.Flag.VERSATILE); |
| 193 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 194 | DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0)); |
| 195 | flowObjectiveService.forward(source, fwdBuilder.remove()); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 196 | |
| 197 | policyStore.remove(policyInfo.id()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 198 | } |
| 199 | } else { |
| 200 | log.warn("Policy {} was not found", policyInfo.id()); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 201 | return Result.POLICY_NOT_FOUND; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 202 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 203 | |
| 204 | return Result.SUCCESS; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 205 | } |
| 206 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 207 | |
| 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 SHIMIZU | 43d842e | 2015-09-02 20:37:26 -0700 | [diff] [blame] | 219 | Short ipProto = IpProtocol.valueOf(policy.ipProto()).value(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 220 | tsb.matchIPProtocol(ipProto.byteValue()); |
| 221 | if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) { |
| 222 | if (policy.srcPort() != 0) { |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 223 | tsb.matchTcpSrc(TpPort.tpPort(policy.srcPort())); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 224 | } |
| 225 | if (policy.dstPort() != 0) { |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 226 | tsb.matchTcpDst(TpPort.tpPort(policy.dstPort())); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 227 | } |
| 228 | } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) { |
| 229 | if (policy.srcPort() != 0) { |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 230 | tsb.matchUdpSrc(TpPort.tpPort(policy.srcPort())); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 231 | } |
| 232 | if (policy.dstPort() != 0) { |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 233 | tsb.matchUdpDst(TpPort.tpPort(policy.dstPort())); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return tsb.build(); |
| 239 | } |
| 240 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 241 | } |