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