Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 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 | */ |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.intent.impl.compiler; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 17 | |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.ConnectPoint; |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.Device.Type; |
| 30 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 31 | import org.onosproject.net.Link; |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceService; |
| 33 | import org.onosproject.net.device.DeviceServiceAdapter; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultFlowRule; |
| 35 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 36 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onosproject.net.flow.FlowRule; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.criteria.Criteria; |
| 41 | import org.onosproject.net.flow.instructions.Instructions; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.FlowRuleIntent; |
| 43 | import org.onosproject.net.intent.Intent; |
| 44 | import org.onosproject.net.intent.IntentCompiler; |
| 45 | import org.onosproject.net.intent.IntentExtensionService; |
| 46 | import org.onosproject.net.intent.OpticalPathIntent; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 47 | import org.onosproject.net.intent.PathIntent; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 50 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 51 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 52 | import java.util.LinkedList; |
| 53 | import java.util.List; |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 54 | import java.util.Optional; |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 55 | import java.util.Set; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 56 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 57 | @Component(immediate = true) |
| 58 | public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> { |
| 59 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 60 | private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class); |
| 61 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected IntentExtensionService intentManager; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected CoreService coreService; |
| 67 | |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected DeviceService deviceService = new DeviceServiceAdapter(); |
| 70 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 71 | private ApplicationId appId; |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 72 | // Devices which are wavelength transparent and thus do not require wavelength-based match/actions |
| 73 | private static final Set<Type> TRANSPARENT_DEVICES = |
| 74 | ImmutableSet.of(Type.OPTICAL_AMPLIFIER, Type.FIBER_SWITCH); |
| 75 | // Devices which don't accept flow rules |
| 76 | private static final Set<Type> NO_FLOWRULE_DEVICES = |
| 77 | ImmutableSet.of(Type.OPTICAL_AMPLIFIER); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 78 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 79 | @Activate |
| 80 | public void activate() { |
| 81 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 82 | intentManager.registerCompiler(OpticalPathIntent.class, this); |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | public void deactivate() { |
| 87 | intentManager.unregisterCompiler(OpticalPathIntent.class); |
| 88 | } |
| 89 | |
| 90 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 91 | public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable) { |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 92 | log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst()); |
| 93 | |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 94 | // Create rules for forward and reverse path |
| 95 | List<FlowRule> rules = createRules(intent); |
| 96 | if (intent.isBidirectional()) { |
| 97 | rules.addAll(createReverseRules(intent)); |
| 98 | } |
| 99 | |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 100 | return Collections.singletonList( |
| 101 | new FlowRuleIntent(appId, |
| 102 | intent.key(), |
| 103 | rules, |
| 104 | intent.resources(), |
| 105 | PathIntent.ProtectionType.PRIMARY, |
| 106 | intent.resourceGroup() |
| 107 | ) |
| 108 | ); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 111 | /** |
| 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 Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 117 | private List<FlowRule> createRules(OpticalPathIntent intent) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 118 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 119 | selectorBuilder.matchInPort(intent.src().port()); |
| 120 | |
| 121 | List<FlowRule> rules = new LinkedList<>(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 122 | ConnectPoint current = intent.src(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 123 | |
| 124 | for (Link link : intent.path().links()) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 125 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 126 | if (!isTransparent(current.deviceId())) { |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 127 | treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda())); |
| 128 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 129 | treatmentBuilder.setOutput(link.src().port()); |
| 130 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 131 | FlowRule rule = DefaultFlowRule.builder() |
| 132 | .forDevice(current.deviceId()) |
| 133 | .withSelector(selectorBuilder.build()) |
| 134 | .withTreatment(treatmentBuilder.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 135 | .withPriority(intent.priority()) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 136 | .fromApp(appId) |
| 137 | .makePermanent() |
| 138 | .build(); |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 139 | selectorBuilder = DefaultTrafficSelector.builder(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 140 | |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 141 | if (!isNoFlowRule(current.deviceId())) { |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 142 | rules.add(rule); |
| 143 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 144 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 145 | current = link.dst(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 146 | selectorBuilder.matchInPort(link.dst().port()); |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 147 | if (!isTransparent(current.deviceId())) { |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 148 | selectorBuilder.add(Criteria.matchLambda(intent.lambda())); |
| 149 | selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType())); |
| 150 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 153 | // Build the egress ROADM rule |
| 154 | TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 155 | treatmentLast.setOutput(intent.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 156 | |
| 157 | FlowRule rule = new DefaultFlowRule.Builder() |
| 158 | .forDevice(intent.dst().deviceId()) |
| 159 | .withSelector(selectorBuilder.build()) |
| 160 | .withTreatment(treatmentLast.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 161 | .withPriority(intent.priority()) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 162 | .fromApp(appId) |
| 163 | .makePermanent() |
| 164 | .build(); |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 165 | |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 166 | if (!isNoFlowRule(intent.dst().deviceId())) { |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 167 | rules.add(rule); |
| 168 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 169 | |
| 170 | return rules; |
| 171 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Create rules for the reverse path of the intent. |
| 175 | * |
| 176 | * @param intent the intent |
| 177 | * @return list of flow rules |
| 178 | */ |
| 179 | private List<FlowRule> createReverseRules(OpticalPathIntent intent) { |
| 180 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 181 | selectorBuilder.matchInPort(intent.dst().port()); |
| 182 | |
| 183 | List<FlowRule> rules = new LinkedList<>(); |
| 184 | ConnectPoint current = intent.dst(); |
| 185 | |
| 186 | for (Link link : Lists.reverse(intent.path().links())) { |
| 187 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 188 | if (!isTransparent(current.deviceId())) { |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 189 | treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda())); |
| 190 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 191 | treatmentBuilder.setOutput(link.dst().port()); |
| 192 | |
| 193 | FlowRule rule = DefaultFlowRule.builder() |
| 194 | .forDevice(current.deviceId()) |
| 195 | .withSelector(selectorBuilder.build()) |
| 196 | .withTreatment(treatmentBuilder.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 197 | .withPriority(intent.priority()) |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 198 | .fromApp(appId) |
| 199 | .makePermanent() |
| 200 | .build(); |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 201 | selectorBuilder = DefaultTrafficSelector.builder(); |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 202 | |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 203 | if (!isNoFlowRule(current.deviceId())) { |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 204 | rules.add(rule); |
| 205 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 206 | |
| 207 | current = link.src(); |
| 208 | selectorBuilder.matchInPort(link.src().port()); |
Marc De Leenheer | 43b91ad | 2017-03-17 14:45:28 -0700 | [diff] [blame] | 209 | if (!isTransparent(current.deviceId())) { |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 210 | selectorBuilder.add(Criteria.matchLambda(intent.lambda())); |
| 211 | selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType())); |
| 212 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Build the egress ROADM rule |
| 216 | TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder(); |
| 217 | treatmentLast.setOutput(intent.src().port()); |
| 218 | |
| 219 | FlowRule rule = new DefaultFlowRule.Builder() |
| 220 | .forDevice(intent.src().deviceId()) |
| 221 | .withSelector(selectorBuilder.build()) |
| 222 | .withTreatment(treatmentLast.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 223 | .withPriority(intent.priority()) |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 224 | .fromApp(appId) |
| 225 | .makePermanent() |
| 226 | .build(); |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 227 | |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 228 | if (!isNoFlowRule(intent.src().deviceId())) { |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 229 | rules.add(rule); |
| 230 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 231 | |
| 232 | return rules; |
| 233 | } |
Marc De Leenheer | b6f95e2 | 2017-03-15 12:18:59 -0700 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * Returns true if device does not accept flow rules, false otherwise. |
| 237 | * |
| 238 | * @param deviceId the device |
| 239 | * @return true if device does not accept flow rule, false otherwise |
| 240 | */ |
| 241 | private boolean isNoFlowRule(DeviceId deviceId) { |
| 242 | return NO_FLOWRULE_DEVICES.contains( |
| 243 | Optional.ofNullable(deviceService.getDevice(deviceId)) |
| 244 | .map(Device::type) |
| 245 | .orElse(Type.OTHER)); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Returns true if device is wavelength transparent, false otherwise. |
| 250 | * |
| 251 | * @param deviceId the device |
| 252 | * @return true if wavelength transparent, false otherwise |
| 253 | */ |
| 254 | private boolean isTransparent(DeviceId deviceId) { |
| 255 | return TRANSPARENT_DEVICES.contains( |
| 256 | Optional.ofNullable(deviceService.getDevice(deviceId)) |
| 257 | .map(Device::type) |
| 258 | .orElse(Type.OTHER)); |
| 259 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 260 | } |