Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.ConnectPoint; |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.Device.Type; |
| 29 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Link; |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.device.DeviceServiceAdapter; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.DefaultFlowRule; |
| 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.FlowRule; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flow.criteria.Criteria; |
| 40 | import org.onosproject.net.flow.instructions.Instructions; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.FlowRuleIntent; |
| 42 | import org.onosproject.net.intent.Intent; |
| 43 | import org.onosproject.net.intent.IntentCompiler; |
| 44 | import org.onosproject.net.intent.IntentExtensionService; |
| 45 | import org.onosproject.net.intent.OpticalPathIntent; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.PathIntent; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 49 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 50 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 51 | import java.util.LinkedList; |
| 52 | import java.util.List; |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 53 | import java.util.Optional; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 54 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 55 | @Component(immediate = true) |
| 56 | public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> { |
| 57 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 58 | private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class); |
| 59 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected IntentExtensionService intentManager; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected CoreService coreService; |
| 65 | |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected DeviceService deviceService = new DeviceServiceAdapter(); |
| 68 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 69 | private ApplicationId appId; |
| 70 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 71 | @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 SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 83 | public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable) { |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 84 | log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst()); |
| 85 | |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 86 | // 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 Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 92 | 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 SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 103 | 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 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 | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 126 | treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 127 | treatmentBuilder.setOutput(link.src().port()); |
| 128 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 129 | FlowRule rule = DefaultFlowRule.builder() |
| 130 | .forDevice(current.deviceId()) |
| 131 | .withSelector(selectorBuilder.build()) |
| 132 | .withTreatment(treatmentBuilder.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 133 | .withPriority(intent.priority()) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 134 | .fromApp(appId) |
| 135 | .makePermanent() |
| 136 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 137 | |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 138 | if (!blockFlowRule(current.deviceId())) { |
| 139 | rules.add(rule); |
| 140 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 141 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 142 | current = link.dst(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 143 | selectorBuilder.matchInPort(link.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 144 | selectorBuilder.add(Criteria.matchLambda(intent.lambda())); |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 145 | selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 148 | // Build the egress ROADM rule |
| 149 | TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 150 | treatmentLast.setOutput(intent.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 151 | |
| 152 | FlowRule rule = new DefaultFlowRule.Builder() |
| 153 | .forDevice(intent.dst().deviceId()) |
| 154 | .withSelector(selectorBuilder.build()) |
| 155 | .withTreatment(treatmentLast.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 156 | .withPriority(intent.priority()) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 157 | .fromApp(appId) |
| 158 | .makePermanent() |
| 159 | .build(); |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 160 | |
| 161 | if (!blockFlowRule(intent.dst().deviceId())) { |
| 162 | rules.add(rule); |
| 163 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 164 | |
| 165 | return rules; |
| 166 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 167 | |
| 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'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 190 | .withPriority(intent.priority()) |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 191 | .fromApp(appId) |
| 192 | .makePermanent() |
| 193 | .build(); |
| 194 | |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 195 | if (!blockFlowRule(current.deviceId())) { |
| 196 | rules.add(rule); |
| 197 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 198 | |
| 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'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 213 | .withPriority(intent.priority()) |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 214 | .fromApp(appId) |
| 215 | .makePermanent() |
| 216 | .build(); |
Yuta HIGUCHI | 09697d0 | 2017-03-03 16:53:39 -0800 | [diff] [blame] | 217 | |
| 218 | if (!blockFlowRule(intent.src().deviceId())) { |
| 219 | rules.add(rule); |
| 220 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 221 | |
| 222 | return rules; |
| 223 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 224 | } |