blob: e08c9421018f0bcd0d9749fd5c249e4710dff2df [file] [log] [blame]
Yuta HIGUCHI41289382014-12-19 17:47:12 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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
17package org.onosproject.provider.lldp.impl;
18
19import java.util.Map;
20import java.util.Map.Entry;
21import java.util.Set;
22
23import org.onosproject.net.Annotations;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Element;
27import org.onosproject.net.Port;
28
29import com.google.common.collect.ImmutableMap;
30import com.google.common.collect.ImmutableSet;
31
32public class SuppressionRules {
33
34 public static final String ANY_VALUE = "(any)";
35
36 private final Set<DeviceId> suppressedDevice;
37 private final Set<Device.Type> suppressedDeviceType;
38 private final Map<String, String> suppressedAnnotation;
39
40 public SuppressionRules(Set<DeviceId> suppressedDevice,
41 Set<Device.Type> suppressedType,
42 Map<String, String> suppressedAnnotation) {
43
44 this.suppressedDevice = ImmutableSet.copyOf(suppressedDevice);
45 this.suppressedDeviceType = ImmutableSet.copyOf(suppressedType);
46 this.suppressedAnnotation = ImmutableMap.copyOf(suppressedAnnotation);
47 }
48
49 public boolean isSuppressed(Device device) {
50 if (suppressedDevice.contains(device.id())) {
51 return true;
52 }
53 if (suppressedDeviceType.contains(device.type())) {
54 return true;
55 }
56 final Annotations annotations = device.annotations();
57 if (containsSuppressionAnnotation(annotations)) {
58 return true;
59 }
60 return false;
61 }
62
63 public boolean isSuppressed(Port port) {
64 Element parent = port.element();
65 if (parent instanceof Device) {
66 if (isSuppressed((Device) parent)) {
67 return true;
68 }
69 }
70
71 final Annotations annotations = port.annotations();
72 if (containsSuppressionAnnotation(annotations)) {
73 return true;
74 }
75 return false;
76 }
77
78 private boolean containsSuppressionAnnotation(final Annotations annotations) {
79 for (Entry<String, String> entry : suppressedAnnotation.entrySet()) {
80 final String suppValue = entry.getValue();
81 final String suppKey = entry.getKey();
82 if (suppValue == ANY_VALUE) {
83 if (annotations.keys().contains(suppKey)) {
84 return true;
85 }
86 } else {
87 if (suppValue.equals(annotations.value(suppKey))) {
88 return true;
89 }
90 }
91 }
92 return false;
93 }
94
95 Set<DeviceId> getSuppressedDevice() {
96 return suppressedDevice;
97 }
98
99 Set<Device.Type> getSuppressedDeviceType() {
100 return suppressedDeviceType;
101 }
102
103 Map<String, String> getSuppressedAnnotation() {
104 return suppressedAnnotation;
105 }
106}