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