blob: 7add217305dbe9f1b553ecdaf0633e05b389280c [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.net.intent.impl.compiler;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.Link;
27import org.onosproject.net.flow.DefaultFlowRule;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.FlowRule;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.FlowRuleIntent;
34import org.onosproject.net.intent.Intent;
35import org.onosproject.net.intent.IntentCompiler;
36import org.onosproject.net.intent.IntentExtensionService;
37import org.onosproject.net.intent.PathIntent;
Brian O'Connor6de2e202015-05-21 14:30:41 -070038import org.onosproject.net.resource.link.LinkResourceAllocations;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080039
40import java.util.ArrayList;
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070041import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080042import java.util.List;
43import java.util.Set;
44
45@Component(immediate = true)
46public class PathIntentCompiler implements IntentCompiler<PathIntent> {
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected CoreService coreService;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected IntentExtensionService intentManager;
53
54 private ApplicationId appId;
55
56 @Activate
57 public void activate() {
58 appId = coreService.registerApplication("org.onosproject.net.intent");
59 intentManager.registerCompiler(PathIntent.class, this);
60 }
61
62 @Deactivate
63 public void deactivate() {
64 intentManager.unregisterCompiler(PathIntent.class);
65 }
66
67 @Override
68 public List<Intent> compile(PathIntent intent, List<Intent> installable,
69 Set<LinkResourceAllocations> resources) {
70 // Note: right now recompile is not considered
71 // TODO: implement recompile behavior
72
73 List<Link> links = intent.path().links();
74 List<FlowRule> rules = new ArrayList<>(links.size() - 1);
75
76 for (int i = 0; i < links.size() - 1; i++) {
77 ConnectPoint ingress = links.get(i).dst();
78 ConnectPoint egress = links.get(i + 1).src();
Brian O'Connor81134662015-06-25 17:23:33 -040079 FlowRule rule = createFlowRule(intent.selector(), intent.treatment(),
80 ingress, egress, intent.priority(),
81 isLast(links, i));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080082 rules.add(rule);
83 }
84
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070085 return Collections.singletonList(new FlowRuleIntent(appId, null, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080086 }
87
88 private FlowRule createFlowRule(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
Brian O'Connor81134662015-06-25 17:23:33 -040089 ConnectPoint ingress, ConnectPoint egress,
90 int priority, boolean last) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080091 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
92 .matchInPort(ingress.port())
93 .build();
94
95 TrafficTreatment.Builder treatmentBuilder;
96 if (last) {
97 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
98 } else {
99 treatmentBuilder = DefaultTrafficTreatment.builder();
100 }
101 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
102
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700103 return DefaultFlowRule.builder()
104 .forDevice(ingress.deviceId())
105 .withSelector(selector)
106 .withTreatment(treatment)
Brian O'Connor81134662015-06-25 17:23:33 -0400107 .withPriority(priority)
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700108 .fromApp(appId)
109 .makePermanent()
110 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800111 }
112
113 private boolean isLast(List<Link> links, int i) {
114 return i == links.size() - 2;
115 }
116}