blob: 318db5d31c869334029ec5014a4055f245160012 [file] [log] [blame]
Yuta HIGUCHI41289382014-12-19 17:47:12 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080026import org.onosproject.net.Element;
27import org.onosproject.net.Port;
28
29import com.google.common.collect.ImmutableMap;
30import com.google.common.collect.ImmutableSet;
Naoki Shiota399a0b32015-11-15 20:36:13 -060031import com.google.common.base.MoreObjects;
Yuta HIGUCHI41289382014-12-19 17:47:12 -080032
33public class SuppressionRules {
34
35 public static final String ANY_VALUE = "(any)";
36
Yuta HIGUCHI41289382014-12-19 17:47:12 -080037 private final Set<Device.Type> suppressedDeviceType;
38 private final Map<String, String> suppressedAnnotation;
39
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080040 public SuppressionRules(Set<Device.Type> suppressedType,
41 Map<String, String> suppressedAnnotation) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -080042
Yuta HIGUCHI41289382014-12-19 17:47:12 -080043 this.suppressedDeviceType = ImmutableSet.copyOf(suppressedType);
44 this.suppressedAnnotation = ImmutableMap.copyOf(suppressedAnnotation);
45 }
46
47 public boolean isSuppressed(Device device) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -080048 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 HIGUCHI41289382014-12-19 17:47:12 -080090 Set<Device.Type> getSuppressedDeviceType() {
91 return suppressedDeviceType;
92 }
93
94 Map<String, String> getSuppressedAnnotation() {
95 return suppressedAnnotation;
96 }
Naoki Shiota399a0b32015-11-15 20:36:13 -060097
98 @Override
99 public int hashCode() {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800100 return Objects.hash(suppressedDeviceType,
Naoki Shiota399a0b32015-11-15 20:36:13 -0600101 suppressedAnnotation);
102 }
103
104 @Override
105 public boolean equals(Object object) {
106 if (object != null && getClass() == object.getClass()) {
107 SuppressionRules that = (SuppressionRules) object;
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800108 return Objects.equals(this.suppressedDeviceType,
Naoki Shiota399a0b32015-11-15 20:36:13 -0600109 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 Shiota399a0b32015-11-15 20:36:13 -0600119 .add("suppressedDeviceType", suppressedDeviceType)
120 .add("suppressedAnnotation", suppressedAnnotation)
121 .toString();
122 }
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800123}