Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 19 | import com.google.common.base.Predicate; |
| 20 | import com.google.common.collect.Iterables; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.net.Link; |
| 23 | import org.onosproject.net.Path; |
| 24 | import org.onosproject.net.flow.TrafficSelector; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 26 | |
Jonathan Hart | 23b5a76 | 2015-01-26 14:47:33 -0800 | [diff] [blame] | 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkArgument; |
| 31 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Abstraction of explicitly path specified connectivity intent. |
| 34 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 35 | public class PathIntent extends ConnectivityIntent { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 36 | |
| 37 | private final Path path; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new point-to-point intent with the supplied ingress/egress |
| 41 | * ports and using the specified explicit path. |
| 42 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 43 | * @param appId application identifier |
| 44 | * @param selector traffic selector |
| 45 | * @param treatment treatment |
| 46 | * @param path traversed links |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 47 | * @throws NullPointerException {@code path} is null |
| 48 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 49 | public PathIntent(ApplicationId appId, TrafficSelector selector, |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 50 | TrafficTreatment treatment, Path path) { |
Sho SHIMIZU | c3df36b | 2014-11-11 18:19:29 -0800 | [diff] [blame] | 51 | this(appId, selector, treatment, path, Collections.emptyList()); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 54 | /** |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 55 | * Creates a new point-to-point intent with the supplied ingress/egress |
| 56 | * ports and using the specified explicit path. |
| 57 | * |
| 58 | * @param appId application identifier |
| 59 | * @param selector traffic selector |
| 60 | * @param treatment treatment |
| 61 | * @param path traversed links |
| 62 | * @param constraints optional list of constraints |
| 63 | * @throws NullPointerException {@code path} is null |
| 64 | */ |
| 65 | public PathIntent(ApplicationId appId, TrafficSelector selector, |
| 66 | TrafficTreatment treatment, Path path, List<Constraint> constraints) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 67 | super(appId, resources(path.links()), selector, treatment, constraints); |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 68 | PathIntent.validate(path.links()); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 69 | this.path = path; |
| 70 | } |
| 71 | |
| 72 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 73 | * Constructor for serializer. |
| 74 | */ |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 75 | protected PathIntent() { |
| 76 | super(); |
| 77 | this.path = null; |
| 78 | } |
| 79 | |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 80 | // NOTE: This methods takes linear time with the number of links. |
| 81 | /** |
| 82 | * Validates that source element ID and destination element ID of a link are |
| 83 | * different for the specified all links and that destination element ID of a link and source |
| 84 | * element ID of the next adjacent source element ID are same for the specified all links. |
| 85 | * |
Sho SHIMIZU | 7338ccc | 2014-11-21 15:00:53 -0800 | [diff] [blame] | 86 | * @param links links to be validated |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 87 | */ |
| 88 | public static void validate(List<Link> links) { |
| 89 | checkArgument(Iterables.all(links, new Predicate<Link>() { |
| 90 | @Override |
| 91 | public boolean apply(Link link) { |
| 92 | return !link.src().elementId().equals(link.dst().elementId()); |
| 93 | } |
| 94 | }), "element of src and dst in a link must be different: {}", links); |
| 95 | |
| 96 | boolean adjacentSame = true; |
| 97 | for (int i = 0; i < links.size() - 1; i++) { |
| 98 | if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) { |
| 99 | adjacentSame = false; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | checkArgument(adjacentSame, "adjacent links must share the same element: {}", links); |
| 104 | } |
| 105 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Returns the links which the traffic goes along. |
| 108 | * |
| 109 | * @return traversed links |
| 110 | */ |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 111 | public Path path() { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 112 | return path; |
| 113 | } |
| 114 | |
| 115 | @Override |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 116 | public boolean isInstallable() { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 120 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 121 | @Override |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 122 | public String toString() { |
| 123 | return MoreObjects.toStringHelper(getClass()) |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 124 | .add("id", id()) |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 125 | .add("appId", appId()) |
Jonathan Hart | 23b5a76 | 2015-01-26 14:47:33 -0800 | [diff] [blame] | 126 | .add("resources", resources()) |
Thomas Vachuska | e291c84 | 2014-10-21 02:52:38 -0700 | [diff] [blame] | 127 | .add("selector", selector()) |
| 128 | .add("treatment", treatment()) |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 129 | .add("constraints", constraints()) |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 130 | .add("path", path) |
| 131 | .toString(); |
| 132 | } |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 133 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 134 | } |