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; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 40 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 41 | import org.onosproject.net.resource.link.LinkResourceService; |
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 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 62 | protected LinkResourceService resourceService; |
| 63 | |
| 64 | private ApplicationId appId; |
| 65 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 66 | @Activate |
| 67 | public void activate() { |
| 68 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 69 | intentManager.registerCompiler(OpticalPathIntent.class, this); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
| 74 | intentManager.unregisterCompiler(OpticalPathIntent.class); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable, |
| 79 | Set<LinkResourceAllocations> resources) { |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 80 | log.debug("Compiling optical path intent between {} and {}", intent.src(), intent.dst()); |
| 81 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 82 | return Collections.singletonList( |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 83 | new FlowRuleIntent(appId, createRules(intent), intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 86 | private List<FlowRule> createRules(OpticalPathIntent intent) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 87 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 88 | selectorBuilder.matchInPort(intent.src().port()); |
| 89 | |
| 90 | List<FlowRule> rules = new LinkedList<>(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 91 | ConnectPoint current = intent.src(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 92 | |
| 93 | for (Link link : intent.path().links()) { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 94 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 95 | treatmentBuilder.add(Instructions.modL0Lambda(intent.lambda())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 96 | treatmentBuilder.setOutput(link.src().port()); |
| 97 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 98 | FlowRule rule = DefaultFlowRule.builder() |
| 99 | .forDevice(current.deviceId()) |
| 100 | .withSelector(selectorBuilder.build()) |
| 101 | .withTreatment(treatmentBuilder.build()) |
| 102 | .withPriority(100) |
| 103 | .fromApp(appId) |
| 104 | .makePermanent() |
| 105 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 106 | |
| 107 | rules.add(rule); |
| 108 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 109 | current = link.dst(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 110 | selectorBuilder.matchInPort(link.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 111 | selectorBuilder.add(Criteria.matchLambda(intent.lambda())); |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 112 | selectorBuilder.add(Criteria.matchOchSignalType(intent.signalType())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 115 | // Build the egress ROADM rule |
| 116 | TrafficTreatment.Builder treatmentLast = DefaultTrafficTreatment.builder(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 117 | treatmentLast.setOutput(intent.dst().port()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 118 | |
| 119 | FlowRule rule = new DefaultFlowRule.Builder() |
| 120 | .forDevice(intent.dst().deviceId()) |
| 121 | .withSelector(selectorBuilder.build()) |
| 122 | .withTreatment(treatmentLast.build()) |
| 123 | .withPriority(100) |
| 124 | .fromApp(appId) |
| 125 | .makePermanent() |
| 126 | .build(); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 127 | rules.add(rule); |
| 128 | |
| 129 | return rules; |
| 130 | } |
| 131 | } |