blob: b6aceba986566a1d22db28cd2bfe823e39f864af [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
Sho SHIMIZU075268b2014-11-05 15:16:32 -080048 @Override
49 public boolean isValid(Link link, LinkResourceService resourceService) {
50 DeviceId src = link.src().deviceId();
51 DeviceId dst = link.dst().deviceId();
52
53 return !(obstacles.contains(src) || obstacles.contains(dst));
54 }
55
56 @Override
Sho SHIMIZU075268b2014-11-05 15:16:32 -080057 public int hashCode() {
58 return Objects.hash(obstacles);
59 }
60
61 @Override
62 public boolean equals(Object obj) {
63 if (this == obj) {
64 return true;
65 }
66
67 if (!(obj instanceof ObstacleConstraint)) {
68 return false;
69 }
70
71 final ObstacleConstraint that = (ObstacleConstraint) obj;
72 return Objects.equals(this.obstacles, that.obstacles);
73 }
74
75 @Override
76 public String toString() {
77 return MoreObjects.toStringHelper(this)
78 .add("obstacles", obstacles)
79 .toString();
80 }
81}