blob: 26196b7643f9063a7912e5e7858bc914a957ee14 [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;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080043
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070044import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045import java.util.LinkedList;
46import java.util.List;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080048@Component(immediate = true)
49public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> {
50
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070051 private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class);
52
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected IntentExtensionService intentManager;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected CoreService coreService;
58
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080059 private ApplicationId appId;
60
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080061 @Activate
62 public void activate() {
63 appId = coreService.registerApplication("org.onosproject.net.intent");
64 intentManager.registerCompiler(OpticalPathIntent.class, this);
65 }
66
67 @Deactivate
68 public void deactivate() {
69 intentManager.unregisterCompiler(OpticalPathIntent.class);
70 }
71
72 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080073 public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070074 log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst());
75
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070076 // Create rules for forward and reverse path
77 List<FlowRule> rules = createRules(intent);
78 if (intent.isBidirectional()) {
79 rules.addAll(createReverseRules(intent));
80 }
81
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -070082 return Collections.singletonList(new FlowRuleIntent(appId,
83 intent.key(),
84 rules,
85 intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080086 }
87
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070088 /**
89 * Create rules for the forward path of the intent.
90 *
91 * @param intent the intent
92 * @return list of flow rules
93 */
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070094 private List<FlowRule> createRules(OpticalPathIntent intent) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080095 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
96 selectorBuilder.matchInPort(intent.src().port());
97
98 List<FlowRule> rules = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070099 ConnectPoint current = intent.src();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800100
101 for (Link link : intent.path().links()) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800102 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700103 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800104 treatmentBuilder.setOutput(link.src().port());
105
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700106 FlowRule rule = DefaultFlowRule.builder()
107 .forDevice(current.deviceId())
108 .withSelector(selectorBuilder.build())
109 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400110 .withPriority(intent.priority())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700111 .fromApp(appId)
112 .makePermanent()
113 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800114
115 rules.add(rule);
116
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700117 current = link.dst();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800118 selectorBuilder.matchInPort(link.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700119 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700120 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800121 }
122
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700123 // Build the egress ROADM rule
124 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800125 treatmentLast.setOutput(intent.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700126
127 FlowRule rule = new DefaultFlowRule.Builder()
128 .forDevice(intent.dst().deviceId())
129 .withSelector(selectorBuilder.build())
130 .withTreatment(treatmentLast.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400131 .withPriority(intent.priority())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700132 .fromApp(appId)
133 .makePermanent()
134 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800135 rules.add(rule);
136
137 return rules;
138 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700139
140 /**
141 * Create rules for the reverse path of the intent.
142 *
143 * @param intent the intent
144 * @return list of flow rules
145 */
146 private List<FlowRule> createReverseRules(OpticalPathIntent intent) {
147 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
148 selectorBuilder.matchInPort(intent.dst().port());
149
150 List<FlowRule> rules = new LinkedList<>();
151 ConnectPoint current = intent.dst();
152
153 for (Link link : Lists.reverse(intent.path().links())) {
154 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
155 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
156 treatmentBuilder.setOutput(link.dst().port());
157
158 FlowRule rule = DefaultFlowRule.builder()
159 .forDevice(current.deviceId())
160 .withSelector(selectorBuilder.build())
161 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400162 .withPriority(intent.priority())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700163 .fromApp(appId)
164 .makePermanent()
165 .build();
166
167 rules.add(rule);
168
169 current = link.src();
170 selectorBuilder.matchInPort(link.src().port());
171 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
172 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
173 }
174
175 // Build the egress ROADM rule
176 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
177 treatmentLast.setOutput(intent.src().port());
178
179 FlowRule rule = new DefaultFlowRule.Builder()
180 .forDevice(intent.src().deviceId())
181 .withSelector(selectorBuilder.build())
182 .withTreatment(treatmentLast.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400183 .withPriority(intent.priority())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700184 .fromApp(appId)
185 .makePermanent()
186 .build();
187 rules.add(rule);
188
189 return rules;
190 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800191}