blob: abc08d16dd2a066eb3a6a2d39e093e49ba5e2907 [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
Jonathan Hartab17b272014-12-18 15:01:17 -080018import com.google.common.collect.HashMultimap;
Brian O'Connore2eac102015-02-12 18:30:22 -080019import com.google.common.collect.Lists;
Jonathan Hartab17b272014-12-18 15:01:17 -080020import com.google.common.collect.SetMultimap;
Ray Milkey0742ec92014-10-13 08:39:55 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Yuta HIGUCHI467ccf72014-12-17 18:03:53 -080028import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.DefaultFlowRule;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.FlowRuleBatchEntry;
38import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
39import org.onosproject.net.flow.FlowRuleBatchOperation;
40import org.onosproject.net.flow.TrafficSelector;
41import org.onosproject.net.flow.TrafficTreatment;
42import org.onosproject.net.intent.IntentExtensionService;
43import org.onosproject.net.intent.IntentInstaller;
44import org.onosproject.net.intent.LinkCollectionIntent;
45import org.onosproject.net.intent.PathIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070046
Jonathan Hartab17b272014-12-18 15:01:17 -080047import java.util.Collections;
48import java.util.List;
49import java.util.Set;
50import java.util.stream.Collectors;
Ray Milkey0742ec92014-10-13 08:39:55 -070051
Ray Milkey0742ec92014-10-13 08:39:55 -070052/**
Brian O'Connorabafb502014-12-02 22:26:20 -080053 * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path
Michele Santuari4a338072014-11-05 18:38:55 +010054 * segment intents.
Ray Milkey0742ec92014-10-13 08:39:55 -070055 */
56@Component(immediate = true)
Michele Santuari4a338072014-11-05 18:38:55 +010057public class LinkCollectionIntentInstaller
58 implements IntentInstaller<LinkCollectionIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070059
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected IntentExtensionService intentManager;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkey0742ec92014-10-13 08:39:55 -070064 protected CoreService coreService;
65
66 private ApplicationId appId;
67
68 @Activate
69 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080070 appId = coreService.registerApplication("org.onosproject.net.intent");
Ray Milkey0742ec92014-10-13 08:39:55 -070071 intentManager.registerInstaller(LinkCollectionIntent.class, this);
72 }
73
74 @Deactivate
75 public void deactivate() {
76 intentManager.unregisterInstaller(PathIntent.class);
77 }
78
Ray Milkey0742ec92014-10-13 08:39:55 -070079 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070080 public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) {
Jonathan Hartab17b272014-12-18 15:01:17 -080081 return generateBatchOperations(intent, FlowRuleOperation.ADD);
Ray Milkey0742ec92014-10-13 08:39:55 -070082 }
83
84 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070085 public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
Jonathan Hartab17b272014-12-18 15:01:17 -080086 return generateBatchOperations(intent, FlowRuleOperation.REMOVE);
87 }
88
89 private List<FlowRuleBatchOperation> generateBatchOperations(
90 LinkCollectionIntent intent, FlowRuleOperation operation) {
91
92 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
Ray Milkey0742ec92014-10-13 08:39:55 -070093
94 for (Link link : intent.links()) {
Jonathan Hartab17b272014-12-18 15:01:17 -080095 outputPorts.put(link.src().deviceId(), link.src().port());
Ray Milkey0742ec92014-10-13 08:39:55 -070096 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070097
Michele Santuari4a338072014-11-05 18:38:55 +010098 for (ConnectPoint egressPoint : intent.egressPoints()) {
Jonathan Hartab17b272014-12-18 15:01:17 -080099 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
Michele Santuari4a338072014-11-05 18:38:55 +0100100 }
101
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800102 //FIXME change to new api
Jonathan Hartab17b272014-12-18 15:01:17 -0800103 FlowRuleBatchOperation batchOperation =
104 new FlowRuleBatchOperation(outputPorts
105 .keys()
106 .stream()
107 .map(deviceId -> createBatchEntry(operation,
108 intent, deviceId,
109 outputPorts.get(deviceId)))
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800110 .collect(Collectors.toList()), null, 0);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700111
Jonathan Hartab17b272014-12-18 15:01:17 -0800112 return Collections.singletonList(batchOperation);
Ray Milkey0742ec92014-10-13 08:39:55 -0700113 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700114
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700115 @Override
Brian O'Connore2eac102015-02-12 18:30:22 -0800116 public List<FlowRuleBatchOperation> replace(LinkCollectionIntent oldIntent,
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700117 LinkCollectionIntent newIntent) {
Brian O'Connore2eac102015-02-12 18:30:22 -0800118 // FIXME: implement this in a more intelligent/less brute force way
119 List<FlowRuleBatchOperation> batches = Lists.newArrayList();
120 batches.addAll(uninstall(oldIntent));
121 batches.addAll(install(newIntent));
122 return batches;
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700123 }
124
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700125 /**
126 * Creates a FlowRuleBatchEntry based on the provided parameters.
127 *
128 * @param operation the FlowRuleOperation to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700129 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700130 * @param deviceId the device ID for the flow rule
Jonathan Hartab17b272014-12-18 15:01:17 -0800131 * @param outPorts the set of output ports for the flow rule
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700132 * @return the new flow rule batch entry
133 */
134 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
Michele Santuari4a338072014-11-05 18:38:55 +0100135 LinkCollectionIntent intent,
136 DeviceId deviceId,
137 Set<PortNumber> outPorts) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700138
Michele Santuari4a338072014-11-05 18:38:55 +0100139 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
140 .builder(intent.treatment());
Jonathan Hart6e88c682014-10-21 17:05:25 -0700141
Michele Santuari4a338072014-11-05 18:38:55 +0100142 for (PortNumber outPort : outPorts) {
143 treatmentBuilder.setOutput(outPort);
144 }
145 TrafficTreatment treatment = treatmentBuilder.build();
Jonathan Hart6e88c682014-10-21 17:05:25 -0700146
Michele Santuari4a338072014-11-05 18:38:55 +0100147 TrafficSelector selector = DefaultTrafficSelector
148 .builder(intent.selector()).build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700149
150 FlowRule rule = new DefaultFlowRule(deviceId,
Jonathan Hartab17b272014-12-18 15:01:17 -0800151 selector, treatment, 123, appId,
152 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
153 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700154
155 return new FlowRuleBatchEntry(operation, rule);
156 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700157}