blob: f5272f2020bcf09ccd8a44cecfe2d2298b9565f7 [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(),
weibitf32383b2014-10-22 10:17:31 -070087 path.src(),
88 path.dst(),
89 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;
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 };
weibitf32383b2014-10-22 10:17:31 -0700114
weibit7e583462014-10-23 10:14:05 -0700115 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
weibitf32383b2014-10-22 10:17:31 -0700135 }
136
137}