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 | */ |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent.impl; |
| 17 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 18 | import java.util.Arrays; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.Host; |
| 28 | import org.onlab.onos.net.HostId; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.Link; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.Path; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.flow.TrafficSelector; |
| 32 | import org.onlab.onos.net.host.HostService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.intent.HostToHostIntent; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.intent.Intent; |
| 35 | import org.onlab.onos.net.intent.IntentCompiler; |
| 36 | import org.onlab.onos.net.intent.IntentExtensionService; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.intent.PathIntent; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.topology.LinkWeight; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 39 | import org.onlab.onos.net.resource.LinkResourceRequest; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 40 | import org.onlab.onos.net.topology.PathService; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.topology.TopologyEdge; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 42 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 43 | import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder; |
| 44 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 45 | /** |
| 46 | * A intent compiler for {@link HostToHostIntent}. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | public class HostToHostIntentCompiler |
| 50 | implements IntentCompiler<HostToHostIntent> { |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected IntentExtensionService intentManager; |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected PathService pathService; |
| 57 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected HostService hostService; |
| 60 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 61 | @Activate |
| 62 | public void activate() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 63 | intentManager.registerCompiler(HostToHostIntent.class, this); |
| 64 | } |
| 65 | |
| 66 | @Deactivate |
| 67 | public void deactivate() { |
| 68 | intentManager.unregisterCompiler(HostToHostIntent.class); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public List<Intent> compile(HostToHostIntent intent) { |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 73 | Path pathOne = getPath(intent.one(), intent.two()); |
| 74 | Path pathTwo = getPath(intent.two(), intent.one()); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 75 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 76 | Host one = hostService.getHost(intent.one()); |
| 77 | Host two = hostService.getHost(intent.two()); |
| 78 | |
| 79 | return Arrays.asList(createPathIntent(pathOne, one, two, intent), |
| 80 | createPathIntent(pathTwo, two, one, intent)); |
| 81 | } |
| 82 | |
| 83 | // Creates a path intent from the specified path and original connectivity intent. |
| 84 | private Intent createPathIntent(Path path, Host src, Host dst, |
| 85 | HostToHostIntent intent) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 86 | TrafficSelector selector = builder(intent.selector()) |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 87 | .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 88 | return new PathIntent(intent.appId(), selector, intent.treatment(), |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame^] | 89 | path, new LinkResourceRequest[0]); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private Path getPath(HostId one, HostId two) { |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 93 | Set<Path> paths = pathService.getPaths(one, two, new LinkWeight() { |
| 94 | @Override |
| 95 | public double weight(TopologyEdge edge) { |
| 96 | return edge.link().type() == Link.Type.OPTICAL ? -1 : +1; |
| 97 | } |
| 98 | }); |
| 99 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 100 | if (paths.isEmpty()) { |
| 101 | throw new PathNotFoundException("No path from host " + one + " to " + two); |
| 102 | } |
| 103 | // TODO: let's be more intelligent about this eventually |
| 104 | return paths.iterator().next(); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 105 | } |
| 106 | } |