blob: 047ace20f01b92677a5d7a434af9a58dfbedf20f [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
Jonathan Hartf5e35802014-12-01 20:45:18 -080018import java.util.Arrays;
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22import java.util.Set;
23
Ray Milkey0742ec92014-10-13 08:39:55 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.onlab.onos.net.ConnectPoint;
Jonathan Hartf5e35802014-12-01 20:45:18 -080030import org.onlab.onos.net.DeviceId;
Ray Milkey0742ec92014-10-13 08:39:55 -070031import org.onlab.onos.net.Link;
32import org.onlab.onos.net.Path;
Ray Milkey0742ec92014-10-13 08:39:55 -070033import org.onlab.onos.net.intent.Intent;
34import org.onlab.onos.net.intent.IntentCompiler;
35import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkey0742ec92014-10-13 08:39:55 -070036import org.onlab.onos.net.intent.LinkCollectionIntent;
37import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
38import org.onlab.onos.net.intent.PointToPointIntent;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070039import org.onlab.onos.net.resource.LinkResourceAllocations;
Ray Milkey0742ec92014-10-13 08:39:55 -070040import org.onlab.onos.net.topology.PathService;
41
Jonathan Hartf5e35802014-12-01 20:45:18 -080042import com.google.common.collect.Sets;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070043
Ray Milkey0742ec92014-10-13 08:39:55 -070044/**
45 * An intent compiler for
46 * {@link org.onlab.onos.net.intent.MultiPointToSinglePointIntent}.
47 */
48@Component(immediate = true)
49public class MultiPointToSinglePointIntentCompiler
50 implements IntentCompiler<MultiPointToSinglePointIntent> {
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected IntentExtensionService intentManager;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected PathService pathService;
57
Ray Milkey0742ec92014-10-13 08:39:55 -070058 @Activate
59 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070060 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
61 }
62
63 @Deactivate
64 public void deactivate() {
65 intentManager.unregisterCompiler(PointToPointIntent.class);
66 }
67
68 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070069 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable,
70 Set<LinkResourceAllocations> resources) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080071 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070072
73 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
74 Path path = getPath(ingressPoint, intent.egressPoint());
Jonathan Hartf5e35802014-12-01 20:45:18 -080075 for (Link link : path.links()) {
76 if (links.containsKey(link.src().deviceId())) {
77 // We've already reached the existing tree with the first
78 // part of this path. Don't add the remainder of the path
79 // in case it differs from the path we already have.
80 break;
81 }
82
83 links.put(link.src().deviceId(), link);
84 }
Ray Milkey0742ec92014-10-13 08:39:55 -070085 }
86
Thomas Vachuskab97cf282014-10-20 23:31:12 -070087 Intent result = new LinkCollectionIntent(intent.appId(),
88 intent.selector(), intent.treatment(),
Jonathan Hartf5e35802014-12-01 20:45:18 -080089 Sets.newHashSet(links.values()), intent.egressPoint());
Ray Milkey0742ec92014-10-13 08:39:55 -070090 return Arrays.asList(result);
91 }
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 org.onlab.onos.net.intent.impl.PathNotFoundException if a path cannot be found
100 */
101 private Path getPath(ConnectPoint one, ConnectPoint two) {
102 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
103 if (paths.isEmpty()) {
104 throw new PathNotFoundException("No path from " + one + " to " + two);
105 }
106 // TODO: let's be more intelligent about this eventually
107 return paths.iterator().next();
108 }
109}