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; |
| 33 | import org.onosproject.net.intent.FlowRuleIntent; |
| 34 | import org.onosproject.net.intent.Intent; |
| 35 | import org.onosproject.net.intent.IntentCompiler; |
| 36 | import org.onosproject.net.intent.IntentExtensionService; |
| 37 | import org.onosproject.net.intent.OpticalPathIntent; |
| 38 | import org.onosproject.net.intent.impl.IntentCompilationException; |
| 39 | import org.onosproject.net.resource.DefaultLinkResourceRequest; |
| 40 | import org.onosproject.net.resource.Lambda; |
| 41 | import org.onosproject.net.resource.LambdaResourceAllocation; |
| 42 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 43 | import org.onosproject.net.resource.LinkResourceRequest; |
| 44 | import org.onosproject.net.resource.LinkResourceService; |
| 45 | import org.onosproject.net.resource.ResourceAllocation; |
| 46 | import org.onosproject.net.resource.ResourceType; |
| 47 | import org.onosproject.net.topology.TopologyService; |
| 48 | |
| 49 | import java.util.Arrays; |
| 50 | import java.util.LinkedList; |
| 51 | import java.util.List; |
| 52 | import java.util.Set; |
| 53 | |
| 54 | import static org.onosproject.net.flow.DefaultTrafficTreatment.builder; |
| 55 | |
| 56 | @Component(immediate = true) |
| 57 | public class OpticalPathIntentCompiler implements IntentCompiler<OpticalPathIntent> { |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected IntentExtensionService intentManager; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected CoreService coreService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected TopologyService topologyService; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected LinkResourceService resourceService; |
| 70 | |
| 71 | private ApplicationId appId; |
| 72 | |
| 73 | static final short SIGNAL_TYPE = (short) 1; |
| 74 | |
| 75 | @Activate |
| 76 | public void activate() { |
| 77 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 78 | intentManager.registerCompiler(OpticalPathIntent.class, this); |
| 79 | } |
| 80 | |
| 81 | @Deactivate |
| 82 | public void deactivate() { |
| 83 | intentManager.unregisterCompiler(OpticalPathIntent.class); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public List<Intent> compile(OpticalPathIntent intent, List<Intent> installable, |
| 88 | Set<LinkResourceAllocations> resources) { |
| 89 | LinkResourceAllocations allocations = assignWavelength(intent); |
| 90 | |
| 91 | return Arrays.asList(new FlowRuleIntent(appId, createRules(intent, allocations))); |
| 92 | } |
| 93 | |
| 94 | private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) { |
| 95 | LinkResourceRequest.Builder request = DefaultLinkResourceRequest |
| 96 | .builder(intent.id(), intent.path().links()) |
| 97 | .addLambdaRequest(); |
| 98 | return resourceService.requestResources(request.build()); |
| 99 | } |
| 100 | |
| 101 | private List<FlowRule> createRules(OpticalPathIntent intent, LinkResourceAllocations allocations) { |
| 102 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 103 | selectorBuilder.matchInPort(intent.src().port()); |
| 104 | |
| 105 | List<FlowRule> rules = new LinkedList<>(); |
| 106 | ConnectPoint prev = intent.src(); |
| 107 | |
| 108 | for (Link link : intent.path().links()) { |
| 109 | ResourceAllocation allocation = allocations.getResourceAllocation(link).stream() |
| 110 | .filter(x -> x.type() == ResourceType.LAMBDA) |
| 111 | .findFirst() |
| 112 | .orElseThrow(() -> new IntentCompilationException("Lambda was not assigned successfully")); |
| 113 | Lambda la = ((LambdaResourceAllocation) allocation).lambda(); |
| 114 | |
| 115 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 116 | treatmentBuilder.setLambda((short) la.toInt()); |
| 117 | treatmentBuilder.setOutput(link.src().port()); |
| 118 | |
| 119 | FlowRule rule = new DefaultFlowRule(prev.deviceId(), |
| 120 | selectorBuilder.build(), |
| 121 | treatmentBuilder.build(), |
| 122 | 100, |
| 123 | appId, |
| 124 | 100, |
| 125 | true); |
| 126 | |
| 127 | rules.add(rule); |
| 128 | |
| 129 | prev = link.dst(); |
| 130 | selectorBuilder.matchInPort(link.dst().port()); |
| 131 | selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); |
| 132 | selectorBuilder.matchLambda((short) la.toInt()); |
| 133 | |
| 134 | } |
| 135 | |
| 136 | // build the last T port rule |
| 137 | TrafficTreatment.Builder treatmentLast = builder(); |
| 138 | treatmentLast.setOutput(intent.dst().port()); |
| 139 | FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(), |
| 140 | selectorBuilder.build(), |
| 141 | treatmentLast.build(), |
| 142 | 100, |
| 143 | appId, |
| 144 | 100, |
| 145 | true); |
| 146 | rules.add(rule); |
| 147 | |
| 148 | return rules; |
| 149 | } |
| 150 | } |