blob: 9b8b149df180c5206b75672b843f1fa5ea2f27b6 [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 Milkey0742ec92014-10-13 08:39:55 -070016package org.onlab.onos.net.intent.impl;
17
Ray Milkey0742ec92014-10-13 08:39:55 -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.Link;
25import org.onlab.onos.net.Path;
Ray Milkey0742ec92014-10-13 08:39:55 -070026import org.onlab.onos.net.intent.Intent;
27import org.onlab.onos.net.intent.IntentCompiler;
28import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkey0742ec92014-10-13 08:39:55 -070029import org.onlab.onos.net.intent.LinkCollectionIntent;
30import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
31import org.onlab.onos.net.intent.PointToPointIntent;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070032import org.onlab.onos.net.resource.LinkResourceAllocations;
Ray Milkey0742ec92014-10-13 08:39:55 -070033import org.onlab.onos.net.topology.PathService;
34
Thomas Vachuskab97cf282014-10-20 23:31:12 -070035import java.util.Arrays;
36import java.util.HashSet;
37import java.util.List;
38import java.util.Set;
39
Ray Milkey0742ec92014-10-13 08:39:55 -070040/**
41 * An intent compiler for
42 * {@link org.onlab.onos.net.intent.MultiPointToSinglePointIntent}.
43 */
44@Component(immediate = true)
45public class MultiPointToSinglePointIntentCompiler
46 implements IntentCompiler<MultiPointToSinglePointIntent> {
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected IntentExtensionService intentManager;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected PathService pathService;
53
Ray Milkey0742ec92014-10-13 08:39:55 -070054 @Activate
55 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070056 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
57 }
58
59 @Deactivate
60 public void deactivate() {
61 intentManager.unregisterCompiler(PointToPointIntent.class);
62 }
63
64 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070065 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable,
66 Set<LinkResourceAllocations> resources) {
Ray Milkey0742ec92014-10-13 08:39:55 -070067 Set<Link> links = new HashSet<>();
68
69 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
70 Path path = getPath(ingressPoint, intent.egressPoint());
71 links.addAll(path.links());
72 }
73
Thomas Vachuskab97cf282014-10-20 23:31:12 -070074 Intent result = new LinkCollectionIntent(intent.appId(),
75 intent.selector(), intent.treatment(),
76 links, intent.egressPoint());
Ray Milkey0742ec92014-10-13 08:39:55 -070077 return Arrays.asList(result);
78 }
79
80 /**
81 * Computes a path between two ConnectPoints.
82 *
83 * @param one start of the path
84 * @param two end of the path
85 * @return Path between the two
86 * @throws org.onlab.onos.net.intent.impl.PathNotFoundException if a path cannot be found
87 */
88 private Path getPath(ConnectPoint one, ConnectPoint two) {
89 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
90 if (paths.isEmpty()) {
91 throw new PathNotFoundException("No path from " + one + " to " + two);
92 }
93 // TODO: let's be more intelligent about this eventually
94 return paths.iterator().next();
95 }
96}