blob: e44a01b312d24acd9c53da898d4641b85c7f0639 [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;
17import org.onlab.onos.net.DefaultEdgeLink;
weibitf32383b2014-10-22 10:17:31 -070018import org.onlab.onos.net.Link;
19import org.onlab.onos.net.Path;
20import org.onlab.onos.net.flow.TrafficSelector;
21import org.onlab.onos.net.flow.TrafficTreatment;
22
weibitf32383b2014-10-22 10:17:31 -070023import org.onlab.onos.net.intent.Intent;
24import org.onlab.onos.net.intent.IntentCompiler;
25import org.onlab.onos.net.intent.IntentExtensionService;
weibit7e583462014-10-23 10:14:05 -070026
weibitf32383b2014-10-22 10:17:31 -070027import org.onlab.onos.net.intent.OpticalConnectivityIntent;
28import org.onlab.onos.net.intent.OpticalPathIntent;
weibitf32383b2014-10-22 10:17:31 -070029import org.onlab.onos.net.provider.ProviderId;
weibit7e583462014-10-23 10:14:05 -070030import org.onlab.onos.net.resource.LinkResourceService;
31import org.onlab.onos.net.topology.LinkWeight;
weibitf32383b2014-10-22 10:17:31 -070032import org.onlab.onos.net.topology.PathService;
weibit7e583462014-10-23 10:14:05 -070033import org.onlab.onos.net.topology.Topology;
34import org.onlab.onos.net.topology.TopologyEdge;
35
36import org.onlab.onos.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070037import 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)
45public 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
weibit7e583462014-10-23 10:14:05 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070058
weibit7e583462014-10-23 10:14:05 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected LinkResourceService resourceService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected CoreService coreService;
weibitf32383b2014-10-22 10:17:31 -070064
65 @Activate
66 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070067 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) {
weibit7e583462014-10-23 10:14:05 -070077 // TODO: compute multiple paths using the K-shortest path algorithm
78 List<Intent> retList = new ArrayList<>();
weibitf32383b2014-10-22 10:17:31 -070079 Path path = calculatePath(intent.getSrcConnectPoint(), intent.getDst());
weibit7e583462014-10-23 10:14:05 -070080 if (path == null) {
81 return retList;
82 } else {
83 log.info("the computed lightpath is : {}.", path.toString());
84 }
weibitf32383b2014-10-22 10:17:31 -070085
86 List<Link> links = new ArrayList<>();
87 links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true));
88 links.addAll(path.links());
89 links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false));
90
weibitf32383b2014-10-22 10:17:31 -070091 TrafficSelector opticalSelector = null;
92 TrafficTreatment opticalTreatment = null;
93
weibitf32383b2014-10-22 10:17:31 -070094 // create a new opticalPathIntent
weibit7e583462014-10-23 10:14:05 -070095 Intent newIntent = new OpticalPathIntent(intent.appId(),
weibitf32383b2014-10-22 10:17:31 -070096 path.src(),
97 path.dst(),
weibit7e583462014-10-23 10:14:05 -070098 opticalSelector,
99 opticalTreatment,
weibitf32383b2014-10-22 10:17:31 -0700100 path);
101
102 retList.add(newIntent);
103
104 return retList;
105 }
106
weibit7e583462014-10-23 10:14:05 -0700107 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 };
weibitf32383b2014-10-22 10:17:31 -0700125
weibit7e583462014-10-23 10:14:05 -0700126 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
weibitf32383b2014-10-22 10:17:31 -0700146 }
147
148}