Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.constraint; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.Link; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 23 | import org.onosproject.net.intent.ResourceContext; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 24 | |
Sho SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 25 | import java.util.Collections; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 26 | import java.util.Objects; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | /** |
| 30 | * Constraint that evaluates elements not passed through. |
| 31 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 32 | @Beta |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 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 | |
Sho SHIMIZU | cd4bac8 | 2014-11-11 17:07:25 -0800 | [diff] [blame] | 45 | // Constructor for serialization |
| 46 | private ObstacleConstraint() { |
| 47 | this.obstacles = Collections.emptySet(); |
| 48 | } |
| 49 | |
Ray Milkey | 3cbfbc4 | 2015-01-20 14:11:25 -0800 | [diff] [blame] | 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 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 59 | // doesn't use LinkResourceService |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 60 | @Override |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 61 | 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) { |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 67 | if (link.type() != Link.Type.EDGE) { |
| 68 | DeviceId src = link.src().deviceId(); |
| 69 | DeviceId dst = link.dst().deviceId(); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 70 | |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 71 | return !(obstacles.contains(src) || obstacles.contains(dst)); |
| 72 | |
| 73 | } else { |
| 74 | |
| 75 | boolean isSrc = true; |
| 76 | if (link.src().elementId() instanceof DeviceId) { |
| 77 | |
| 78 | DeviceId src = link.src().deviceId(); |
| 79 | isSrc = !(obstacles.contains(src)); |
| 80 | } |
| 81 | |
| 82 | boolean isDst = true; |
| 83 | if (link.dst().elementId() instanceof DeviceId) { |
| 84 | DeviceId dst = link.dst().deviceId(); |
| 85 | isDst = !(obstacles.contains(dst)); |
| 86 | } |
| 87 | |
| 88 | return isSrc || isDst; |
| 89 | } |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 93 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 94 | return obstacles.hashCode(); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean equals(Object obj) { |
| 99 | if (this == obj) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | if (!(obj instanceof ObstacleConstraint)) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | final ObstacleConstraint that = (ObstacleConstraint) obj; |
| 108 | return Objects.equals(this.obstacles, that.obstacles); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String toString() { |
| 113 | return MoreObjects.toStringHelper(this) |
| 114 | .add("obstacles", obstacles) |
| 115 | .toString(); |
| 116 | } |
| 117 | } |