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