blob: 65b63148fc66a4fd12529867bd1a7f724fd7c121 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Brian O'Connor66630c82014-10-02 21:08:19 -070017
Brian O'Connora4cab072014-10-03 18:46:39 -070018import java.util.Iterator;
alshabib902d41b2014-10-07 16:52:05 -070019import java.util.List;
Ray Milkey71ade562015-02-18 15:08:07 -080020import java.util.Set;
Brian O'Connora4cab072014-10-03 18:46:39 -070021
Brian O'Connor66630c82014-10-02 21:08:19 -070022import 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Yuta HIGUCHI467ccf72014-12-17 18:03:53 -080029import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.Link;
32import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.FlowRule;
Ray Milkey71ade562015-02-18 15:08:07 -080035import org.onosproject.net.flow.FlowRuleOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.net.intent.Constraint;
39import org.onosproject.net.intent.IntentExtensionService;
40import org.onosproject.net.intent.IntentInstaller;
41import org.onosproject.net.intent.PathIntent;
42import org.onosproject.net.resource.DefaultLinkResourceRequest;
43import org.onosproject.net.resource.LinkResourceAllocations;
44import org.onosproject.net.resource.LinkResourceRequest;
45import org.onosproject.net.resource.LinkResourceService;
alshabib8ca53902014-10-07 13:11:17 -070046import org.slf4j.Logger;
Brian O'Connor66630c82014-10-02 21:08:19 -070047
Ray Milkey71ade562015-02-18 15:08:07 -080048import com.google.common.collect.ImmutableSet;
alshabib902d41b2014-10-07 16:52:05 -070049import com.google.common.collect.Lists;
Ray Milkey71ade562015-02-18 15:08:07 -080050import com.google.common.collect.Sets;
alshabib902d41b2014-10-07 16:52:05 -070051
Brian O'Connorabafb502014-12-02 22:26:20 -080052import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
Ray Milkeycaa450b2014-10-29 15:54:24 -070053import static org.slf4j.LoggerFactory.getLogger;
54
Brian O'Connor66630c82014-10-02 21:08:19 -070055/**
Thomas Vachuska425a2d72014-10-29 11:28:28 -070056 * Installer for {@link PathIntent packet path connectivity intents}.
Brian O'Connor66630c82014-10-02 21:08:19 -070057 */
58@Component(immediate = true)
tom9a693fd2014-10-03 11:32:19 -070059public class PathIntentInstaller implements IntentInstaller<PathIntent> {
60
alshabib8ca53902014-10-07 13:11:17 -070061 private final Logger log = getLogger(getClass());
62
Brian O'Connor66630c82014-10-02 21:08:19 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected IntentExtensionService intentManager;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib92c65ad2014-10-08 21:56:05 -070067 protected CoreService coreService;
68
Ray Milkeycaa450b2014-10-29 15:54:24 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected LinkResourceService resourceService;
71
Ray Milkey8d3ce432014-11-07 16:21:10 -080072 protected ApplicationId appId;
Brian O'Connor66630c82014-10-02 21:08:19 -070073
74 @Activate
75 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080076 appId = coreService.registerApplication("org.onosproject.net.intent");
Brian O'Connor66630c82014-10-02 21:08:19 -070077 intentManager.registerInstaller(PathIntent.class, this);
78 }
79
80 @Deactivate
81 public void deactivate() {
82 intentManager.unregisterInstaller(PathIntent.class);
83 }
84
85 @Override
Ray Milkey71ade562015-02-18 15:08:07 -080086 public List<Set<FlowRuleOperation>> install(PathIntent intent) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080087 LinkResourceAllocations allocations = allocateResources(intent);
Thomas Vachuskae3784c92014-10-29 21:09:10 -070088
tom9a693fd2014-10-03 11:32:19 -070089 TrafficSelector.Builder builder =
tom85258ee2014-10-07 00:10:02 -070090 DefaultTrafficSelector.builder(intent.selector());
91 Iterator<Link> links = intent.path().links().iterator();
Brian O'Connor66630c82014-10-02 21:08:19 -070092 ConnectPoint prev = links.next().dst();
Ray Milkey71ade562015-02-18 15:08:07 -080093 Set<FlowRuleOperation> rules = Sets.newHashSet();
Brian O'Connorf2dbde52014-10-10 16:20:24 -070094 // TODO Generate multiple batches
Brian O'Connor66630c82014-10-02 21:08:19 -070095 while (links.hasNext()) {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080096 builder.matchInPort(prev.port());
Brian O'Connor66630c82014-10-02 21:08:19 -070097 Link link = links.next();
Jonathan Hartab17b272014-12-18 15:01:17 -080098 // if this is the last flow rule, apply the intent's treatments
alshabib5c05f862014-11-19 18:28:37 -080099 TrafficTreatment treatment =
alshabib5c05f862014-11-19 18:28:37 -0800100 (links.hasNext() ? builder() : builder(intent.treatment()))
tomf5c9d922014-10-03 15:22:03 -0700101 .setOutput(link.src().port()).build();
alshabib902d41b2014-10-07 16:52:05 -0700102
tom9a693fd2014-10-03 11:32:19 -0700103 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
Brian O'Connor392619e2014-11-20 12:06:33 -0800104 builder.build(), treatment, 123, //FIXME 123
Jonathan Hartab17b272014-12-18 15:01:17 -0800105 appId,
106 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
107 0, true);
Ray Milkey71ade562015-02-18 15:08:07 -0800108 rules.add(new FlowRuleOperation(rule, FlowRuleOperation.Type.ADD));
Brian O'Connor66630c82014-10-02 21:08:19 -0700109 prev = link.dst();
110 }
Ray Milkey71ade562015-02-18 15:08:07 -0800111
112 return Lists.newArrayList(ImmutableSet.of(rules));
Brian O'Connor66630c82014-10-02 21:08:19 -0700113 }
114
115 @Override
Ray Milkey71ade562015-02-18 15:08:07 -0800116 public List<Set<FlowRuleOperation>> uninstall(PathIntent intent) {
Brian O'Connorb715f622015-02-18 20:50:25 -0800117 deallocateResources(intent);
Brian O'Connora4cab072014-10-03 18:46:39 -0700118 TrafficSelector.Builder builder =
tom85258ee2014-10-07 00:10:02 -0700119 DefaultTrafficSelector.builder(intent.selector());
120 Iterator<Link> links = intent.path().links().iterator();
Brian O'Connora4cab072014-10-03 18:46:39 -0700121 ConnectPoint prev = links.next().dst();
Ray Milkey71ade562015-02-18 15:08:07 -0800122 Set<FlowRuleOperation> rules = Sets.newHashSet();
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700123 // TODO Generate multiple batches
Brian O'Connora4cab072014-10-03 18:46:39 -0700124 while (links.hasNext()) {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800125 builder.matchInPort(prev.port());
Brian O'Connora4cab072014-10-03 18:46:39 -0700126 Link link = links.next();
Jonathan Hartab17b272014-12-18 15:01:17 -0800127 // if this is the last flow rule, apply the intent's treatments
128 TrafficTreatment treatment =
alshabib2be9bea2014-11-24 18:26:07 -0500129 (links.hasNext() ? builder() : builder(intent.treatment()))
130 .setOutput(link.src().port()).build();
Brian O'Connora4cab072014-10-03 18:46:39 -0700131 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
Jonathan Hartab17b272014-12-18 15:01:17 -0800132 builder.build(), treatment, 123, appId,
133 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
134 0, true);
Ray Milkey71ade562015-02-18 15:08:07 -0800135 rules.add(new FlowRuleOperation(rule, FlowRuleOperation.Type.REMOVE));
Brian O'Connora4cab072014-10-03 18:46:39 -0700136 prev = link.dst();
137 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800138 // FIXME this should change to new api
Ray Milkey71ade562015-02-18 15:08:07 -0800139 return Lists.newArrayList(ImmutableSet.of(rules));
Brian O'Connorcb900f42014-10-07 21:55:33 -0700140 }
141
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700142 @Override
Ray Milkey71ade562015-02-18 15:08:07 -0800143 public List<Set<FlowRuleOperation>> replace(PathIntent oldIntent, PathIntent newIntent) {
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700144 // FIXME: implement this
Ray Milkey71ade562015-02-18 15:08:07 -0800145 List<Set<FlowRuleOperation>> batches = Lists.newArrayList();
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700146 batches.addAll(uninstall(oldIntent));
147 batches.addAll(install(newIntent));
148 return batches;
149 }
150
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800151 /**
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 Vachuskaedc944c2014-11-04 15:42:25 -0800158 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 Milkeycaa450b2014-10-29 15:54:24 -0700165 }
Brian O'Connorb715f622015-02-18 20:50:25 -0800166
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'Connor66630c82014-10-02 21:08:19 -0700182}