blob: 3a132f0893453d01eff8226e7d22dbd8a71881af [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 */
16package org.onlab.onos.net.intent.constraint;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableList;
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080020import org.onlab.onos.net.DeviceId;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080021import org.onlab.onos.net.Link;
22import org.onlab.onos.net.Path;
23import org.onlab.onos.net.intent.Constraint;
24import org.onlab.onos.net.resource.LinkResourceService;
25
26import java.util.LinkedList;
27import java.util.List;
28import java.util.Objects;
29
30import static com.google.common.base.Preconditions.checkArgument;
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * Constraint that evaluates elements passed through in order.
35 */
36public class WaypointConstraint implements Constraint {
37
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080038 private final List<DeviceId> waypoints;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080039
40 /**
41 * Creates a new waypoint constraint.
42 *
43 * @param waypoints waypoints
44 */
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080045 public WaypointConstraint(DeviceId... waypoints) {
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080046 checkNotNull(waypoints, "waypoints cannot be null");
47 checkArgument(waypoints.length > 0, "length of waypoints should be more than 0");
48 this.waypoints = ImmutableList.copyOf(waypoints);
49 }
50
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080051 public List<DeviceId> waypoints() {
Sho SHIMIZU14ccab52014-11-06 11:11:40 -080052 return waypoints;
53 }
54
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080055 @Override
56 public double cost(Link link, LinkResourceService resourceService) {
57 // Always consider the number of hops
58 return 1;
59 }
60
61 @Override
62 public boolean validate(Path path, LinkResourceService resourceService) {
Sho SHIMIZUfa2cd3b2014-11-11 14:10:32 -080063 LinkedList<DeviceId> waypoints = new LinkedList<>(this.waypoints);
64 DeviceId current = waypoints.poll();
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080065 // This is safe because Path class ensures the number of links are more than 0
66 Link firstLink = path.links().get(0);
67 if (firstLink.src().elementId().equals(current)) {
68 current = waypoints.poll();
69 }
70
71 for (Link link : path.links()) {
72 if (link.dst().elementId().equals(current)) {
73 current = waypoints.poll();
74 // Empty waypoints means passing through all waypoints in the specified order
75 if (current == null) {
76 return true;
77 }
78 }
79 }
80
81 return false;
82 }
83
84 @Override
85 public int hashCode() {
86 return Objects.hash(waypoints);
87 }
88
89 @Override
90 public boolean equals(Object obj) {
91 if (this == obj) {
92 return true;
93 }
94
95 if (!(obj instanceof WaypointConstraint)) {
96 return false;
97 }
98
99 final WaypointConstraint that = (WaypointConstraint) obj;
100 return Objects.equals(this.waypoints, that.waypoints);
101 }
102
103 @Override
104 public String toString() {
105 return MoreObjects.toStringHelper(this)
106 .add("waypoints", waypoints)
107 .toString();
108 }
109}