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 | |
| 6 | import java.util.Iterator; |
| 7 | import java.util.List; |
| 8 | import java.util.concurrent.Future; |
| 9 | |
| 10 | import org.apache.felix.scr.annotations.Activate; |
| 11 | import org.apache.felix.scr.annotations.Component; |
| 12 | import org.apache.felix.scr.annotations.Deactivate; |
| 13 | import org.apache.felix.scr.annotations.Reference; |
| 14 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 15 | import org.onlab.onos.ApplicationId; |
| 16 | import org.onlab.onos.CoreService; |
| 17 | import org.onlab.onos.net.ConnectPoint; |
| 18 | import org.onlab.onos.net.Link; |
| 19 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
| 20 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 21 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 22 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.flow.FlowRule; |
| 24 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 25 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 26 | import org.onlab.onos.net.flow.FlowRuleService; |
| 27 | import org.onlab.onos.net.flow.TrafficSelector; |
| 28 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 29 | import org.onlab.onos.net.flow.FlowRuleBatchEntry.FlowRuleOperation; |
| 30 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 31 | import org.onlab.onos.net.intent.IntentInstaller; |
| 32 | import org.onlab.onos.net.intent.OpticalPathIntent; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 33 | import org.onlab.onos.net.resource.Lambda; |
| 34 | import org.onlab.onos.net.resource.LinkResourceAllocations; |
| 35 | import org.onlab.onos.net.resource.LinkResourceRequest; |
| 36 | import org.onlab.onos.net.resource.LinkResourceService; |
| 37 | import org.onlab.onos.net.resource.ResourceRequest; |
| 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 | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 70 | final static short WAVELENGTH = 80; |
| 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) { |
| 85 | Lambda la = assignWavelength(intent.path().links()); |
| 86 | if (la == null) { |
| 87 | return null; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 88 | } |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 89 | // resourceService.requestResources(la); |
| 90 | // la.toInt(); |
| 91 | //intent.selector().criteria(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 92 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 93 | //TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
| 94 | //builder.matchLambdaType(la.toInt()) |
| 95 | // .matchInport(intent.getSrcConnectPoint().port()); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 96 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 97 | TrafficSelector.Builder builder = |
| 98 | DefaultTrafficSelector.builder(intent.selector()); |
| 99 | Iterator<Link> links = intent.path().links().iterator(); |
| 100 | ConnectPoint prev = links.next().dst(); |
| 101 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 102 | // TODO Generate multiple batches |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 103 | while (links.hasNext()) { |
| 104 | builder.matchInport(prev.port()); |
| 105 | Link link = links.next(); |
| 106 | TrafficTreatment treatment = builder() |
| 107 | .setOutput(link.src().port()).build(); |
| 108 | |
| 109 | FlowRule rule = new DefaultFlowRule(link.src().deviceId(), |
| 110 | builder.build(), |
| 111 | treatment, |
| 112 | 100, |
| 113 | appId, |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 114 | 100, |
| 115 | true); |
| 116 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule)); |
| 117 | prev = link.dst(); |
| 118 | } |
| 119 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
| 120 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 121 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 122 | private Lambda assignWavelength(List<Link> links) { |
| 123 | // TODO More wavelength assignment algorithm |
| 124 | int wavenum = 0; |
| 125 | Iterator<Link> itrlink = links.iterator(); |
| 126 | for (int i = 1; i <= WAVELENGTH; i++) { |
| 127 | wavenum = i; |
| 128 | boolean found = true; |
| 129 | while (itrlink.hasNext()) { |
| 130 | Link link = itrlink.next(); |
| 131 | if (isWavelengthUsed(link, i)) { |
| 132 | found = false; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | // First-Fit wavelength assignment algorithm |
| 137 | if (found) { |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (wavenum == 0) { |
| 143 | return null; |
| 144 | } |
| 145 | |
| 146 | Lambda wave = Lambda.valueOf(wavenum); |
| 147 | return wave; |
| 148 | } |
| 149 | |
| 150 | private boolean isWavelengthUsed(Link link, int i) { |
| 151 | Iterable<LinkResourceAllocations> wave = resourceService.getAllocations(link); |
| 152 | for (LinkResourceAllocations ir : wave) { |
| 153 | //if ir.resources().contains(i) { |
| 154 | //} |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public List<FlowRuleBatchOperation> uninstall(OpticalPathIntent intent) { |
| 161 | TrafficSelector.Builder builder = |
| 162 | DefaultTrafficSelector.builder(intent.selector()); |
| 163 | Iterator<Link> links = intent.path().links().iterator(); |
| 164 | ConnectPoint prev = links.next().dst(); |
| 165 | List<FlowRuleBatchEntry> rules = Lists.newLinkedList(); |
| 166 | // TODO Generate multiple batches |
| 167 | while (links.hasNext()) { |
| 168 | builder.matchInport(prev.port()); |
| 169 | Link link = links.next(); |
| 170 | TrafficTreatment treatment = builder() |
| 171 | .setOutput(link.src().port()).build(); |
| 172 | FlowRule rule = new DefaultFlowRule(link.src().deviceId(), |
| 173 | builder.build(), |
| 174 | treatment, |
| 175 | 100, |
| 176 | appId, |
| 177 | 100, |
| 178 | true); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 179 | rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule)); |
| 180 | prev = link.dst(); |
| 181 | } |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame^] | 182 | return Lists.newArrayList(new FlowRuleBatchOperation(rules)); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } |