Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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; |
| 24 | import org.onosproject.net.intent.Constraint; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.ResourceContext; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 26 | |
Sho SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 27 | import java.util.Collections; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 28 | import java.util.LinkedList; |
| 29 | import java.util.List; |
| 30 | import java.util.Objects; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkArgument; |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
| 36 | * Constraint that evaluates elements passed through in order. |
| 37 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 38 | @Beta |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 39 | public class WaypointConstraint implements Constraint { |
| 40 | |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 41 | private final List<DeviceId> waypoints; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Creates a new waypoint constraint. |
| 45 | * |
| 46 | * @param waypoints waypoints |
| 47 | */ |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 48 | public WaypointConstraint(DeviceId... waypoints) { |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 49 | checkNotNull(waypoints, "waypoints cannot be null"); |
| 50 | checkArgument(waypoints.length > 0, "length of waypoints should be more than 0"); |
| 51 | this.waypoints = ImmutableList.copyOf(waypoints); |
| 52 | } |
| 53 | |
Sho SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 54 | // Constructor for serialization |
| 55 | private WaypointConstraint() { |
| 56 | this.waypoints = Collections.emptyList(); |
| 57 | } |
| 58 | |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 59 | public List<DeviceId> waypoints() { |
Sho SHIMIZU | 14ccab5 | 2014-11-06 11:11:40 -0800 | [diff] [blame] | 60 | return waypoints; |
| 61 | } |
| 62 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 63 | // doesn't use LinkResourceService |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 64 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 65 | public double cost(Link link, ResourceContext context) { |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 66 | // Always consider the number of hops |
| 67 | return 1; |
| 68 | } |
| 69 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 70 | // doesn't use LinkResourceService |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 71 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 72 | public boolean validate(Path path, ResourceContext context) { |
| 73 | // explicitly call a method not depending on LinkResourceService |
| 74 | return validate(path); |
| 75 | } |
| 76 | |
| 77 | private boolean validate(Path path) { |
Sho SHIMIZU | fa2cd3b | 2014-11-11 14:10:32 -0800 | [diff] [blame] | 78 | LinkedList<DeviceId> waypoints = new LinkedList<>(this.waypoints); |
| 79 | DeviceId current = waypoints.poll(); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 80 | // This is safe because Path class ensures the number of links are more than 0 |
| 81 | Link firstLink = path.links().get(0); |
| 82 | if (firstLink.src().elementId().equals(current)) { |
| 83 | current = waypoints.poll(); |
| 84 | } |
| 85 | |
| 86 | for (Link link : path.links()) { |
| 87 | if (link.dst().elementId().equals(current)) { |
| 88 | current = waypoints.poll(); |
| 89 | // Empty waypoints means passing through all waypoints in the specified order |
| 90 | if (current == null) { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 101 | return waypoints.hashCode(); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean equals(Object obj) { |
| 106 | if (this == obj) { |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | if (!(obj instanceof WaypointConstraint)) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | final WaypointConstraint that = (WaypointConstraint) obj; |
| 115 | return Objects.equals(this.waypoints, that.waypoints); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public String toString() { |
| 120 | return MoreObjects.toStringHelper(this) |
| 121 | .add("waypoints", waypoints) |
| 122 | .toString(); |
| 123 | } |
| 124 | } |