Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.compiler; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | ab753e1 | 2016-06-13 19:41:05 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.DefaultLink; |
| 25 | import org.onosproject.net.DefaultPath; |
| 26 | import org.onosproject.net.Host; |
| 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.Path; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.host.HostService; |
| 31 | import org.onosproject.net.intent.HostToHostIntent; |
| 32 | import org.onosproject.net.intent.Intent; |
| 33 | import org.onosproject.net.intent.PathIntent; |
| 34 | import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 35 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 36 | import java.util.ArrayList; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 37 | import java.util.Arrays; |
| 38 | import java.util.List; |
Thomas Vachuska | ab753e1 | 2016-06-13 19:41:05 -0700 | [diff] [blame^] | 39 | import java.util.Objects; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 40 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import static org.onosproject.net.flow.DefaultTrafficSelector.builder; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 42 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 43 | /** |
| 44 | * A intent compiler for {@link HostToHostIntent}. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | public class HostToHostIntentCompiler |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 48 | extends ConnectivityIntentCompiler<HostToHostIntent> { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 49 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected HostService hostService; |
| 52 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 53 | @Activate |
| 54 | public void activate() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 55 | intentManager.registerCompiler(HostToHostIntent.class, this); |
| 56 | } |
| 57 | |
| 58 | @Deactivate |
| 59 | public void deactivate() { |
| 60 | intentManager.unregisterCompiler(HostToHostIntent.class); |
| 61 | } |
| 62 | |
| 63 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 64 | public List<Intent> compile(HostToHostIntent intent, List<Intent> installable) { |
Thomas Vachuska | ab753e1 | 2016-06-13 19:41:05 -0700 | [diff] [blame^] | 65 | // If source and destination are the same, there are never any installables. |
| 66 | if (Objects.equals(intent.one(), intent.two())) { |
| 67 | return ImmutableList.of(); |
| 68 | } |
| 69 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 70 | boolean isAsymmetric = intent.constraints().contains(new AsymmetricPathConstraint()); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 71 | Path pathOne = getPath(intent, intent.one(), intent.two()); |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 72 | Path pathTwo = isAsymmetric ? |
| 73 | getPath(intent, intent.two(), intent.one()) : invertPath(pathOne); |
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 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 82 | // Inverts the specified path. This makes an assumption that each link in |
| 83 | // the path has a reverse link available. Under most circumstances, this |
| 84 | // assumption will hold. |
| 85 | private Path invertPath(Path path) { |
| 86 | List<Link> reverseLinks = new ArrayList<>(path.links().size()); |
| 87 | for (Link link : path.links()) { |
| 88 | reverseLinks.add(0, reverseLink(link)); |
| 89 | } |
| 90 | return new DefaultPath(path.providerId(), reverseLinks, path.cost()); |
| 91 | } |
| 92 | |
| 93 | // Produces a reverse variant of the specified link. |
| 94 | private Link reverseLink(Link link) { |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 95 | return DefaultLink.builder().providerId(link.providerId()) |
| 96 | .src(link.dst()) |
| 97 | .dst(link.src()) |
| 98 | .type(link.type()) |
| 99 | .state(link.state()) |
| 100 | .isExpected(link.isExpected()) |
| 101 | .build(); |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 102 | } |
| 103 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 104 | // Creates a path intent from the specified path and original connectivity intent. |
| 105 | private Intent createPathIntent(Path path, Host src, Host dst, |
| 106 | HostToHostIntent intent) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 107 | TrafficSelector selector = builder(intent.selector()) |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 108 | .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build(); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 109 | return PathIntent.builder() |
| 110 | .appId(intent.appId()) |
| 111 | .selector(selector) |
| 112 | .treatment(intent.treatment()) |
| 113 | .path(path) |
| 114 | .constraints(intent.constraints()) |
| 115 | .priority(intent.priority()) |
| 116 | .build(); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 119 | } |