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