Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onlab.onos.net.ConnectPoint; |
| 24 | import org.onlab.onos.net.Link; |
| 25 | import org.onlab.onos.net.Path; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.intent.Intent; |
| 27 | import org.onlab.onos.net.intent.IntentCompiler; |
| 28 | import org.onlab.onos.net.intent.IntentExtensionService; |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.intent.LinkCollectionIntent; |
| 30 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 31 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 32 | import org.onlab.onos.net.topology.PathService; |
| 33 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 34 | import java.util.Arrays; |
| 35 | import java.util.HashSet; |
| 36 | import java.util.List; |
| 37 | import java.util.Set; |
| 38 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 39 | /** |
| 40 | * An intent compiler for |
| 41 | * {@link org.onlab.onos.net.intent.MultiPointToSinglePointIntent}. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public class MultiPointToSinglePointIntentCompiler |
| 45 | implements IntentCompiler<MultiPointToSinglePointIntent> { |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected IntentExtensionService intentManager; |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected PathService pathService; |
| 52 | |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 53 | @Activate |
| 54 | public void activate() { |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 55 | intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this); |
| 56 | } |
| 57 | |
| 58 | @Deactivate |
| 59 | public void deactivate() { |
| 60 | intentManager.unregisterCompiler(PointToPointIntent.class); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public List<Intent> compile(MultiPointToSinglePointIntent intent) { |
| 65 | Set<Link> links = new HashSet<>(); |
| 66 | |
| 67 | for (ConnectPoint ingressPoint : intent.ingressPoints()) { |
| 68 | Path path = getPath(ingressPoint, intent.egressPoint()); |
| 69 | links.addAll(path.links()); |
| 70 | } |
| 71 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 72 | Intent result = new LinkCollectionIntent(intent.appId(), |
| 73 | intent.selector(), intent.treatment(), |
| 74 | links, intent.egressPoint()); |
Ray Milkey | 0742ec9 | 2014-10-13 08:39:55 -0700 | [diff] [blame] | 75 | return Arrays.asList(result); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Computes a path between two ConnectPoints. |
| 80 | * |
| 81 | * @param one start of the path |
| 82 | * @param two end of the path |
| 83 | * @return Path between the two |
| 84 | * @throws org.onlab.onos.net.intent.impl.PathNotFoundException if a path cannot be found |
| 85 | */ |
| 86 | private Path getPath(ConnectPoint one, ConnectPoint two) { |
| 87 | Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId()); |
| 88 | if (paths.isEmpty()) { |
| 89 | throw new PathNotFoundException("No path from " + one + " to " + two); |
| 90 | } |
| 91 | // TODO: let's be more intelligent about this eventually |
| 92 | return paths.iterator().next(); |
| 93 | } |
| 94 | } |