Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 20 | import java.util.List; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 21 | |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 27 | import org.onlab.onos.core.ApplicationId; |
| 28 | import org.onlab.onos.core.CoreService; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.DeviceId; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.Link; |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.PortNumber; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 33 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.flow.FlowRule; |
| 36 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 37 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 38 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.flow.TrafficSelector; |
| 40 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 41 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 42 | import org.onlab.onos.net.intent.IntentInstaller; |
| 43 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 44 | import org.onlab.onos.net.intent.PathIntent; |
| 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import com.google.common.collect.Lists; |
| 48 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Installer for {@link org.onlab.onos.net.intent.LinkCollectionIntent} |
| 51 | * path segment intents. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | public 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 Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 62 | 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 Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 77 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 78 | public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 79 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 80 | for (Link link : intent.links()) { |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 81 | rules.add(createBatchEntry(FlowRuleOperation.ADD, |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 82 | intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 83 | link.src().deviceId(), |
| 84 | link.src().port())); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 87 | rules.add(createBatchEntry(FlowRuleOperation.ADD, |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 88 | intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 89 | intent.egressPoint().deviceId(), |
| 90 | intent.egressPoint().port())); |
| 91 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 92 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 96 | public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 97 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 98 | |
| 99 | for (Link link : intent.links()) { |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 100 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 101 | intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 102 | link.src().deviceId(), |
| 103 | link.src().port())); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 104 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 105 | |
| 106 | rules.add(createBatchEntry(FlowRuleOperation.REMOVE, |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 107 | intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 108 | intent.egressPoint().deviceId(), |
| 109 | intent.egressPoint().port())); |
| 110 | |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 111 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 112 | } |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 113 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public List<FlowRuleBatchOperation> replace(LinkCollectionIntent intent, |
| 116 | LinkCollectionIntent newIntent) { |
| 117 | // FIXME: implement |
| 118 | return null; |
| 119 | } |
| 120 | |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Creates a FlowRuleBatchEntry based on the provided parameters. |
| 123 | * |
| 124 | * @param operation the FlowRuleOperation to use |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 125 | * @param intent the link collection intent |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 126 | * @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 Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 131 | LinkCollectionIntent intent, |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 132 | DeviceId deviceId, |
| 133 | PortNumber outPort) { |
| 134 | |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame] | 135 | 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 Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 142 | |
| 143 | FlowRule rule = new DefaultFlowRule(deviceId, |
Brian O'Connor | 392619e | 2014-11-20 12:06:33 -0800 | [diff] [blame^] | 144 | selector, treatment, 123, |
| 145 | appId, (short) (intent.id().fingerprint() & 0xffff), 0, true); |
Jonathan Hart | 6b2ffc3 | 2014-10-18 02:09:22 -0700 | [diff] [blame] | 146 | |
| 147 | return new FlowRuleBatchEntry(operation, rule); |
| 148 | } |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 149 | } |