blob: d6187cd6d78afc89fc8ff40630cfaeccf4f75a3f [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();
79 FlowRule rule = createFlowRule(intent.selector(), intent.treatment(), ingress, egress, isLast(links, i));
80 rules.add(rule);
81 }
82
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070083 return Collections.singletonList(new FlowRuleIntent(appId, null, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080084 }
85
86 private FlowRule createFlowRule(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
87 ConnectPoint ingress, ConnectPoint egress, boolean last) {
88 TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
89 .matchInPort(ingress.port())
90 .build();
91
92 TrafficTreatment.Builder treatmentBuilder;
93 if (last) {
94 treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
95 } else {
96 treatmentBuilder = DefaultTrafficTreatment.builder();
97 }
98 TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
99
100 return new DefaultFlowRule(ingress.deviceId(), selector, treatment, 123, appId, 0, true);
101 }
102
103 private boolean isLast(List<Link> links, int i) {
104 return i == links.size() - 2;
105 }
106}