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