blob: fb8e2d40e9216fc44fe98260b39de7031333b6da [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
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070018import static org.slf4j.LoggerFactory.getLogger;
19
Ray Milkey0742ec92014-10-13 08:39:55 -070020import java.util.List;
Ray Milkey0742ec92014-10-13 08:39:55 -070021
22import 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;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070027import org.onlab.onos.core.ApplicationId;
28import org.onlab.onos.core.CoreService;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070029import org.onlab.onos.net.DeviceId;
Ray Milkey0742ec92014-10-13 08:39:55 -070030import org.onlab.onos.net.Link;
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070031import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -070032import org.onlab.onos.net.flow.DefaultFlowRule;
33import org.onlab.onos.net.flow.DefaultTrafficSelector;
Jonathan Hart6e88c682014-10-21 17:05:25 -070034import org.onlab.onos.net.flow.DefaultTrafficTreatment;
Ray Milkey0742ec92014-10-13 08:39:55 -070035import org.onlab.onos.net.flow.FlowRule;
36import org.onlab.onos.net.flow.FlowRuleBatchEntry;
37import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
38import org.onlab.onos.net.flow.FlowRuleBatchOperation;
Ray Milkey0742ec92014-10-13 08:39:55 -070039import org.onlab.onos.net.flow.TrafficSelector;
40import org.onlab.onos.net.flow.TrafficTreatment;
41import org.onlab.onos.net.intent.IntentExtensionService;
42import org.onlab.onos.net.intent.IntentInstaller;
43import org.onlab.onos.net.intent.LinkCollectionIntent;
44import org.onlab.onos.net.intent.PathIntent;
45import org.slf4j.Logger;
46
47import com.google.common.collect.Lists;
48
Ray Milkey0742ec92014-10-13 08:39:55 -070049/**
50 * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent}
51 * path segment intents.
52 */
53@Component(immediate = true)
54public class LinkCollectionIntentInstaller implements IntentInstaller<LinkCollectionIntent> {
55
56 private final Logger log = getLogger(getClass());
57
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() {
68 appId = coreService.registerApplication("org.onlab.onos.net.intent");
69 intentManager.registerInstaller(LinkCollectionIntent.class, this);
70 }
71
72 @Deactivate
73 public void deactivate() {
74 intentManager.unregisterInstaller(PathIntent.class);
75 }
76
Ray Milkey0742ec92014-10-13 08:39:55 -070077 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070078 public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) {
Ray Milkey0742ec92014-10-13 08:39:55 -070079 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
80 for (Link link : intent.links()) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070081 rules.add(createBatchEntry(FlowRuleOperation.ADD,
Jonathan Hart6e88c682014-10-21 17:05:25 -070082 intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070083 link.src().deviceId(),
84 link.src().port()));
Ray Milkey0742ec92014-10-13 08:39:55 -070085 }
86
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070087 rules.add(createBatchEntry(FlowRuleOperation.ADD,
Jonathan Hart6e88c682014-10-21 17:05:25 -070088 intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -070089 intent.egressPoint().deviceId(),
90 intent.egressPoint().port()));
91
Brian O'Connorf2dbde52014-10-10 16:20:24 -070092 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
Ray Milkey0742ec92014-10-13 08:39:55 -070093 }
94
95 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070096 public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
Ray Milkey0742ec92014-10-13 08:39:55 -070097 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
98
99 for (Link link : intent.links()) {
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700100 rules.add(createBatchEntry(FlowRuleOperation.REMOVE,
Jonathan Hart6e88c682014-10-21 17:05:25 -0700101 intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700102 link.src().deviceId(),
103 link.src().port()));
Ray Milkey0742ec92014-10-13 08:39:55 -0700104 }
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700105
106 rules.add(createBatchEntry(FlowRuleOperation.REMOVE,
Jonathan Hart6e88c682014-10-21 17:05:25 -0700107 intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700108 intent.egressPoint().deviceId(),
109 intent.egressPoint().port()));
110
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700111 return Lists.newArrayList(new FlowRuleBatchOperation(rules));
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
127 * @param outPort the output port of the flow rule
128 * @return the new flow rule batch entry
129 */
130 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
Jonathan Hart6e88c682014-10-21 17:05:25 -0700131 LinkCollectionIntent intent,
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700132 DeviceId deviceId,
133 PortNumber outPort) {
134
Jonathan Hart6e88c682014-10-21 17:05:25 -0700135 TrafficTreatment.Builder treatmentBuilder =
136 DefaultTrafficTreatment.builder(intent.treatment());
137
138 TrafficTreatment treatment = treatmentBuilder.setOutput(outPort).build();
139
140 TrafficSelector selector = DefaultTrafficSelector.builder(intent.selector())
141 .build();
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700142
143 FlowRule rule = new DefaultFlowRule(deviceId,
Brian O'Connor392619e2014-11-20 12:06:33 -0800144 selector, treatment, 123,
145 appId, (short) (intent.id().fingerprint() & 0xffff), 0, true);
Jonathan Hart6b2ffc32014-10-18 02:09:22 -0700146
147 return new FlowRuleBatchEntry(operation, rule);
148 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700149}