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