blob: d888ed355ac998664aa49afa57e57c5329f437de [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;
6
7import 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;
15import org.onlab.onos.net.ConnectPoint;
16import org.onlab.onos.net.DefaultEdgeLink;
17
18import 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
23import org.onlab.onos.net.intent.IdGenerator;
24import org.onlab.onos.net.intent.Intent;
25import org.onlab.onos.net.intent.IntentCompiler;
26import org.onlab.onos.net.intent.IntentExtensionService;
27import org.onlab.onos.net.intent.IntentId;
28import org.onlab.onos.net.intent.OpticalConnectivityIntent;
29import org.onlab.onos.net.intent.OpticalPathIntent;
30
31import org.onlab.onos.net.provider.ProviderId;
32import org.onlab.onos.net.topology.PathService;
33import org.slf4j.Logger;
34
35/**
36 * Optical compiler for OpticalConnectivityIntent.
37 * It firstly computes K-shortest paths in the optical-layer, then choose the optimal one to assign a wavelength.
38 * Finally, it generates one or more opticalPathintent(s) with opticalMatchs and opticalActions.
39 */
40@Component(immediate = true)
41public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
42
43 private final Logger log = getLogger(getClass());
44 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected IntentExtensionService intentManager;
48
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected PathService pathService;
51
52 // protected LinkResourceService resourceService;
53
54 protected IdGenerator<IntentId> intentIdGenerator;
55
56 @Activate
57 public void activate() {
58 IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator();
59 intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator);
60 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
61 }
62
63 @Deactivate
64 public void deactivate() {
65 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
66 }
67
68 @Override
69 public List<Intent> compile(OpticalConnectivityIntent intent) {
70 // TO DO: compute multiple paths using the K-shortest path algorithm
71 Path path = calculatePath(intent.getSrcConnectPoint(), intent.getDst());
72 log.info("calculate the lightpath: {}.", path.toString());
73
74 List<Link> links = new ArrayList<>();
75 links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true));
76 links.addAll(path.links());
77 links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false));
78
79 // TO DO: choose a wavelength using the first-fit algorithm
80 TrafficSelector opticalSelector = null;
81 TrafficTreatment opticalTreatment = null;
82
83 List<Intent> retList = new ArrayList<>();
84 int wavelength = assignWavelength(path);
85 log.info("assign the wavelength: {}.", wavelength);
86
87 // create a new opticalPathIntent
88 Intent newIntent = new OpticalPathIntent(intentIdGenerator.getNewId(),
89 opticalSelector,
90 opticalTreatment,
91 path.src(),
92 path.dst(),
93 path);
94
95 retList.add(newIntent);
96
97 return retList;
98 }
99
100 private Path calculatePath(ConnectPoint one, ConnectPoint two) {
101 // TODO: K-shortest path computation algorithm
102 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
103 if (paths.isEmpty()) {
104 throw new PathNotFoundException("No optical path from " + one + " to " + two);
105 }
106 return paths.iterator().next();
107 }
108
109 private int assignWavelength(Path path) {
110 // TODO: wavelength assignment
111 return 1;
112 }
113
114}