blob: 387361192b5ea245ddd637674d00add38ef9fff3 [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'Connorb876bf12014-10-02 14:59:37 -070016package org.onlab.onos.net.intent;
17
Sho SHIMIZUc3df36b2014-11-11 18:19:29 -080018import java.util.Collections;
Ray Milkey460f4022014-11-05 15:41:43 -080019import java.util.List;
20
tom95329eb2014-10-06 08:40:06 -070021import com.google.common.base.MoreObjects;
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080022import com.google.common.base.Predicate;
23import com.google.common.collect.Iterables;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070024import org.onlab.onos.core.ApplicationId;
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080025import org.onlab.onos.net.Link;
Brian O'Connorb876bf12014-10-02 14:59:37 -070026import org.onlab.onos.net.Path;
27import org.onlab.onos.net.flow.TrafficSelector;
28import org.onlab.onos.net.flow.TrafficTreatment;
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) {
Sho SHIMIZUc3df36b2014-11-11 18:19:29 -080051 this(appId, selector, treatment, path, Collections.emptyList());
Brian O'Connorb876bf12014-10-02 14:59:37 -070052 }
53
Thomas Vachuskac96058a2014-10-20 23:00:16 -070054 /**
Ray Milkey460f4022014-11-05 15:41:43 -080055 * Creates a new point-to-point intent with the supplied ingress/egress
56 * ports and using the specified explicit path.
57 *
58 * @param appId application identifier
59 * @param selector traffic selector
60 * @param treatment treatment
61 * @param path traversed links
62 * @param constraints optional list of constraints
63 * @throws NullPointerException {@code path} is null
64 */
65 public PathIntent(ApplicationId appId, TrafficSelector selector,
66 TrafficTreatment treatment, Path path, List<Constraint> constraints) {
67 super(id(PathIntent.class, selector, treatment, path, constraints), appId,
68 resources(path.links()), selector, treatment, constraints);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080069 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080070 this.path = path;
71 }
72
73 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070074 * Constructor for serializer.
75 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070076 protected PathIntent() {
77 super();
78 this.path = null;
79 }
80
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080081 // NOTE: This methods takes linear time with the number of links.
82 /**
83 * Validates that source element ID and destination element ID of a link are
84 * different for the specified all links and that destination element ID of a link and source
85 * element ID of the next adjacent source element ID are same for the specified all links.
86 *
87 * @param links
88 */
89 public static void validate(List<Link> links) {
90 checkArgument(Iterables.all(links, new Predicate<Link>() {
91 @Override
92 public boolean apply(Link link) {
93 return !link.src().elementId().equals(link.dst().elementId());
94 }
95 }), "element of src and dst in a link must be different: {}", links);
96
97 boolean adjacentSame = true;
98 for (int i = 0; i < links.size() - 1; i++) {
99 if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) {
100 adjacentSame = false;
101 break;
102 }
103 }
104 checkArgument(adjacentSame, "adjacent links must share the same element: {}", links);
105 }
106
Brian O'Connorb876bf12014-10-02 14:59:37 -0700107 /**
108 * Returns the links which the traffic goes along.
109 *
110 * @return traversed links
111 */
tom85258ee2014-10-07 00:10:02 -0700112 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700113 return path;
114 }
115
116 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700117 public boolean isInstallable() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700118 return true;
119 }
120
Ray Milkeycaa450b2014-10-29 15:54:24 -0700121
Brian O'Connorb876bf12014-10-02 14:59:37 -0700122 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700123 public String toString() {
124 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700125 .add("id", id())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700126 .add("appId", appId())
127 .add("selector", selector())
128 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800129 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700130 .add("path", path)
131 .toString();
132 }
tom95329eb2014-10-06 08:40:06 -0700133
Brian O'Connorb876bf12014-10-02 14:59:37 -0700134}