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