blob: cb1e6b2bf2a7910c141cb518245e24b906e9d467 [file] [log] [blame]
Sho SHIMIZU075268b2014-11-05 15:16:32 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Sho SHIMIZU075268b2014-11-05 15:16:32 -08003 *
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
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080019import com.google.common.base.MoreObjects;
20import com.google.common.collect.ImmutableSet;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.DeviceId;
22import org.onosproject.net.Link;
Brian O'Connor6de2e202015-05-21 14:30:41 -070023import org.onosproject.net.resource.link.LinkResourceService;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080024
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080025import java.util.Collections;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080026import java.util.Objects;
27import java.util.Set;
28
29/**
30 * Constraint that evaluates elements not passed through.
31 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040032@Beta
Sho SHIMIZU075268b2014-11-05 15:16:32 -080033public class ObstacleConstraint extends BooleanConstraint {
34
35 private final Set<DeviceId> obstacles;
36
37 /**
38 * Creates a new constraint that the specified device are not passed through.
39 * @param obstacles devices not to be passed
40 */
41 public ObstacleConstraint(DeviceId... obstacles) {
42 this.obstacles = ImmutableSet.copyOf(obstacles);
43 }
44
Sho SHIMIZUcd4bac82014-11-11 17:07:25 -080045 // Constructor for serialization
46 private ObstacleConstraint() {
47 this.obstacles = Collections.emptySet();
48 }
49
Ray Milkey3cbfbc42015-01-20 14:11:25 -080050 /**
51 * Returns the obstacle device ids.
52 *
53 * @return Set of obstacle device ids
54 */
55 public Set<DeviceId> obstacles() {
56 return obstacles;
57 }
58
Sho SHIMIZU075268b2014-11-05 15:16:32 -080059 @Override
60 public boolean isValid(Link link, LinkResourceService resourceService) {
61 DeviceId src = link.src().deviceId();
62 DeviceId dst = link.dst().deviceId();
63
64 return !(obstacles.contains(src) || obstacles.contains(dst));
65 }
66
67 @Override
Sho SHIMIZU075268b2014-11-05 15:16:32 -080068 public int hashCode() {
69 return Objects.hash(obstacles);
70 }
71
72 @Override
73 public boolean equals(Object obj) {
74 if (this == obj) {
75 return true;
76 }
77
78 if (!(obj instanceof ObstacleConstraint)) {
79 return false;
80 }
81
82 final ObstacleConstraint that = (ObstacleConstraint) obj;
83 return Objects.equals(this.obstacles, that.obstacles);
84 }
85
86 @Override
87 public String toString() {
88 return MoreObjects.toStringHelper(this)
89 .add("obstacles", obstacles)
90 .toString();
91 }
92}