Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.Link; |
| 22 | import org.onosproject.net.Path; |
| 23 | import org.onosproject.net.intent.Constraint; |
| 24 | import org.onosproject.net.resource.LinkResourceService; |
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 | */ |
| 37 | public class WaypointConstraint implements Constraint { |
| 38 | |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 39 | private final List<DeviceId> waypoints; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Creates a new waypoint constraint. |
| 43 | * |
| 44 | * @param waypoints waypoints |
| 45 | */ |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 46 | public WaypointConstraint(DeviceId... waypoints) { |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 47 | checkNotNull(waypoints, "waypoints cannot be null"); |
| 48 | checkArgument(waypoints.length > 0, "length of waypoints should be more than 0"); |
| 49 | this.waypoints = ImmutableList.copyOf(waypoints); |
| 50 | } |
| 51 | |
Sho SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 52 | // Constructor for serialization |
| 53 | private WaypointConstraint() { |
| 54 | this.waypoints = Collections.emptyList(); |
| 55 | } |
| 56 | |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 57 | public List<DeviceId> waypoints() { |
Sho SHIMIZU | 14ccab5 | 2014-11-06 11:11:40 -0800 | [diff] [blame] | 58 | return waypoints; |
| 59 | } |
| 60 | |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 61 | @Override |
| 62 | public double cost(Link link, LinkResourceService resourceService) { |
| 63 | // Always consider the number of hops |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean validate(Path path, LinkResourceService resourceService) { |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 69 | LinkedList<DeviceId> waypoints = new LinkedList<>(this.waypoints); |
| 70 | DeviceId current = waypoints.poll(); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 71 | // This is safe because Path class ensures the number of links are more than 0 |
| 72 | Link firstLink = path.links().get(0); |
| 73 | if (firstLink.src().elementId().equals(current)) { |
| 74 | current = waypoints.poll(); |
| 75 | } |
| 76 | |
| 77 | for (Link link : path.links()) { |
| 78 | if (link.dst().elementId().equals(current)) { |
| 79 | current = waypoints.poll(); |
| 80 | // Empty waypoints means passing through all waypoints in the specified order |
| 81 | if (current == null) { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int hashCode() { |
| 92 | return Objects.hash(waypoints); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean equals(Object obj) { |
| 97 | if (this == obj) { |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | if (!(obj instanceof WaypointConstraint)) { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | final WaypointConstraint that = (WaypointConstraint) obj; |
| 106 | return Objects.equals(this.waypoints, that.waypoints); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public String toString() { |
| 111 | return MoreObjects.toStringHelper(this) |
| 112 | .add("waypoints", waypoints) |
| 113 | .toString(); |
| 114 | } |
| 115 | } |