Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 3 | * |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.constraint; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.Link; |
| 23 | import org.onosproject.net.Path; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 24 | import org.onosproject.net.intent.ResourceContext; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 25 | |
Sho SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 26 | import java.util.Collections; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 27 | import java.util.LinkedList; |
| 28 | import java.util.List; |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkArgument; |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | |
| 34 | /** |
| 35 | * Constraint that evaluates elements passed through in order. |
| 36 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 37 | @Beta |
Yuta HIGUCHI | e37560f | 2017-02-02 19:53:26 -0800 | [diff] [blame] | 38 | public final class WaypointConstraint extends PathViabilityConstraint { |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 39 | |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 40 | private final List<DeviceId> waypoints; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Creates a new waypoint constraint. |
| 44 | * |
| 45 | * @param waypoints waypoints |
| 46 | */ |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 47 | public WaypointConstraint(DeviceId... waypoints) { |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 48 | 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 SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 53 | // Constructor for serialization |
| 54 | private WaypointConstraint() { |
| 55 | this.waypoints = Collections.emptyList(); |
| 56 | } |
| 57 | |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 58 | public List<DeviceId> waypoints() { |
Sho SHIMIZU | 14ccab5 | 2014-11-06 11:11:40 -0800 | [diff] [blame] | 59 | return waypoints; |
| 60 | } |
| 61 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 62 | // doesn't use LinkResourceService |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 63 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 64 | 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 SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 70 | LinkedList<DeviceId> waypoints = new LinkedList<>(this.waypoints); |
| 71 | DeviceId current = waypoints.poll(); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 72 | // 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 Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 93 | return waypoints.hashCode(); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 94 | } |
| 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 | } |