blob: b90be503e9d5d20c374f37f115a94a5203617f7d [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;
Brian O'Connora4cab072014-10-03 18:46:39 -070020
Brian O'Connor66630c82014-10-02 21:08:19 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Yuta HIGUCHI467ccf72014-12-17 18:03:53 -080028import org.onosproject.core.DefaultGroupId;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.Link;
31import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.FlowRuleBatchEntry;
35import org.onosproject.net.flow.FlowRuleBatchEntry.FlowRuleOperation;
36import org.onosproject.net.flow.FlowRuleBatchOperation;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.intent.Constraint;
40import org.onosproject.net.intent.IntentExtensionService;
41import org.onosproject.net.intent.IntentInstaller;
42import org.onosproject.net.intent.PathIntent;
43import org.onosproject.net.resource.DefaultLinkResourceRequest;
44import org.onosproject.net.resource.LinkResourceAllocations;
45import org.onosproject.net.resource.LinkResourceRequest;
46import org.onosproject.net.resource.LinkResourceService;
alshabib8ca53902014-10-07 13:11:17 -070047import org.slf4j.Logger;
Brian O'Connor66630c82014-10-02 21:08:19 -070048
alshabib902d41b2014-10-07 16:52:05 -070049import com.google.common.collect.Lists;
50
Brian O'Connorabafb502014-12-02 22:26:20 -080051import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
Ray Milkeycaa450b2014-10-29 15:54:24 -070052import static org.slf4j.LoggerFactory.getLogger;
53
Brian O'Connor66630c82014-10-02 21:08:19 -070054/**
Thomas Vachuska425a2d72014-10-29 11:28:28 -070055 * Installer for {@link PathIntent packet path connectivity intents}.
Brian O'Connor66630c82014-10-02 21:08:19 -070056 */
57@Component(immediate = true)
tom9a693fd2014-10-03 11:32:19 -070058public class PathIntentInstaller implements IntentInstaller<PathIntent> {
59
alshabib8ca53902014-10-07 13:11:17 -070060 private final Logger log = getLogger(getClass());
61
Brian O'Connor66630c82014-10-02 21:08:19 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected IntentExtensionService intentManager;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib92c65ad2014-10-08 21:56:05 -070066 protected CoreService coreService;
67
Ray Milkeycaa450b2014-10-29 15:54:24 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected LinkResourceService resourceService;
70
Ray Milkey8d3ce432014-11-07 16:21:10 -080071 protected ApplicationId appId;
Brian O'Connor66630c82014-10-02 21:08:19 -070072
73 @Activate
74 public void activate() {
Brian O'Connorabafb502014-12-02 22:26:20 -080075 appId = coreService.registerApplication("org.onosproject.net.intent");
Brian O'Connor66630c82014-10-02 21:08:19 -070076 intentManager.registerInstaller(PathIntent.class, this);
77 }
78
79 @Deactivate
80 public void deactivate() {
81 intentManager.unregisterInstaller(PathIntent.class);
82 }
83
84 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -070085 public List<FlowRuleBatchOperation> install(PathIntent intent) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080086 LinkResourceAllocations allocations = allocateResources(intent);
Thomas Vachuskae3784c92014-10-29 21:09:10 -070087
tom9a693fd2014-10-03 11:32:19 -070088 TrafficSelector.Builder builder =
tom85258ee2014-10-07 00:10:02 -070089 DefaultTrafficSelector.builder(intent.selector());
90 Iterator<Link> links = intent.path().links().iterator();
Brian O'Connor66630c82014-10-02 21:08:19 -070091 ConnectPoint prev = links.next().dst();
alshabib902d41b2014-10-07 16:52:05 -070092 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Brian O'Connorf2dbde52014-10-10 16:20:24 -070093 // TODO Generate multiple batches
Brian O'Connor66630c82014-10-02 21:08:19 -070094 while (links.hasNext()) {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -080095 builder.matchInPort(prev.port());
Brian O'Connor66630c82014-10-02 21:08:19 -070096 Link link = links.next();
Jonathan Hartab17b272014-12-18 15:01:17 -080097 // if this is the last flow rule, apply the intent's treatments
alshabib5c05f862014-11-19 18:28:37 -080098 TrafficTreatment treatment =
alshabib5c05f862014-11-19 18:28:37 -080099 (links.hasNext() ? builder() : builder(intent.treatment()))
tomf5c9d922014-10-03 15:22:03 -0700100 .setOutput(link.src().port()).build();
alshabib902d41b2014-10-07 16:52:05 -0700101
tom9a693fd2014-10-03 11:32:19 -0700102 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
Brian O'Connor392619e2014-11-20 12:06:33 -0800103 builder.build(), treatment, 123, //FIXME 123
Jonathan Hartab17b272014-12-18 15:01:17 -0800104 appId,
105 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
106 0, true);
Brian O'Connor427a1762014-11-19 18:40:32 -0800107 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule,
108 intent.id().fingerprint()));
Brian O'Connor66630c82014-10-02 21:08:19 -0700109 prev = link.dst();
110 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800111 //FIXME this should change to new api.
112 return Lists.newArrayList(new FlowRuleBatchOperation(rules, null, 0));
Brian O'Connor66630c82014-10-02 21:08:19 -0700113 }
114
115 @Override
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700116 public List<FlowRuleBatchOperation> uninstall(PathIntent intent) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800117 LinkResourceAllocations allocatedResources = resourceService.getAllocations(intent.id());
118 if (allocatedResources != null) {
119 resourceService.releaseResources(allocatedResources);
120 }
Brian O'Connora4cab072014-10-03 18:46:39 -0700121 TrafficSelector.Builder builder =
tom85258ee2014-10-07 00:10:02 -0700122 DefaultTrafficSelector.builder(intent.selector());
123 Iterator<Link> links = intent.path().links().iterator();
Brian O'Connora4cab072014-10-03 18:46:39 -0700124 ConnectPoint prev = links.next().dst();
alshabib902d41b2014-10-07 16:52:05 -0700125 List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
Brian O'Connorf2dbde52014-10-10 16:20:24 -0700126 // TODO Generate multiple batches
Brian O'Connora4cab072014-10-03 18:46:39 -0700127 while (links.hasNext()) {
Pavlin Radoslavov320e6c92015-02-02 16:51:58 -0800128 builder.matchInPort(prev.port());
Brian O'Connora4cab072014-10-03 18:46:39 -0700129 Link link = links.next();
Jonathan Hartab17b272014-12-18 15:01:17 -0800130 // if this is the last flow rule, apply the intent's treatments
131 TrafficTreatment treatment =
alshabib2be9bea2014-11-24 18:26:07 -0500132 (links.hasNext() ? builder() : builder(intent.treatment()))
133 .setOutput(link.src().port()).build();
Brian O'Connora4cab072014-10-03 18:46:39 -0700134 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
Jonathan Hartab17b272014-12-18 15:01:17 -0800135 builder.build(), treatment, 123, appId,
136 new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
137 0, true);
Brian O'Connor427a1762014-11-19 18:40:32 -0800138 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule,
139 intent.id().fingerprint()));
Brian O'Connora4cab072014-10-03 18:46:39 -0700140 prev = link.dst();
141 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800142 // FIXME this should change to new api
143 return Lists.newArrayList(new FlowRuleBatchOperation(rules, null, 0));
Brian O'Connorcb900f42014-10-07 21:55:33 -0700144 }
145
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700146 @Override
147 public List<FlowRuleBatchOperation> replace(PathIntent oldIntent, PathIntent newIntent) {
148 // FIXME: implement this
149 List<FlowRuleBatchOperation> batches = Lists.newArrayList();
150 batches.addAll(uninstall(oldIntent));
151 batches.addAll(install(newIntent));
152 return batches;
153 }
154
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800155 /**
156 * Allocate resources required for an intent.
157 *
158 * @param intent intent to allocate resource for
159 * @return allocated resources if any are required, null otherwise
160 */
161 private LinkResourceAllocations allocateResources(PathIntent intent) {
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800162 LinkResourceRequest.Builder builder =
163 DefaultLinkResourceRequest.builder(intent.id(), intent.path().links());
164 for (Constraint constraint : intent.constraints()) {
165 builder.addConstraint(constraint);
166 }
167 LinkResourceRequest request = builder.build();
168 return request.resources().isEmpty() ? null : resourceService.requestResources(request);
Ray Milkeycaa450b2014-10-29 15:54:24 -0700169 }
Brian O'Connor66630c82014-10-02 21:08:19 -0700170}