blob: 8cd0e14a3f44d0f16d650d08c484ffec58936a9c [file] [log] [blame]
Ray Milkeya058c732014-10-08 13:52:34 -07001package org.onlab.onos.net.intent.impl;
2
Ray Milkeya058c732014-10-08 13:52:34 -07003import org.apache.felix.scr.annotations.Activate;
4import org.apache.felix.scr.annotations.Component;
5import org.apache.felix.scr.annotations.Deactivate;
6import org.apache.felix.scr.annotations.Reference;
7import org.apache.felix.scr.annotations.ReferenceCardinality;
8import org.onlab.onos.net.ConnectPoint;
9import org.onlab.onos.net.DefaultEdgeLink;
10import org.onlab.onos.net.DefaultPath;
11import org.onlab.onos.net.Link;
12import org.onlab.onos.net.Path;
13import org.onlab.onos.net.host.HostService;
Ray Milkeya058c732014-10-08 13:52:34 -070014import org.onlab.onos.net.intent.Intent;
15import org.onlab.onos.net.intent.IntentCompiler;
16import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkeya058c732014-10-08 13:52:34 -070017import org.onlab.onos.net.intent.PathIntent;
18import org.onlab.onos.net.intent.PointToPointIntent;
19import org.onlab.onos.net.provider.ProviderId;
weibit50eb95b2014-10-25 21:47:54 -070020import org.onlab.onos.net.topology.LinkWeight;
21//import org.onlab.onos.net.topology.LinkWeight;
Ray Milkeya058c732014-10-08 13:52:34 -070022import org.onlab.onos.net.topology.PathService;
weibit50eb95b2014-10-25 21:47:54 -070023import org.onlab.onos.net.topology.Topology;
24import org.onlab.onos.net.topology.TopologyEdge;
25//import org.onlab.onos.net.topology.Topology;
26//import org.onlab.onos.net.topology.TopologyEdge;
27import org.onlab.onos.net.topology.TopologyService;
Ray Milkeya058c732014-10-08 13:52:34 -070028
Thomas Vachuskab97cf282014-10-20 23:31:12 -070029import java.util.ArrayList;
30import java.util.Arrays;
weibit50eb95b2014-10-25 21:47:54 -070031import java.util.Iterator;
32//import java.util.Iterator;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070033import java.util.List;
34import java.util.Set;
35
Ray Milkeya058c732014-10-08 13:52:34 -070036/**
37 * A intent compiler for {@link org.onlab.onos.net.intent.HostToHostIntent}.
38 */
39@Component(immediate = true)
40public class PointToPointIntentCompiler
41 implements IntentCompiler<PointToPointIntent> {
42
43 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
44 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected IntentExtensionService intentManager;
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected PathService pathService;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected HostService hostService;
52
weibit50eb95b2014-10-25 21:47:54 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected TopologyService topologyService;
55
Ray Milkeya058c732014-10-08 13:52:34 -070056 @Activate
57 public void activate() {
Ray Milkeya058c732014-10-08 13:52:34 -070058 intentManager.registerCompiler(PointToPointIntent.class, this);
59 }
60
61 @Deactivate
62 public void deactivate() {
63 intentManager.unregisterCompiler(PointToPointIntent.class);
64 }
65
66 @Override
67 public List<Intent> compile(PointToPointIntent intent) {
68 Path path = getPath(intent.ingressPoint(), intent.egressPoint());
69
70 List<Link> links = new ArrayList<>();
71 links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
72 links.addAll(path.links());
73 links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
74
75 return Arrays.asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2,
Thomas Vachuskab97cf282014-10-20 23:31:12 -070076 path.annotations()),
77 intent));
Ray Milkeya058c732014-10-08 13:52:34 -070078 }
79
80 /**
81 * Creates a path intent from the specified path and original
82 * connectivity intent.
83 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -070084 * @param path path to create an intent for
Ray Milkeya058c732014-10-08 13:52:34 -070085 * @param intent original intent
86 */
87 private Intent createPathIntent(Path path,
88 PointToPointIntent intent) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070089 return new PathIntent(intent.appId(),
90 intent.selector(), intent.treatment(), path);
Ray Milkeya058c732014-10-08 13:52:34 -070091 }
92
93 /**
94 * Computes a path between two ConnectPoints.
95 *
96 * @param one start of the path
97 * @param two end of the path
98 * @return Path between the two
99 * @throws PathNotFoundException if a path cannot be found
100 */
101 private Path getPath(ConnectPoint one, ConnectPoint two) {
weibit50eb95b2014-10-25 21:47:54 -0700102 // Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
103 Topology topology = topologyService.currentTopology();
104 LinkWeight weight = new LinkWeight() {
105 @Override
106 public double weight(TopologyEdge edge) {
107 Link.Type lt = edge.link().type();
108 if (lt == Link.Type.OPTICAL) {
109 return 1000.0;
110 } else {
111 return 1.0;
112 }
113 }
114 };
115
116 Set<Path> paths = topologyService.getPaths(topology,
117 one.deviceId(),
118 two.deviceId(),
119 weight);
120
121 ArrayList<Path> localPaths = new ArrayList<>();
122 Iterator<Path> itr = paths.iterator();
123 while (itr.hasNext()) {
124 Path path = itr.next();
125 if (path.cost() >= 1000) {
126 continue;
127 }
128 localPaths.add(path);
129 }
130
131 if (localPaths.isEmpty()) {
132 throw new PathNotFoundException("No packet path from " + one + " to " + two);
Ray Milkeya058c732014-10-08 13:52:34 -0700133 }
134 // TODO: let's be more intelligent about this eventually
weibit50eb95b2014-10-25 21:47:54 -0700135 return localPaths.iterator().next();
Ray Milkeya058c732014-10-08 13:52:34 -0700136 }
137}