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