blob: 893b7ea84742b546261530ed1230a041bbaf8c53 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ray Milkeya058c732014-10-08 13:52:34 -070016package org.onlab.onos.net.intent.impl;
17
Ray Milkeycaa450b2014-10-29 15:54:24 -070018import java.util.ArrayList;
19import java.util.List;
20import java.util.Set;
21
Ray Milkeya058c732014-10-08 13:52:34 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.onlab.onos.net.ConnectPoint;
28import org.onlab.onos.net.DefaultEdgeLink;
29import org.onlab.onos.net.DefaultPath;
30import org.onlab.onos.net.Link;
31import org.onlab.onos.net.Path;
Ray Milkeya058c732014-10-08 13:52:34 -070032import org.onlab.onos.net.intent.Intent;
33import org.onlab.onos.net.intent.IntentCompiler;
34import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkeya058c732014-10-08 13:52:34 -070035import org.onlab.onos.net.intent.PathIntent;
36import org.onlab.onos.net.intent.PointToPointIntent;
37import org.onlab.onos.net.provider.ProviderId;
Ray Milkeycaa450b2014-10-29 15:54:24 -070038import org.onlab.onos.net.resource.LinkResourceRequest;
weibit50eb95b2014-10-25 21:47:54 -070039import org.onlab.onos.net.topology.LinkWeight;
weibit50eb95b2014-10-25 21:47:54 -070040import org.onlab.onos.net.topology.Topology;
41import org.onlab.onos.net.topology.TopologyEdge;
weibit50eb95b2014-10-25 21:47:54 -070042import org.onlab.onos.net.topology.TopologyService;
Ray Milkeya058c732014-10-08 13:52:34 -070043
Thomas Vachuska425a2d72014-10-29 11:28:28 -070044import static java.util.Arrays.asList;
45
Ray Milkeya058c732014-10-08 13:52:34 -070046/**
Thomas Vachuska425a2d72014-10-29 11:28:28 -070047 * An intent compiler for {@link org.onlab.onos.net.intent.PointToPointIntent}.
Ray Milkeya058c732014-10-08 13:52:34 -070048 */
49@Component(immediate = true)
50public class PointToPointIntentCompiler
51 implements IntentCompiler<PointToPointIntent> {
52
53 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService intentManager;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit50eb95b2014-10-25 21:47:54 -070058 protected TopologyService topologyService;
59
Ray Milkeya058c732014-10-08 13:52:34 -070060 @Activate
61 public void activate() {
Ray Milkeya058c732014-10-08 13:52:34 -070062 intentManager.registerCompiler(PointToPointIntent.class, this);
63 }
64
65 @Deactivate
66 public void deactivate() {
67 intentManager.unregisterCompiler(PointToPointIntent.class);
68 }
69
70 @Override
71 public List<Intent> compile(PointToPointIntent intent) {
72 Path path = getPath(intent.ingressPoint(), intent.egressPoint());
73
74 List<Link> links = new ArrayList<>();
75 links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
76 links.addAll(path.links());
77 links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
78
Thomas Vachuska425a2d72014-10-29 11:28:28 -070079 return asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2,
Ray Milkeycaa450b2014-10-29 15:54:24 -070080 path.annotations()), intent));
Ray Milkeya058c732014-10-08 13:52:34 -070081 }
82
83 /**
84 * Creates a path intent from the specified path and original
85 * connectivity intent.
86 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -070087 * @param path path to create an intent for
Ray Milkeya058c732014-10-08 13:52:34 -070088 * @param intent original intent
89 */
90 private Intent createPathIntent(Path path,
91 PointToPointIntent intent) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070092 return new PathIntent(intent.appId(),
Ray Milkeycaa450b2014-10-29 15:54:24 -070093 intent.selector(), intent.treatment(), path,
94 new LinkResourceRequest[0]);
Ray Milkeya058c732014-10-08 13:52:34 -070095 }
96
97 /**
98 * Computes a path between two ConnectPoints.
99 *
100 * @param one start of the path
101 * @param two end of the path
102 * @return Path between the two
103 * @throws PathNotFoundException if a path cannot be found
104 */
105 private Path getPath(ConnectPoint one, ConnectPoint two) {
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700106 Topology topology = topologyService.currentTopology();
weibit50eb95b2014-10-25 21:47:54 -0700107 LinkWeight weight = new LinkWeight() {
108 @Override
109 public double weight(TopologyEdge edge) {
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700110 return edge.link().type() == Link.Type.OPTICAL ? -1 : +1;
weibit50eb95b2014-10-25 21:47:54 -0700111 }
112 };
113
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700114 Set<Path> paths = topologyService.getPaths(topology, one.deviceId(),
115 two.deviceId(), weight);
116 if (paths.isEmpty()) {
weibit50eb95b2014-10-25 21:47:54 -0700117 throw new PathNotFoundException("No packet path from " + one + " to " + two);
Ray Milkeya058c732014-10-08 13:52:34 -0700118 }
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700119
Ray Milkeya058c732014-10-08 13:52:34 -0700120 // TODO: let's be more intelligent about this eventually
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700121 return paths.iterator().next();
Ray Milkeya058c732014-10-08 13:52:34 -0700122 }
123}