blob: 0c831fd5dabe1b6431fb9fd495d68f759de69ed3 [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;
Ray Milkeyebc5d222015-03-18 15:45:36 -070028import 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 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040035@Beta
Thomas Vachuskac96058a2014-10-20 23:00:16 -070036public class PathIntent extends ConnectivityIntent {
Brian O'Connorb876bf12014-10-02 14:59:37 -070037
38 private final Path path;
39
40 /**
41 * Creates a new point-to-point intent with the supplied ingress/egress
42 * ports and using the specified explicit path.
43 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070044 * @param appId application identifier
45 * @param selector traffic selector
46 * @param treatment treatment
47 * @param path traversed links
Ray Milkey460f4022014-11-05 15:41:43 -080048 * @param constraints optional list of constraints
Ray Milkeyc24cde32015-03-10 18:20:18 -070049 * @param priority priority to use for the generated flows
Ray Milkey460f4022014-11-05 15:41:43 -080050 * @throws NullPointerException {@code path} is null
51 */
Ray Milkeyebc5d222015-03-18 15:45:36 -070052 protected PathIntent(ApplicationId appId,
53 TrafficSelector selector,
54 TrafficTreatment treatment,
55 Path path,
56 List<Constraint> constraints,
57 int priority) {
58 super(appId, null, resources(path.links()), selector, treatment, constraints,
Ray Milkeyc24cde32015-03-10 18:20:18 -070059 priority);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080060 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080061 this.path = path;
62 }
63
64 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070065 * Constructor for serializer.
66 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070067 protected PathIntent() {
68 super();
69 this.path = null;
70 }
71
Ray Milkeyebc5d222015-03-18 15:45:36 -070072 /**
73 * Returns a new host to host intent builder.
74 *
75 * @return host to host intent builder
76 */
77 public static Builder builder() {
78 return new Builder();
79 }
80
81 /**
82 * Builder of a host to host intent.
83 */
84 public static class Builder extends ConnectivityIntent.Builder {
85 Path path;
86
87 protected Builder() {
88 // Hide default constructor
89 }
90
91 @Override
92 public Builder appId(ApplicationId appId) {
93 return (Builder) super.appId(appId);
94 }
95
96 @Override
97 public Builder key(Key key) {
98 return (Builder) super.key(key);
99 }
100
101 @Override
102 public Builder selector(TrafficSelector selector) {
103 return (Builder) super.selector(selector);
104 }
105
106 @Override
107 public Builder treatment(TrafficTreatment treatment) {
108 return (Builder) super.treatment(treatment);
109 }
110
111 @Override
112 public Builder constraints(List<Constraint> constraints) {
113 return (Builder) super.constraints(constraints);
114 }
115
116 @Override
117 public Builder priority(int priority) {
118 return (Builder) super.priority(priority);
119 }
120
121 /**
122 * Sets the path of the intent that will be built.
123 *
124 * @param path path for the intent
125 * @return this builder
126 */
127 public Builder path(Path path) {
128 this.path = path;
129 return this;
130 }
131
132 /**
133 * Builds a path intent from the accumulated parameters.
134 *
135 * @return point to point intent
136 */
137 public PathIntent build() {
138
139 return new PathIntent(
140 appId,
141 selector,
142 treatment,
143 path,
144 constraints,
145 priority
146 );
147 }
148 }
149
150
151
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800152 // NOTE: This methods takes linear time with the number of links.
153 /**
154 * Validates that source element ID and destination element ID of a link are
155 * different for the specified all links and that destination element ID of a link and source
156 * element ID of the next adjacent source element ID are same for the specified all links.
157 *
Sho SHIMIZU7338ccc2014-11-21 15:00:53 -0800158 * @param links links to be validated
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800159 */
160 public static void validate(List<Link> links) {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700161 checkArgument(Iterables.all(links, link -> !link.src().elementId().equals(link.dst().elementId())),
162 "element of src and dst in a link must be different: {}", links);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800163
164 boolean adjacentSame = true;
165 for (int i = 0; i < links.size() - 1; i++) {
166 if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) {
167 adjacentSame = false;
168 break;
169 }
170 }
171 checkArgument(adjacentSame, "adjacent links must share the same element: {}", links);
172 }
173
Brian O'Connorb876bf12014-10-02 14:59:37 -0700174 /**
175 * Returns the links which the traffic goes along.
176 *
177 * @return traversed links
178 */
tom85258ee2014-10-07 00:10:02 -0700179 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700180 return path;
181 }
182
183 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700184 public String toString() {
185 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700186 .add("id", id())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700187 .add("appId", appId())
Ray Milkeyc24cde32015-03-10 18:20:18 -0700188 .add("priority", priority())
Jonathan Hart23b5a762015-01-26 14:47:33 -0800189 .add("resources", resources())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700190 .add("selector", selector())
191 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800192 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700193 .add("path", path)
194 .toString();
195 }
tom95329eb2014-10-06 08:40:06 -0700196
Brian O'Connorb876bf12014-10-02 14:59:37 -0700197}