blob: 9a2a8911a1b2302813888d12325421bbda25a51c [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 *
Thomas Vachuska68154242015-07-30 11:59:07 -070062 * @param appId segment routing application ID
63 * @param deviceConfiguration DeviceConfiguration reference
sanghobd812f82015-06-29 14:58:47 -070064 * @param flowObjectiveService FlowObjectiveService reference
Thomas Vachuska68154242015-07-30 11:59:07 -070065 * @param tunnelHandler tunnel handler reference
66 * @param policyStore policy store
sangho27462c62015-05-14 00:39:53 -070067 */
sanghobd812f82015-06-29 14:58:47 -070068 public PolicyHandler(ApplicationId appId,
69 DeviceConfiguration deviceConfiguration,
70 FlowObjectiveService flowObjectiveService,
71 TunnelHandler tunnelHandler,
sangho4a5c42a2015-05-20 22:16:38 -070072 EventuallyConsistentMap<String, Policy> policyStore) {
sanghobd812f82015-06-29 14:58:47 -070073 this.appId = appId;
74 this.deviceConfiguration = deviceConfiguration;
75 this.flowObjectiveService = flowObjectiveService;
76 this.tunnelHandler = tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070077 this.policyStore = policyStore;
sangho27462c62015-05-14 00:39:53 -070078 }
79
80 /**
81 * Returns the policies.
82 *
83 * @return policy list
84 */
85 public List<Policy> getPolicies() {
86 List<Policy> policies = new ArrayList<>();
sangho4a5c42a2015-05-20 22:16:38 -070087 policyStore.values().forEach(policy -> policies.add(
sangho27462c62015-05-14 00:39:53 -070088 new TunnelPolicy((TunnelPolicy) policy)));
89
90 return policies;
91 }
92
93 /**
94 * Creates a policy using the policy information given.
sanghobd812f82015-06-29 14:58:47 -070095 * @param policy policy reference to create
96 * @return ID_EXISTS if the same policy ID exists,
97 * POLICY_EXISTS if the same policy exists, TUNNEL_NOT_FOUND if the tunnel
98 * does not exists, UNSUPPORTED_TYPE if the policy type is not supported,
99 * SUCCESS if the policy is created successfully
sangho27462c62015-05-14 00:39:53 -0700100 */
sanghobd812f82015-06-29 14:58:47 -0700101 public Result createPolicy(Policy policy) {
sangho4a5c42a2015-05-20 22:16:38 -0700102
103 if (policyStore.containsKey(policy.id())) {
104 log.warn("The policy id {} exists already", policy.id());
sanghobd812f82015-06-29 14:58:47 -0700105 return Result.ID_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700106 }
107
108 if (policyStore.containsValue(policy)) {
109 log.warn("The same policy exists already");
sanghobd812f82015-06-29 14:58:47 -0700110 return Result.POLICY_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700111 }
112
113 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
114
115 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700116 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700117 if (tunnel == null) {
sanghobd812f82015-06-29 14:58:47 -0700118 return Result.TUNNEL_NOT_FOUND;
sangho4a5c42a2015-05-20 22:16:38 -0700119 }
120
121 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
122 .builder()
sanghobd812f82015-06-29 14:58:47 -0700123 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700124 .makePermanent()
125 .nextStep(tunnel.groupId())
126 .withPriority(tunnelPolicy.priority())
127 .withSelector(buildSelector(policy))
128 .withFlag(ForwardingObjective.Flag.VERSATILE);
129
sanghobd812f82015-06-29 14:58:47 -0700130 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
131 flowObjectiveService.forward(source, fwdBuilder.add());
sangho4a5c42a2015-05-20 22:16:38 -0700132
133 } else {
134 log.warn("Policy type {} is not supported yet.", policy.type());
sanghobd812f82015-06-29 14:58:47 -0700135 return Result.UNSUPPORTED_TYPE;
sangho4a5c42a2015-05-20 22:16:38 -0700136 }
137
138 policyStore.put(policy.id(), policy);
sanghobd812f82015-06-29 14:58:47 -0700139
140 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700141 }
142
143 /**
144 * Removes the policy given.
145 *
146 * @param policyInfo policy information to remove
sanghobd812f82015-06-29 14:58:47 -0700147 * @return POLICY_NOT_FOUND if the policy to remove does not exists,
148 * SUCCESS if it is removed successfully
sangho27462c62015-05-14 00:39:53 -0700149 */
sanghobd812f82015-06-29 14:58:47 -0700150 public Result removePolicy(Policy policyInfo) {
sangho4a5c42a2015-05-20 22:16:38 -0700151
152 if (policyStore.get(policyInfo.id()) != null) {
153 Policy policy = policyStore.get(policyInfo.id());
154 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
155 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700156 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700157
158 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
159 .builder()
sanghobd812f82015-06-29 14:58:47 -0700160 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700161 .makePermanent()
162 .withSelector(buildSelector(policy))
163 .withPriority(tunnelPolicy.priority())
164 .nextStep(tunnel.groupId())
165 .withFlag(ForwardingObjective.Flag.VERSATILE);
166
sanghobd812f82015-06-29 14:58:47 -0700167 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
168 flowObjectiveService.forward(source, fwdBuilder.remove());
sangho4a5c42a2015-05-20 22:16:38 -0700169
170 policyStore.remove(policyInfo.id());
sangho27462c62015-05-14 00:39:53 -0700171 }
172 } else {
173 log.warn("Policy {} was not found", policyInfo.id());
sanghobd812f82015-06-29 14:58:47 -0700174 return Result.POLICY_NOT_FOUND;
sangho27462c62015-05-14 00:39:53 -0700175 }
sanghobd812f82015-06-29 14:58:47 -0700176
177 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700178 }
179
sangho4a5c42a2015-05-20 22:16:38 -0700180
181 private TrafficSelector buildSelector(Policy policy) {
182
183 TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
184 tsb.matchEthType(Ethernet.TYPE_IPV4);
185 if (policy.dstIp() != null && !policy.dstIp().isEmpty()) {
186 tsb.matchIPDst(IpPrefix.valueOf(policy.dstIp()));
187 }
188 if (policy.srcIp() != null && !policy.srcIp().isEmpty()) {
189 tsb.matchIPSrc(IpPrefix.valueOf(policy.srcIp()));
190 }
191 if (policy.ipProto() != null && !policy.ipProto().isEmpty()) {
192 Short ipProto = Short.valueOf(IpProtocol.valueOf(policy.ipProto()).value());
193 tsb.matchIPProtocol(ipProto.byteValue());
194 if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) {
195 if (policy.srcPort() != 0) {
196 tsb.matchTcpSrc(policy.srcPort());
197 }
198 if (policy.dstPort() != 0) {
199 tsb.matchTcpDst(policy.dstPort());
200 }
201 } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) {
202 if (policy.srcPort() != 0) {
203 tsb.matchUdpSrc(policy.srcPort());
204 }
205 if (policy.dstPort() != 0) {
206 tsb.matchUdpDst(policy.dstPort());
207 }
208 }
209 }
210
211 return tsb.build();
212 }
213
sangho27462c62015-05-14 00:39:53 -0700214}