blob: cb6c6e4b92fae11b6c24a09bae8c73ea7b7ff91c [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;
Ray Milkey0742ec92014-10-13 08:39:55 -070045
Jonathan Hartab17b272014-12-18 15:01:17 -080046import java.util.Collections;
47import java.util.List;
48import java.util.Set;
49import java.util.stream.Collectors;
Ray Milkey0742ec92014-10-13 08:39:55 -070050
Ray Milkey0742ec92014-10-13 08:39:55 -070051/**
Brian O'Connorabafb502014-12-02 22:26:20 -080052 * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path
Michele Santuari4a338072014-11-05 18:38:55 +010053 * segment intents.
Ray Milkey0742ec92014-10-13 08:39:55 -070054 */
55@Component(immediate = true)
Michele Santuari4a338072014-11-05 18:38:55 +010056public class LinkCollectionIntentInstaller
57 implements IntentInstaller<LinkCollectionIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070058
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected IntentExtensionService intentManager;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkey0742ec92014-10-13 08:39:55 -070063 protected CoreService coreService;
64
65 private ApplicationId appId;
66
67 @Activate
68 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080069 appId = coreService.registerApplication("org.onosproject.net.intent");
Ray Milkey0742ec92014-10-13 08:39:55 -070070 intentManager.registerInstaller(LinkCollectionIntent.class, this);
71 }
72
73 @Deactivate
74 public void deactivate() {
Pavlin Radoslavova1e26562015-02-13 16:01:24 -080075 intentManager.unregisterInstaller(LinkCollectionIntent.class);
Ray Milkey0742ec92014-10-13 08:39:55 -070076 }
77
Ray Milkey0742ec92014-10-13 08:39:55 -070078 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070079 public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) {
Jonathan Hartab17b272014-12-18 15:01:17 -080080 return generateBatchOperations(intent, FlowRuleOperation.ADD);
Ray Milkey0742ec92014-10-13 08:39:55 -070081 }
82
83 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070084 public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
Jonathan Hartab17b272014-12-18 15:01:17 -080085 return generateBatchOperations(intent, FlowRuleOperation.REMOVE);
86 }
87
88 private List<FlowRuleBatchOperation> generateBatchOperations(
89 LinkCollectionIntent intent, FlowRuleOperation operation) {
90
91 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
Ray Milkey0742ec92014-10-13 08:39:55 -070092
93 for (Link link : intent.links()) {
Jonathan Hartab17b272014-12-18 15:01:17 -080094 outputPorts.put(link.src().deviceId(), link.src().port());
Ray Milkey0742ec92014-10-13 08:39:55 -070095 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070096
Michele Santuari4a338072014-11-05 18:38:55 +010097 for (ConnectPoint egressPoint : intent.egressPoints()) {
Jonathan Hartab17b272014-12-18 15:01:17 -080098 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
Michele Santuari4a338072014-11-05 18:38:55 +010099 }
100
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800101 //FIXME change to new api
Jonathan Hartab17b272014-12-18 15:01:17 -0800102 FlowRuleBatchOperation batchOperation =
103 new FlowRuleBatchOperation(outputPorts
104 .keys()
105 .stream()
106 .map(deviceId -> createBatchEntry(operation,
107 intent, deviceId,
108 outputPorts.get(deviceId)))
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800109 .collect(Collectors.toList()), null, 0);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700110
Jonathan Hartab17b272014-12-18 15:01:17 -0800111 return Collections.singletonList(batchOperation);
Ray Milkey0742ec92014-10-13 08:39:55 -0700112 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700113
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700114 @Override
Brian O'Connore2eac102015-02-12 18:30:22 -0800115 public List<FlowRuleBatchOperation> replace(LinkCollectionIntent oldIntent,
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700116 LinkCollectionIntent newIntent) {
Brian O'Connore2eac102015-02-12 18:30:22 -0800117 // FIXME: implement this in a more intelligent/less brute force way
118 List<FlowRuleBatchOperation> batches = Lists.newArrayList();
119 batches.addAll(uninstall(oldIntent));
120 batches.addAll(install(newIntent));
121 return batches;
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700122 }
123
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700124 /**
125 * Creates a FlowRuleBatchEntry based on the provided parameters.
126 *
127 * @param operation the FlowRuleOperation to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700128 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700129 * @param deviceId the device ID for the flow rule
Jonathan Hartab17b272014-12-18 15:01:17 -0800130 * @param outPorts the set of output ports for the flow rule
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700131 * @return the new flow rule batch entry
132 */
133 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
Michele Santuari4a338072014-11-05 18:38:55 +0100134 LinkCollectionIntent intent,
135 DeviceId deviceId,
136 Set<PortNumber> outPorts) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700137
Michele Santuari4a338072014-11-05 18:38:55 +0100138 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
139 .builder(intent.treatment());
Jonathan Hart6e88c682014-10-21 17:05:25 -0700140
Michele Santuari4a338072014-11-05 18:38:55 +0100141 for (PortNumber outPort : outPorts) {
142 treatmentBuilder.setOutput(outPort);
143 }
144 TrafficTreatment treatment = treatmentBuilder.build();
Jonathan Hart6e88c682014-10-21 17:05:25 -0700145
Michele Santuari4a338072014-11-05 18:38:55 +0100146 TrafficSelector selector = DefaultTrafficSelector
147 .builder(intent.selector()).build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700148
149 FlowRule rule = new DefaultFlowRule(deviceId,
Jonathan Hartab17b272014-12-18 15:01:17 -0800150 selector, treatment, 123, appId,
151 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
152 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700153
154 return new FlowRuleBatchEntry(operation, rule);
155 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700156}