blob: 5913e5b9874fd804646c6a8e06142dacfba6cc67 [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
sangho4a5c42a2015-05-20 22:16:38 -070042 private final SegmentRoutingManager srManager;
43 private final EventuallyConsistentMap<String, Policy> policyStore;
sangho27462c62015-05-14 00:39:53 -070044
45 /**
46 * Creates a reference.
sangho4a5c42a2015-05-20 22:16:38 -070047 * @param policyStore
sangho27462c62015-05-14 00:39:53 -070048 */
sangho4a5c42a2015-05-20 22:16:38 -070049 public PolicyHandler(SegmentRoutingManager srManager,
50 EventuallyConsistentMap<String, Policy> policyStore) {
51 this.srManager = srManager;
52 this.policyStore = policyStore;
sangho27462c62015-05-14 00:39:53 -070053 }
54
55 /**
56 * Returns the policies.
57 *
58 * @return policy list
59 */
60 public List<Policy> getPolicies() {
61 List<Policy> policies = new ArrayList<>();
sangho4a5c42a2015-05-20 22:16:38 -070062 policyStore.values().forEach(policy -> policies.add(
sangho27462c62015-05-14 00:39:53 -070063 new TunnelPolicy((TunnelPolicy) policy)));
64
65 return policies;
66 }
67
68 /**
69 * Creates a policy using the policy information given.
70 *
71 * @param policy policy reference to create
72 */
73 public void createPolicy(Policy policy) {
sangho4a5c42a2015-05-20 22:16:38 -070074
75 if (policyStore.containsKey(policy.id())) {
76 log.warn("The policy id {} exists already", policy.id());
77 return;
78 }
79
80 if (policyStore.containsValue(policy)) {
81 log.warn("The same policy exists already");
82 return;
83 }
84
85 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
86
87 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
88 Tunnel tunnel = srManager.getTunnel(tunnelPolicy.tunnelId());
89 if (tunnel == null) {
90 log.error("Tunnel {} does not exists");
91 return;
92 }
93
94 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
95 .builder()
96 .fromApp(srManager.appId)
97 .makePermanent()
98 .nextStep(tunnel.groupId())
99 .withPriority(tunnelPolicy.priority())
100 .withSelector(buildSelector(policy))
101 .withFlag(ForwardingObjective.Flag.VERSATILE);
102
103 DeviceId source = srManager.deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
104 srManager.flowObjectiveService.forward(source, fwdBuilder.add());
105
106 } else {
107 log.warn("Policy type {} is not supported yet.", policy.type());
108 }
109
110 policyStore.put(policy.id(), policy);
sangho27462c62015-05-14 00:39:53 -0700111 }
112
113 /**
114 * Removes the policy given.
115 *
116 * @param policyInfo policy information to remove
117 */
118 public void removePolicy(Policy policyInfo) {
sangho4a5c42a2015-05-20 22:16:38 -0700119
120 if (policyStore.get(policyInfo.id()) != null) {
121 Policy policy = policyStore.get(policyInfo.id());
122 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
123 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
124 Tunnel tunnel = srManager.getTunnel(tunnelPolicy.tunnelId());
125
126 ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective
127 .builder()
128 .fromApp(srManager.appId)
129 .makePermanent()
130 .withSelector(buildSelector(policy))
131 .withPriority(tunnelPolicy.priority())
132 .nextStep(tunnel.groupId())
133 .withFlag(ForwardingObjective.Flag.VERSATILE);
134
135 DeviceId source = srManager.deviceConfiguration.getDeviceId(tunnel.labelIds().get(0));
136 srManager.flowObjectiveService.forward(source, fwdBuilder.remove());
137
138 policyStore.remove(policyInfo.id());
sangho27462c62015-05-14 00:39:53 -0700139 }
140 } else {
141 log.warn("Policy {} was not found", policyInfo.id());
142 }
143 }
144
sangho4a5c42a2015-05-20 22:16:38 -0700145
146 private TrafficSelector buildSelector(Policy policy) {
147
148 TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
149 tsb.matchEthType(Ethernet.TYPE_IPV4);
150 if (policy.dstIp() != null && !policy.dstIp().isEmpty()) {
151 tsb.matchIPDst(IpPrefix.valueOf(policy.dstIp()));
152 }
153 if (policy.srcIp() != null && !policy.srcIp().isEmpty()) {
154 tsb.matchIPSrc(IpPrefix.valueOf(policy.srcIp()));
155 }
156 if (policy.ipProto() != null && !policy.ipProto().isEmpty()) {
157 Short ipProto = Short.valueOf(IpProtocol.valueOf(policy.ipProto()).value());
158 tsb.matchIPProtocol(ipProto.byteValue());
159 if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.TCP)) {
160 if (policy.srcPort() != 0) {
161 tsb.matchTcpSrc(policy.srcPort());
162 }
163 if (policy.dstPort() != 0) {
164 tsb.matchTcpDst(policy.dstPort());
165 }
166 } else if (IpProtocol.valueOf(policy.ipProto()).equals(IpProtocol.UDP)) {
167 if (policy.srcPort() != 0) {
168 tsb.matchUdpSrc(policy.srcPort());
169 }
170 if (policy.dstPort() != 0) {
171 tsb.matchUdpDst(policy.dstPort());
172 }
173 }
174 }
175
176 return tsb.build();
177 }
178
sangho27462c62015-05-14 00:39:53 -0700179}