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; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 6 | import java.util.Iterator; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 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; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 15 | import org.onlab.onos.CoreService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.ConnectPoint; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.Link; |
| 18 | import org.onlab.onos.net.Path; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.intent.Intent; |
| 20 | import org.onlab.onos.net.intent.IntentCompiler; |
| 21 | import org.onlab.onos.net.intent.IntentExtensionService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.intent.OpticalConnectivityIntent; |
| 23 | import org.onlab.onos.net.intent.OpticalPathIntent; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.provider.ProviderId; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.resource.LinkResourceService; |
| 26 | import org.onlab.onos.net.topology.LinkWeight; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.topology.PathService; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.topology.Topology; |
| 29 | import org.onlab.onos.net.topology.TopologyEdge; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.topology.TopologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | /** |
| 34 | * Optical compiler for OpticalConnectivityIntent. |
| 35 | * It firstly computes K-shortest paths in the optical-layer, then choose the optimal one to assign a wavelength. |
| 36 | * Finally, it generates one or more opticalPathintent(s) with opticalMatchs and opticalActions. |
| 37 | */ |
| 38 | @Component(immediate = true) |
| 39 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 40 | |
| 41 | private final Logger log = getLogger(getClass()); |
| 42 | private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true); |
| 43 | |
| 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected IntentExtensionService intentManager; |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected PathService pathService; |
| 49 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 52 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected LinkResourceService resourceService; |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected CoreService coreService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 58 | |
| 59 | @Activate |
| 60 | public void activate() { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 61 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | public void deactivate() { |
| 66 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public List<Intent> compile(OpticalConnectivityIntent intent) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 71 | // TODO: compute multiple paths using the K-shortest path algorithm |
| 72 | List<Intent> retList = new ArrayList<>(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 73 | Path path = calculatePath(intent.getSrcConnectPoint(), intent.getDst()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 74 | if (path == null) { |
| 75 | return retList; |
| 76 | } else { |
| 77 | log.info("the computed lightpath is : {}.", path.toString()); |
| 78 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 79 | |
| 80 | List<Link> links = new ArrayList<>(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 81 | // links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true)); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 82 | links.addAll(path.links()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 83 | //links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false)); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 84 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 85 | // create a new opticalPathIntent |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 86 | Intent newIntent = new OpticalPathIntent(intent.appId(), |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 87 | path.src(), |
| 88 | path.dst(), |
| 89 | path); |
| 90 | |
| 91 | retList.add(newIntent); |
| 92 | |
| 93 | return retList; |
| 94 | } |
| 95 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 96 | private Path calculatePath(ConnectPoint start, ConnectPoint end) { |
| 97 | // TODO: support user policies |
| 98 | Topology topology = topologyService.currentTopology(); |
| 99 | LinkWeight weight = new LinkWeight() { |
| 100 | @Override |
| 101 | public double weight(TopologyEdge edge) { |
| 102 | boolean isOptical = false; |
| 103 | String t = edge.link().annotations().value("linkType"); |
| 104 | if (t.equals("WDM")) { |
| 105 | isOptical = true; |
| 106 | } |
| 107 | if (isOptical) { |
| 108 | return 1; // optical links |
| 109 | } else { |
| 110 | return 10000; // packet links |
| 111 | } |
| 112 | } |
| 113 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 114 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 115 | Set<Path> paths = topologyService.getPaths(topology, |
| 116 | start.deviceId(), |
| 117 | end.deviceId(), |
| 118 | weight); |
| 119 | |
| 120 | Iterator<Path> itr = paths.iterator(); |
| 121 | while (itr.hasNext()) { |
| 122 | Path path = itr.next(); |
| 123 | if (path.cost() >= 10000) { |
| 124 | itr.remove(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (paths.isEmpty()) { |
| 129 | log.info("No optical path found from " + start + " to " + end); |
| 130 | return null; |
| 131 | } else { |
| 132 | return paths.iterator().next(); |
| 133 | } |
| 134 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } |