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