blob: 4839feecc5ac2418dfcce8381145b7f3b27ec72d [file] [log] [blame]
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -08001/*
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'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;
24import org.onosproject.net.intent.Constraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070025import org.onosproject.net.resource.link.LinkResourceService;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080026
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080027import java.util.Collections;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080028import java.util.LinkedList;
29import java.util.List;
30import java.util.Objects;
31
32import static com.google.common.base.Preconditions.checkArgument;
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Constraint that evaluates elements passed through in order.
37 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040038@Beta
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080039public class WaypointConstraint implements Constraint {
40
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080041 private final List<DeviceId> waypoints;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080042
43 /**
44 * Creates a new waypoint constraint.
45 *
46 * @param waypoints waypoints
47 */
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080048 public WaypointConstraint(DeviceId... waypoints) {
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080049 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 SHIMIZUcd4bac82014-11-11 17:07:25 -080054 // Constructor for serialization
55 private WaypointConstraint() {
56 this.waypoints = Collections.emptyList();
57 }
58
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080059 public List<DeviceId> waypoints() {
Sho SHIMIZU14ccab52014-11-06 11:11:40 -080060 return waypoints;
61 }
62
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080063 @Override
64 public double cost(Link link, LinkResourceService resourceService) {
65 // Always consider the number of hops
66 return 1;
67 }
68
69 @Override
70 public boolean validate(Path path, LinkResourceService resourceService) {
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080071 LinkedList<DeviceId> waypoints = new LinkedList<>(this.waypoints);
72 DeviceId current = waypoints.poll();
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080073 // This is safe because Path class ensures the number of links are more than 0
74 Link firstLink = path.links().get(0);
75 if (firstLink.src().elementId().equals(current)) {
76 current = waypoints.poll();
77 }
78
79 for (Link link : path.links()) {
80 if (link.dst().elementId().equals(current)) {
81 current = waypoints.poll();
82 // Empty waypoints means passing through all waypoints in the specified order
83 if (current == null) {
84 return true;
85 }
86 }
87 }
88
89 return false;
90 }
91
92 @Override
93 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070094 return waypoints.hashCode();
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080095 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (this == obj) {
100 return true;
101 }
102
103 if (!(obj instanceof WaypointConstraint)) {
104 return false;
105 }
106
107 final WaypointConstraint that = (WaypointConstraint) obj;
108 return Objects.equals(this.waypoints, that.waypoints);
109 }
110
111 @Override
112 public String toString() {
113 return MoreObjects.toStringHelper(this)
114 .add("waypoints", waypoints)
115 .toString();
116 }
117}