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