blob: 66e5fb11137798a1287a0003da2c25687f19624a [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 */
Ray Milkey0742ec92014-10-13 08:39:55 -070016package org.onlab.onos.net.intent.impl;
17
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;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070030import org.onlab.onos.core.ApplicationId;
31import org.onlab.onos.core.CoreService;
Michele Santuari4a338072014-11-05 18:38:55 +010032import org.onlab.onos.net.ConnectPoint;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070033import org.onlab.onos.net.DeviceId;
Ray Milkey0742ec92014-10-13 08:39:55 -070034import org.onlab.onos.net.Link;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070035import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -070036import org.onlab.onos.net.flow.DefaultFlowRule;
37import org.onlab.onos.net.flow.DefaultTrafficSelector;
Jonathan Hart6e88c682014-10-21 17:05:25 -070038import org.onlab.onos.net.flow.DefaultTrafficTreatment;
Ray Milkey0742ec92014-10-13 08:39:55 -070039import org.onlab.onos.net.flow.FlowRule;
40import org.onlab.onos.net.flow.FlowRuleBatchEntry;
41import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
42import org.onlab.onos.net.flow.FlowRuleBatchOperation;
Ray Milkey0742ec92014-10-13 08:39:55 -070043import org.onlab.onos.net.flow.TrafficSelector;
44import org.onlab.onos.net.flow.TrafficTreatment;
45import org.onlab.onos.net.intent.IntentExtensionService;
46import org.onlab.onos.net.intent.IntentInstaller;
47import org.onlab.onos.net.intent.LinkCollectionIntent;
48import org.onlab.onos.net.intent.PathIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070049
50import com.google.common.collect.Lists;
51
Ray Milkey0742ec92014-10-13 08:39:55 -070052/**
Michele Santuari4a338072014-11-05 18:38:55 +010053 * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent} path
54 * segment intents.
Ray Milkey0742ec92014-10-13 08:39:55 -070055 */
56@Component(immediate = true)
Michele Santuari4a338072014-11-05 18:38:55 +010057public class LinkCollectionIntentInstaller
58 implements IntentInstaller<LinkCollectionIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070059
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected IntentExtensionService intentManager;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ray Milkey0742ec92014-10-13 08:39:55 -070064 protected CoreService coreService;
65
66 private ApplicationId appId;
67
68 @Activate
69 public void activate() {
70 appId = coreService.registerApplication("org.onlab.onos.net.intent");
71 intentManager.registerInstaller(LinkCollectionIntent.class, this);
72 }
73
74 @Deactivate
75 public void deactivate() {
76 intentManager.unregisterInstaller(PathIntent.class);
77 }
78
Ray Milkey0742ec92014-10-13 08:39:55 -070079 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070080 public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) {
Michele Santuari4a338072014-11-05 18:38:55 +010081 Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>();
Ray Milkey0742ec92014-10-13 08:39:55 -070082 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Michele Santuari4a338072014-11-05 18:38:55 +010083
Ray Milkey0742ec92014-10-13 08:39:55 -070084 for (Link link : intent.links()) {
Michele Santuari4a338072014-11-05 18:38:55 +010085 if (outputMap.get(link.src().deviceId()) == null) {
86 outputMap.put(link.src().deviceId(), new HashSet<PortNumber>());
87 }
88 outputMap.get(link.src().deviceId()).add(link.src().port());
89
Ray Milkey0742ec92014-10-13 08:39:55 -070090 }
91
Michele Santuari4a338072014-11-05 18:38:55 +010092 for (ConnectPoint egressPoint : intent.egressPoints()) {
93 if (outputMap.get(egressPoint.deviceId()) == null) {
94 outputMap
95 .put(egressPoint.deviceId(), new HashSet<PortNumber>());
96 }
97 outputMap.get(egressPoint.deviceId()).add(egressPoint.port());
98
99 }
100
101 for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) {
102 rules.add(createBatchEntry(FlowRuleOperation.ADD, intent,
103 entry.getKey(), entry.getValue()));
104 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700105
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700106 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -0700107 }
108
109 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700110 public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
Michele Santuari4a338072014-11-05 18:38:55 +0100111 Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>();
Ray Milkey0742ec92014-10-13 08:39:55 -0700112 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
113
114 for (Link link : intent.links()) {
Michele Santuari4a338072014-11-05 18:38:55 +0100115 if (outputMap.get(link.src().deviceId()) == null) {
116 outputMap.put(link.src().deviceId(), new HashSet<PortNumber>());
117 }
118 outputMap.get(link.src().deviceId()).add(link.src().port());
Ray Milkey0742ec92014-10-13 08:39:55 -0700119 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700120
Michele Santuari4a338072014-11-05 18:38:55 +0100121 for (ConnectPoint egressPoint : intent.egressPoints()) {
122 if (outputMap.get(egressPoint.deviceId()) == null) {
123 outputMap
124 .put(egressPoint.deviceId(), new HashSet<PortNumber>());
125 }
126 outputMap.get(egressPoint.deviceId()).add(egressPoint.port());
127 }
128
129 for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) {
130 rules.add(createBatchEntry(FlowRuleOperation.REMOVE, intent,
131 entry.getKey(), entry.getValue()));
132 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700133
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700134 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -0700135 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700136
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700137 @Override
138 public List<FlowRuleBatchOperation> replace(LinkCollectionIntent intent,
139 LinkCollectionIntent newIntent) {
140 // FIXME: implement
141 return null;
142 }
143
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700144 /**
145 * Creates a FlowRuleBatchEntry based on the provided parameters.
146 *
147 * @param operation the FlowRuleOperation to use
Jonathan Hart6e88c682014-10-21 17:05:25 -0700148 * @param intent the link collection intent
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700149 * @param deviceId the device ID for the flow rule
150 * @param outPort the output port of the flow rule
151 * @return the new flow rule batch entry
152 */
153 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
Michele Santuari4a338072014-11-05 18:38:55 +0100154 LinkCollectionIntent intent,
155 DeviceId deviceId,
156 Set<PortNumber> outPorts) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700157
Michele Santuari4a338072014-11-05 18:38:55 +0100158 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment
159 .builder(intent.treatment());
Jonathan Hart6e88c682014-10-21 17:05:25 -0700160
Michele Santuari4a338072014-11-05 18:38:55 +0100161 for (PortNumber outPort : outPorts) {
162 treatmentBuilder.setOutput(outPort);
163 }
164 TrafficTreatment treatment = treatmentBuilder.build();
Jonathan Hart6e88c682014-10-21 17:05:25 -0700165
Michele Santuari4a338072014-11-05 18:38:55 +0100166 TrafficSelector selector = DefaultTrafficSelector
167 .builder(intent.selector()).build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700168
169 FlowRule rule = new DefaultFlowRule(deviceId,
Brian O'Connor392619e2014-11-20 12:06:33 -0800170 selector, treatment, 123,
171 appId, (short) (intent.id().fingerprint() & 0xffff), 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700172
173 return new FlowRuleBatchEntry(operation, rule);
174 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700175}