blob: 1cb960f73378182f4c63ad492a470ab74db80461 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
Ray Milkeyebc5d222015-03-18 15:45:36 -070018import java.util.List;
19
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.core.ApplicationId;
21import org.onosproject.net.Link;
22import org.onosproject.net.Path;
23import org.onosproject.net.flow.TrafficSelector;
24import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorb876bf12014-10-02 14:59:37 -070025
Ray Milkeyebc5d222015-03-18 15:45:36 -070026import com.google.common.base.MoreObjects;
27import com.google.common.base.Predicate;
28import com.google.common.collect.Iterables;
Jonathan Hart23b5a762015-01-26 14:47:33 -080029
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080030import static com.google.common.base.Preconditions.checkArgument;
31
Brian O'Connorb876bf12014-10-02 14:59:37 -070032/**
33 * Abstraction of explicitly path specified connectivity intent.
34 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070035public class PathIntent extends ConnectivityIntent {
Brian O'Connorb876bf12014-10-02 14:59:37 -070036
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 Vachuskac96058a2014-10-20 23:00:16 -070043 * @param appId application identifier
44 * @param selector traffic selector
45 * @param treatment treatment
46 * @param path traversed links
Ray Milkey460f4022014-11-05 15:41:43 -080047 * @param constraints optional list of constraints
Ray Milkeyc24cde32015-03-10 18:20:18 -070048 * @param priority priority to use for the generated flows
Ray Milkey460f4022014-11-05 15:41:43 -080049 * @throws NullPointerException {@code path} is null
50 */
Ray Milkeyebc5d222015-03-18 15:45:36 -070051 protected PathIntent(ApplicationId appId,
52 TrafficSelector selector,
53 TrafficTreatment treatment,
54 Path path,
55 List<Constraint> constraints,
56 int priority) {
57 super(appId, null, resources(path.links()), selector, treatment, constraints,
Ray Milkeyc24cde32015-03-10 18:20:18 -070058 priority);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080059 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080060 this.path = path;
61 }
62
63 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070064 * Constructor for serializer.
65 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070066 protected PathIntent() {
67 super();
68 this.path = null;
69 }
70
Ray Milkeyebc5d222015-03-18 15:45:36 -070071 /**
72 * Returns a new host to host intent builder.
73 *
74 * @return host to host intent builder
75 */
76 public static Builder builder() {
77 return new Builder();
78 }
79
80 /**
81 * Builder of a host to host intent.
82 */
83 public static class Builder extends ConnectivityIntent.Builder {
84 Path path;
85
86 protected Builder() {
87 // Hide default constructor
88 }
89
90 @Override
91 public Builder appId(ApplicationId appId) {
92 return (Builder) super.appId(appId);
93 }
94
95 @Override
96 public Builder key(Key key) {
97 return (Builder) super.key(key);
98 }
99
100 @Override
101 public Builder selector(TrafficSelector selector) {
102 return (Builder) super.selector(selector);
103 }
104
105 @Override
106 public Builder treatment(TrafficTreatment treatment) {
107 return (Builder) super.treatment(treatment);
108 }
109
110 @Override
111 public Builder constraints(List<Constraint> constraints) {
112 return (Builder) super.constraints(constraints);
113 }
114
115 @Override
116 public Builder priority(int priority) {
117 return (Builder) super.priority(priority);
118 }
119
120 /**
121 * Sets the path of the intent that will be built.
122 *
123 * @param path path for the intent
124 * @return this builder
125 */
126 public Builder path(Path path) {
127 this.path = path;
128 return this;
129 }
130
131 /**
132 * Builds a path intent from the accumulated parameters.
133 *
134 * @return point to point intent
135 */
136 public PathIntent build() {
137
138 return new PathIntent(
139 appId,
140 selector,
141 treatment,
142 path,
143 constraints,
144 priority
145 );
146 }
147 }
148
149
150
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800151 // NOTE: This methods takes linear time with the number of links.
152 /**
153 * Validates that source element ID and destination element ID of a link are
154 * different for the specified all links and that destination element ID of a link and source
155 * element ID of the next adjacent source element ID are same for the specified all links.
156 *
Sho SHIMIZU7338ccc2014-11-21 15:00:53 -0800157 * @param links links to be validated
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800158 */
159 public static void validate(List<Link> links) {
160 checkArgument(Iterables.all(links, new Predicate<Link>() {
161 @Override
162 public boolean apply(Link link) {
163 return !link.src().elementId().equals(link.dst().elementId());
164 }
165 }), "element of src and dst in a link must be different: {}", links);
166
167 boolean adjacentSame = true;
168 for (int i = 0; i < links.size() - 1; i++) {
169 if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) {
170 adjacentSame = false;
171 break;
172 }
173 }
174 checkArgument(adjacentSame, "adjacent links must share the same element: {}", links);
175 }
176
Brian O'Connorb876bf12014-10-02 14:59:37 -0700177 /**
178 * Returns the links which the traffic goes along.
179 *
180 * @return traversed links
181 */
tom85258ee2014-10-07 00:10:02 -0700182 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700183 return path;
184 }
185
186 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700187 public boolean isInstallable() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700188 return true;
189 }
190
Ray Milkeycaa450b2014-10-29 15:54:24 -0700191
Brian O'Connorb876bf12014-10-02 14:59:37 -0700192 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700193 public String toString() {
194 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700195 .add("id", id())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700196 .add("appId", appId())
Ray Milkeyc24cde32015-03-10 18:20:18 -0700197 .add("priority", priority())
Jonathan Hart23b5a762015-01-26 14:47:33 -0800198 .add("resources", resources())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700199 .add("selector", selector())
200 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800201 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700202 .add("path", path)
203 .toString();
204 }
tom95329eb2014-10-06 08:40:06 -0700205
Brian O'Connorb876bf12014-10-02 14:59:37 -0700206}