blob: e7e0562467ff2ef5199ecae1cd4191a7ea520856 [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;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070042import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080044
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070045import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080046import java.util.LinkedList;
47import java.util.List;
48import java.util.Set;
49
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080050@Component(immediate = true)
51public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> {
52
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070053 private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class);
54
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected IntentExtensionService intentManager;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected CoreService coreService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080062 protected LinkResourceService resourceService;
63
64 private ApplicationId appId;
65
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080066 @Activate
67 public void activate() {
68 appId = coreService.registerApplication("org.onosproject.net.intent");
69 intentManager.registerCompiler(OpticalPathIntent.class, this);
70 }
71
72 @Deactivate
73 public void deactivate() {
74 intentManager.unregisterCompiler(OpticalPathIntent.class);
75 }
76
77 @Override
78 public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable,
79 Set<LinkResourceAllocations> resources) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070080 log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst());
81
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070082 return Collections.singletonList(
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070083 new FlowRuleIntent(appId, createRules(intent), intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080084 }
85
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070086 private List<FlowRule> createRules(OpticalPathIntent intent) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080087 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
88 selectorBuilder.matchInPort(intent.src().port());
89
90 List<FlowRule> rules = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070091 ConnectPoint current = intent.src();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080092
93 for (Link link : intent.path().links()) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080094 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070095 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080096 treatmentBuilder.setOutput(link.src().port());
97
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070098 FlowRule rule = DefaultFlowRule.builder()
99 .forDevice(current.deviceId())
100 .withSelector(selectorBuilder.build())
101 .withTreatment(treatmentBuilder.build())
102 .withPriority(100)
103 .fromApp(appId)
104 .makePermanent()
105 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800106
107 rules.add(rule);
108
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700109 current = link.dst();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800110 selectorBuilder.matchInPort(link.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700111 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700112 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800113 }
114
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700115 // Build the egress ROADM rule
116 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800117 treatmentLast.setOutput(intent.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700118
119 FlowRule rule = new DefaultFlowRule.Builder()
120 .forDevice(intent.dst().deviceId())
121 .withSelector(selectorBuilder.build())
122 .withTreatment(treatmentLast.build())
123 .withPriority(100)
124 .fromApp(appId)
125 .makePermanent()
126 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800127 rules.add(rule);
128
129 return rules;
130 }
131}