27c75ebd120a17f6b011c7c813c1b5caced353b3
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 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
17 package org.onosproject.provider.lldp.impl;
18
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
22
23 import org.onosproject.net.Annotations;
24 import org.onosproject.net.Device;
25 import org.onosproject.net.DeviceId;
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;
31
32 public 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 }