blob: ef6e8b50bfd778d895a8a51d9741bfbc2a12c5fc [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
Ray Milkey71ade562015-02-18 15:08:07 -080018import java.util.List;
19import java.util.Set;
20import java.util.stream.Collectors;
21
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
Ray Milkey71ade562015-02-18 15:08:07 -080045import com.google.common.collect.HashMultimap;
46import com.google.common.collect.ImmutableSet;
47import com.google.common.collect.Lists;
48import com.google.common.collect.SetMultimap;
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
Ray Milkey71ade562015-02-18 15:08:07 -080078 public List<Set<FlowRuleOperation>> install(LinkCollectionIntent intent) {
79 return generateBatchOperations(intent, FlowRuleOperation.Type.ADD);
Ray Milkey0742ec92014-10-13 08:39:55 -070080 }
81
82 @Override
Ray Milkey71ade562015-02-18 15:08:07 -080083 public List<Set<FlowRuleOperation>> uninstall(LinkCollectionIntent intent) {
84 return generateBatchOperations(intent, FlowRuleOperation.Type.REMOVE);
Jonathan Hartab17b272014-12-18 15:01:17 -080085 }
86
Ray Milkey71ade562015-02-18 15:08:07 -080087 private List<Set<FlowRuleOperation>> generateBatchOperations(
88 LinkCollectionIntent intent, FlowRuleOperation.Type operation) {
Jonathan Hartab17b272014-12-18 15:01:17 -080089
90 SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
Ray Milkey0742ec92014-10-13 08:39:55 -070091
92 for (Link link : intent.links()) {
Jonathan Hartab17b272014-12-18 15:01:17 -080093 outputPorts.put(link.src().deviceId(), link.src().port());
Ray Milkey0742ec92014-10-13 08:39:55 -070094 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070095
Michele Santuari4a338072014-11-05 18:38:55 +010096 for (ConnectPoint egressPoint : intent.egressPoints()) {
Jonathan Hartab17b272014-12-18 15:01:17 -080097 outputPorts.put(egressPoint.deviceId(), egressPoint.port());
Michele Santuari4a338072014-11-05 18:38:55 +010098 }
99
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800100 //FIXME change to new api
Ray Milkey71ade562015-02-18 15:08:07 -0800101 /* Fear of streams */
102 /*
103 Set<FlowRuleBatchEntry> rules = Sets.newHashSet();
104 for (DeviceId deviceId : outputPorts.keys()) {
105 rules.add(createBatchEntry(operation,
106 intent, deviceId,
107 outputPorts.get(deviceId)));
108 }
109 */
110
111 Set<FlowRuleOperation> rules =
112 outputPorts
Jonathan Hartab17b272014-12-18 15:01:17 -0800113 .keys()
114 .stream()
115 .map(deviceId -> createBatchEntry(operation,
Ray Milkey71ade562015-02-18 15:08:07 -0800116 intent, deviceId,
117 outputPorts.get(deviceId)))
118 .collect(Collectors.toSet());
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700119
Ray Milkey71ade562015-02-18 15:08:07 -0800120 return Lists.newArrayList(ImmutableSet.of(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -0700121 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700122
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700123 @Override
Ray Milkey71ade562015-02-18 15:08:07 -0800124 public List<Set<FlowRuleOperation>> replace(LinkCollectionIntent oldIntent,
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700125 LinkCollectionIntent newIntent) {
Brian O'Connore2eac102015-02-12 18:30:22 -0800126 // FIXME: implement this in a more intelligent/less brute force way
Ray Milkey71ade562015-02-18 15:08:07 -0800127 List<Set<FlowRuleOperation>> batches = Lists.newArrayList();
Brian O'Connore2eac102015-02-12 18:30:22 -0800128 batches.addAll(uninstall(oldIntent));
129 batches.addAll(install(newIntent));
130 return batches;
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700131 }
132
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700133 /**
134 * Creates a FlowRuleBatchEntry based on the provided parameters.
135 *
136 * @param operation the FlowRuleOperation to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700137 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700138 * @param deviceId the device ID for the flow rule
Jonathan Hartab17b272014-12-18 15:01:17 -0800139 * @param outPorts the set of output ports for the flow rule
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700140 * @return the new flow rule batch entry
141 */
Ray Milkey71ade562015-02-18 15:08:07 -0800142 private FlowRuleOperation createBatchEntry(FlowRuleOperation.Type operation,
143 LinkCollectionIntent intent,
144 DeviceId deviceId,
145 Set<PortNumber> outPorts) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700146
Michele Santuari4a338072014-11-05 18:38:55 +0100147 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
148 .builder(intent.treatment());
Jonathan Hart6e88c682014-10-21 17:05:25 -0700149
Michele Santuari4a338072014-11-05 18:38:55 +0100150 for (PortNumber outPort : outPorts) {
151 treatmentBuilder.setOutput(outPort);
152 }
153 TrafficTreatment treatment = treatmentBuilder.build();
Jonathan Hart6e88c682014-10-21 17:05:25 -0700154
Michele Santuari4a338072014-11-05 18:38:55 +0100155 TrafficSelector selector = DefaultTrafficSelector
156 .builder(intent.selector()).build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700157
158 FlowRule rule = new DefaultFlowRule(deviceId,
Jonathan Hartab17b272014-12-18 15:01:17 -0800159 selector, treatment, 123, appId,
160 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
161 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700162
Ray Milkey71ade562015-02-18 15:08:07 -0800163 return new FlowRuleOperation(rule, operation);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700164 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700165}