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 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.flow.DefaultFlowRule; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.FlowRule; |
| 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 33 | import org.onosproject.net.flow.criteria.Criteria; |
| 34 | import org.onosproject.net.flow.instructions.Instructions; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.FlowRuleIntent; |
| 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentCompiler; |
| 38 | import org.onosproject.net.intent.IntentExtensionService; |
| 39 | import org.onosproject.net.intent.OpticalPathIntent; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 40 | import org.onosproject.net.resource.LinkResourceAllocations; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 41 | import org.onosproject.net.resource.LinkResourceService; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 42 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 43 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 44 | import java.util.LinkedList; |
| 45 | import java.util.List; |
| 46 | import java.util.Set; |
| 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 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected IntentExtensionService intentManager; |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected CoreService coreService; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 58 | protected LinkResourceService resourceService; |
| 59 | |
| 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 |
| 74 | public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable, |
| 75 | Set<LinkResourceAllocations> resources) { |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 76 | return Collections.singletonList( |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 77 | new FlowRuleIntent(appId, createRules(intent), intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 80 | private List<FlowRule> createRules(OpticalPathIntent intent) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 81 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 82 | selectorBuilder.matchInPort(intent.src().port()); |
| 83 | |
| 84 | List<FlowRule> rules = new LinkedList<>(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 85 | ConnectPoint current = intent.src(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 86 | |
| 87 | for (Link link : intent.path().links()) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 88 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 89 | treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 90 | treatmentBuilder.setOutput(link.src().port()); |
| 91 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 92 | FlowRule rule = DefaultFlowRule.builder() |
| 93 | .forDevice(current.deviceId()) |
| 94 | .withSelector(selectorBuilder.build()) |
| 95 | .withTreatment(treatmentBuilder.build()) |
| 96 | .withPriority(100) |
| 97 | .fromApp(appId) |
| 98 | .makePermanent() |
| 99 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 100 | |
| 101 | rules.add(rule); |
| 102 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 103 | current = link.dst(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 104 | selectorBuilder.matchInPort(link.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 105 | selectorBuilder.add(Criteria.matchLambda(intent.lambda())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 108 | // Build the egress ROADM rule |
| 109 | TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 110 | treatmentLast.setOutput(intent.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame^] | 111 | |
| 112 | FlowRule rule = new DefaultFlowRule.Builder() |
| 113 | .forDevice(intent.dst().deviceId()) |
| 114 | .withSelector(selectorBuilder.build()) |
| 115 | .withTreatment(treatmentLast.build()) |
| 116 | .withPriority(100) |
| 117 | .fromApp(appId) |
| 118 | .makePermanent() |
| 119 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 120 | rules.add(rule); |
| 121 | |
| 122 | return rules; |
| 123 | } |
| 124 | } |