blob: f4b11dbc4144a2496ff9cde4d7e2509cf87e4d64 [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;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070033import org.onosproject.net.flow.criteria.Criteria;
34import org.onosproject.net.flow.instructions.Instructions;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080035import org.onosproject.net.intent.FlowRuleIntent;
36import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentCompiler;
38import org.onosproject.net.intent.IntentExtensionService;
39import org.onosproject.net.intent.OpticalPathIntent;
Brian O'Connor6de2e202015-05-21 14:30:41 -070040import org.onosproject.net.resource.link.LinkResourceAllocations;
41import org.onosproject.net.resource.link.LinkResourceService;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080042
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070043import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080044import java.util.LinkedList;
45import java.util.List;
46import java.util.Set;
47
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048@Component(immediate = true)
49public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> {
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected IntentExtensionService intentManager;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected CoreService coreService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080058 protected LinkResourceService resourceService;
59
60 private ApplicationId appId;
61
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080062 @Activate
63 public void activate() {
64 appId = coreService.registerApplication("org.onosproject.net.intent");
65 intentManager.registerCompiler(OpticalPathIntent.class, this);
66 }
67
68 @Deactivate
69 public void deactivate() {
70 intentManager.unregisterCompiler(OpticalPathIntent.class);
71 }
72
73 @Override
74 public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable,
75 Set<LinkResourceAllocations> resources) {
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070076 return Collections.singletonList(
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070077 new FlowRuleIntent(appId, createRules(intent), intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080078 }
79
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070080 private List<FlowRule> createRules(OpticalPathIntent intent) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080081 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
82 selectorBuilder.matchInPort(intent.src().port());
83
84 List<FlowRule> rules = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070085 ConnectPoint current = intent.src();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080086
87 for (Link link : intent.path().links()) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080088 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070089 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080090 treatmentBuilder.setOutput(link.src().port());
91
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070092 FlowRule rule = DefaultFlowRule.builder()
93 .forDevice(current.deviceId())
94 .withSelector(selectorBuilder.build())
95 .withTreatment(treatmentBuilder.build())
96 .withPriority(100)
97 .fromApp(appId)
98 .makePermanent()
99 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800100
101 rules.add(rule);
102
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700103 current = link.dst();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800104 selectorBuilder.matchInPort(link.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700105 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800106 }
107
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700108 // Build the egress ROADM rule
109 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800110 treatmentLast.setOutput(intent.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700111
112 FlowRule rule = new DefaultFlowRule.Builder()
113 .forDevice(intent.dst().deviceId())
114 .withSelector(selectorBuilder.build())
115 .withTreatment(treatmentLast.build())
116 .withPriority(100)
117 .fromApp(appId)
118 .makePermanent()
119 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800120 rules.add(rule);
121
122 return rules;
123 }
124}