blob: 0fcfee463496fe6bb3158be7c1e764113aadfd33 [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;
21import org.onosproject.cli.net.IpProtocol;
sanghobd812f82015-06-29 14:58:47 -070022import org.onosproject.core.ApplicationId;
sangho4a5c42a2015-05-20 22:16:38 -070023import org.onosproject.net.DeviceId;
24import org.onosproject.net.flow.DefaultTrafficSelector;
25import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flowobjective.DefaultForwardingObjective;
sanghobd812f82015-06-29 14:58:47 -070027import org.onosproject.net.flowobjective.FlowObjectiveService;
sangho4a5c42a2015-05-20 22:16:38 -070028import org.onosproject.net.flowobjective.ForwardingObjective;
29import org.onosproject.store.service.EventuallyConsistentMap;
sangho27462c62015-05-14 00:39:53 -070030import org.slf4j.Logger;
31
32import java.util.ArrayList;
sangho27462c62015-05-14 00:39:53 -070033import java.util.List;
34
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Segment Routing Policy Handler.
39 */
40public class PolicyHandler {
41
42 protected final Logger log = getLogger(getClass());
43
sanghobd812f82015-06-29 14:58:47 -070044 private ApplicationId appId;
45 private DeviceConfiguration deviceConfiguration;
46 private FlowObjectiveService flowObjectiveService;
47 private TunnelHandler tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070048 private final EventuallyConsistentMap<String, Policy> policyStore;
sangho27462c62015-05-14 00:39:53 -070049
sanghobd812f82015-06-29 14:58:47 -070050 public enum Result {
51 SUCCESS,
52 POLICY_EXISTS,
53 ID_EXISTS,
54 TUNNEL_NOT_FOUND,
55 POLICY_NOT_FOUND,
56 UNSUPPORTED_TYPE
57 }
58
sangho27462c62015-05-14 00:39:53 -070059 /**
60 * Creates a reference.
Thomas Vachuskad62989f2015-05-21 16:41:41 -070061 *
sanghobd812f82015-06-29 14:58:47 -070062 * @param appId segment routing application ID
63 * @param deviceConfiguration DeviceConfiguration reference
64 * @param flowObjectiveService FlowObjectiveService reference
Thomas Vachuskad62989f2015-05-21 16:41:41 -070065 * @param policyStore policy store
sangho27462c62015-05-14 00:39:53 -070066 */
sanghobd812f82015-06-29 14:58:47 -070067 public PolicyHandler(ApplicationId appId,
68 DeviceConfiguration deviceConfiguration,
69 FlowObjectiveService flowObjectiveService,
70 TunnelHandler tunnelHandler,
sangho4a5c42a2015-05-20 22:16:38 -070071 EventuallyConsistentMap<String, Policy> policyStore) {
sanghobd812f82015-06-29 14:58:47 -070072 this.appId = appId;
73 this.deviceConfiguration = deviceConfiguration;
74 this.flowObjectiveService = flowObjectiveService;
75 this.tunnelHandler = tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070076 this.policyStore = policyStore;
sangho27462c62015-05-14 00:39:53 -070077 }
78
79 /**
80 * Returns the policies.
81 *
82 * @return policy list
83 */
84 public List<Policy> getPolicies() {
85 List<Policy> policies = new ArrayList<>();
sangho4a5c42a2015-05-20 22:16:38 -070086 policyStore.values().forEach(policy -> policies.add(
sangho27462c62015-05-14 00:39:53 -070087 new TunnelPolicy((TunnelPolicy) policy)));
88
89 return policies;
90 }
91
92 /**
93 * Creates a policy using the policy information given.
sanghobd812f82015-06-29 14:58:47 -070094 * @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
sangho27462c62015-05-14 00:39:53 -070099 */
sanghobd812f82015-06-29 14:58:47 -0700100 public Result createPolicy(Policy policy) {
sangho4a5c42a2015-05-20 22:16:38 -0700101
102 if (policyStore.containsKey(policy.id())) {
103 log.warn("The policy id {} exists already", policy.id());
sanghobd812f82015-06-29 14:58:47 -0700104 return Result.ID_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700105 }
106
107 if (policyStore.containsValue(policy)) {
108 log.warn("The same policy exists already");
sanghobd812f82015-06-29 14:58:47 -0700109 return Result.POLICY_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700110 }
111
112 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
113
114 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700115 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700116 if (tunnel == null) {
sanghobd812f82015-06-29 14:58:47 -0700117 return Result.TUNNEL_NOT_FOUND;
sangho4a5c42a2015-05-20 22:16:38 -0700118 }
119
120 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
121 .builder()
sanghobd812f82015-06-29 14:58:47 -0700122 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700123 .makePermanent()
124 .nextStep(tunnel.groupId())
125 .withPriority(tunnelPolicy.priority())
126 .withSelector(buildSelector(policy))
127 .withFlag(ForwardingObjective.Flag.VERSATILE);
128
sanghobd812f82015-06-29 14:58:47 -0700129 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
130 flowObjectiveService.forward(source, fwdBuilder.add());
sangho4a5c42a2015-05-20 22:16:38 -0700131
132 } else {
133 log.warn("Policy type {} is not supported yet.", policy.type());
sanghobd812f82015-06-29 14:58:47 -0700134 return Result.UNSUPPORTED_TYPE;
sangho4a5c42a2015-05-20 22:16:38 -0700135 }
136
137 policyStore.put(policy.id(), policy);
sanghobd812f82015-06-29 14:58:47 -0700138
139 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700140 }
141
142 /**
143 * Removes the policy given.
144 *
145 * @param policyInfo policy information to remove
sanghobd812f82015-06-29 14:58:47 -0700146 * @return POLICY_NOT_FOUND if the policy to remove does not exists,
147 * SUCCESS if it is removed successfully
sangho27462c62015-05-14 00:39:53 -0700148 */
sanghobd812f82015-06-29 14:58:47 -0700149 public Result removePolicy(Policy policyInfo) {
sangho4a5c42a2015-05-20 22:16:38 -0700150
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;
sanghobd812f82015-06-29 14:58:47 -0700155 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700156
157 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
158 .builder()
sanghobd812f82015-06-29 14:58:47 -0700159 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700160 .makePermanent()
161 .withSelector(buildSelector(policy))
162 .withPriority(tunnelPolicy.priority())
163 .nextStep(tunnel.groupId())
164 .withFlag(ForwardingObjective.Flag.VERSATILE);
165
sanghobd812f82015-06-29 14:58:47 -0700166 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
167 flowObjectiveService.forward(source, fwdBuilder.remove());
sangho4a5c42a2015-05-20 22:16:38 -0700168
169 policyStore.remove(policyInfo.id());
sangho27462c62015-05-14 00:39:53 -0700170 }
171 } else {
172 log.warn("Policy {} was not found", policyInfo.id());
sanghobd812f82015-06-29 14:58:47 -0700173 return Result.POLICY_NOT_FOUND;
sangho27462c62015-05-14 00:39:53 -0700174 }
sanghobd812f82015-06-29 14:58:47 -0700175
176 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700177 }
178
sangho4a5c42a2015-05-20 22:16:38 -0700179
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
sangho27462c62015-05-14 00:39:53 -0700213}