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; |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.DefaultLink; |
| 26 | import org.onosproject.net.DefaultPath; |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.FilteredConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.Host; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.Path; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.host.HostService; |
| 34 | import org.onosproject.net.intent.HostToHostIntent; |
| 35 | import org.onosproject.net.intent.Intent; |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentCompilationException; |
| 37 | import org.onosproject.net.intent.LinkCollectionIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.PathIntent; |
| 39 | import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 41 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 42 | import java.util.ArrayList; |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 43 | import java.util.Arrays; |
| 44 | import java.util.List; |
Thomas Vachuska | ab753e1 | 2016-06-13 19:41:05 -0700 | [diff] [blame] | 45 | import java.util.Objects; |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 46 | import java.util.Set; |
| 47 | import java.util.stream.Collectors; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 48 | |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 49 | import static org.onosproject.net.Link.Type.EDGE; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import static org.onosproject.net.flow.DefaultTrafficSelector.builder; |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 52 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 53 | /** |
| 54 | * A intent compiler for {@link HostToHostIntent}. |
| 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | public class HostToHostIntentCompiler |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 58 | extends ConnectivityIntentCompiler<HostToHostIntent> { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 59 | |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 60 | private final Logger log = getLogger(getClass()); |
| 61 | |
| 62 | private static final String DEVICE_ID_NOT_FOUND = "Didn't find device id in the link"; |
| 63 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected HostService hostService; |
| 66 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 67 | @Activate |
| 68 | public void activate() { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 69 | intentManager.registerCompiler(HostToHostIntent.class, this); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
| 74 | intentManager.unregisterCompiler(HostToHostIntent.class); |
| 75 | } |
| 76 | |
| 77 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 78 | public List<Intent> compile(HostToHostIntent intent, List<Intent> installable) { |
Thomas Vachuska | ab753e1 | 2016-06-13 19:41:05 -0700 | [diff] [blame] | 79 | // If source and destination are the same, there are never any installables. |
| 80 | if (Objects.equals(intent.one(), intent.two())) { |
| 81 | return ImmutableList.of(); |
| 82 | } |
| 83 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 84 | boolean isAsymmetric = intent.constraints().contains(new AsymmetricPathConstraint()); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 85 | Path pathOne = getPath(intent, intent.one(), intent.two()); |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 86 | Path pathTwo = isAsymmetric ? |
| 87 | getPath(intent, intent.two(), intent.one()) : invertPath(pathOne); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 88 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 89 | Host one = hostService.getHost(intent.one()); |
| 90 | Host two = hostService.getHost(intent.two()); |
| 91 | |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 92 | return Arrays.asList(createLinkCollectionIntent(pathOne, one, two, intent), |
| 93 | createLinkCollectionIntent(pathTwo, two, one, intent)); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 96 | // Inverts the specified path. This makes an assumption that each link in |
| 97 | // the path has a reverse link available. Under most circumstances, this |
| 98 | // assumption will hold. |
| 99 | private Path invertPath(Path path) { |
| 100 | List<Link> reverseLinks = new ArrayList<>(path.links().size()); |
| 101 | for (Link link : path.links()) { |
| 102 | reverseLinks.add(0, reverseLink(link)); |
| 103 | } |
| 104 | return new DefaultPath(path.providerId(), reverseLinks, path.cost()); |
| 105 | } |
| 106 | |
| 107 | // Produces a reverse variant of the specified link. |
| 108 | private Link reverseLink(Link link) { |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 109 | return DefaultLink.builder().providerId(link.providerId()) |
| 110 | .src(link.dst()) |
| 111 | .dst(link.src()) |
| 112 | .type(link.type()) |
| 113 | .state(link.state()) |
| 114 | .isExpected(link.isExpected()) |
| 115 | .build(); |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 116 | } |
| 117 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 118 | // Creates a path intent from the specified path and original connectivity intent. |
| 119 | private Intent createPathIntent(Path path, Host src, Host dst, |
| 120 | HostToHostIntent intent) { |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 121 | TrafficSelector selector = builder(intent.selector()) |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 122 | .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build(); |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 123 | return PathIntent.builder() |
| 124 | .appId(intent.appId()) |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame^] | 125 | .key(intent.key()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 126 | .selector(selector) |
| 127 | .treatment(intent.treatment()) |
| 128 | .path(path) |
| 129 | .constraints(intent.constraints()) |
| 130 | .priority(intent.priority()) |
| 131 | .build(); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Pier Ventre | 98308ab | 2016-10-12 14:35:05 -0700 | [diff] [blame] | 134 | private FilteredConnectPoint getFilteredPointFromLink(Link link) { |
| 135 | FilteredConnectPoint filteredConnectPoint; |
| 136 | if (link.src().elementId() instanceof DeviceId) { |
| 137 | filteredConnectPoint = new FilteredConnectPoint(link.src()); |
| 138 | } else if (link.dst().elementId() instanceof DeviceId) { |
| 139 | filteredConnectPoint = new FilteredConnectPoint(link.dst()); |
| 140 | } else { |
| 141 | throw new IntentCompilationException(DEVICE_ID_NOT_FOUND); |
| 142 | } |
| 143 | return filteredConnectPoint; |
| 144 | } |
| 145 | |
| 146 | private Intent createLinkCollectionIntent(Path path, |
| 147 | Host src, |
| 148 | Host dst, |
| 149 | HostToHostIntent intent) { |
| 150 | /* |
| 151 | * The path contains also the edge links, these are not necessary |
| 152 | * for the LinkCollectionIntent. |
| 153 | */ |
| 154 | Set<Link> coreLinks = path.links() |
| 155 | .stream() |
| 156 | .filter(link -> !link.type().equals(EDGE)) |
| 157 | .collect(Collectors.toSet()); |
| 158 | |
| 159 | Link ingressLink = path.links().get(0); |
| 160 | Link egressLink = path.links().get(path.links().size() - 1); |
| 161 | |
| 162 | FilteredConnectPoint ingressPoint = getFilteredPointFromLink(ingressLink); |
| 163 | FilteredConnectPoint egressPoint = getFilteredPointFromLink(egressLink); |
| 164 | |
| 165 | TrafficSelector selector = builder(intent.selector()) |
| 166 | .matchEthSrc(src.mac()) |
| 167 | .matchEthDst(dst.mac()) |
| 168 | .build(); |
| 169 | |
| 170 | return LinkCollectionIntent.builder() |
| 171 | .key(intent.key()) |
| 172 | .appId(intent.appId()) |
| 173 | .selector(selector) |
| 174 | .treatment(intent.treatment()) |
| 175 | .links(coreLinks) |
| 176 | .filteredIngressPoints(ImmutableSet.of( |
| 177 | ingressPoint |
| 178 | )) |
| 179 | .filteredEgressPoints(ImmutableSet.of( |
| 180 | egressPoint |
| 181 | )) |
| 182 | .applyTreatmentOnEgress(true) |
| 183 | .constraints(intent.constraints()) |
| 184 | .priority(intent.priority()) |
| 185 | .build(); |
| 186 | } |
| 187 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 188 | } |