blob: 7c0b145fe9c95cc7ec7e4616e9b754e47e6e88b6 [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
Michele Santuari4a338072014-11-05 18:38:55 +010018import java.util.HashMap;
19import java.util.HashSet;
Ray Milkey0742ec92014-10-13 08:39:55 -070020import java.util.List;
Michele Santuari4a338072014-11-05 18:38:55 +010021import java.util.Map;
22import java.util.Map.Entry;
23import java.util.Set;
Ray Milkey0742ec92014-10-13 08:39:55 -070024
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Yuta HIGUCHI467ccf72014-12-17 18:03:53 -080032import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Link;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.flow.DefaultFlowRule;
38import org.onosproject.net.flow.DefaultTrafficSelector;
39import org.onosproject.net.flow.DefaultTrafficTreatment;
40import org.onosproject.net.flow.FlowRule;
41import org.onosproject.net.flow.FlowRuleBatchEntry;
42import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
43import org.onosproject.net.flow.FlowRuleBatchOperation;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
46import org.onosproject.net.intent.IntentExtensionService;
47import org.onosproject.net.intent.IntentInstaller;
48import org.onosproject.net.intent.LinkCollectionIntent;
49import org.onosproject.net.intent.PathIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070050
51import com.google.common.collect.Lists;
52
Ray Milkey0742ec92014-10-13 08:39:55 -070053/**
Brian O'Connorabafb502014-12-02 22:26:20 -080054 * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path
Michele Santuari4a338072014-11-05 18:38:55 +010055 * segment intents.
Ray Milkey0742ec92014-10-13 08:39:55 -070056 */
57@Component(immediate = true)
Michele Santuari4a338072014-11-05 18:38:55 +010058public class LinkCollectionIntentInstaller
59 implements IntentInstaller<LinkCollectionIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070060
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected IntentExtensionService intentManager;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkey0742ec92014-10-13 08:39:55 -070065 protected CoreService coreService;
66
67 private ApplicationId appId;
68
69 @Activate
70 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080071 appId = coreService.registerApplication("org.onosproject.net.intent");
Ray Milkey0742ec92014-10-13 08:39:55 -070072 intentManager.registerInstaller(LinkCollectionIntent.class, this);
73 }
74
75 @Deactivate
76 public void deactivate() {
77 intentManager.unregisterInstaller(PathIntent.class);
78 }
79
Ray Milkey0742ec92014-10-13 08:39:55 -070080 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070081 public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) {
Michele Santuari4a338072014-11-05 18:38:55 +010082 Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>();
Ray Milkey0742ec92014-10-13 08:39:55 -070083 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Michele Santuari4a338072014-11-05 18:38:55 +010084
Ray Milkey0742ec92014-10-13 08:39:55 -070085 for (Link link : intent.links()) {
Michele Santuari4a338072014-11-05 18:38:55 +010086 if (outputMap.get(link.src().deviceId()) == null) {
87 outputMap.put(link.src().deviceId(), new HashSet<PortNumber>());
88 }
89 outputMap.get(link.src().deviceId()).add(link.src().port());
90
Ray Milkey0742ec92014-10-13 08:39:55 -070091 }
92
Michele Santuari4a338072014-11-05 18:38:55 +010093 for (ConnectPoint egressPoint : intent.egressPoints()) {
94 if (outputMap.get(egressPoint.deviceId()) == null) {
95 outputMap
96 .put(egressPoint.deviceId(), new HashSet<PortNumber>());
97 }
98 outputMap.get(egressPoint.deviceId()).add(egressPoint.port());
99
100 }
101
102 for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) {
103 rules.add(createBatchEntry(FlowRuleOperation.ADD, intent,
104 entry.getKey(), entry.getValue()));
105 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700106
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700107 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -0700108 }
109
110 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700111 public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
Michele Santuari4a338072014-11-05 18:38:55 +0100112 Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>();
Ray Milkey0742ec92014-10-13 08:39:55 -0700113 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
114
115 for (Link link : intent.links()) {
Michele Santuari4a338072014-11-05 18:38:55 +0100116 if (outputMap.get(link.src().deviceId()) == null) {
117 outputMap.put(link.src().deviceId(), new HashSet<PortNumber>());
118 }
119 outputMap.get(link.src().deviceId()).add(link.src().port());
Ray Milkey0742ec92014-10-13 08:39:55 -0700120 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700121
Michele Santuari4a338072014-11-05 18:38:55 +0100122 for (ConnectPoint egressPoint : intent.egressPoints()) {
123 if (outputMap.get(egressPoint.deviceId()) == null) {
124 outputMap
125 .put(egressPoint.deviceId(), new HashSet<PortNumber>());
126 }
127 outputMap.get(egressPoint.deviceId()).add(egressPoint.port());
128 }
129
130 for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) {
131 rules.add(createBatchEntry(FlowRuleOperation.REMOVE, intent,
132 entry.getKey(), entry.getValue()));
133 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700134
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700135 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -0700136 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700137
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700138 @Override
139 public List<FlowRuleBatchOperation> replace(LinkCollectionIntent intent,
140 LinkCollectionIntent newIntent) {
141 // FIXME: implement
142 return null;
143 }
144
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700145 /**
146 * Creates a FlowRuleBatchEntry based on the provided parameters.
147 *
148 * @param operation the FlowRuleOperation to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700149 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700150 * @param deviceId the device ID for the flow rule
151 * @param outPort the output port of the flow rule
152 * @return the new flow rule batch entry
153 */
154 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
Michele Santuari4a338072014-11-05 18:38:55 +0100155 LinkCollectionIntent intent,
156 DeviceId deviceId,
157 Set<PortNumber> outPorts) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700158
Michele Santuari4a338072014-11-05 18:38:55 +0100159 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
160 .builder(intent.treatment());
Jonathan Hart6e88c682014-10-21 17:05:25 -0700161
Michele Santuari4a338072014-11-05 18:38:55 +0100162 for (PortNumber outPort : outPorts) {
163 treatmentBuilder.setOutput(outPort);
164 }
165 TrafficTreatment treatment = treatmentBuilder.build();
Jonathan Hart6e88c682014-10-21 17:05:25 -0700166
Michele Santuari4a338072014-11-05 18:38:55 +0100167 TrafficSelector selector = DefaultTrafficSelector
168 .builder(intent.selector()).build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700169
170 FlowRule rule = new DefaultFlowRule(deviceId,
Brian O'Connor392619e2014-11-20 12:06:33 -0800171 selector, treatment, 123,
Yuta HIGUCHI467ccf72014-12-17 18:03:53 -0800172 appId, new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700173
174 return new FlowRuleBatchEntry(operation, rule);
175 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700176}