blob: 03bd91a99c7fc094be0df4a9f17ee75cc591f0ec [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;
28import org.onlab.onos.net.host.HostService;
Ray Milkeya058c732014-10-08 13:52:34 -070029import org.onlab.onos.net.intent.Intent;
30import org.onlab.onos.net.intent.IntentCompiler;
31import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkeya058c732014-10-08 13:52:34 -070032import org.onlab.onos.net.intent.PathIntent;
33import org.onlab.onos.net.intent.PointToPointIntent;
34import org.onlab.onos.net.provider.ProviderId;
weibit50eb95b2014-10-25 21:47:54 -070035import org.onlab.onos.net.topology.LinkWeight;
36//import org.onlab.onos.net.topology.LinkWeight;
Ray Milkeya058c732014-10-08 13:52:34 -070037import org.onlab.onos.net.topology.PathService;
weibit50eb95b2014-10-25 21:47:54 -070038import org.onlab.onos.net.topology.Topology;
39import org.onlab.onos.net.topology.TopologyEdge;
40//import org.onlab.onos.net.topology.Topology;
41//import org.onlab.onos.net.topology.TopologyEdge;
42import org.onlab.onos.net.topology.TopologyService;
Ray Milkeya058c732014-10-08 13:52:34 -070043
Thomas Vachuskab97cf282014-10-20 23:31:12 -070044import java.util.ArrayList;
45import java.util.Arrays;
weibit50eb95b2014-10-25 21:47:54 -070046import java.util.Iterator;
47//import java.util.Iterator;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070048import java.util.List;
49import java.util.Set;
50
Ray Milkeya058c732014-10-08 13:52:34 -070051/**
52 * A intent compiler for {@link org.onlab.onos.net.intent.HostToHostIntent}.
53 */
54@Component(immediate = true)
55public class PointToPointIntentCompiler
56 implements IntentCompiler<PointToPointIntent> {
57
58 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected IntentExtensionService intentManager;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected PathService pathService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected HostService hostService;
67
weibit50eb95b2014-10-25 21:47:54 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected TopologyService topologyService;
70
Ray Milkeya058c732014-10-08 13:52:34 -070071 @Activate
72 public void activate() {
Ray Milkeya058c732014-10-08 13:52:34 -070073 intentManager.registerCompiler(PointToPointIntent.class, this);
74 }
75
76 @Deactivate
77 public void deactivate() {
78 intentManager.unregisterCompiler(PointToPointIntent.class);
79 }
80
81 @Override
82 public List<Intent> compile(PointToPointIntent intent) {
83 Path path = getPath(intent.ingressPoint(), intent.egressPoint());
84
85 List<Link> links = new ArrayList<>();
86 links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
87 links.addAll(path.links());
88 links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
89
90 return Arrays.asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2,
Thomas Vachuskab97cf282014-10-20 23:31:12 -070091 path.annotations()),
92 intent));
Ray Milkeya058c732014-10-08 13:52:34 -070093 }
94
95 /**
96 * Creates a path intent from the specified path and original
97 * connectivity intent.
98 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -070099 * @param path path to create an intent for
Ray Milkeya058c732014-10-08 13:52:34 -0700100 * @param intent original intent
101 */
102 private Intent createPathIntent(Path path,
103 PointToPointIntent intent) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700104 return new PathIntent(intent.appId(),
105 intent.selector(), intent.treatment(), path);
Ray Milkeya058c732014-10-08 13:52:34 -0700106 }
107
108 /**
109 * Computes a path between two ConnectPoints.
110 *
111 * @param one start of the path
112 * @param two end of the path
113 * @return Path between the two
114 * @throws PathNotFoundException if a path cannot be found
115 */
116 private Path getPath(ConnectPoint one, ConnectPoint two) {
weibit50eb95b2014-10-25 21:47:54 -0700117 // Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
118 Topology topology = topologyService.currentTopology();
119 LinkWeight weight = new LinkWeight() {
120 @Override
121 public double weight(TopologyEdge edge) {
122 Link.Type lt = edge.link().type();
123 if (lt == Link.Type.OPTICAL) {
124 return 1000.0;
125 } else {
Thomas Vachuska4353a5a2014-10-27 15:18:10 -0700126 return Double.MIN_VALUE;
weibit50eb95b2014-10-25 21:47:54 -0700127 }
128 }
129 };
130
131 Set<Path> paths = topologyService.getPaths(topology,
132 one.deviceId(),
133 two.deviceId(),
134 weight);
135
136 ArrayList<Path> localPaths = new ArrayList<>();
137 Iterator<Path> itr = paths.iterator();
138 while (itr.hasNext()) {
139 Path path = itr.next();
140 if (path.cost() >= 1000) {
141 continue;
142 }
143 localPaths.add(path);
144 }
145
146 if (localPaths.isEmpty()) {
147 throw new PathNotFoundException("No packet path from " + one + " to " + two);
Ray Milkeya058c732014-10-08 13:52:34 -0700148 }
149 // TODO: let's be more intelligent about this eventually
weibit50eb95b2014-10-25 21:47:54 -0700150 return localPaths.iterator().next();
Ray Milkeya058c732014-10-08 13:52:34 -0700151 }
152}