blob: 3ed3fae1e3ec3754b1743a9836b0ec55cc939003 [file] [log] [blame]
Sho SHIMIZU075268b2014-11-05 15:16:32 -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.ImmutableSet;
20import org.onlab.onos.net.DeviceId;
21import org.onlab.onos.net.Link;
22import org.onlab.onos.net.Path;
23import org.onlab.onos.net.resource.LinkResourceService;
24
25import java.util.Objects;
26import java.util.Set;
27
28/**
29 * Constraint that evaluates elements not passed through.
30 */
31public class ObstacleConstraint extends BooleanConstraint {
32
33 private final Set<DeviceId> obstacles;
34
35 /**
36 * Creates a new constraint that the specified device are not passed through.
37 * @param obstacles devices not to be passed
38 */
39 public ObstacleConstraint(DeviceId... obstacles) {
40 this.obstacles = ImmutableSet.copyOf(obstacles);
41 }
42
43 @Override
44 public boolean isValid(Link link, LinkResourceService resourceService) {
45 DeviceId src = link.src().deviceId();
46 DeviceId dst = link.dst().deviceId();
47
48 return !(obstacles.contains(src) || obstacles.contains(dst));
49 }
50
51 @Override
52 public boolean validate(Path path, LinkResourceService resourceService) {
53 for (Link link : path.links()) {
54 if (!isValid(link, resourceService)) {
55 return false;
56 }
57 }
58
59 return true;
60 }
61
62 @Override
63 public int hashCode() {
64 return Objects.hash(obstacles);
65 }
66
67 @Override
68 public boolean equals(Object obj) {
69 if (this == obj) {
70 return true;
71 }
72
73 if (!(obj instanceof ObstacleConstraint)) {
74 return false;
75 }
76
77 final ObstacleConstraint that = (ObstacleConstraint) obj;
78 return Objects.equals(this.obstacles, that.obstacles);
79 }
80
81 @Override
82 public String toString() {
83 return MoreObjects.toStringHelper(this)
84 .add("obstacles", obstacles)
85 .toString();
86 }
87}