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