weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.net.intent.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.ArrayList; |
| 6 | |
| 7 | import java.util.List; |
| 8 | import java.util.Set; |
| 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.net.ConnectPoint; |
| 16 | import org.onlab.onos.net.DefaultEdgeLink; |
| 17 | |
| 18 | import org.onlab.onos.net.Link; |
| 19 | import org.onlab.onos.net.Path; |
| 20 | import org.onlab.onos.net.flow.TrafficSelector; |
| 21 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 22 | |
| 23 | import org.onlab.onos.net.intent.IdGenerator; |
| 24 | import org.onlab.onos.net.intent.Intent; |
| 25 | import org.onlab.onos.net.intent.IntentCompiler; |
| 26 | import org.onlab.onos.net.intent.IntentExtensionService; |
| 27 | import org.onlab.onos.net.intent.IntentId; |
| 28 | import org.onlab.onos.net.intent.OpticalConnectivityIntent; |
| 29 | import org.onlab.onos.net.intent.OpticalPathIntent; |
| 30 | |
| 31 | import org.onlab.onos.net.provider.ProviderId; |
| 32 | import org.onlab.onos.net.topology.PathService; |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | /** |
| 36 | * Optical compiler for OpticalConnectivityIntent. |
| 37 | * It firstly computes K-shortest paths in the optical-layer, then choose the optimal one to assign a wavelength. |
| 38 | * Finally, it generates one or more opticalPathintent(s) with opticalMatchs and opticalActions. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true); |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected IntentExtensionService intentManager; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected PathService pathService; |
| 51 | |
| 52 | // protected LinkResourceService resourceService; |
| 53 | |
| 54 | protected IdGenerator<IntentId> intentIdGenerator; |
| 55 | |
| 56 | @Activate |
| 57 | public void activate() { |
| 58 | IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator(); |
| 59 | intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator); |
| 60 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
| 64 | public void deactivate() { |
| 65 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public List<Intent> compile(OpticalConnectivityIntent intent) { |
| 70 | // TO DO: compute multiple paths using the K-shortest path algorithm |
| 71 | Path path = calculatePath(intent.getSrcConnectPoint(), intent.getDst()); |
| 72 | log.info("calculate the lightpath: {}.", path.toString()); |
| 73 | |
| 74 | List<Link> links = new ArrayList<>(); |
| 75 | links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true)); |
| 76 | links.addAll(path.links()); |
| 77 | links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false)); |
| 78 | |
| 79 | // TO DO: choose a wavelength using the first-fit algorithm |
| 80 | TrafficSelector opticalSelector = null; |
| 81 | TrafficTreatment opticalTreatment = null; |
| 82 | |
| 83 | List<Intent> retList = new ArrayList<>(); |
| 84 | int wavelength = assignWavelength(path); |
| 85 | log.info("assign the wavelength: {}.", wavelength); |
| 86 | |
| 87 | // create a new opticalPathIntent |
| 88 | Intent newIntent = new OpticalPathIntent(intentIdGenerator.getNewId(), |
| 89 | opticalSelector, |
| 90 | opticalTreatment, |
| 91 | path.src(), |
| 92 | path.dst(), |
| 93 | path); |
| 94 | |
| 95 | retList.add(newIntent); |
| 96 | |
| 97 | return retList; |
| 98 | } |
| 99 | |
| 100 | private Path calculatePath(ConnectPoint one, ConnectPoint two) { |
| 101 | // TODO: K-shortest path computation algorithm |
| 102 | Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId()); |
| 103 | if (paths.isEmpty()) { |
| 104 | throw new PathNotFoundException("No optical path from " + one + " to " + two); |
| 105 | } |
| 106 | return paths.iterator().next(); |
| 107 | } |
| 108 | |
| 109 | private int assignWavelength(Path path) { |
| 110 | // TODO: wavelength assignment |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | } |