blob: 9066032cb6c3f9ef0aeaa8ec101f94291fa704d0 [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08003 *
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 */
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070016package org.onosproject.net.optical.intent.impl.compiler;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080017
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070018import com.google.common.collect.Lists;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Link;
28import org.onosproject.net.flow.DefaultFlowRule;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.FlowRule;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070034import org.onosproject.net.flow.criteria.Criteria;
35import org.onosproject.net.flow.instructions.Instructions;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080036import org.onosproject.net.intent.FlowRuleIntent;
37import org.onosproject.net.intent.Intent;
38import org.onosproject.net.intent.IntentCompiler;
39import org.onosproject.net.intent.IntentExtensionService;
40import org.onosproject.net.intent.OpticalPathIntent;
Luca Prete670ac5d2017-02-03 15:55:43 -080041import org.onosproject.net.intent.PathIntent;
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;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080049@Component(immediate = true)
50public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> {
51
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070052 private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class);
53
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService intentManager;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected CoreService coreService;
59
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080060 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
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080074 public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070075 log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst());
76
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070077 // Create rules for forward and reverse path
78 List<FlowRule> rules = createRules(intent);
79 if (intent.isBidirectional()) {
80 rules.addAll(createReverseRules(intent));
81 }
82
Luca Prete670ac5d2017-02-03 15:55:43 -080083 return Collections.singletonList(
84 new FlowRuleIntent(appId,
85 intent.key(),
86 rules,
87 intent.resources(),
88 PathIntent.ProtectionType.PRIMARY,
89 intent.resourceGroup()
90 )
91 );
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080092 }
93
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070094 /**
95 * Create rules for the forward path of the intent.
96 *
97 * @param intent the intent
98 * @return list of flow rules
99 */
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700100 private List<FlowRule> createRules(OpticalPathIntent intent) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800101 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
102 selectorBuilder.matchInPort(intent.src().port());
103
104 List<FlowRule> rules = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700105 ConnectPoint current = intent.src();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800106
107 for (Link link : intent.path().links()) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800108 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700109 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800110 treatmentBuilder.setOutput(link.src().port());
111
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700112 FlowRule rule = DefaultFlowRule.builder()
113 .forDevice(current.deviceId())
114 .withSelector(selectorBuilder.build())
115 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400116 .withPriority(intent.priority())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700117 .fromApp(appId)
118 .makePermanent()
119 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800120
121 rules.add(rule);
122
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700123 current = link.dst();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800124 selectorBuilder.matchInPort(link.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700125 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700126 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800127 }
128
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700129 // Build the egress ROADM rule
130 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800131 treatmentLast.setOutput(intent.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700132
133 FlowRule rule = new DefaultFlowRule.Builder()
134 .forDevice(intent.dst().deviceId())
135 .withSelector(selectorBuilder.build())
136 .withTreatment(treatmentLast.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400137 .withPriority(intent.priority())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700138 .fromApp(appId)
139 .makePermanent()
140 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800141 rules.add(rule);
142
143 return rules;
144 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700145
146 /**
147 * Create rules for the reverse path of the intent.
148 *
149 * @param intent the intent
150 * @return list of flow rules
151 */
152 private List<FlowRule> createReverseRules(OpticalPathIntent intent) {
153 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
154 selectorBuilder.matchInPort(intent.dst().port());
155
156 List<FlowRule> rules = new LinkedList<>();
157 ConnectPoint current = intent.dst();
158
159 for (Link link : Lists.reverse(intent.path().links())) {
160 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
161 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
162 treatmentBuilder.setOutput(link.dst().port());
163
164 FlowRule rule = DefaultFlowRule.builder()
165 .forDevice(current.deviceId())
166 .withSelector(selectorBuilder.build())
167 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400168 .withPriority(intent.priority())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700169 .fromApp(appId)
170 .makePermanent()
171 .build();
172
173 rules.add(rule);
174
175 current = link.src();
176 selectorBuilder.matchInPort(link.src().port());
177 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
178 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
179 }
180
181 // Build the egress ROADM rule
182 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
183 treatmentLast.setOutput(intent.src().port());
184
185 FlowRule rule = new DefaultFlowRule.Builder()
186 .forDevice(intent.src().deviceId())
187 .withSelector(selectorBuilder.build())
188 .withTreatment(treatmentLast.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400189 .withPriority(intent.priority())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700190 .fromApp(appId)
191 .makePermanent()
192 .build();
193 rules.add(rule);
194
195 return rules;
196 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800197}