blob: 3136204fb009c2b0f40211843b606ca6fbb621ef [file] [log] [blame]
Sho SHIMIZUee2aa652015-02-25 18:56:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-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
Marc De Leenheere48f84e2015-07-02 16:06:50 -070082 return Collections.singletonList(new FlowRuleIntent(appId, rules, intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080083 }
84
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070085 /**
86 * Create rules for the forward path of the intent.
87 *
88 * @param intent the intent
89 * @return list of flow rules
90 */
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070091 private List<FlowRule> createRules(OpticalPathIntent intent) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080092 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
93 selectorBuilder.matchInPort(intent.src().port());
94
95 List<FlowRule> rules = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070096 ConnectPoint current = intent.src();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080097
98 for (Link link : intent.path().links()) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080099 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700100 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800101 treatmentBuilder.setOutput(link.src().port());
102
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700103 FlowRule rule = DefaultFlowRule.builder()
104 .forDevice(current.deviceId())
105 .withSelector(selectorBuilder.build())
106 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400107 .withPriority(intent.priority())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700108 .fromApp(appId)
109 .makePermanent()
110 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800111
112 rules.add(rule);
113
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700114 current = link.dst();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800115 selectorBuilder.matchInPort(link.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700116 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700117 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800118 }
119
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700120 // Build the egress ROADM rule
121 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800122 treatmentLast.setOutput(intent.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700123
124 FlowRule rule = new DefaultFlowRule.Builder()
125 .forDevice(intent.dst().deviceId())
126 .withSelector(selectorBuilder.build())
127 .withTreatment(treatmentLast.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400128 .withPriority(intent.priority())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700129 .fromApp(appId)
130 .makePermanent()
131 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800132 rules.add(rule);
133
134 return rules;
135 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700136
137 /**
138 * Create rules for the reverse path of the intent.
139 *
140 * @param intent the intent
141 * @return list of flow rules
142 */
143 private List<FlowRule> createReverseRules(OpticalPathIntent intent) {
144 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
145 selectorBuilder.matchInPort(intent.dst().port());
146
147 List<FlowRule> rules = new LinkedList<>();
148 ConnectPoint current = intent.dst();
149
150 for (Link link : Lists.reverse(intent.path().links())) {
151 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
152 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
153 treatmentBuilder.setOutput(link.dst().port());
154
155 FlowRule rule = DefaultFlowRule.builder()
156 .forDevice(current.deviceId())
157 .withSelector(selectorBuilder.build())
158 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400159 .withPriority(intent.priority())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700160 .fromApp(appId)
161 .makePermanent()
162 .build();
163
164 rules.add(rule);
165
166 current = link.src();
167 selectorBuilder.matchInPort(link.src().port());
168 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
169 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
170 }
171
172 // Build the egress ROADM rule
173 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
174 treatmentLast.setOutput(intent.src().port());
175
176 FlowRule rule = new DefaultFlowRule.Builder()
177 .forDevice(intent.src().deviceId())
178 .withSelector(selectorBuilder.build())
179 .withTreatment(treatmentLast.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400180 .withPriority(intent.priority())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700181 .fromApp(appId)
182 .makePermanent()
183 .build();
184 rules.add(rule);
185
186 return rules;
187 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800188}