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