blob: 745c64f0e06ff15825696620c8c9125d1efda33f [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
sangho27462c62015-05-14 00:39:53 -070033import java.util.List;
Sho SHIMIZU594c2672015-09-02 18:47:40 -070034import java.util.stream.Collectors;
sangho27462c62015-05-14 00:39:53 -070035
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() {
Sho SHIMIZU594c2672015-09-02 18:47:40 -070087 return policyStore.values()
88 .stream()
89 // keep the original behavior, but it may cause a cast error
90 // it is better to use filter() to omit instances not being TunnelPolicy
91 .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()) {
194 Short ipProto = Short.valueOf(IpProtocol.valueOf(policy.ipProto()).value());
195 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}