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