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; |
| 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.flow.DefaultFlowRule; |
| 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onosproject.net.flow.FlowRule; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.criteria.Criteria; |
| 35 | import org.onosproject.net.flow.instructions.Instructions; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.FlowRuleIntent; |
| 37 | import org.onosproject.net.intent.Intent; |
| 38 | import org.onosproject.net.intent.IntentCompiler; |
| 39 | import org.onosproject.net.intent.IntentExtensionService; |
| 40 | import org.onosproject.net.intent.OpticalPathIntent; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.PathIntent; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 44 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 45 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 46 | import java.util.LinkedList; |
| 47 | import java.util.List; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 48 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 49 | @Component(immediate = true) |
| 50 | public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> { |
| 51 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 52 | private static final Logger log = LoggerFactory.getLogger(OpticalPathIntentCompiler.class); |
| 53 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected IntentExtensionService intentManager; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected CoreService coreService; |
| 59 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 60 | private ApplicationId appId; |
| 61 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 62 | @Activate |
| 63 | public void activate() { |
| 64 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 65 | intentManager.registerCompiler(OpticalPathIntent.class, this); |
| 66 | } |
| 67 | |
| 68 | @Deactivate |
| 69 | public void deactivate() { |
| 70 | intentManager.unregisterCompiler(OpticalPathIntent.class); |
| 71 | } |
| 72 | |
| 73 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 74 | public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable) { |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 75 | log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst()); |
| 76 | |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 77 | // Create rules for forward and reverse path |
| 78 | List<FlowRule> rules = createRules(intent); |
| 79 | if (intent.isBidirectional()) { |
| 80 | rules.addAll(createReverseRules(intent)); |
| 81 | } |
| 82 | |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 83 | return Collections.singletonList( |
| 84 | new FlowRuleIntent(appId, |
| 85 | intent.key(), |
| 86 | rules, |
| 87 | intent.resources(), |
| 88 | PathIntent.ProtectionType.PRIMARY, |
| 89 | intent.resourceGroup() |
| 90 | ) |
| 91 | ); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Create rules for the forward path of the intent. |
| 96 | * |
| 97 | * @param intent the intent |
| 98 | * @return list of flow rules |
| 99 | */ |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 100 | private List<FlowRule> createRules(OpticalPathIntent intent) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 101 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 102 | selectorBuilder.matchInPort(intent.src().port()); |
| 103 | |
| 104 | List<FlowRule> rules = new LinkedList<>(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 105 | ConnectPoint current = intent.src(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 106 | |
| 107 | for (Link link : intent.path().links()) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 108 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 109 | treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 110 | treatmentBuilder.setOutput(link.src().port()); |
| 111 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 112 | FlowRule rule = DefaultFlowRule.builder() |
| 113 | .forDevice(current.deviceId()) |
| 114 | .withSelector(selectorBuilder.build()) |
| 115 | .withTreatment(treatmentBuilder.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 116 | .withPriority(intent.priority()) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 117 | .fromApp(appId) |
| 118 | .makePermanent() |
| 119 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 120 | |
| 121 | rules.add(rule); |
| 122 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 123 | current = link.dst(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 124 | selectorBuilder.matchInPort(link.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 125 | selectorBuilder.add(Criteria.matchLambda(intent.lambda())); |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 126 | selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 129 | // Build the egress ROADM rule |
| 130 | TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 131 | treatmentLast.setOutput(intent.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 132 | |
| 133 | FlowRule rule = new DefaultFlowRule.Builder() |
| 134 | .forDevice(intent.dst().deviceId()) |
| 135 | .withSelector(selectorBuilder.build()) |
| 136 | .withTreatment(treatmentLast.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 137 | .withPriority(intent.priority()) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 138 | .fromApp(appId) |
| 139 | .makePermanent() |
| 140 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 141 | rules.add(rule); |
| 142 | |
| 143 | return rules; |
| 144 | } |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Create rules for the reverse path of the intent. |
| 148 | * |
| 149 | * @param intent the intent |
| 150 | * @return list of flow rules |
| 151 | */ |
| 152 | private List<FlowRule> createReverseRules(OpticalPathIntent intent) { |
| 153 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 154 | selectorBuilder.matchInPort(intent.dst().port()); |
| 155 | |
| 156 | List<FlowRule> rules = new LinkedList<>(); |
| 157 | ConnectPoint current = intent.dst(); |
| 158 | |
| 159 | for (Link link : Lists.reverse(intent.path().links())) { |
| 160 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 161 | treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda())); |
| 162 | treatmentBuilder.setOutput(link.dst().port()); |
| 163 | |
| 164 | FlowRule rule = DefaultFlowRule.builder() |
| 165 | .forDevice(current.deviceId()) |
| 166 | .withSelector(selectorBuilder.build()) |
| 167 | .withTreatment(treatmentBuilder.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 168 | .withPriority(intent.priority()) |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 169 | .fromApp(appId) |
| 170 | .makePermanent() |
| 171 | .build(); |
| 172 | |
| 173 | rules.add(rule); |
| 174 | |
| 175 | current = link.src(); |
| 176 | selectorBuilder.matchInPort(link.src().port()); |
| 177 | selectorBuilder.add(Criteria.matchLambda(intent.lambda())); |
| 178 | selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType())); |
| 179 | } |
| 180 | |
| 181 | // Build the egress ROADM rule |
| 182 | TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder(); |
| 183 | treatmentLast.setOutput(intent.src().port()); |
| 184 | |
| 185 | FlowRule rule = new DefaultFlowRule.Builder() |
| 186 | .forDevice(intent.src().deviceId()) |
| 187 | .withSelector(selectorBuilder.build()) |
| 188 | .withTreatment(treatmentLast.build()) |
Brian O'Connor | 8113466 | 2015-06-25 17:23:33 -0400 | [diff] [blame] | 189 | .withPriority(intent.priority()) |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 190 | .fromApp(appId) |
| 191 | .makePermanent() |
| 192 | .build(); |
| 193 | rules.add(rule); |
| 194 | |
| 195 | return rules; |
| 196 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 197 | } |