blob: c19bba0bc6870798026d00caeb72d26fc866d88d [file] [log] [blame]
weibitf32383b2014-10-22 10:17:31 -07001package org.onlab.onos.net.intent.impl;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.ArrayList;
weibit7e583462014-10-23 10:14:05 -07006import java.util.Iterator;
weibitf32383b2014-10-22 10:17:31 -07007import java.util.List;
8import java.util.Set;
9
10import org.apache.felix.scr.annotations.Activate;
11import org.apache.felix.scr.annotations.Component;
12import org.apache.felix.scr.annotations.Deactivate;
13import org.apache.felix.scr.annotations.Reference;
14import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070015import org.onlab.onos.core.CoreService;
weibitf32383b2014-10-22 10:17:31 -070016import org.onlab.onos.net.ConnectPoint;
weibitf32383b2014-10-22 10:17:31 -070017import org.onlab.onos.net.Link;
18import org.onlab.onos.net.Path;
weibitf32383b2014-10-22 10:17:31 -070019import org.onlab.onos.net.intent.Intent;
20import org.onlab.onos.net.intent.IntentCompiler;
21import org.onlab.onos.net.intent.IntentExtensionService;
weibitf32383b2014-10-22 10:17:31 -070022import org.onlab.onos.net.intent.OpticalConnectivityIntent;
23import org.onlab.onos.net.intent.OpticalPathIntent;
weibitf32383b2014-10-22 10:17:31 -070024import org.onlab.onos.net.provider.ProviderId;
weibit7e583462014-10-23 10:14:05 -070025import org.onlab.onos.net.resource.LinkResourceService;
26import org.onlab.onos.net.topology.LinkWeight;
weibitf32383b2014-10-22 10:17:31 -070027import org.onlab.onos.net.topology.PathService;
weibit7e583462014-10-23 10:14:05 -070028import org.onlab.onos.net.topology.Topology;
29import org.onlab.onos.net.topology.TopologyEdge;
weibit7e583462014-10-23 10:14:05 -070030import org.onlab.onos.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070031import 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)
39public 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
weibit7e583462014-10-23 10:14:05 -070050 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070052
weibit7e583462014-10-23 10:14:05 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected LinkResourceService resourceService;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected CoreService coreService;
weibitf32383b2014-10-22 10:17:31 -070058
59 @Activate
60 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070061 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) {
weibit7e583462014-10-23 10:14:05 -070071 // TODO: compute multiple paths using the K-shortest path algorithm
72 List<Intent> retList = new ArrayList<>();
weibitaca14602014-10-24 10:26:26 -070073 log.info("The system is comipling the OpticalConnectivityIntent:" + intent.toString());
weibit50eb95b2014-10-25 21:47:54 -070074 Path path = calculateOpticalPath(intent.getSrcConnectPoint(), intent.getDst());
weibit7e583462014-10-23 10:14:05 -070075 if (path == null) {
76 return retList;
77 } else {
78 log.info("the computed lightpath is : {}.", path.toString());
79 }
weibitf32383b2014-10-22 10:17:31 -070080
81 List<Link> links = new ArrayList<>();
weibit9e622ac2014-10-23 13:45:44 -070082 // links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true));
weibitf32383b2014-10-22 10:17:31 -070083 links.addAll(path.links());
weibit9e622ac2014-10-23 13:45:44 -070084 //links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false));
weibitf32383b2014-10-22 10:17:31 -070085
weibitf32383b2014-10-22 10:17:31 -070086 // create a new opticalPathIntent
weibit7e583462014-10-23 10:14:05 -070087 Intent newIntent = new OpticalPathIntent(intent.appId(),
weibit253c8652014-10-23 16:30:03 -070088 intent.getSrcConnectPoint(),
89 intent.getDst(),
weibitf32383b2014-10-22 10:17:31 -070090 path);
91
weibitaca14602014-10-24 10:26:26 -070092 log.info("a new OpticalPathIntent was created: " + newIntent.toString());
93
weibitf32383b2014-10-22 10:17:31 -070094 retList.add(newIntent);
95
96 return retList;
97 }
98
weibit50eb95b2014-10-25 21:47:54 -070099 private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) {
weibit7e583462014-10-23 10:14:05 -0700100 // TODO: support user policies
101 Topology topology = topologyService.currentTopology();
102 LinkWeight weight = new LinkWeight() {
103 @Override
104 public double weight(TopologyEdge edge) {
weibit253c8652014-10-23 16:30:03 -0700105 Link.Type lt = edge.link().type();
weibit253c8652014-10-23 16:30:03 -0700106 if (lt == Link.Type.OPTICAL) {
weibit50eb95b2014-10-25 21:47:54 -0700107 return 1.0;
weibit7e583462014-10-23 10:14:05 -0700108 } else {
weibit50eb95b2014-10-25 21:47:54 -0700109 return 1000.0;
weibit7e583462014-10-23 10:14:05 -0700110 }
111 }
112 };
weibitf32383b2014-10-22 10:17:31 -0700113
weibit7e583462014-10-23 10:14:05 -0700114 Set<Path> paths = topologyService.getPaths(topology,
115 start.deviceId(),
116 end.deviceId(),
117 weight);
118
weibit50eb95b2014-10-25 21:47:54 -0700119 ArrayList<Path> localPaths = new ArrayList<>();
weibit7e583462014-10-23 10:14:05 -0700120 Iterator<Path> itr = paths.iterator();
121 while (itr.hasNext()) {
122 Path path = itr.next();
weibit50eb95b2014-10-25 21:47:54 -0700123 if (path.cost() >= 1000) {
124 continue;
weibit7e583462014-10-23 10:14:05 -0700125 }
weibit50eb95b2014-10-25 21:47:54 -0700126 localPaths.add(path);
weibit7e583462014-10-23 10:14:05 -0700127 }
128
weibit50eb95b2014-10-25 21:47:54 -0700129 if (localPaths.isEmpty()) {
130 throw new PathNotFoundException("No fiber path from " + start + " to " + end);
weibit7e583462014-10-23 10:14:05 -0700131 } else {
weibit50eb95b2014-10-25 21:47:54 -0700132 return localPaths.iterator().next();
weibit7e583462014-10-23 10:14:05 -0700133 }
134
weibitf32383b2014-10-22 10:17:31 -0700135 }
136
137}