blob: b848dc1a403de15da6ad8bf96d140b09f00684d2 [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;
Yuta HIGUCHI09697d02017-03-03 16:53:39 -080027import org.onosproject.net.Device;
28import org.onosproject.net.Device.Type;
29import org.onosproject.net.DeviceId;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080030import org.onosproject.net.Link;
Yuta HIGUCHI09697d02017-03-03 16:53:39 -080031import org.onosproject.net.device.DeviceService;
32import org.onosproject.net.device.DeviceServiceAdapter;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080033import org.onosproject.net.flow.DefaultFlowRule;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070039import org.onosproject.net.flow.criteria.Criteria;
40import org.onosproject.net.flow.instructions.Instructions;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080041import org.onosproject.net.intent.FlowRuleIntent;
42import org.onosproject.net.intent.Intent;
43import org.onosproject.net.intent.IntentCompiler;
44import org.onosproject.net.intent.IntentExtensionService;
45import org.onosproject.net.intent.OpticalPathIntent;
Luca Prete670ac5d2017-02-03 15:55:43 -080046import org.onosproject.net.intent.PathIntent;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080049
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070050import java.util.Collections;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080051import java.util.LinkedList;
52import java.util.List;
Yuta HIGUCHI09697d02017-03-03 16:53:39 -080053import java.util.Optional;
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080054
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080055@Component(immediate = true)
56public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> {
57
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070058 private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class);
59
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080060 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected IntentExtensionService intentManager;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected CoreService coreService;
65
Yuta HIGUCHI09697d02017-03-03 16:53:39 -080066 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected DeviceService deviceService = new DeviceServiceAdapter();
68
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080069 private ApplicationId appId;
70
Sho SHIMIZUee2aa652015-02-25 18:56:43 -080071 @Activate
72 public void activate() {
73 appId = coreService.registerApplication("org.onosproject.net.intent");
74 intentManager.registerCompiler(OpticalPathIntent.class, this);
75 }
76
77 @Deactivate
78 public void deactivate() {
79 intentManager.unregisterCompiler(OpticalPathIntent.class);
80 }
81
82 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080083 public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable) {
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070084 log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst());
85
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -070086 // Create rules for forward and reverse path
87 List<FlowRule> rules = createRules(intent);
88 if (intent.isBidirectional()) {
89 rules.addAll(createReverseRules(intent));
90 }
91
Luca Prete670ac5d2017-02-03 15:55:43 -080092 return Collections.singletonList(
93 new FlowRuleIntent(appId,
94 intent.key(),
95 rules,
96 intent.resources(),
97 PathIntent.ProtectionType.PRIMARY,
98 intent.resourceGroup()
99 )
100 );
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800101 }
102
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800103 private boolean blockFlowRule(DeviceId deviceId) {
104 Type devType = Optional.ofNullable(deviceService.getDevice(deviceId))
105 .map(Device::type)
106 .orElse(Device.Type.OTHER);
107 // TODO add condition for protection switch
108 return devType == Device.Type.OPTICAL_AMPLIFIER;
109 }
110
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700111 /**
112 * Create rules for the forward path of the intent.
113 *
114 * @param intent the intent
115 * @return list of flow rules
116 */
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700117 private List<FlowRule> createRules(OpticalPathIntent intent) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800118 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
119 selectorBuilder.matchInPort(intent.src().port());
120
121 List<FlowRule> rules = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700122 ConnectPoint current = intent.src();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800123
124 for (Link link : intent.path().links()) {
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800125 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700126 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800127 treatmentBuilder.setOutput(link.src().port());
128
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700129 FlowRule rule = DefaultFlowRule.builder()
130 .forDevice(current.deviceId())
131 .withSelector(selectorBuilder.build())
132 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400133 .withPriority(intent.priority())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700134 .fromApp(appId)
135 .makePermanent()
136 .build();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800137
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800138 if (!blockFlowRule(current.deviceId())) {
139 rules.add(rule);
140 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800141
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700142 current = link.dst();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800143 selectorBuilder.matchInPort(link.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700144 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700145 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800146 }
147
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700148 // Build the egress ROADM rule
149 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800150 treatmentLast.setOutput(intent.dst().port());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700151
152 FlowRule rule = new DefaultFlowRule.Builder()
153 .forDevice(intent.dst().deviceId())
154 .withSelector(selectorBuilder.build())
155 .withTreatment(treatmentLast.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400156 .withPriority(intent.priority())
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700157 .fromApp(appId)
158 .makePermanent()
159 .build();
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800160
161 if (!blockFlowRule(intent.dst().deviceId())) {
162 rules.add(rule);
163 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800164
165 return rules;
166 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700167
168 /**
169 * Create rules for the reverse path of the intent.
170 *
171 * @param intent the intent
172 * @return list of flow rules
173 */
174 private List<FlowRule> createReverseRules(OpticalPathIntent intent) {
175 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
176 selectorBuilder.matchInPort(intent.dst().port());
177
178 List<FlowRule> rules = new LinkedList<>();
179 ConnectPoint current = intent.dst();
180
181 for (Link link : Lists.reverse(intent.path().links())) {
182 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
183 treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda()));
184 treatmentBuilder.setOutput(link.dst().port());
185
186 FlowRule rule = DefaultFlowRule.builder()
187 .forDevice(current.deviceId())
188 .withSelector(selectorBuilder.build())
189 .withTreatment(treatmentBuilder.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400190 .withPriority(intent.priority())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700191 .fromApp(appId)
192 .makePermanent()
193 .build();
194
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800195 if (!blockFlowRule(current.deviceId())) {
196 rules.add(rule);
197 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700198
199 current = link.src();
200 selectorBuilder.matchInPort(link.src().port());
201 selectorBuilder.add(Criteria.matchLambda(intent.lambda()));
202 selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType()));
203 }
204
205 // Build the egress ROADM rule
206 TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder();
207 treatmentLast.setOutput(intent.src().port());
208
209 FlowRule rule = new DefaultFlowRule.Builder()
210 .forDevice(intent.src().deviceId())
211 .withSelector(selectorBuilder.build())
212 .withTreatment(treatmentLast.build())
Brian O'Connor81134662015-06-25 17:23:33 -0400213 .withPriority(intent.priority())
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700214 .fromApp(appId)
215 .makePermanent()
216 .build();
Yuta HIGUCHI09697d02017-03-03 16:53:39 -0800217
218 if (!blockFlowRule(intent.src().deviceId())) {
219 rules.add(rule);
220 }
Marc De Leenheer4a1c1fa2015-06-01 18:08:56 -0700221
222 return rules;
223 }
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800224}