blob: 58586dc711850c4ea7c45f476f90b2bec707d3a7 [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;
weibit7e583462014-10-23 10:14:05 -070015import org.onlab.onos.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<>();
weibitf32383b2014-10-22 10:17:31 -070073 Path path = calculatePath(intent.getSrcConnectPoint(), intent.getDst());
weibit7e583462014-10-23 10:14:05 -070074 if (path == null) {
75 return retList;
76 } else {
77 log.info("the computed lightpath is : {}.", path.toString());
78 }
weibitf32383b2014-10-22 10:17:31 -070079
80 List<Link> links = new ArrayList<>();
weibit9e622ac2014-10-23 13:45:44 -070081 // links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true));
weibitf32383b2014-10-22 10:17:31 -070082 links.addAll(path.links());
weibit9e622ac2014-10-23 13:45:44 -070083 //links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false));
weibitf32383b2014-10-22 10:17:31 -070084
weibitf32383b2014-10-22 10:17:31 -070085 // create a new opticalPathIntent
weibit7e583462014-10-23 10:14:05 -070086 Intent newIntent = new OpticalPathIntent(intent.appId(),
weibit253c8652014-10-23 16:30:03 -070087 intent.getSrcConnectPoint(),
88 intent.getDst(),
weibitf32383b2014-10-22 10:17:31 -070089 path);
90
91 retList.add(newIntent);
92
93 return retList;
94 }
95
weibit7e583462014-10-23 10:14:05 -070096 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;
weibit253c8652014-10-23 16:30:03 -0700103
104 Link.Type lt = edge.link().type();
105
106 //String t = edge.link().annotations().value("linkType");
107 if (lt == Link.Type.OPTICAL) {
weibit7e583462014-10-23 10:14:05 -0700108 isOptical = true;
109 }
110 if (isOptical) {
111 return 1; // optical links
112 } else {
113 return 10000; // packet links
114 }
115 }
116 };
weibitf32383b2014-10-22 10:17:31 -0700117
weibit7e583462014-10-23 10:14:05 -0700118 Set<Path> paths = topologyService.getPaths(topology,
119 start.deviceId(),
120 end.deviceId(),
121 weight);
122
123 Iterator<Path> itr = paths.iterator();
124 while (itr.hasNext()) {
125 Path path = itr.next();
126 if (path.cost() >= 10000) {
127 itr.remove();
128 }
129 }
130
131 if (paths.isEmpty()) {
132 log.info("No optical path found from " + start + " to " + end);
133 return null;
134 } else {
135 return paths.iterator().next();
136 }
137
weibitf32383b2014-10-22 10:17:31 -0700138 }
139
140}