blob: 7908b5887426b2f32cd1fcc3320335968c011858 [file] [log] [blame]
Sho SHIMIZU075268b2014-11-05 15:16:32 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080023import org.onosproject.net.intent.ResourceContext;
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 SHIMIZUb1681bd2016-02-22 12:47:50 -080059 // doesn't use LinkResourceService
Sho SHIMIZU075268b2014-11-05 15:16:32 -080060 @Override
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080061 public boolean isValid(Link link, ResourceContext context) {
62 // explicitly call a method not depending on LinkResourceService
63 return isValid(link);
64 }
65
66 private boolean isValid(Link link) {
Sho SHIMIZU075268b2014-11-05 15:16:32 -080067 DeviceId src = link.src().deviceId();
68 DeviceId dst = link.dst().deviceId();
69
70 return !(obstacles.contains(src) || obstacles.contains(dst));
71 }
72
73 @Override
Sho SHIMIZU075268b2014-11-05 15:16:32 -080074 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -070075 return obstacles.hashCode();
Sho SHIMIZU075268b2014-11-05 15:16:32 -080076 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj) {
81 return true;
82 }
83
84 if (!(obj instanceof ObstacleConstraint)) {
85 return false;
86 }
87
88 final ObstacleConstraint that = (ObstacleConstraint) obj;
89 return Objects.equals(this.obstacles, that.obstacles);
90 }
91
92 @Override
93 public String toString() {
94 return MoreObjects.toStringHelper(this)
95 .add("obstacles", obstacles)
96 .toString();
97 }
98}