Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
| 18 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 21 | import java.util.List; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 22 | |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 28 | import org.onlab.onos.core.ApplicationId; |
| 29 | import org.onlab.onos.core.CoreService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.ConnectPoint; |
| 31 | import org.onlab.onos.net.Link; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 33 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.flow.FlowRule; |
| 36 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 39 | import org.onlab.onos.net.flow.FlowRuleService; |
| 40 | import org.onlab.onos.net.flow.TrafficSelector; |
| 41 | import org.onlab.onos.net.flow.TrafficTreatment; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 43 | import org.onlab.onos.net.intent.IntentInstaller; |
| 44 | import org.onlab.onos.net.intent.OpticalPathIntent; |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 45 | import org.onlab.onos.net.resource.DefaultLinkResourceRequest; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 46 | import org.onlab.onos.net.resource.Lambda; |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 47 | import org.onlab.onos.net.resource.LambdaResourceAllocation; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 48 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
| 49 | import org.onlab.onos.net.resource.LinkResourceRequest; |
| 50 | import org.onlab.onos.net.resource.LinkResourceService; |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 51 | import org.onlab.onos.net.resource.ResourceAllocation; |
| 52 | import org.onlab.onos.net.resource.ResourceType; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 53 | import org.onlab.onos.net.topology.TopologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | |
| 56 | import com.google.common.collect.Lists; |
| 57 | |
| 58 | /** |
| 59 | * OpticaliIntentInstaller for optical path intents. |
| 60 | * It essentially generates optical FlowRules and |
| 61 | * call the flowRule service to execute them. |
| 62 | */ |
| 63 | |
| 64 | @Component(immediate = true) |
| 65 | public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected IntentExtensionService intentManager; |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected FlowRuleService flowRuleService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected CoreService coreService; |
| 76 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected TopologyService topologyService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected LinkResourceService resourceService; |
| 82 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 83 | private ApplicationId appId; |
| 84 | |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 85 | //final short WAVELENGTH = 80; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 86 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
| 89 | appId = coreService.registerApplication("org.onlab.onos.net.intent"); |
| 90 | intentManager.registerInstaller(OpticalPathIntent.class, this); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | public void deactivate() { |
| 95 | intentManager.unregisterInstaller(OpticalPathIntent.class); |
| 96 | } |
| 97 | |
| 98 | @Override |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 99 | public List<FlowRuleBatchOperation> install(OpticalPathIntent intent) { |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 100 | LinkResourceAllocations allocations = assignWavelength(intent); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 101 | |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 102 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 103 | selectorBuilder.matchInport(intent.src().port()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 104 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 105 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 106 | ConnectPoint prev = intent.src(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 107 | |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 108 | //TODO throw exception if the lambda was not assigned successfully |
| 109 | for (Link link : intent.path().links()) { |
| 110 | Lambda la = null; |
| 111 | for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) { |
| 112 | if (allocation.type() == ResourceType.LAMBDA) { |
| 113 | la = ((LambdaResourceAllocation) allocation).lambda(); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (la == null) { |
| 119 | log.info("Lambda was not assigned successfully"); |
| 120 | return null; |
| 121 | } |
| 122 | |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 123 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 124 | treatmentBuilder.setOutput(link.src().port()); |
weibit | 2543d5a | 2014-10-23 14:13:35 -0700 | [diff] [blame] | 125 | treatmentBuilder.setLambda((short) la.toInt()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 126 | |
| 127 | FlowRule rule = new DefaultFlowRule(prev.deviceId(), |
| 128 | selectorBuilder.build(), |
| 129 | treatmentBuilder.build(), |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 130 | 100, |
| 131 | appId, |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 132 | 100, |
| 133 | true); |
weibit | aca1460 | 2014-10-24 10:26:26 -0700 | [diff] [blame] | 134 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 135 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 136 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 137 | prev = link.dst(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 138 | selectorBuilder.matchInport(link.dst().port()); |
weibit | 2543d5a | 2014-10-23 14:13:35 -0700 | [diff] [blame] | 139 | selectorBuilder.matchLambda((short) la.toInt()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 140 | } |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 141 | |
| 142 | // build the last T port rule |
| 143 | TrafficTreatment treatmentLast = builder() |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 144 | .setOutput(intent.dst().port()).build(); |
| 145 | FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(), |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 146 | selectorBuilder.build(), |
| 147 | treatmentLast, |
| 148 | 100, |
| 149 | appId, |
| 150 | 100, |
| 151 | true); |
| 152 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)); |
| 153 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 154 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
| 155 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 156 | |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 157 | private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) { |
| 158 | LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(), |
| 159 | intent.path().links()) |
| 160 | .addLambdaRequest(); |
| 161 | LinkResourceAllocations retLambda = resourceService.requestResources(request.build()); |
| 162 | return retLambda; |
| 163 | } |
| 164 | |
| 165 | /*private Lambda assignWavelength(List<Link> links) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 166 | // TODO More wavelength assignment algorithm |
| 167 | int wavenum = 0; |
| 168 | Iterator<Link> itrlink = links.iterator(); |
| 169 | for (int i = 1; i <= WAVELENGTH; i++) { |
| 170 | wavenum = i; |
| 171 | boolean found = true; |
| 172 | while (itrlink.hasNext()) { |
| 173 | Link link = itrlink.next(); |
| 174 | if (isWavelengthUsed(link, i)) { |
| 175 | found = false; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | // First-Fit wavelength assignment algorithm |
| 180 | if (found) { |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (wavenum == 0) { |
| 186 | return null; |
| 187 | } |
| 188 | |
| 189 | Lambda wave = Lambda.valueOf(wavenum); |
| 190 | return wave; |
| 191 | } |
| 192 | |
| 193 | private boolean isWavelengthUsed(Link link, int i) { |
| 194 | Iterable<LinkResourceAllocations> wave = resourceService.getAllocations(link); |
| 195 | for (LinkResourceAllocations ir : wave) { |
| 196 | //if ir.resources().contains(i) { |
| 197 | //} |
| 198 | } |
| 199 | return false; |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 200 | }*/ |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 201 | |
| 202 | @Override |
| 203 | public List<FlowRuleBatchOperation> uninstall(OpticalPathIntent intent) { |
Toshio Koide | 86160f5 | 2014-10-23 14:59:07 -0700 | [diff] [blame] | 204 | LinkResourceAllocations allocations = resourceService.getAllocations(intent.id()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 205 | |
| 206 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 207 | selectorBuilder.matchInport(intent.src().port()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 208 | |
| 209 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 210 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 211 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 212 | ConnectPoint prev = intent.src(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 213 | |
| 214 | //TODO throw exception if the lambda was not retrieved successfully |
| 215 | for (Link link : intent.path().links()) { |
| 216 | Lambda la = null; |
| 217 | for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) { |
| 218 | if (allocation.type() == ResourceType.LAMBDA) { |
| 219 | la = ((LambdaResourceAllocation) allocation).lambda(); |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (la == null) { |
| 225 | log.info("Lambda was not retrieved successfully"); |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | treatmentBuilder.setOutput(link.src().port()); |
weibit | 2543d5a | 2014-10-23 14:13:35 -0700 | [diff] [blame] | 230 | treatmentBuilder.setLambda((short) la.toInt()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 231 | |
| 232 | FlowRule rule = new DefaultFlowRule(prev.deviceId(), |
| 233 | selectorBuilder.build(), |
| 234 | treatmentBuilder.build(), |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 235 | 100, |
| 236 | appId, |
| 237 | 100, |
| 238 | true); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 239 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 240 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 241 | prev = link.dst(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 242 | selectorBuilder.matchInport(link.dst().port()); |
weibit | 2543d5a | 2014-10-23 14:13:35 -0700 | [diff] [blame] | 243 | selectorBuilder.matchLambda((short) la.toInt()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 244 | } |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 245 | |
| 246 | // build the last T port rule |
| 247 | TrafficTreatment treatmentLast = builder() |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 248 | .setOutput(intent.dst().port()).build(); |
| 249 | FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(), |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 250 | selectorBuilder.build(), |
| 251 | treatmentLast, |
| 252 | 100, |
| 253 | appId, |
| 254 | 100, |
| 255 | true); |
| 256 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)); |
| 257 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 258 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | } |