blob: dffbabfea2d65ee26e462debd453743438008b65 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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'Connor9476fa12015-06-25 15:17:17 -040020import com.google.common.annotations.Beta;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
22import org.onosproject.net.Link;
23import org.onosproject.net.Path;
24import org.onosproject.net.flow.TrafficSelector;
25import org.onosproject.net.flow.TrafficTreatment;
Brian O'Connorb876bf12014-10-02 14:59:37 -070026
Ray Milkeyebc5d222015-03-18 15:45:36 -070027import com.google.common.base.MoreObjects;
28import com.google.common.base.Predicate;
29import com.google.common.collect.Iterables;
Jonathan Hart23b5a762015-01-26 14:47:33 -080030
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080031import static com.google.common.base.Preconditions.checkArgument;
32
Brian O'Connorb876bf12014-10-02 14:59:37 -070033/**
34 * Abstraction of explicitly path specified connectivity intent.
35 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040036@Beta
Thomas Vachuskac96058a2014-10-20 23:00:16 -070037public class PathIntent extends ConnectivityIntent {
Brian O'Connorb876bf12014-10-02 14:59:37 -070038
39 private final Path path;
40
41 /**
42 * Creates a new point-to-point intent with the supplied ingress/egress
43 * ports and using the specified explicit path.
44 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070045 * @param appId application identifier
46 * @param selector traffic selector
47 * @param treatment treatment
48 * @param path traversed links
Ray Milkey460f4022014-11-05 15:41:43 -080049 * @param constraints optional list of constraints
Ray Milkeyc24cde32015-03-10 18:20:18 -070050 * @param priority priority to use for the generated flows
Ray Milkey460f4022014-11-05 15:41:43 -080051 * @throws NullPointerException {@code path} is null
52 */
Ray Milkeyebc5d222015-03-18 15:45:36 -070053 protected PathIntent(ApplicationId appId,
54 TrafficSelector selector,
55 TrafficTreatment treatment,
56 Path path,
57 List<Constraint> constraints,
58 int priority) {
59 super(appId, null, resources(path.links()), selector, treatment, constraints,
Ray Milkeyc24cde32015-03-10 18:20:18 -070060 priority);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080061 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080062 this.path = path;
63 }
64
65 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070066 * Constructor for serializer.
67 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070068 protected PathIntent() {
69 super();
70 this.path = null;
71 }
72
Ray Milkeyebc5d222015-03-18 15:45:36 -070073 /**
74 * Returns a new host to host intent builder.
75 *
76 * @return host to host intent builder
77 */
78 public static Builder builder() {
79 return new Builder();
80 }
81
82 /**
83 * Builder of a host to host intent.
84 */
85 public static class Builder extends ConnectivityIntent.Builder {
86 Path path;
87
88 protected Builder() {
89 // Hide default constructor
90 }
91
92 @Override
93 public Builder appId(ApplicationId appId) {
94 return (Builder) super.appId(appId);
95 }
96
97 @Override
98 public Builder key(Key key) {
99 return (Builder) super.key(key);
100 }
101
102 @Override
103 public Builder selector(TrafficSelector selector) {
104 return (Builder) super.selector(selector);
105 }
106
107 @Override
108 public Builder treatment(TrafficTreatment treatment) {
109 return (Builder) super.treatment(treatment);
110 }
111
112 @Override
113 public Builder constraints(List<Constraint> constraints) {
114 return (Builder) super.constraints(constraints);
115 }
116
117 @Override
118 public Builder priority(int priority) {
119 return (Builder) super.priority(priority);
120 }
121
122 /**
123 * Sets the path of the intent that will be built.
124 *
125 * @param path path for the intent
126 * @return this builder
127 */
128 public Builder path(Path path) {
129 this.path = path;
130 return this;
131 }
132
133 /**
134 * Builds a path intent from the accumulated parameters.
135 *
136 * @return point to point intent
137 */
138 public PathIntent build() {
139
140 return new PathIntent(
141 appId,
142 selector,
143 treatment,
144 path,
145 constraints,
146 priority
147 );
148 }
149 }
150
151
152
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800153 // NOTE: This methods takes linear time with the number of links.
154 /**
155 * Validates that source element ID and destination element ID of a link are
156 * different for the specified all links and that destination element ID of a link and source
157 * element ID of the next adjacent source element ID are same for the specified all links.
158 *
Sho SHIMIZU7338ccc2014-11-21 15:00:53 -0800159 * @param links links to be validated
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800160 */
161 public static void validate(List<Link> links) {
162 checkArgument(Iterables.all(links, new Predicate<Link>() {
163 @Override
164 public boolean apply(Link link) {
165 return !link.src().elementId().equals(link.dst().elementId());
166 }
167 }), "element of src and dst in a link must be different: {}", links);
168
169 boolean adjacentSame = true;
170 for (int i = 0; i < links.size() - 1; i++) {
171 if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) {
172 adjacentSame = false;
173 break;
174 }
175 }
176 checkArgument(adjacentSame, "adjacent links must share the same element: {}", links);
177 }
178
Brian O'Connorb876bf12014-10-02 14:59:37 -0700179 /**
180 * Returns the links which the traffic goes along.
181 *
182 * @return traversed links
183 */
tom85258ee2014-10-07 00:10:02 -0700184 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700185 return path;
186 }
187
188 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700189 public String toString() {
190 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700191 .add("id", id())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700192 .add("appId", appId())
Ray Milkeyc24cde32015-03-10 18:20:18 -0700193 .add("priority", priority())
Jonathan Hart23b5a762015-01-26 14:47:33 -0800194 .add("resources", resources())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700195 .add("selector", selector())
196 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800197 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700198 .add("path", path)
199 .toString();
200 }
tom95329eb2014-10-06 08:40:06 -0700201
Brian O'Connorb876bf12014-10-02 14:59:37 -0700202}