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; |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 82 | static final short SIGNAL_TYPE = (short) 1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 83 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 84 | @Activate |
| 85 | public void activate() { |
| 86 | appId = coreService.registerApplication("org.onlab.onos.net.intent"); |
| 87 | intentManager.registerInstaller(OpticalPathIntent.class, this); |
| 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | public void deactivate() { |
| 92 | intentManager.unregisterInstaller(OpticalPathIntent.class); |
| 93 | } |
| 94 | |
| 95 | @Override |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 96 | public List<FlowRuleBatchOperation> install(OpticalPathIntent intent) { |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 97 | LinkResourceAllocations allocations = assignWavelength(intent); |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 98 | return generateRules(intent, allocations, FlowRuleOperation.ADD); |
| 99 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 100 | |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 101 | @Override |
| 102 | public List<FlowRuleBatchOperation> uninstall(OpticalPathIntent intent) { |
| 103 | LinkResourceAllocations allocations = resourceService.getAllocations(intent.id()); |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 104 | List<FlowRuleBatchOperation> rules = generateRules(intent, allocations, FlowRuleOperation.REMOVE); |
| 105 | log.info("uninstall rules: {}", rules); |
| 106 | return rules; |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 109 | @Override |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 110 | public List<FlowRuleBatchOperation> replace(OpticalPathIntent oldIntent, |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 111 | OpticalPathIntent newIntent) { |
| 112 | // FIXME: implement this |
Brian O'Connor | 772852a | 2014-11-17 15:51:19 -0800 | [diff] [blame] | 113 | List<FlowRuleBatchOperation> batches = Lists.newArrayList(); |
| 114 | batches.addAll(uninstall(oldIntent)); |
| 115 | batches.addAll(install(newIntent)); |
| 116 | return batches; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 119 | private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) { |
| 120 | LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(), |
| 121 | intent.path().links()) |
| 122 | .addLambdaRequest(); |
| 123 | LinkResourceAllocations retLambda = resourceService.requestResources(request.build()); |
| 124 | return retLambda; |
| 125 | } |
| 126 | |
| 127 | private List<FlowRuleBatchOperation> generateRules(OpticalPathIntent intent, |
| 128 | LinkResourceAllocations allocations, |
| 129 | FlowRuleOperation operation) { |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 130 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 131 | selectorBuilder.matchInport(intent.src().port()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 132 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 133 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 134 | ConnectPoint prev = intent.src(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 135 | |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 136 | //TODO throw exception if the lambda was not assigned successfully |
| 137 | for (Link link : intent.path().links()) { |
| 138 | Lambda la = null; |
| 139 | for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) { |
| 140 | if (allocation.type() == ResourceType.LAMBDA) { |
| 141 | la = ((LambdaResourceAllocation) allocation).lambda(); |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (la == null) { |
| 147 | log.info("Lambda was not assigned successfully"); |
| 148 | return null; |
| 149 | } |
| 150 | |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 151 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 152 | treatmentBuilder.setOutput(link.src().port()); |
weibit | 2543d5a | 2014-10-23 14:13:35 -0700 | [diff] [blame] | 153 | treatmentBuilder.setLambda((short) la.toInt()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 154 | |
| 155 | FlowRule rule = new DefaultFlowRule(prev.deviceId(), |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 156 | selectorBuilder.build(), |
| 157 | treatmentBuilder.build(), |
| 158 | 100, |
| 159 | appId, |
| 160 | 100, |
| 161 | true); |
weibit | aca1460 | 2014-10-24 10:26:26 -0700 | [diff] [blame] | 162 | |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 163 | rules.add(new FlowRuleBatchEntry(operation, rule)); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 164 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 165 | prev = link.dst(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 166 | selectorBuilder.matchInport(link.dst().port()); |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 167 | selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); //todo |
weibit | 2543d5a | 2014-10-23 14:13:35 -0700 | [diff] [blame] | 168 | selectorBuilder.matchLambda((short) la.toInt()); |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 169 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 170 | } |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 171 | |
| 172 | // build the last T port rule |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 173 | TrafficTreatment.Builder treatmentLast = builder(); |
| 174 | treatmentLast.setOutput(intent.dst().port()); |
Brian O'Connor | 086724e | 2014-10-23 15:47:32 -0700 | [diff] [blame] | 175 | FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(), |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 176 | selectorBuilder.build(), |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 177 | treatmentLast.build(), |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 178 | 100, |
| 179 | appId, |
| 180 | 100, |
| 181 | true); |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 182 | rules.add(new FlowRuleBatchEntry(operation, rule)); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 183 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 184 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
| 185 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 186 | |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 187 | /*private Lambda assignWavelength(List<Link> links) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 188 | // TODO More wavelength assignment algorithm |
| 189 | int wavenum = 0; |
| 190 | Iterator<Link> itrlink = links.iterator(); |
| 191 | for (int i = 1; i <= WAVELENGTH; i++) { |
| 192 | wavenum = i; |
| 193 | boolean found = true; |
| 194 | while (itrlink.hasNext()) { |
| 195 | Link link = itrlink.next(); |
| 196 | if (isWavelengthUsed(link, i)) { |
| 197 | found = false; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | // First-Fit wavelength assignment algorithm |
| 202 | if (found) { |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (wavenum == 0) { |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | Lambda wave = Lambda.valueOf(wavenum); |
| 212 | return wave; |
| 213 | } |
| 214 | |
| 215 | private boolean isWavelengthUsed(Link link, int i) { |
| 216 | Iterable<LinkResourceAllocations> wave = resourceService.getAllocations(link); |
| 217 | for (LinkResourceAllocations ir : wave) { |
| 218 | //if ir.resources().contains(i) { |
| 219 | //} |
| 220 | } |
| 221 | return false; |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 222 | }*/ |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 223 | } |