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