blob: 0d788e599f096f5b4bd49e0876dc24bab97c2c90 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Ray Milkey0742ec92014-10-13 08:39:55 -070017
Brian O'Connor64a0369d2015-02-20 22:02:59 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.collect.Lists;
21import com.google.common.collect.SetMultimap;
Ray Milkey0742ec92014-10-13 08:39:55 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Yuta HIGUCHI467ccf72014-12-17 18:03:53 -080029import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.Link;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.flow.DefaultFlowRule;
35import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
37import org.onosproject.net.flow.FlowRule;
Ray Milkey71ade562015-02-18 15:08:07 -080038import org.onosproject.net.flow.FlowRuleOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.IntentInstaller;
43import org.onosproject.net.intent.LinkCollectionIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070044
Brian O'Connor64a0369d2015-02-20 22:02:59 -080045import java.util.Collection;
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080046import java.util.HashSet;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080047import java.util.List;
48import java.util.Set;
Ray Milkey0742ec92014-10-13 08:39:55 -070049
Ray Milkey0742ec92014-10-13 08:39:55 -070050/**
Brian O'Connorabafb502014-12-02 22:26:20 -080051 * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path
Michele Santuari4a338072014-11-05 18:38:55 +010052 * segment intents.
Ray Milkey0742ec92014-10-13 08:39:55 -070053 */
54@Component(immediate = true)
Michele Santuari4a338072014-11-05 18:38:55 +010055public class LinkCollectionIntentInstaller
56 implements IntentInstaller<LinkCollectionIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070057
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected IntentExtensionService intentManager;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkey0742ec92014-10-13 08:39:55 -070062 protected CoreService coreService;
63
64 private ApplicationId appId;
65
66 @Activate
67 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080068 appId = coreService.registerApplication("org.onosproject.net.intent");
Ray Milkey0742ec92014-10-13 08:39:55 -070069 intentManager.registerInstaller(LinkCollectionIntent.class, this);
70 }
71
72 @Deactivate
73 public void deactivate() {
Pavlin Radoslavova1e26562015-02-13 16:01:24 -080074 intentManager.unregisterInstaller(LinkCollectionIntent.class);
Ray Milkey0742ec92014-10-13 08:39:55 -070075 }
76
Ray Milkey0742ec92014-10-13 08:39:55 -070077 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -080078 public List<Collection<FlowRuleOperation>> install(LinkCollectionIntent intent) {
Ray Milkey71ade562015-02-18 15:08:07 -080079 return generateBatchOperations(intent, FlowRuleOperation.Type.ADD);
Ray Milkey0742ec92014-10-13 08:39:55 -070080 }
81
82 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -080083 public List<Collection<FlowRuleOperation>> uninstall(LinkCollectionIntent intent) {
Ray Milkey71ade562015-02-18 15:08:07 -080084 return generateBatchOperations(intent, FlowRuleOperation.Type.REMOVE);
Jonathan Hartab17b272014-12-18 15:01:17 -080085 }
86
Brian O'Connor64a0369d2015-02-20 22:02:59 -080087 private List<Collection<FlowRuleOperation>> generateBatchOperations(
Ray Milkey71ade562015-02-18 15:08:07 -080088 LinkCollectionIntent intent, FlowRuleOperation.Type operation) {
Jonathan Hartab17b272014-12-18 15:01:17 -080089
Brian O'Connor64a0369d2015-02-20 22:02:59 -080090 //TODO do we need a set here?
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080091 SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
Jonathan Hartab17b272014-12-18 15:01:17 -080092 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
Ray Milkey0742ec92014-10-13 08:39:55 -070093
94 for (Link link : intent.links()) {
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080095 inputPorts.put(link.dst().deviceId(), link.dst().port());
Jonathan Hartab17b272014-12-18 15:01:17 -080096 outputPorts.put(link.src().deviceId(), link.src().port());
Ray Milkey0742ec92014-10-13 08:39:55 -070097 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070098
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080099 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
100 inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
101 }
102
Michele Santuari4a338072014-11-05 18:38:55 +0100103 for (ConnectPoint egressPoint : intent.egressPoints()) {
Jonathan Hartab17b272014-12-18 15:01:17 -0800104 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
Michele Santuari4a338072014-11-05 18:38:55 +0100105 }
106
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800107 List<FlowRuleOperation> rules = Lists.newArrayList();
108 outputPorts.keys().stream()
109 .map(deviceId -> createBatchEntries(operation,
110 intent, deviceId,
111 inputPorts.get(deviceId),
112 outputPorts.get(deviceId)))
113 .forEach(rules::addAll);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700114
Ray Milkey71ade562015-02-18 15:08:07 -0800115 return Lists.newArrayList(ImmutableSet.of(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -0700116 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700117
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700118 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800119 public List<Collection<FlowRuleOperation>> replace(LinkCollectionIntent oldIntent,
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700120 LinkCollectionIntent newIntent) {
Brian O'Connore2eac102015-02-12 18:30:22 -0800121 // FIXME: implement this in a more intelligent/less brute force way
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800122 List<Collection<FlowRuleOperation>> batches = Lists.newArrayList();
Brian O'Connore2eac102015-02-12 18:30:22 -0800123 batches.addAll(uninstall(oldIntent));
124 batches.addAll(install(newIntent));
125 return batches;
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700126 }
127
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700128 /**
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800129 * Creates a collection of FlowRuleOperation based on the provided
130 * parameters.
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700131 *
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800132 * @param operation the FlowRuleOperation type to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700133 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700134 * @param deviceId the device ID for the flow rule
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800135 * @param inPorts the logical input ports of the flow rule
Jonathan Hartab17b272014-12-18 15:01:17 -0800136 * @param outPorts the set of output ports for the flow rule
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800137 * @return a collection with the new flow rule batch entries
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700138 */
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800139 private Collection<FlowRuleOperation> createBatchEntries(
140 FlowRuleOperation.Type operation,
141 LinkCollectionIntent intent,
142 DeviceId deviceId,
143 Set<PortNumber> inPorts,
144 Set<PortNumber> outPorts) {
145 Collection<FlowRuleOperation> result = Lists.newLinkedList();
146 Set<PortNumber> ingressPorts = new HashSet<PortNumber>();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700147
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800148 //
149 // Collect all ingress ports for this device.
150 // The intent treatment is applied only on those ports.
151 //
152 for (ConnectPoint cp : intent.ingressPoints()) {
153 if (cp.deviceId().equals(deviceId)) {
154 ingressPorts.add(cp.port());
155 }
Michele Santuari4a338072014-11-05 18:38:55 +0100156 }
Jonathan Hart6e88c682014-10-21 17:05:25 -0700157
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800158 //
159 // Create two treatments: one for setting the output ports,
160 // and a second one that applies the intent treatment and sets the
161 // output ports.
162 // NOTE: The second one is created only if there are ingress ports.
163 //
164 TrafficTreatment.Builder defaultTreatmentBuilder =
165 DefaultTrafficTreatment.builder();
166 for (PortNumber outPort : outPorts) {
167 defaultTreatmentBuilder.setOutput(outPort);
168 }
169 TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
170 TrafficTreatment intentTreatment = null;
171 if (!ingressPorts.isEmpty()) {
172 TrafficTreatment.Builder intentTreatmentBuilder =
173 DefaultTrafficTreatment.builder(intent.treatment());
174 for (PortNumber outPort : outPorts) {
175 intentTreatmentBuilder.setOutput(outPort);
176 }
177 intentTreatment = intentTreatmentBuilder.build();
178 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700179
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800180 for (PortNumber inPort : inPorts) {
181 TrafficSelector selector = DefaultTrafficSelector
182 .builder(intent.selector()).matchInPort(inPort).build();
183 TrafficTreatment treatment = defaultTreatment;
184 if (ingressPorts.contains(inPort)) {
185 // Use the intent treatment if this is ingress port
186 treatment = intentTreatment;
187 }
188 FlowRule rule = new DefaultFlowRule(deviceId,
Jonathan Hartab17b272014-12-18 15:01:17 -0800189 selector, treatment, 123, appId,
190 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
191 0, true);
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800192 result.add(new FlowRuleOperation(rule, operation));
193 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700194
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800195 return result;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700196 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700197}