blob: 5c79d5ca63f07195a4acac1d612cd978fd0d8da4 [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 Milkeya058c732014-10-08 13:52:34 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.DefaultEdgeLink;
25import org.onlab.onos.net.DefaultPath;
26import org.onlab.onos.net.Link;
27import org.onlab.onos.net.Path;
Ray Milkeya058c732014-10-08 13:52:34 -070028import org.onlab.onos.net.intent.Intent;
29import org.onlab.onos.net.intent.IntentCompiler;
30import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkeya058c732014-10-08 13:52:34 -070031import org.onlab.onos.net.intent.PathIntent;
32import org.onlab.onos.net.intent.PointToPointIntent;
33import org.onlab.onos.net.provider.ProviderId;
weibit50eb95b2014-10-25 21:47:54 -070034import org.onlab.onos.net.topology.LinkWeight;
weibit50eb95b2014-10-25 21:47:54 -070035import org.onlab.onos.net.topology.Topology;
36import org.onlab.onos.net.topology.TopologyEdge;
weibit50eb95b2014-10-25 21:47:54 -070037import org.onlab.onos.net.topology.TopologyService;
Ray Milkeya058c732014-10-08 13:52:34 -070038
Thomas Vachuskab97cf282014-10-20 23:31:12 -070039import java.util.ArrayList;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070040import java.util.List;
41import java.util.Set;
42
Thomas Vachuska425a2d72014-10-29 11:28:28 -070043import static java.util.Arrays.asList;
44
Ray Milkeya058c732014-10-08 13:52:34 -070045/**
Thomas Vachuska425a2d72014-10-29 11:28:28 -070046 * An intent compiler for {@link org.onlab.onos.net.intent.PointToPointIntent}.
Ray Milkeya058c732014-10-08 13:52:34 -070047 */
48@Component(immediate = true)
49public class PointToPointIntentCompiler
50 implements IntentCompiler<PointToPointIntent> {
51
52 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected IntentExtensionService intentManager;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit50eb95b2014-10-25 21:47:54 -070057 protected TopologyService topologyService;
58
Ray Milkeya058c732014-10-08 13:52:34 -070059 @Activate
60 public void activate() {
Ray Milkeya058c732014-10-08 13:52:34 -070061 intentManager.registerCompiler(PointToPointIntent.class, this);
62 }
63
64 @Deactivate
65 public void deactivate() {
66 intentManager.unregisterCompiler(PointToPointIntent.class);
67 }
68
69 @Override
70 public List<Intent> compile(PointToPointIntent intent) {
71 Path path = getPath(intent.ingressPoint(), intent.egressPoint());
72
73 List<Link> links = new ArrayList<>();
74 links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
75 links.addAll(path.links());
76 links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
77
Thomas Vachuska425a2d72014-10-29 11:28:28 -070078 return asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2,
79 path.annotations()), intent));
Ray Milkeya058c732014-10-08 13:52:34 -070080 }
81
82 /**
83 * Creates a path intent from the specified path and original
84 * connectivity intent.
85 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -070086 * @param path path to create an intent for
Ray Milkeya058c732014-10-08 13:52:34 -070087 * @param intent original intent
88 */
89 private Intent createPathIntent(Path path,
90 PointToPointIntent intent) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070091 return new PathIntent(intent.appId(),
92 intent.selector(), intent.treatment(), path);
Ray Milkeya058c732014-10-08 13:52:34 -070093 }
94
95 /**
96 * Computes a path between two ConnectPoints.
97 *
98 * @param one start of the path
99 * @param two end of the path
100 * @return Path between the two
101 * @throws PathNotFoundException if a path cannot be found
102 */
103 private Path getPath(ConnectPoint one, ConnectPoint two) {
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700104 Topology topology = topologyService.currentTopology();
weibit50eb95b2014-10-25 21:47:54 -0700105 LinkWeight weight = new LinkWeight() {
106 @Override
107 public double weight(TopologyEdge edge) {
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700108 return edge.link().type() == Link.Type.OPTICAL ? -1 : +1;
weibit50eb95b2014-10-25 21:47:54 -0700109 }
110 };
111
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700112 Set<Path> paths = topologyService.getPaths(topology, one.deviceId(),
113 two.deviceId(), weight);
114 if (paths.isEmpty()) {
weibit50eb95b2014-10-25 21:47:54 -0700115 throw new PathNotFoundException("No packet path from " + one + " to " + two);
Ray Milkeya058c732014-10-08 13:52:34 -0700116 }
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700117
Ray Milkeya058c732014-10-08 13:52:34 -0700118 // TODO: let's be more intelligent about this eventually
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700119 return paths.iterator().next();
Ray Milkeya058c732014-10-08 13:52:34 -0700120 }
121}