blob: a21509d5852ad1ea7140149c8b86b94bd3fbc0cf [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) {
Brian O'Connor520c0522014-11-23 23:50:47 -080067 super(appId, resources(path.links()), selector, treatment, constraints);
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080068 PathIntent.validate(path.links());
Ray Milkey460f4022014-11-05 15:41:43 -080069 this.path = path;
70 }
71
72 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -070073 * Constructor for serializer.
74 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070075 protected PathIntent() {
76 super();
77 this.path = null;
78 }
79
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080080 // NOTE: This methods takes linear time with the number of links.
81 /**
82 * Validates that source element ID and destination element ID of a link are
83 * different for the specified all links and that destination element ID of a link and source
84 * element ID of the next adjacent source element ID are same for the specified all links.
85 *
Sho SHIMIZU7338ccc2014-11-21 15:00:53 -080086 * @param links links to be validated
Sho SHIMIZU3908fde2014-11-19 16:30:22 -080087 */
88 public static void validate(List<Link> links) {
89 checkArgument(Iterables.all(links, new Predicate<Link>() {
90 @Override
91 public boolean apply(Link link) {
92 return !link.src().elementId().equals(link.dst().elementId());
93 }
94 }), "element of src and dst in a link must be different: {}", links);
95
96 boolean adjacentSame = true;
97 for (int i = 0; i < links.size() - 1; i++) {
98 if (!links.get(i).dst().elementId().equals(links.get(i + 1).src().elementId())) {
99 adjacentSame = false;
100 break;
101 }
102 }
103 checkArgument(adjacentSame, "adjacent links must share the same element: {}", links);
104 }
105
Brian O'Connorb876bf12014-10-02 14:59:37 -0700106 /**
107 * Returns the links which the traffic goes along.
108 *
109 * @return traversed links
110 */
tom85258ee2014-10-07 00:10:02 -0700111 public Path path() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700112 return path;
113 }
114
115 @Override
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700116 public boolean isInstallable() {
Brian O'Connorb876bf12014-10-02 14:59:37 -0700117 return true;
118 }
119
Ray Milkeycaa450b2014-10-29 15:54:24 -0700120
Brian O'Connorb876bf12014-10-02 14:59:37 -0700121 @Override
Brian O'Connorb876bf12014-10-02 14:59:37 -0700122 public String toString() {
123 return MoreObjects.toStringHelper(getClass())
tom85258ee2014-10-07 00:10:02 -0700124 .add("id", id())
Thomas Vachuskae291c842014-10-21 02:52:38 -0700125 .add("appId", appId())
126 .add("selector", selector())
127 .add("treatment", treatment())
Ray Milkey460f4022014-11-05 15:41:43 -0800128 .add("constraints", constraints())
Brian O'Connorb876bf12014-10-02 14:59:37 -0700129 .add("path", path)
130 .toString();
131 }
tom95329eb2014-10-06 08:40:06 -0700132
Brian O'Connorb876bf12014-10-02 14:59:37 -0700133}