blob: 816fc126bb26370bfe02f35b1e1ed61fd9a05f20 [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;
19import com.google.common.collect.SetMultimap;
Ray Milkey0742ec92014-10-13 08:39:55 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Yuta HIGUCHI467ccf72014-12-17 18:03:53 -080027import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Link;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.FlowRule;
36import org.onosproject.net.flow.FlowRuleBatchEntry;
37import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
38import org.onosproject.net.flow.FlowRuleBatchOperation;
39import 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;
44import org.onosproject.net.intent.PathIntent;
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() {
75 intentManager.unregisterInstaller(PathIntent.class);
76 }
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
115 public List<FlowRuleBatchOperation> replace(LinkCollectionIntent intent,
116 LinkCollectionIntent newIntent) {
117 // FIXME: implement
118 return null;
119 }
120
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700121 /**
122 * Creates a FlowRuleBatchEntry based on the provided parameters.
123 *
124 * @param operation the FlowRuleOperation to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700125 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700126 * @param deviceId the device ID for the flow rule
Jonathan Hartab17b272014-12-18 15:01:17 -0800127 * @param outPorts the set of output ports for the flow rule
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700128 * @return the new flow rule batch entry
129 */
130 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
Michele Santuari4a338072014-11-05 18:38:55 +0100131 LinkCollectionIntent intent,
132 DeviceId deviceId,
133 Set<PortNumber> outPorts) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700134
Michele Santuari4a338072014-11-05 18:38:55 +0100135 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
136 .builder(intent.treatment());
Jonathan Hart6e88c682014-10-21 17:05:25 -0700137
Michele Santuari4a338072014-11-05 18:38:55 +0100138 for (PortNumber outPort : outPorts) {
139 treatmentBuilder.setOutput(outPort);
140 }
141 TrafficTreatment treatment = treatmentBuilder.build();
Jonathan Hart6e88c682014-10-21 17:05:25 -0700142
Michele Santuari4a338072014-11-05 18:38:55 +0100143 TrafficSelector selector = DefaultTrafficSelector
144 .builder(intent.selector()).build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700145
146 FlowRule rule = new DefaultFlowRule(deviceId,
Jonathan Hartab17b272014-12-18 15:01:17 -0800147 selector, treatment, 123, appId,
148 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
149 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700150
151 return new FlowRuleBatchEntry(operation, rule);
152 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700153}