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