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 | aca1460 | 2014-10-24 10:26:26 -0700 | [diff] [blame] | 73 | log.info("The system is comipling the OpticalConnectivityIntent:" + intent.toString()); |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 74 | Path path = calculateOpticalPath(intent.getSrcConnectPoint(), intent.getDst()); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 75 | if (path == null) { |
| 76 | return retList; |
| 77 | } else { |
| 78 | log.info("the computed lightpath is : {}.", path.toString()); |
| 79 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 80 | |
| 81 | List<Link> links = new ArrayList<>(); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 82 | // links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true)); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 83 | links.addAll(path.links()); |
weibit | 9e622ac | 2014-10-23 13:45:44 -0700 | [diff] [blame] | 84 | //links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false)); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 85 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 86 | // create a new opticalPathIntent |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 87 | Intent newIntent = new OpticalPathIntent(intent.appId(), |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 88 | intent.getSrcConnectPoint(), |
| 89 | intent.getDst(), |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 90 | path); |
| 91 | |
weibit | aca1460 | 2014-10-24 10:26:26 -0700 | [diff] [blame] | 92 | log.info("a new OpticalPathIntent was created: " + newIntent.toString()); |
| 93 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 94 | retList.add(newIntent); |
| 95 | |
| 96 | return retList; |
| 97 | } |
| 98 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 99 | private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) { |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 100 | // TODO: support user policies |
| 101 | Topology topology = topologyService.currentTopology(); |
| 102 | LinkWeight weight = new LinkWeight() { |
| 103 | @Override |
| 104 | public double weight(TopologyEdge edge) { |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 105 | Link.Type lt = edge.link().type(); |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 106 | if (lt == Link.Type.OPTICAL) { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 107 | return 1.0; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 108 | } else { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 109 | return 1000.0; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 113 | |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 114 | Set<Path> paths = topologyService.getPaths(topology, |
| 115 | start.deviceId(), |
| 116 | end.deviceId(), |
| 117 | weight); |
| 118 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 119 | ArrayList<Path> localPaths = new ArrayList<>(); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 120 | Iterator<Path> itr = paths.iterator(); |
| 121 | while (itr.hasNext()) { |
| 122 | Path path = itr.next(); |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 123 | if (path.cost() >= 1000) { |
| 124 | continue; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 125 | } |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 126 | localPaths.add(path); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 127 | } |
| 128 | |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 129 | if (localPaths.isEmpty()) { |
| 130 | throw new PathNotFoundException("No fiber path from " + start + " to " + end); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 131 | } else { |
weibit | 50eb95b | 2014-10-25 21:47:54 -0700 | [diff] [blame^] | 132 | return localPaths.iterator().next(); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 133 | } |
| 134 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } |