blob: fffb66c3efd879a1178ddd19062385a91bfa6b1e [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;
20import org.onlab.onos.net.ElementId;
21import 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
38 private final List<ElementId> waypoints;
39
40 /**
41 * Creates a new waypoint constraint.
42 *
43 * @param waypoints waypoints
44 */
45 public WaypointConstraint(ElementId... waypoints) {
46 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
51 @Override
52 public double cost(Link link, LinkResourceService resourceService) {
53 // Always consider the number of hops
54 return 1;
55 }
56
57 @Override
58 public boolean validate(Path path, LinkResourceService resourceService) {
59 LinkedList<ElementId> waypoints = new LinkedList<>(this.waypoints);
60 ElementId current = waypoints.poll();
61 // This is safe because Path class ensures the number of links are more than 0
62 Link firstLink = path.links().get(0);
63 if (firstLink.src().elementId().equals(current)) {
64 current = waypoints.poll();
65 }
66
67 for (Link link : path.links()) {
68 if (link.dst().elementId().equals(current)) {
69 current = waypoints.poll();
70 // Empty waypoints means passing through all waypoints in the specified order
71 if (current == null) {
72 return true;
73 }
74 }
75 }
76
77 return false;
78 }
79
80 @Override
81 public int hashCode() {
82 return Objects.hash(waypoints);
83 }
84
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj) {
88 return true;
89 }
90
91 if (!(obj instanceof WaypointConstraint)) {
92 return false;
93 }
94
95 final WaypointConstraint that = (WaypointConstraint) obj;
96 return Objects.equals(this.waypoints, that.waypoints);
97 }
98
99 @Override
100 public String toString() {
101 return MoreObjects.toStringHelper(this)
102 .add("waypoints", waypoints)
103 .toString();
104 }
105}