blob: 4cc93bb789a36aaf49b207d225912d19427dd069 [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
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;
Brian O'Connor6de2e202015-05-21 14:30:41 -070041import org.onosproject.net.resource.link.LinkResourceAllocations;
42import org.onosproject.net.resource.link.LinkResourceService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080045
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070046import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080047import java.util.LinkedList;
48import java.util.List;
49import java.util.Set;
50
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080051@Component(immediate = true)
52public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> {
53
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070054 private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class);
55
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected IntentExtensionService intentManager;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected CoreService coreService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080063 protected LinkResourceService resourceService;
64
65 private ApplicationId appId;
66
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080067 @Activate
68 public void activate() {
69 appId = coreService.registerApplication("org.onosproject.net.intent");
70 intentManager.registerCompiler(OpticalPathIntent.class, this);
71 }
72
73 @Deactivate
74 public void deactivate() {
75 intentManager.unregisterCompiler(OpticalPathIntent.class);
76 }
77
78 @Override
79 public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable,
80 Set<LinkResourceAllocations> resources) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070081 log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst());
82
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070083 // Create rules for forward and reverse path
84 List<FlowRule> rules = createRules(intent);
85 if (intent.isBidirectional()) {
86 rules.addAll(createReverseRules(intent));
87 }
88
89 return Collections.singletonList(new FlowRuleIntent(appId, createRules(intent), intent.resources()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080090 }
91
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070092 /**
93 * Create rules for the forward path of the intent.
94 *
95 * @param intent the intent
96 * @return list of flow rules
97 */
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070098 private List<FlowRule> createRules(OpticalPathIntent intent) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080099 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
100 selectorBuilder.matchInPort(intent.src().port());
101
102 List<FlowRule> rules = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700103 ConnectPoint current = intent.src();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800104
105 for (Link link : intent.path().links()) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800106 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700107 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800108 treatmentBuilder.setOutput(link.src().port());
109
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700110 FlowRule rule = DefaultFlowRule.builder()
111 .forDevice(current.deviceId())
112 .withSelector(selectorBuilder.build())
113 .withTreatment(treatmentBuilder.build())
114 .withPriority(100)
115 .fromApp(appId)
116 .makePermanent()
117 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800118
119 rules.add(rule);
120
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700121 current = link.dst();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800122 selectorBuilder.matchInPort(link.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700123 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700124 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800125 }
126
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700127 // Build the egress ROADM rule
128 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800129 treatmentLast.setOutput(intent.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700130
131 FlowRule rule = new DefaultFlowRule.Builder()
132 .forDevice(intent.dst().deviceId())
133 .withSelector(selectorBuilder.build())
134 .withTreatment(treatmentLast.build())
135 .withPriority(100)
136 .fromApp(appId)
137 .makePermanent()
138 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800139 rules.add(rule);
140
141 return rules;
142 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700143
144 /**
145 * Create rules for the reverse path of the intent.
146 *
147 * @param intent the intent
148 * @return list of flow rules
149 */
150 private List<FlowRule> createReverseRules(OpticalPathIntent intent) {
151 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
152 selectorBuilder.matchInPort(intent.dst().port());
153
154 List<FlowRule> rules = new LinkedList<>();
155 ConnectPoint current = intent.dst();
156
157 for (Link link : Lists.reverse(intent.path().links())) {
158 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
159 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
160 treatmentBuilder.setOutput(link.dst().port());
161
162 FlowRule rule = DefaultFlowRule.builder()
163 .forDevice(current.deviceId())
164 .withSelector(selectorBuilder.build())
165 .withTreatment(treatmentBuilder.build())
166 .withPriority(100)
167 .fromApp(appId)
168 .makePermanent()
169 .build();
170
171 rules.add(rule);
172
173 current = link.src();
174 selectorBuilder.matchInPort(link.src().port());
175 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
176 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
177 }
178
179 // Build the egress ROADM rule
180 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
181 treatmentLast.setOutput(intent.src().port());
182
183 FlowRule rule = new DefaultFlowRule.Builder()
184 .forDevice(intent.src().deviceId())
185 .withSelector(selectorBuilder.build())
186 .withTreatment(treatmentLast.build())
187 .withPriority(100)
188 .fromApp(appId)
189 .makePermanent()
190 .build();
191 rules.add(rule);
192
193 return rules;
194 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800195}