blob: 5ff8db90c81ed17fc05635744e6c260989411681 [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;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.flow.DefaultTrafficSelector;
24import org.onosproject.net.flow.TrafficSelector;
25import org.onosproject.net.flowobjective.DefaultForwardingObjective;
26import org.onosproject.net.flowobjective.ForwardingObjective;
27import org.onosproject.store.service.EventuallyConsistentMap;
sangho27462c62015-05-14 00:39:53 -070028import org.slf4j.Logger;
29
30import java.util.ArrayList;
sangho27462c62015-05-14 00:39:53 -070031import java.util.List;
32
33import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Segment Routing Policy Handler.
37 */
38public class PolicyHandler {
39
40 protected final Logger log = getLogger(getClass());
41
Thomas Vachuskad62989f2015-05-21 16:41:41 -070042 // FIXME: references to manager components should be avoided
sangho4a5c42a2015-05-20 22:16:38 -070043 private final SegmentRoutingManager srManager;
44 private final EventuallyConsistentMap<String, Policy> policyStore;
sangho27462c62015-05-14 00:39:53 -070045
46 /**
47 * Creates a reference.
Thomas Vachuskad62989f2015-05-21 16:41:41 -070048 *
49 * @param srManager segment routing manager
50 * @param policyStore policy store
sangho27462c62015-05-14 00:39:53 -070051 */
sangho4a5c42a2015-05-20 22:16:38 -070052 public PolicyHandler(SegmentRoutingManager srManager,
53 EventuallyConsistentMap<String, Policy> policyStore) {
54 this.srManager = srManager;
55 this.policyStore = policyStore;
sangho27462c62015-05-14 00:39:53 -070056 }
57
58 /**
59 * Returns the policies.
60 *
61 * @return policy list
62 */
63 public List<Policy> getPolicies() {
64 List<Policy> policies = new ArrayList<>();
sangho4a5c42a2015-05-20 22:16:38 -070065 policyStore.values().forEach(policy -> policies.add(
sangho27462c62015-05-14 00:39:53 -070066 new TunnelPolicy((TunnelPolicy) policy)));
67
68 return policies;
69 }
70
71 /**
72 * Creates a policy using the policy information given.
73 *
74 * @param policy policy reference to create
75 */
76 public void createPolicy(Policy policy) {
sangho4a5c42a2015-05-20 22:16:38 -070077
78 if (policyStore.containsKey(policy.id())) {
79 log.warn("The policy id {} exists already", policy.id());
80 return;
81 }
82
83 if (policyStore.containsValue(policy)) {
84 log.warn("The same policy exists already");
85 return;
86 }
87
88 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
89
90 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
91 Tunnel tunnel = srManager.getTunnel(tunnelPolicy.tunnelId());
92 if (tunnel == null) {
93 log.error("Tunnel {} does not exists");
94 return;
95 }
96
97 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
98 .builder()
99 .fromApp(srManager.appId)
100 .makePermanent()
101 .nextStep(tunnel.groupId())
102 .withPriority(tunnelPolicy.priority())
103 .withSelector(buildSelector(policy))
104 .withFlag(ForwardingObjective.Flag.VERSATILE);
105
106 DeviceId source = srManager.deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
107 srManager.flowObjectiveService.forward(source, fwdBuilder.add());
108
109 } else {
110 log.warn("Policy type {} is not supported yet.", policy.type());
111 }
112
113 policyStore.put(policy.id(), policy);
sangho27462c62015-05-14 00:39:53 -0700114 }
115
116 /**
117 * Removes the policy given.
118 *
119 * @param policyInfo policy information to remove
120 */
121 public void removePolicy(Policy policyInfo) {
sangho4a5c42a2015-05-20 22:16:38 -0700122
123 if (policyStore.get(policyInfo.id()) != null) {
124 Policy policy = policyStore.get(policyInfo.id());
125 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
126 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
127 Tunnel tunnel = srManager.getTunnel(tunnelPolicy.tunnelId());
128
129 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
130 .builder()
131 .fromApp(srManager.appId)
132 .makePermanent()
133 .withSelector(buildSelector(policy))
134 .withPriority(tunnelPolicy.priority())
135 .nextStep(tunnel.groupId())
136 .withFlag(ForwardingObjective.Flag.VERSATILE);
137
138 DeviceId source = srManager.deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
139 srManager.flowObjectiveService.forward(source, fwdBuilder.remove());
140
141 policyStore.remove(policyInfo.id());
sangho27462c62015-05-14 00:39:53 -0700142 }
143 } else {
144 log.warn("Policy {} was not found", policyInfo.id());
145 }
146 }
147
sangho4a5c42a2015-05-20 22:16:38 -0700148
149 private TrafficSelector buildSelector(Policy policy) {
150
151 TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
152 tsb.matchEthType(Ethernet.TYPE_IPV4);
153 if (policy.dstIp() != null && !policy.dstIp().isEmpty()) {
154 tsb.matchIPDst(IpPrefix.valueOf(policy.dstIp()));
155 }
156 if (policy.srcIp() != null && !policy.srcIp().isEmpty()) {
157 tsb.matchIPSrc(IpPrefix.valueOf(policy.srcIp()));
158 }
159 if (policy.ipProto() != null && !policy.ipProto().isEmpty()) {
160 Short ipProto = Short.valueOf(IpProtocol.valueOf(policy.ipProto()).value());
161 tsb.matchIPProtocol(ipProto.byteValue());
162 if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) {
163 if (policy.srcPort() != 0) {
164 tsb.matchTcpSrc(policy.srcPort());
165 }
166 if (policy.dstPort() != 0) {
167 tsb.matchTcpDst(policy.dstPort());
168 }
169 } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) {
170 if (policy.srcPort() != 0) {
171 tsb.matchUdpSrc(policy.srcPort());
172 }
173 if (policy.dstPort() != 0) {
174 tsb.matchUdpDst(policy.dstPort());
175 }
176 }
177 }
178
179 return tsb.build();
180 }
181
sangho27462c62015-05-14 00:39:53 -0700182}