sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 33 | import java.util.ArrayList; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 34 | import java.util.List; |
| 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; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 50 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 51 | public enum Result { |
| 52 | SUCCESS, |
| 53 | POLICY_EXISTS, |
| 54 | ID_EXISTS, |
| 55 | TUNNEL_NOT_FOUND, |
| 56 | POLICY_NOT_FOUND, |
| 57 | UNSUPPORTED_TYPE |
| 58 | } |
| 59 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Creates a reference. |
Thomas Vachuska | d62989f | 2015-05-21 16:41:41 -0700 | [diff] [blame] | 62 | * |
Thomas Vachuska | 6815424 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 63 | * @param appId segment routing application ID |
| 64 | * @param deviceConfiguration DeviceConfiguration reference |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 65 | * @param flowObjectiveService FlowObjectiveService reference |
Thomas Vachuska | 6815424 | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 66 | * @param tunnelHandler tunnel handler reference |
| 67 | * @param policyStore policy store |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 68 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 69 | public PolicyHandler(ApplicationId appId, |
| 70 | DeviceConfiguration deviceConfiguration, |
| 71 | FlowObjectiveService flowObjectiveService, |
| 72 | TunnelHandler tunnelHandler, |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 73 | EventuallyConsistentMap<String, Policy> policyStore) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 74 | this.appId = appId; |
| 75 | this.deviceConfiguration = deviceConfiguration; |
| 76 | this.flowObjectiveService = flowObjectiveService; |
| 77 | this.tunnelHandler = tunnelHandler; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 78 | this.policyStore = policyStore; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the policies. |
| 83 | * |
| 84 | * @return policy list |
| 85 | */ |
| 86 | public List<Policy> getPolicies() { |
| 87 | List<Policy> policies = new ArrayList<>(); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 88 | policyStore.values().forEach(policy -> policies.add( |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 89 | new TunnelPolicy((TunnelPolicy) policy))); |
| 90 | |
| 91 | return policies; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates a policy using the policy information given. |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 96 | * @param policy policy reference to create |
| 97 | * @return ID_EXISTS if the same policy ID exists, |
| 98 | * POLICY_EXISTS if the same policy exists, TUNNEL_NOT_FOUND if the tunnel |
| 99 | * does not exists, UNSUPPORTED_TYPE if the policy type is not supported, |
| 100 | * SUCCESS if the policy is created successfully |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 101 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 102 | public Result createPolicy(Policy policy) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 103 | |
| 104 | if (policyStore.containsKey(policy.id())) { |
| 105 | log.warn("The policy id {} exists already", policy.id()); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 106 | return Result.ID_EXISTS; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (policyStore.containsValue(policy)) { |
| 110 | log.warn("The same policy exists already"); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 111 | return Result.POLICY_EXISTS; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 115 | |
| 116 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 117 | Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId()); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 118 | if (tunnel == null) { |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 119 | return Result.TUNNEL_NOT_FOUND; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 123 | .builder() |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 124 | .fromApp(appId) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 125 | .makePermanent() |
| 126 | .nextStep(tunnel.groupId()) |
| 127 | .withPriority(tunnelPolicy.priority()) |
| 128 | .withSelector(buildSelector(policy)) |
| 129 | .withFlag(ForwardingObjective.Flag.VERSATILE); |
| 130 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 131 | DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0)); |
| 132 | flowObjectiveService.forward(source, fwdBuilder.add()); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 133 | |
| 134 | } else { |
| 135 | log.warn("Policy type {} is not supported yet.", policy.type()); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 136 | return Result.UNSUPPORTED_TYPE; |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | policyStore.put(policy.id(), policy); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 140 | |
| 141 | return Result.SUCCESS; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Removes the policy given. |
| 146 | * |
| 147 | * @param policyInfo policy information to remove |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 148 | * @return POLICY_NOT_FOUND if the policy to remove does not exists, |
| 149 | * SUCCESS if it is removed successfully |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 150 | */ |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 151 | public Result removePolicy(Policy policyInfo) { |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 152 | |
| 153 | if (policyStore.get(policyInfo.id()) != null) { |
| 154 | Policy policy = policyStore.get(policyInfo.id()); |
| 155 | if (policy.type() == Policy.Type.TUNNEL_FLOW) { |
| 156 | TunnelPolicy tunnelPolicy = (TunnelPolicy) policy; |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 157 | Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId()); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 158 | |
| 159 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective |
| 160 | .builder() |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 161 | .fromApp(appId) |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 162 | .makePermanent() |
| 163 | .withSelector(buildSelector(policy)) |
| 164 | .withPriority(tunnelPolicy.priority()) |
| 165 | .nextStep(tunnel.groupId()) |
| 166 | .withFlag(ForwardingObjective.Flag.VERSATILE); |
| 167 | |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 168 | DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0)); |
| 169 | flowObjectiveService.forward(source, fwdBuilder.remove()); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 170 | |
| 171 | policyStore.remove(policyInfo.id()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 172 | } |
| 173 | } else { |
| 174 | log.warn("Policy {} was not found", policyInfo.id()); |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 175 | return Result.POLICY_NOT_FOUND; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 176 | } |
sangho | bd812f8 | 2015-06-29 14:58:47 -0700 | [diff] [blame] | 177 | |
| 178 | return Result.SUCCESS; |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 179 | } |
| 180 | |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 181 | |
| 182 | private TrafficSelector buildSelector(Policy policy) { |
| 183 | |
| 184 | TrafficSelector.Builder tsb = DefaultTrafficSelector.builder(); |
| 185 | tsb.matchEthType(Ethernet.TYPE_IPV4); |
| 186 | if (policy.dstIp() != null && !policy.dstIp().isEmpty()) { |
| 187 | tsb.matchIPDst(IpPrefix.valueOf(policy.dstIp())); |
| 188 | } |
| 189 | if (policy.srcIp() != null && !policy.srcIp().isEmpty()) { |
| 190 | tsb.matchIPSrc(IpPrefix.valueOf(policy.srcIp())); |
| 191 | } |
| 192 | if (policy.ipProto() != null && !policy.ipProto().isEmpty()) { |
| 193 | Short ipProto = Short.valueOf(IpProtocol.valueOf(policy.ipProto()).value()); |
| 194 | tsb.matchIPProtocol(ipProto.byteValue()); |
| 195 | if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) { |
| 196 | if (policy.srcPort() != 0) { |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame^] | 197 | tsb.matchTcpSrc(TpPort.tpPort(policy.srcPort())); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 198 | } |
| 199 | if (policy.dstPort() != 0) { |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame^] | 200 | tsb.matchTcpDst(TpPort.tpPort(policy.dstPort())); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 201 | } |
| 202 | } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) { |
| 203 | if (policy.srcPort() != 0) { |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame^] | 204 | tsb.matchUdpSrc(TpPort.tpPort(policy.srcPort())); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 205 | } |
| 206 | if (policy.dstPort() != 0) { |
Hyunsun Moon | e11cdb9 | 2015-08-22 21:04:23 -0700 | [diff] [blame^] | 207 | tsb.matchUdpDst(TpPort.tpPort(policy.dstPort())); |
sangho | 4a5c42a | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return tsb.build(); |
| 213 | } |
| 214 | |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 215 | } |