blob: 7a0602408e468c6b5feea943af40e322e1b65fd4 [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 */
Sho SHIMIZU3a7c98e2015-02-25 17:27:04 -080016package org.onosproject.net.intent.impl.installer;
Brian O'Connor66630c82014-10-02 21:08:19 -070017
Brian O'Connor64a0369d2015-02-20 22:02:59 -080018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Lists;
Brian O'Connor66630c82014-10-02 21:08:19 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Yuta HIGUCHI467ccf72014-12-17 18:03:53 -080027import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.Link;
30import org.onosproject.net.flow.DefaultFlowRule;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.FlowRule;
Ray Milkey71ade562015-02-18 15:08:07 -080033import org.onosproject.net.flow.FlowRuleOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
36import org.onosproject.net.intent.Constraint;
37import org.onosproject.net.intent.IntentExtensionService;
38import org.onosproject.net.intent.IntentInstaller;
39import org.onosproject.net.intent.PathIntent;
40import org.onosproject.net.resource.DefaultLinkResourceRequest;
41import org.onosproject.net.resource.LinkResourceAllocations;
42import org.onosproject.net.resource.LinkResourceRequest;
43import org.onosproject.net.resource.LinkResourceService;
alshabib8ca53902014-10-07 13:11:17 -070044import org.slf4j.Logger;
Brian O'Connor66630c82014-10-02 21:08:19 -070045
Brian O'Connor64a0369d2015-02-20 22:02:59 -080046import java.util.Collection;
47import java.util.Iterator;
48import java.util.List;
alshabib902d41b2014-10-07 16:52:05 -070049
Brian O'Connorabafb502014-12-02 22:26:20 -080050import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
Ray Milkeycaa450b2014-10-29 15:54:24 -070051import static org.slf4j.LoggerFactory.getLogger;
52
Brian O'Connor66630c82014-10-02 21:08:19 -070053/**
Thomas Vachuska425a2d72014-10-29 11:28:28 -070054 * Installer for {@link PathIntent packet path connectivity intents}.
Brian O'Connor66630c82014-10-02 21:08:19 -070055 */
56@Component(immediate = true)
tom9a693fd2014-10-03 11:32:19 -070057public class PathIntentInstaller implements IntentInstaller<PathIntent> {
58
alshabib8ca53902014-10-07 13:11:17 -070059 private final Logger log = getLogger(getClass());
60
Brian O'Connor66630c82014-10-02 21:08:19 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected IntentExtensionService intentManager;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib92c65ad2014-10-08 21:56:05 -070065 protected CoreService coreService;
66
Ray Milkeycaa450b2014-10-29 15:54:24 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected LinkResourceService resourceService;
69
Ray Milkey8d3ce432014-11-07 16:21:10 -080070 protected ApplicationId appId;
Brian O'Connor66630c82014-10-02 21:08:19 -070071
72 @Activate
73 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080074 appId = coreService.registerApplication("org.onosproject.net.intent");
Brian O'Connor66630c82014-10-02 21:08:19 -070075 intentManager.registerInstaller(PathIntent.class, this);
76 }
77
78 @Deactivate
79 public void deactivate() {
80 intentManager.unregisterInstaller(PathIntent.class);
81 }
82
83 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -080084 public List<Collection<FlowRuleOperation>> install(PathIntent intent) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080085 LinkResourceAllocations allocations = allocateResources(intent);
Thomas Vachuskae3784c92014-10-29 21:09:10 -070086
tom9a693fd2014-10-03 11:32:19 -070087 TrafficSelector.Builder builder =
tom85258ee2014-10-07 00:10:02 -070088 DefaultTrafficSelector.builder(intent.selector());
89 Iterator<Link> links = intent.path().links().iterator();
Brian O'Connor66630c82014-10-02 21:08:19 -070090 ConnectPoint prev = links.next().dst();
Brian O'Connor64a0369d2015-02-20 22:02:59 -080091 List<FlowRuleOperation> rules = Lists.newLinkedList();
Brian O'Connorf2dbde52014-10-10 16:20:24 -070092 // TODO Generate multiple batches
Brian O'Connor66630c82014-10-02 21:08:19 -070093 while (links.hasNext()) {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080094 builder.matchInPort(prev.port());
Brian O'Connor66630c82014-10-02 21:08:19 -070095 Link link = links.next();
Jonathan Hartab17b272014-12-18 15:01:17 -080096 // if this is the last flow rule, apply the intent's treatments
alshabib5c05f862014-11-19 18:28:37 -080097 TrafficTreatment treatment =
alshabib5c05f862014-11-19 18:28:37 -080098 (links.hasNext() ? builder() : builder(intent.treatment()))
tomf5c9d922014-10-03 15:22:03 -070099 .setOutput(link.src().port()).build();
alshabib902d41b2014-10-07 16:52:05 -0700100
tom9a693fd2014-10-03 11:32:19 -0700101 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
Ray Milkeyc24cde32015-03-10 18:20:18 -0700102 builder.build(), treatment, intent.priority(),
Jonathan Hartab17b272014-12-18 15:01:17 -0800103 appId,
104 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
105 0, true);
Ray Milkey71ade562015-02-18 15:08:07 -0800106 rules.add(new FlowRuleOperation(rule, FlowRuleOperation.Type.ADD));
Brian O'Connor66630c82014-10-02 21:08:19 -0700107 prev = link.dst();
108 }
Ray Milkey71ade562015-02-18 15:08:07 -0800109
110 return Lists.newArrayList(ImmutableSet.of(rules));
Brian O'Connor66630c82014-10-02 21:08:19 -0700111 }
112
113 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800114 public List<Collection<FlowRuleOperation>> uninstall(PathIntent intent) {
Brian O'Connorb715f622015-02-18 20:50:25 -0800115 deallocateResources(intent);
Brian O'Connora4cab072014-10-03 18:46:39 -0700116 TrafficSelector.Builder builder =
tom85258ee2014-10-07 00:10:02 -0700117 DefaultTrafficSelector.builder(intent.selector());
118 Iterator<Link> links = intent.path().links().iterator();
Brian O'Connora4cab072014-10-03 18:46:39 -0700119 ConnectPoint prev = links.next().dst();
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800120 List<FlowRuleOperation> rules = Lists.newLinkedList();
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700121 // TODO Generate multiple batches
Brian O'Connora4cab072014-10-03 18:46:39 -0700122 while (links.hasNext()) {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800123 builder.matchInPort(prev.port());
Brian O'Connora4cab072014-10-03 18:46:39 -0700124 Link link = links.next();
Jonathan Hartab17b272014-12-18 15:01:17 -0800125 // if this is the last flow rule, apply the intent's treatments
126 TrafficTreatment treatment =
alshabib2be9bea2014-11-24 18:26:07 -0500127 (links.hasNext() ? builder() : builder(intent.treatment()))
128 .setOutput(link.src().port()).build();
Brian O'Connora4cab072014-10-03 18:46:39 -0700129 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
Ray Milkeyc24cde32015-03-10 18:20:18 -0700130 builder.build(), treatment, intent.priority(), appId,
Jonathan Hartab17b272014-12-18 15:01:17 -0800131 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
132 0, true);
Ray Milkey71ade562015-02-18 15:08:07 -0800133 rules.add(new FlowRuleOperation(rule, FlowRuleOperation.Type.REMOVE));
Brian O'Connora4cab072014-10-03 18:46:39 -0700134 prev = link.dst();
135 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800136 // FIXME this should change to new api
Ray Milkey71ade562015-02-18 15:08:07 -0800137 return Lists.newArrayList(ImmutableSet.of(rules));
Brian O'Connorcb900f42014-10-07 21:55:33 -0700138 }
139
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700140 @Override
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800141 public List<Collection<FlowRuleOperation>> replace(PathIntent oldIntent, PathIntent newIntent) {
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700142 // FIXME: implement this
Brian O'Connor64a0369d2015-02-20 22:02:59 -0800143 List<Collection<FlowRuleOperation>> batches = Lists.newArrayList();
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700144 batches.addAll(uninstall(oldIntent));
145 batches.addAll(install(newIntent));
146 return batches;
147 }
148
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800149 /**
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 Vachuskaedc944c2014-11-04 15:42:25 -0800156 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 Milkeycaa450b2014-10-29 15:54:24 -0700163 }
Brian O'Connorb715f622015-02-18 20:50:25 -0800164
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'Connor66630c82014-10-02 21:08:19 -0700180}