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