Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -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 | */ |
| 16 | |
| 17 | package org.onosproject.provider.lldp.impl; |
| 18 | |
| 19 | import java.util.Map; |
| 20 | import java.util.Map.Entry; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 21 | import java.util.Objects; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 22 | import java.util.Set; |
| 23 | |
| 24 | import org.onosproject.net.Annotations; |
| 25 | import org.onosproject.net.Device; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 26 | import org.onosproject.net.Element; |
| 27 | import org.onosproject.net.Port; |
| 28 | |
| 29 | import com.google.common.collect.ImmutableMap; |
| 30 | import com.google.common.collect.ImmutableSet; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 31 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 32 | |
| 33 | public class SuppressionRules { |
| 34 | |
| 35 | public static final String ANY_VALUE = "(any)"; |
| 36 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 37 | private final Set<Device.Type> suppressedDeviceType; |
| 38 | private final Map<String, String> suppressedAnnotation; |
| 39 | |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 40 | public SuppressionRules(Set<Device.Type> suppressedType, |
| 41 | Map<String, String> suppressedAnnotation) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 42 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 43 | this.suppressedDeviceType = ImmutableSet.copyOf(suppressedType); |
| 44 | this.suppressedAnnotation = ImmutableMap.copyOf(suppressedAnnotation); |
| 45 | } |
| 46 | |
| 47 | public boolean isSuppressed(Device device) { |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 48 | if (suppressedDeviceType.contains(device.type())) { |
| 49 | return true; |
| 50 | } |
| 51 | final Annotations annotations = device.annotations(); |
| 52 | if (containsSuppressionAnnotation(annotations)) { |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | public boolean isSuppressed(Port port) { |
| 59 | Element parent = port.element(); |
| 60 | if (parent instanceof Device) { |
| 61 | if (isSuppressed((Device) parent)) { |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | final Annotations annotations = port.annotations(); |
| 67 | if (containsSuppressionAnnotation(annotations)) { |
| 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | private boolean containsSuppressionAnnotation(final Annotations annotations) { |
| 74 | for (Entry<String, String> entry : suppressedAnnotation.entrySet()) { |
| 75 | final String suppValue = entry.getValue(); |
| 76 | final String suppKey = entry.getKey(); |
| 77 | if (suppValue == ANY_VALUE) { |
| 78 | if (annotations.keys().contains(suppKey)) { |
| 79 | return true; |
| 80 | } |
| 81 | } else { |
| 82 | if (suppValue.equals(annotations.value(suppKey))) { |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 90 | Set<Device.Type> getSuppressedDeviceType() { |
| 91 | return suppressedDeviceType; |
| 92 | } |
| 93 | |
| 94 | Map<String, String> getSuppressedAnnotation() { |
| 95 | return suppressedAnnotation; |
| 96 | } |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 97 | |
| 98 | @Override |
| 99 | public int hashCode() { |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 100 | return Objects.hash(suppressedDeviceType, |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 101 | suppressedAnnotation); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean equals(Object object) { |
| 106 | if (object != null && getClass() == object.getClass()) { |
| 107 | SuppressionRules that = (SuppressionRules) object; |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 108 | return Objects.equals(this.suppressedDeviceType, |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 109 | that.suppressedDeviceType) |
| 110 | && Objects.equals(this.suppressedAnnotation, |
| 111 | that.suppressedAnnotation); |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public String toString() { |
| 118 | return MoreObjects.toStringHelper(this) |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 119 | .add("suppressedDeviceType", suppressedDeviceType) |
| 120 | .add("suppressedAnnotation", suppressedAnnotation) |
| 121 | .toString(); |
| 122 | } |
Yuta HIGUCHI | 4128938 | 2014-12-19 17:47:12 -0800 | [diff] [blame] | 123 | } |