blob: 68c55dd1a22c59606a5a6672d5e9ae2366c1bdec [file] [log] [blame]
Ray Milkey0742ec92014-10-13 08:39:55 -07001package org.onlab.onos.net.intent.impl;
2
3import java.util.Arrays;
4import java.util.HashSet;
5import java.util.List;
6import java.util.Set;
7
8import org.apache.felix.scr.annotations.Activate;
9import org.apache.felix.scr.annotations.Component;
10import org.apache.felix.scr.annotations.Deactivate;
11import org.apache.felix.scr.annotations.Reference;
12import org.apache.felix.scr.annotations.ReferenceCardinality;
13import org.onlab.onos.net.ConnectPoint;
14import org.onlab.onos.net.Link;
15import org.onlab.onos.net.Path;
16import org.onlab.onos.net.intent.IdGenerator;
17import org.onlab.onos.net.intent.Intent;
18import org.onlab.onos.net.intent.IntentCompiler;
19import org.onlab.onos.net.intent.IntentExtensionService;
20import org.onlab.onos.net.intent.IntentId;
21import org.onlab.onos.net.intent.LinkCollectionIntent;
22import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
23import org.onlab.onos.net.intent.PointToPointIntent;
24import org.onlab.onos.net.topology.PathService;
25
26/**
27 * An intent compiler for
28 * {@link org.onlab.onos.net.intent.MultiPointToSinglePointIntent}.
29 */
30@Component(immediate = true)
31public class MultiPointToSinglePointIntentCompiler
32 implements IntentCompiler<MultiPointToSinglePointIntent> {
33
34 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
35 protected IntentExtensionService intentManager;
36
37 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
38 protected PathService pathService;
39
40 private IdGenerator<IntentId> intentIdGenerator;
41
42 @Activate
43 public void activate() {
44 IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator();
45 intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator);
46 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
47 }
48
49 @Deactivate
50 public void deactivate() {
51 intentManager.unregisterCompiler(PointToPointIntent.class);
52 }
53
54 @Override
55 public List<Intent> compile(MultiPointToSinglePointIntent intent) {
56 Set<Link> links = new HashSet<>();
57
58 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
59 Path path = getPath(ingressPoint, intent.egressPoint());
60 links.addAll(path.links());
61 }
62
63 Intent result = new LinkCollectionIntent(intentIdGenerator.getNewId(),
64 intent.selector(), intent.treatment(),
65 links);
66 return Arrays.asList(result);
67 }
68
69 /**
70 * Computes a path between two ConnectPoints.
71 *
72 * @param one start of the path
73 * @param two end of the path
74 * @return Path between the two
75 * @throws org.onlab.onos.net.intent.impl.PathNotFoundException if a path cannot be found
76 */
77 private Path getPath(ConnectPoint one, ConnectPoint two) {
78 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
79 if (paths.isEmpty()) {
80 throw new PathNotFoundException("No path from " + one + " to " + two);
81 }
82 // TODO: let's be more intelligent about this eventually
83 return paths.iterator().next();
84 }
85}