blob: 13fa61e164fe315475c9eed5172f9888d754ad9e [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
tom95329eb2014-10-06 08:40:06 -070018import com.google.common.base.MoreObjects;
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080019import com.google.common.base.Predicate;
20import com.google.common.collect.Iterables;
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
Jonathan Hart23b5a762015-01-26 14:47:33 -080027import java.util.Collections;
28import java.util.List;
29
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
Brian O'Connorb876bf12014-10-02 14:59:37 -070047 * @throws NullPointerException {@code path} is null
48 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070049 public PathIntent(ApplicationId appId, TrafficSelector selector,
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080050 TrafficTreatment treatment, Path path) {
Ray Milkeyc24cde32015-03-10 18:20:18 -070051 this(appId, selector, treatment, path, Collections.emptyList(),
52 DEFAULT_INTENT_PRIORITY);
Brian O'Connorb876bf12014-10-02 14:59:37 -070053 }
54
Thomas Vachuskac96058a2014-10-20 23:00:16 -070055 /**
Ray Milkey460f4022014-11-05 15:41:43 -080056 * 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 Milkeyc24cde32015-03-10 18:20:18 -070064 * @param priority priority to use for the generated flows
Ray Milkey460f4022014-11-05 15:41:43 -080065 * @throws NullPointerException {@code path} is null
66 */
67 public PathIntent(ApplicationId appId, TrafficSelector selector,
Ray Milkeyc24cde32015-03-10 18:20:18 -070068 TrafficTreatment treatment, Path path, List<Constraint> constraints,
69 int priority) {
70 super(appId, resources(path.links()), selector, treatment, constraints,
71 priority);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080072 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080073 this.path = path;
74 }
75
76 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070077 * Constructor for serializer.
78 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070079 protected PathIntent() {
80 super();
81 this.path = null;
82 }
83
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080084 // 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 SHIMIZU7338ccc2014-11-21 15:00:53 -080090 * @param links links to be validated
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080091 */
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'Connorb876bf12014-10-02 14:59:37 -0700110 /**
111 * Returns the links which the traffic goes along.
112 *
113 * @return traversed links
114 */
tom85258ee2014-10-07 00:10:02 -0700115 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700116 return path;
117 }
118
119 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700120 public boolean isInstallable() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700121 return true;
122 }
123
Ray Milkeycaa450b2014-10-29 15:54:24 -0700124
Brian O'Connorb876bf12014-10-02 14:59:37 -0700125 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700126 public String toString() {
127 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700128 .add("id", id())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700129 .add("appId", appId())
Ray Milkeyc24cde32015-03-10 18:20:18 -0700130 .add("priority", priority())
Jonathan Hart23b5a762015-01-26 14:47:33 -0800131 .add("resources", resources())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700132 .add("selector", selector())
133 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800134 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700135 .add("path", path)
136 .toString();
137 }
tom95329eb2014-10-06 08:40:06 -0700138
Brian O'Connorb876bf12014-10-02 14:59:37 -0700139}