cb1e6b2bf2a7910c141cb518245e24b906e9d467
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 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  */
16 package org.onosproject.net.intent.constraint;
17
18 import com.google.common.annotations.Beta;
19 import com.google.common.base.MoreObjects;
20 import com.google.common.collect.ImmutableSet;
21 import org.onosproject.net.DeviceId;
22 import org.onosproject.net.Link;
23 import org.onosproject.net.resource.link.LinkResourceService;
24
25 import java.util.Collections;
26 import java.util.Objects;
27 import java.util.Set;
28
29 /**
30  * Constraint that evaluates elements not passed through.
31  */
32 @Beta
33 public 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
45     // Constructor for serialization
46     private ObstacleConstraint() {
47         this.obstacles = Collections.emptySet();
48     }
49
50     /**
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
59     @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
68     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 }