blob: 0a4c26d9032b5adf459aab2a93218a99b8a4edeb [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;
Charles Chan319d1a22015-11-03 10:42:14 -080030import org.onosproject.segmentrouting.config.DeviceConfiguration;
sangho4a5c42a2015-05-20 22:16:38 -070031import org.onosproject.store.service.EventuallyConsistentMap;
sangho27462c62015-05-14 00:39:53 -070032import org.slf4j.Logger;
33
sangho27462c62015-05-14 00:39:53 -070034import java.util.List;
Sho SHIMIZU594c2672015-09-02 18:47:40 -070035import java.util.stream.Collectors;
sangho27462c62015-05-14 00:39:53 -070036
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Segment Routing Policy Handler.
41 */
42public class PolicyHandler {
43
44 protected final Logger log = getLogger(getClass());
45
sanghobd812f82015-06-29 14:58:47 -070046 private ApplicationId appId;
47 private DeviceConfiguration deviceConfiguration;
48 private FlowObjectiveService flowObjectiveService;
49 private TunnelHandler tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070050 private final EventuallyConsistentMap<String, Policy> policyStore;
sangho27462c62015-05-14 00:39:53 -070051
sanghobd812f82015-06-29 14:58:47 -070052 public enum Result {
53 SUCCESS,
54 POLICY_EXISTS,
55 ID_EXISTS,
56 TUNNEL_NOT_FOUND,
57 POLICY_NOT_FOUND,
58 UNSUPPORTED_TYPE
59 }
60
sangho27462c62015-05-14 00:39:53 -070061 /**
62 * Creates a reference.
Thomas Vachuskad62989f2015-05-21 16:41:41 -070063 *
Thomas Vachuska68154242015-07-30 11:59:07 -070064 * @param appId segment routing application ID
65 * @param deviceConfiguration DeviceConfiguration reference
sanghobd812f82015-06-29 14:58:47 -070066 * @param flowObjectiveService FlowObjectiveService reference
Thomas Vachuska68154242015-07-30 11:59:07 -070067 * @param tunnelHandler tunnel handler reference
68 * @param policyStore policy store
sangho27462c62015-05-14 00:39:53 -070069 */
sanghobd812f82015-06-29 14:58:47 -070070 public PolicyHandler(ApplicationId appId,
71 DeviceConfiguration deviceConfiguration,
72 FlowObjectiveService flowObjectiveService,
73 TunnelHandler tunnelHandler,
sangho4a5c42a2015-05-20 22:16:38 -070074 EventuallyConsistentMap<String, Policy> policyStore) {
sanghobd812f82015-06-29 14:58:47 -070075 this.appId = appId;
76 this.deviceConfiguration = deviceConfiguration;
77 this.flowObjectiveService = flowObjectiveService;
78 this.tunnelHandler = tunnelHandler;
sangho4a5c42a2015-05-20 22:16:38 -070079 this.policyStore = policyStore;
sangho27462c62015-05-14 00:39:53 -070080 }
81
82 /**
83 * Returns the policies.
84 *
85 * @return policy list
86 */
87 public List<Policy> getPolicies() {
Sho SHIMIZU594c2672015-09-02 18:47:40 -070088 return policyStore.values()
89 .stream()
Sho SHIMIZU0b454632015-09-02 18:49:53 -070090 .filter(policy -> policy instanceof TunnelPolicy)
Sho SHIMIZU594c2672015-09-02 18:47:40 -070091 .map(policy -> new TunnelPolicy((TunnelPolicy) policy))
92 .collect(Collectors.toList());
sangho27462c62015-05-14 00:39:53 -070093 }
94
95 /**
96 * Creates a policy using the policy information given.
sanghobd812f82015-06-29 14:58:47 -070097 * @param policy policy reference to create
98 * @return ID_EXISTS if the same policy ID exists,
99 * POLICY_EXISTS if the same policy exists, TUNNEL_NOT_FOUND if the tunnel
100 * does not exists, UNSUPPORTED_TYPE if the policy type is not supported,
101 * SUCCESS if the policy is created successfully
sangho27462c62015-05-14 00:39:53 -0700102 */
sanghobd812f82015-06-29 14:58:47 -0700103 public Result createPolicy(Policy policy) {
sangho4a5c42a2015-05-20 22:16:38 -0700104
105 if (policyStore.containsKey(policy.id())) {
106 log.warn("The policy id {} exists already", policy.id());
sanghobd812f82015-06-29 14:58:47 -0700107 return Result.ID_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700108 }
109
110 if (policyStore.containsValue(policy)) {
111 log.warn("The same policy exists already");
sanghobd812f82015-06-29 14:58:47 -0700112 return Result.POLICY_EXISTS;
sangho4a5c42a2015-05-20 22:16:38 -0700113 }
114
115 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
116
117 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700118 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700119 if (tunnel == null) {
sanghobd812f82015-06-29 14:58:47 -0700120 return Result.TUNNEL_NOT_FOUND;
sangho4a5c42a2015-05-20 22:16:38 -0700121 }
122
123 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
124 .builder()
sanghobd812f82015-06-29 14:58:47 -0700125 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700126 .makePermanent()
127 .nextStep(tunnel.groupId())
128 .withPriority(tunnelPolicy.priority())
129 .withSelector(buildSelector(policy))
130 .withFlag(ForwardingObjective.Flag.VERSATILE);
131
sanghobd812f82015-06-29 14:58:47 -0700132 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
133 flowObjectiveService.forward(source, fwdBuilder.add());
sangho4a5c42a2015-05-20 22:16:38 -0700134
135 } else {
136 log.warn("Policy type {} is not supported yet.", policy.type());
sanghobd812f82015-06-29 14:58:47 -0700137 return Result.UNSUPPORTED_TYPE;
sangho4a5c42a2015-05-20 22:16:38 -0700138 }
139
140 policyStore.put(policy.id(), policy);
sanghobd812f82015-06-29 14:58:47 -0700141
142 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700143 }
144
145 /**
146 * Removes the policy given.
147 *
148 * @param policyInfo policy information to remove
sanghobd812f82015-06-29 14:58:47 -0700149 * @return POLICY_NOT_FOUND if the policy to remove does not exists,
150 * SUCCESS if it is removed successfully
sangho27462c62015-05-14 00:39:53 -0700151 */
sanghobd812f82015-06-29 14:58:47 -0700152 public Result removePolicy(Policy policyInfo) {
sangho4a5c42a2015-05-20 22:16:38 -0700153
154 if (policyStore.get(policyInfo.id()) != null) {
155 Policy policy = policyStore.get(policyInfo.id());
156 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
157 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
sanghobd812f82015-06-29 14:58:47 -0700158 Tunnel tunnel = tunnelHandler.getTunnel(tunnelPolicy.tunnelId());
sangho4a5c42a2015-05-20 22:16:38 -0700159
160 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
161 .builder()
sanghobd812f82015-06-29 14:58:47 -0700162 .fromApp(appId)
sangho4a5c42a2015-05-20 22:16:38 -0700163 .makePermanent()
164 .withSelector(buildSelector(policy))
165 .withPriority(tunnelPolicy.priority())
166 .nextStep(tunnel.groupId())
167 .withFlag(ForwardingObjective.Flag.VERSATILE);
168
sanghobd812f82015-06-29 14:58:47 -0700169 DeviceId source = deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
170 flowObjectiveService.forward(source, fwdBuilder.remove());
sangho4a5c42a2015-05-20 22:16:38 -0700171
172 policyStore.remove(policyInfo.id());
sangho27462c62015-05-14 00:39:53 -0700173 }
174 } else {
175 log.warn("Policy {} was not found", policyInfo.id());
sanghobd812f82015-06-29 14:58:47 -0700176 return Result.POLICY_NOT_FOUND;
sangho27462c62015-05-14 00:39:53 -0700177 }
sanghobd812f82015-06-29 14:58:47 -0700178
179 return Result.SUCCESS;
sangho27462c62015-05-14 00:39:53 -0700180 }
181
sangho4a5c42a2015-05-20 22:16:38 -0700182
183 private TrafficSelector buildSelector(Policy policy) {
184
185 TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
186 tsb.matchEthType(Ethernet.TYPE_IPV4);
187 if (policy.dstIp() != null && !policy.dstIp().isEmpty()) {
188 tsb.matchIPDst(IpPrefix.valueOf(policy.dstIp()));
189 }
190 if (policy.srcIp() != null && !policy.srcIp().isEmpty()) {
191 tsb.matchIPSrc(IpPrefix.valueOf(policy.srcIp()));
192 }
193 if (policy.ipProto() != null && !policy.ipProto().isEmpty()) {
Sho SHIMIZU43d842e2015-09-02 20:37:26 -0700194 Short ipProto = IpProtocol.valueOf(policy.ipProto()).value();
sangho4a5c42a2015-05-20 22:16:38 -0700195 tsb.matchIPProtocol(ipProto.byteValue());
196 if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) {
197 if (policy.srcPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700198 tsb.matchTcpSrc(TpPort.tpPort(policy.srcPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700199 }
200 if (policy.dstPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700201 tsb.matchTcpDst(TpPort.tpPort(policy.dstPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700202 }
203 } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) {
204 if (policy.srcPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700205 tsb.matchUdpSrc(TpPort.tpPort(policy.srcPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700206 }
207 if (policy.dstPort() != 0) {
Hyunsun Moone11cdb92015-08-22 21:04:23 -0700208 tsb.matchUdpDst(TpPort.tpPort(policy.dstPort()));
sangho4a5c42a2015-05-20 22:16:38 -0700209 }
210 }
211 }
212
213 return tsb.build();
214 }
215
sangho27462c62015-05-14 00:39:53 -0700216}