blob: a0e78eff98d0a50199181adac83d0a315ed9b48e [file] [log] [blame]
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -08003 *
4 * 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
7 *
8 * 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.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080019import com.google.common.base.MoreObjects;
20import com.google.common.collect.ImmutableList;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.DeviceId;
22import org.onosproject.net.Link;
23import org.onosproject.net.Path;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080024import org.onosproject.net.intent.ResourceContext;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080025
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080026import java.util.Collections;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080027import java.util.LinkedList;
28import java.util.List;
29import java.util.Objects;
30
31import static com.google.common.base.Preconditions.checkArgument;
32import static com.google.common.base.Preconditions.checkNotNull;
33
34/**
35 * Constraint that evaluates elements passed through in order.
36 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040037@Beta
Yuta HIGUCHIe37560f2017-02-02 19:53:26 -080038public final class WaypointConstraint extends PathViabilityConstraint {
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080039
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080040 private final List<DeviceId> waypoints;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080041
42 /**
43 * Creates a new waypoint constraint.
44 *
45 * @param waypoints waypoints
46 */
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080047 public WaypointConstraint(DeviceId... waypoints) {
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080048 checkNotNull(waypoints, "waypoints cannot be null");
49 checkArgument(waypoints.length > 0, "length of waypoints should be more than 0");
50 this.waypoints = ImmutableList.copyOf(waypoints);
51 }
52
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080053 // Constructor for serialization
54 private WaypointConstraint() {
55 this.waypoints = Collections.emptyList();
56 }
57
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080058 public List<DeviceId> waypoints() {
Sho SHIMIZU14ccab52014-11-06 11:11:40 -080059 return waypoints;
60 }
61
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080062 // doesn't use LinkResourceService
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080063 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080064 public boolean validate(Path path, ResourceContext context) {
65 // explicitly call a method not depending on LinkResourceService
66 return validate(path);
67 }
68
69 private boolean validate(Path path) {
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080070 LinkedList<DeviceId> waypoints = new LinkedList<>(this.waypoints);
71 DeviceId current = waypoints.poll();
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080072 // This is safe because Path class ensures the number of links are more than 0
73 Link firstLink = path.links().get(0);
74 if (firstLink.src().elementId().equals(current)) {
75 current = waypoints.poll();
76 }
77
78 for (Link link : path.links()) {
79 if (link.dst().elementId().equals(current)) {
80 current = waypoints.poll();
81 // Empty waypoints means passing through all waypoints in the specified order
82 if (current == null) {
83 return true;
84 }
85 }
86 }
87
88 return false;
89 }
90
91 @Override
92 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070093 return waypoints.hashCode();
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080094 }
95
96 @Override
97 public boolean equals(Object obj) {
98 if (this == obj) {
99 return true;
100 }
101
102 if (!(obj instanceof WaypointConstraint)) {
103 return false;
104 }
105
106 final WaypointConstraint that = (WaypointConstraint) obj;
107 return Objects.equals(this.waypoints, that.waypoints);
108 }
109
110 @Override
111 public String toString() {
112 return MoreObjects.toStringHelper(this)
113 .add("waypoints", waypoints)
114 .toString();
115 }
116}