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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
Yuta HIGUCHI | 467ccf7 | 2014-12-17 18:03:53 -0800 | [diff] [blame] | 27 | import org.onosproject.core.DefaultGroupId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.flow.DefaultFlowRule; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowRuleOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.TrafficSelector; |
| 35 | import org.onosproject.net.flow.TrafficTreatment; |
| 36 | import org.onosproject.net.intent.Constraint; |
| 37 | import org.onosproject.net.intent.IntentExtensionService; |
| 38 | import org.onosproject.net.intent.IntentInstaller; |
| 39 | import org.onosproject.net.intent.PathIntent; |
| 40 | import org.onosproject.net.resource.DefaultLinkResourceRequest; |
| 41 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 42 | import org.onosproject.net.resource.LinkResourceRequest; |
| 43 | import org.onosproject.net.resource.LinkResourceService; |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 44 | import org.slf4j.Logger; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 45 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 46 | import java.util.Collection; |
| 47 | import java.util.Iterator; |
| 48 | import java.util.List; |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 49 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import static org.onosproject.net.flow.DefaultTrafficTreatment.builder; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 53 | /** |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 54 | * Installer for {@link PathIntent packet path connectivity intents}. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 55 | */ |
| 56 | @Component(immediate = true) |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 57 | public class PathIntentInstaller implements IntentInstaller<PathIntent> { |
| 58 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 59 | private final Logger log = getLogger(getClass()); |
| 60 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected IntentExtensionService intentManager; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 65 | protected CoreService coreService; |
| 66 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected LinkResourceService resourceService; |
| 69 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 70 | protected ApplicationId appId; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 71 | |
| 72 | @Activate |
| 73 | public void activate() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 74 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 75 | intentManager.registerInstaller(PathIntent.class, this); |
| 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | public void deactivate() { |
| 80 | intentManager.unregisterInstaller(PathIntent.class); |
| 81 | } |
| 82 | |
| 83 | @Override |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 84 | public List<Collection<FlowRuleOperation>> install(PathIntent intent) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 85 | LinkResourceAllocations allocations = allocateResources(intent); |
Thomas Vachuska | e3784c9 | 2014-10-29 21:09:10 -0700 | [diff] [blame] | 86 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 87 | TrafficSelector.Builder builder = |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 88 | DefaultTrafficSelector.builder(intent.selector()); |
| 89 | Iterator<Link> links = intent.path().links().iterator(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 90 | ConnectPoint prev = links.next().dst(); |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 91 | List<FlowRuleOperation> rules = Lists.newLinkedList(); |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 92 | // TODO Generate multiple batches |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 93 | while (links.hasNext()) { |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 94 | builder.matchInPort(prev.port()); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 95 | Link link = links.next(); |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 96 | // if this is the last flow rule, apply the intent's treatments |
alshabib | 5c05f86 | 2014-11-19 18:28:37 -0800 | [diff] [blame] | 97 | TrafficTreatment treatment = |
alshabib | 5c05f86 | 2014-11-19 18:28:37 -0800 | [diff] [blame] | 98 | (links.hasNext() ? builder() : builder(intent.treatment())) |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 99 | .setOutput(link.src().port()).build(); |
alshabib | 902d41b | 2014-10-07 16:52:05 -0700 | [diff] [blame] | 100 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 101 | FlowRule rule = new DefaultFlowRule(link.src().deviceId(), |
Brian O'Connor | 392619e | 2014-11-20 12:06:33 -0800 | [diff] [blame] | 102 | builder.build(), treatment, 123, //FIXME 123 |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 103 | appId, |
| 104 | new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), |
| 105 | 0, true); |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 106 | rules.add(new FlowRuleOperation(rule, FlowRuleOperation.Type.ADD)); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 107 | prev = link.dst(); |
| 108 | } |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 109 | |
| 110 | return Lists.newArrayList(ImmutableSet.of(rules)); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 114 | public List<Collection<FlowRuleOperation>> uninstall(PathIntent intent) { |
Brian O'Connor | b715f62 | 2015-02-18 20:50:25 -0800 | [diff] [blame] | 115 | deallocateResources(intent); |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 116 | TrafficSelector.Builder builder = |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 117 | DefaultTrafficSelector.builder(intent.selector()); |
| 118 | Iterator<Link> links = intent.path().links().iterator(); |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 119 | ConnectPoint prev = links.next().dst(); |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 120 | List<FlowRuleOperation> rules = Lists.newLinkedList(); |
Brian O'Connor | f2dbde5 | 2014-10-10 16:20:24 -0700 | [diff] [blame] | 121 | // TODO Generate multiple batches |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 122 | while (links.hasNext()) { |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 123 | builder.matchInPort(prev.port()); |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 124 | Link link = links.next(); |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 125 | // if this is the last flow rule, apply the intent's treatments |
| 126 | TrafficTreatment treatment = |
alshabib | 2be9bea | 2014-11-24 18:26:07 -0500 | [diff] [blame] | 127 | (links.hasNext() ? builder() : builder(intent.treatment())) |
| 128 | .setOutput(link.src().port()).build(); |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 129 | FlowRule rule = new DefaultFlowRule(link.src().deviceId(), |
Jonathan Hart | ab17b27 | 2014-12-18 15:01:17 -0800 | [diff] [blame] | 130 | builder.build(), treatment, 123, appId, |
| 131 | new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), |
| 132 | 0, true); |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 133 | rules.add(new FlowRuleOperation(rule, FlowRuleOperation.Type.REMOVE)); |
Brian O'Connor | a4cab07 | 2014-10-03 18:46:39 -0700 | [diff] [blame] | 134 | prev = link.dst(); |
| 135 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 136 | // FIXME this should change to new api |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 137 | return Lists.newArrayList(ImmutableSet.of(rules)); |
Brian O'Connor | cb900f4 | 2014-10-07 21:55:33 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 140 | @Override |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 141 | public List<Collection<FlowRuleOperation>> replace(PathIntent oldIntent, PathIntent newIntent) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 142 | // FIXME: implement this |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 143 | List<Collection<FlowRuleOperation>> batches = Lists.newArrayList(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 144 | batches.addAll(uninstall(oldIntent)); |
| 145 | batches.addAll(install(newIntent)); |
| 146 | return batches; |
| 147 | } |
| 148 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 149 | /** |
| 150 | * Allocate resources required for an intent. |
| 151 | * |
| 152 | * @param intent intent to allocate resource for |
| 153 | * @return allocated resources if any are required, null otherwise |
| 154 | */ |
| 155 | private LinkResourceAllocations allocateResources(PathIntent intent) { |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 156 | LinkResourceRequest.Builder builder = |
| 157 | DefaultLinkResourceRequest.builder(intent.id(), intent.path().links()); |
| 158 | for (Constraint constraint : intent.constraints()) { |
| 159 | builder.addConstraint(constraint); |
| 160 | } |
| 161 | LinkResourceRequest request = builder.build(); |
| 162 | return request.resources().isEmpty() ? null : resourceService.requestResources(request); |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 163 | } |
Brian O'Connor | b715f62 | 2015-02-18 20:50:25 -0800 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Deallocate resources held by an intent. |
| 167 | * |
| 168 | * @param intent intent to deallocate resources for |
| 169 | */ |
| 170 | private void deallocateResources(PathIntent intent) { |
| 171 | if (intent.constraints().isEmpty()) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | LinkResourceAllocations allocatedResources = resourceService.getAllocations(intent.id()); |
| 176 | if (allocatedResources != null) { |
| 177 | resourceService.releaseResources(allocatedResources); |
| 178 | } |
| 179 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 180 | } |