blob: 56aefc356d57a85cd73b88e31f43733646b8b9d9 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080017
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableSet;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.DeviceId;
21import org.onosproject.net.Link;
22import org.onosproject.net.resource.LinkResourceService;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080023
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080024import java.util.Collections;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080025import 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
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080043 // Constructor for serialization
44 private ObstacleConstraint() {
45 this.obstacles = Collections.emptySet();
46 }
47
Ray Milkey3cbfbc42015-01-20 14:11:25 -080048 /**
49 * Returns the obstacle device ids.
50 *
51 * @return Set of obstacle device ids
52 */
53 public Set<DeviceId> obstacles() {
54 return obstacles;
55 }
56
Sho SHIMIZU075268b2014-11-05 15:16:32 -080057 @Override
58 public boolean isValid(Link link, LinkResourceService resourceService) {
59 DeviceId src = link.src().deviceId();
60 DeviceId dst = link.dst().deviceId();
61
62 return !(obstacles.contains(src) || obstacles.contains(dst));
63 }
64
65 @Override
Sho SHIMIZU075268b2014-11-05 15:16:32 -080066 public int hashCode() {
67 return Objects.hash(obstacles);
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (this == obj) {
73 return true;
74 }
75
76 if (!(obj instanceof ObstacleConstraint)) {
77 return false;
78 }
79
80 final ObstacleConstraint that = (ObstacleConstraint) obj;
81 return Objects.equals(this.obstacles, that.obstacles);
82 }
83
84 @Override
85 public String toString() {
86 return MoreObjects.toStringHelper(this)
87 .add("obstacles", obstacles)
88 .toString();
89 }
90}