blob: 260bd5b5a477ed652dad3f987e7651063155bc89 [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;
46import java.util.List;
47import java.util.Set;
48import java.util.stream.Collectors;
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?
Jonathan Hartab17b272014-12-18 15:01:17 -080091 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
Ray Milkey71ade562015-02-18 15:08:07 -0800102 /* Fear of streams */
103 /*
104 Set<FlowRuleBatchEntry> rules = Sets.newHashSet();
105 for (DeviceId deviceId : outputPorts.keys()) {
106 rules.add(createBatchEntry(operation,
107 intent, deviceId,
108 outputPorts.get(deviceId)));
109 }
110 */
111
112 Set<FlowRuleOperation> rules =
113 outputPorts
Jonathan Hartab17b272014-12-18 15:01:17 -0800114 .keys()
115 .stream()
116 .map(deviceId -> createBatchEntry(operation,
Ray Milkey71ade562015-02-18 15:08:07 -0800117 intent, deviceId,
118 outputPorts.get(deviceId)))
119 .collect(Collectors.toSet());
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700120
Ray Milkey71ade562015-02-18 15:08:07 -0800121 return Lists.newArrayList(ImmutableSet.of(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -0700122 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700123
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700124 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800125 public List<Collection<FlowRuleOperation>> replace(LinkCollectionIntent oldIntent,
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700126 LinkCollectionIntent newIntent) {
Brian O'Connore2eac102015-02-12 18:30:22 -0800127 // FIXME: implement this in a more intelligent/less brute force way
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800128 List<Collection<FlowRuleOperation>> batches = Lists.newArrayList();
Brian O'Connore2eac102015-02-12 18:30:22 -0800129 batches.addAll(uninstall(oldIntent));
130 batches.addAll(install(newIntent));
131 return batches;
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700132 }
133
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700134 /**
135 * Creates a FlowRuleBatchEntry based on the provided parameters.
136 *
137 * @param operation the FlowRuleOperation to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700138 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700139 * @param deviceId the device ID for the flow rule
Jonathan Hartab17b272014-12-18 15:01:17 -0800140 * @param outPorts the set of output ports for the flow rule
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700141 * @return the new flow rule batch entry
142 */
Ray Milkey71ade562015-02-18 15:08:07 -0800143 private FlowRuleOperation createBatchEntry(FlowRuleOperation.Type operation,
144 LinkCollectionIntent intent,
145 DeviceId deviceId,
146 Set<PortNumber> outPorts) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700147
Michele Santuari4a338072014-11-05 18:38:55 +0100148 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
149 .builder(intent.treatment());
Jonathan Hart6e88c682014-10-21 17:05:25 -0700150
Michele Santuari4a338072014-11-05 18:38:55 +0100151 for (PortNumber outPort : outPorts) {
152 treatmentBuilder.setOutput(outPort);
153 }
154 TrafficTreatment treatment = treatmentBuilder.build();
Jonathan Hart6e88c682014-10-21 17:05:25 -0700155
Michele Santuari4a338072014-11-05 18:38:55 +0100156 TrafficSelector selector = DefaultTrafficSelector
157 .builder(intent.selector()).build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700158
159 FlowRule rule = new DefaultFlowRule(deviceId,
Jonathan Hartab17b272014-12-18 15:01:17 -0800160 selector, treatment, 123, appId,
161 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
162 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700163
Ray Milkey71ade562015-02-18 15:08:07 -0800164 return new FlowRuleOperation(rule, operation);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700165 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700166}