blob: e773e10bd943343ac721b7a3dd3b7e8d997fdb5e [file] [log] [blame]
sangho27462c62015-05-14 00:39:53 -07001/*
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
17package org.onosproject.segmentrouting;
18
sangho4a5c42a2015-05-20 22:16:38 -070019import org.onlab.packet.Ethernet;
20import org.onlab.packet.IpPrefix;
Hyunsun Moone11cdb92015-08-22 21:04:23 -070021import org.onlab.packet.TpPort;
sangho4a5c42a2015-05-20 22:16:38 -070022import org.onosproject.cli.net.IpProtocol;
sanghobd812f82015-06-29 14:58:47 -070023import org.onosproject.core.ApplicationId;
sangho4a5c42a2015-05-20 22:16:38 -070024import org.onosproject.net.DeviceId;
25import org.onosproject.net.flow.DefaultTrafficSelector;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flowobjective.DefaultForwardingObjective;
sanghobd812f82015-06-29 14:58:47 -070028import org.onosproject.net.flowobjective.FlowObjectiveService;
sangho4a5c42a2015-05-20 22:16:38 -070029import org.onosproject.net.flowobjective.ForwardingObjective;
30import org.onosproject.store.service.EventuallyConsistentMap;
sangho27462c62015-05-14 00:39:53 -070031import org.slf4j.Logger;
32
33import java.util.ArrayList;
sangho27462c62015-05-14 00:39:53 -070034import java.util.List;
35
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Segment Routing Policy Handler.
40 */
41public class PolicyHandler {
42
43 protected final Logger log = getLogger(getClass());
44
sanghobd812f82015-06-29 14:58:47 -070045 private ApplicationId appId;
46 private DeviceConfiguration deviceConfiguration;
47 private FlowObjectiveService flowObjectiveService;
48 private TunnelHandler tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070049 private final EventuallyConsistentMap<String, Policy> policyStore;
sangho27462c62015-05-14 00:39:53 -070050
sanghobd812f82015-06-29 14:58:47 -070051 public enum Result {
52 SUCCESS,
53 POLICY_EXISTS,
54 ID_EXISTS,
55 TUNNEL_NOT_FOUND,
56 POLICY_NOT_FOUND,
57 UNSUPPORTED_TYPE
58 }
59
sangho27462c62015-05-14 00:39:53 -070060 /**
61 * Creates a reference.
Thomas Vachuskad62989f2015-05-21 16:41:41 -070062 *
Thomas Vachuska68154242015-07-30 11:59:07 -070063 * @param appId segment routing application ID
64 * @param deviceConfiguration DeviceConfiguration reference
sanghobd812f82015-06-29 14:58:47 -070065 * @param flowObjectiveService FlowObjectiveService reference
Thomas Vachuska68154242015-07-30 11:59:07 -070066 * @param tunnelHandler tunnel handler reference
67 * @param policyStore policy store
sangho27462c62015-05-14 00:39:53 -070068 */
sanghobd812f82015-06-29 14:58:47 -070069 public PolicyHandler(ApplicationId appId,
70 DeviceConfiguration deviceConfiguration,
71 FlowObjectiveService flowObjectiveService,
72 TunnelHandler tunnelHandler,
sangho4a5c42a2015-05-20 22:16:38 -070073 EventuallyConsistentMap<String, Policy> policyStore) {
sanghobd812f82015-06-29 14:58:47 -070074 this.appId = appId;
75 this.deviceConfiguration = deviceConfiguration;
76 this.flowObjectiveService = flowObjectiveService;
77 this.tunnelHandler = tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070078 this.policyStore = policyStore;
sangho27462c62015-05-14 00:39:53 -070079 }
80
81 /**
82 * Returns the policies.
83 *
84 * @return policy list
85 */
86 public List<Policy> getPolicies() {
87 List<Policy> policies = new ArrayList<>();
sangho4a5c42a2015-05-20 22:16:38 -070088 policyStore.values().forEach(policy -> policies.add(
sangho27462c62015-05-14 00:39:53 -070089 new TunnelPolicy((TunnelPolicy) policy)));
90
91 return policies;
92 }
93
94 /**
95 * Creates a policy using the policy information given.
sanghobd812f82015-06-29 14:58:47 -070096 * @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
sangho27462c62015-05-14 00:39:53 -0700101 */
sanghobd812f82015-06-29 14:58:47 -0700102 public Result createPolicy(Policy policy) {
sangho4a5c42a2015-05-20 22:16:38 -0700103
104 if (policyStore.containsKey(policy.id())) {
105 log.warn("The policy id {} exists already", policy.id());
sanghobd812f82015-06-29 14:58:47 -0700106 return Result.ID_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700107 }
108
109 if (policyStore.containsValue(policy)) {
110 log.warn("The same policy exists already");
sanghobd812f82015-06-29 14:58:47 -0700111 return Result.POLICY_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700112 }
113
114 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
115
116 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700117 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700118 if (tunnel == null) {
sanghobd812f82015-06-29 14:58:47 -0700119 return Result.TUNNEL_NOT_FOUND;
sangho4a5c42a2015-05-20 22:16:38 -0700120 }
121
122 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
123 .builder()
sanghobd812f82015-06-29 14:58:47 -0700124 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700125 .makePermanent()
126 .nextStep(tunnel.groupId())
127 .withPriority(tunnelPolicy.priority())
128 .withSelector(buildSelector(policy))
129 .withFlag(ForwardingObjective.Flag.VERSATILE);
130
sanghobd812f82015-06-29 14:58:47 -0700131 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
132 flowObjectiveService.forward(source, fwdBuilder.add());
sangho4a5c42a2015-05-20 22:16:38 -0700133
134 } else {
135 log.warn("Policy type {} is not supported yet.", policy.type());
sanghobd812f82015-06-29 14:58:47 -0700136 return Result.UNSUPPORTED_TYPE;
sangho4a5c42a2015-05-20 22:16:38 -0700137 }
138
139 policyStore.put(policy.id(), policy);
sanghobd812f82015-06-29 14:58:47 -0700140
141 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700142 }
143
144 /**
145 * Removes the policy given.
146 *
147 * @param policyInfo policy information to remove
sanghobd812f82015-06-29 14:58:47 -0700148 * @return POLICY_NOT_FOUND if the policy to remove does not exists,
149 * SUCCESS if it is removed successfully
sangho27462c62015-05-14 00:39:53 -0700150 */
sanghobd812f82015-06-29 14:58:47 -0700151 public Result removePolicy(Policy policyInfo) {
sangho4a5c42a2015-05-20 22:16:38 -0700152
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;
sanghobd812f82015-06-29 14:58:47 -0700157 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700158
159 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
160 .builder()
sanghobd812f82015-06-29 14:58:47 -0700161 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700162 .makePermanent()
163 .withSelector(buildSelector(policy))
164 .withPriority(tunnelPolicy.priority())
165 .nextStep(tunnel.groupId())
166 .withFlag(ForwardingObjective.Flag.VERSATILE);
167
sanghobd812f82015-06-29 14:58:47 -0700168 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
169 flowObjectiveService.forward(source, fwdBuilder.remove());
sangho4a5c42a2015-05-20 22:16:38 -0700170
171 policyStore.remove(policyInfo.id());
sangho27462c62015-05-14 00:39:53 -0700172 }
173 } else {
174 log.warn("Policy {} was not found", policyInfo.id());
sanghobd812f82015-06-29 14:58:47 -0700175 return Result.POLICY_NOT_FOUND;
sangho27462c62015-05-14 00:39:53 -0700176 }
sanghobd812f82015-06-29 14:58:47 -0700177
178 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700179 }
180
sangho4a5c42a2015-05-20 22:16:38 -0700181
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 Moone11cdb92015-08-22 21:04:23 -0700197 tsb.matchTcpSrc(TpPort.tpPort(policy.srcPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700198 }
199 if (policy.dstPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700200 tsb.matchTcpDst(TpPort.tpPort(policy.dstPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700201 }
202 } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) {
203 if (policy.srcPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700204 tsb.matchUdpSrc(TpPort.tpPort(policy.srcPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700205 }
206 if (policy.dstPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700207 tsb.matchUdpDst(TpPort.tpPort(policy.dstPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700208 }
209 }
210 }
211
212 return tsb.build();
213 }
214
sangho27462c62015-05-14 00:39:53 -0700215}