Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [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 | package org.onosproject.provider.lldp.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableMap; |
| 24 | import com.google.common.collect.ImmutableSet; |
| 25 | import com.google.common.collect.Maps; |
| 26 | |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.net.Device; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 29 | import org.onosproject.net.config.Config; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.util.Iterator; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | import java.util.Set; |
| 37 | import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES; |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | |
| 40 | /** |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 41 | * LinkDiscovery suppression config class. |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 42 | */ |
| 43 | public class SuppressionConfig extends Config<ApplicationId> { |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 44 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 45 | private static final String DEVICE_TYPES = "deviceTypes"; |
| 46 | private static final String ANNOTATION = "annotation"; |
| 47 | |
| 48 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 49 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 50 | private static final List<Device.Type> DEFAULT_DEVICE_TYPES |
| 51 | = ImmutableList.copyOf(DEFAULT_RULES.getSuppressedDeviceType()); |
| 52 | |
| 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | /** |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 56 | * Returns types of devices on which LinkDiscovery is suppressed. |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 57 | * |
| 58 | * @return set of device types |
| 59 | */ |
| 60 | public Set<Device.Type> deviceTypes() { |
| 61 | return ImmutableSet.copyOf(getList(DEVICE_TYPES, Device.Type::valueOf, DEFAULT_DEVICE_TYPES)); |
| 62 | } |
| 63 | |
| 64 | /** |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 65 | * Sets types of devices on which LinkDiscovery is suppressed. |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 66 | * |
| 67 | * @param deviceTypes new set of device types; null to clear |
| 68 | * @return self |
| 69 | */ |
| 70 | public SuppressionConfig deviceTypes(Set<Device.Type> deviceTypes) { |
| 71 | return (SuppressionConfig) setOrClear(DEVICE_TYPES, deviceTypes); |
| 72 | } |
| 73 | |
| 74 | /** |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 75 | * Returns annotation of Ports on which LinkDiscovery is suppressed. |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 76 | * |
| 77 | * @return key-value pairs of annotation |
| 78 | */ |
| 79 | public Map<String, String> annotation() { |
| 80 | ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); |
| 81 | |
| 82 | String jsonAnnotation = get(ANNOTATION, null); |
| 83 | if (jsonAnnotation == null || jsonAnnotation.isEmpty()) { |
| 84 | return ImmutableMap.of(); |
| 85 | } |
| 86 | |
| 87 | JsonNode annotationNode; |
| 88 | try { |
| 89 | annotationNode = MAPPER.readTree(jsonAnnotation); |
| 90 | } catch (IOException e) { |
| 91 | log.error("Failed to read JSON tree from: {}", jsonAnnotation); |
| 92 | return ImmutableMap.of(); |
| 93 | } |
| 94 | |
| 95 | if (annotationNode.isObject()) { |
| 96 | ObjectNode obj = (ObjectNode) annotationNode; |
| 97 | Iterator<Map.Entry<String, JsonNode>> it = obj.fields(); |
| 98 | while (it.hasNext()) { |
| 99 | Map.Entry<String, JsonNode> entry = it.next(); |
| 100 | final String key = entry.getKey(); |
| 101 | final JsonNode value = entry.getValue(); |
| 102 | |
| 103 | if (value.isValueNode()) { |
| 104 | if (value.isNull()) { |
| 105 | builder.put(key, SuppressionRules.ANY_VALUE); |
| 106 | } else { |
| 107 | builder.put(key, value.asText()); |
| 108 | } |
| 109 | } else { |
| 110 | log.warn("Encountered unexpected JSON field {} for annotation", entry); |
| 111 | } |
| 112 | } |
| 113 | } else { |
| 114 | log.error("Encountered unexpected JSONNode {} for annotation", annotationNode); |
| 115 | return ImmutableMap.of(); |
| 116 | } |
| 117 | |
| 118 | return builder.build(); |
| 119 | } |
| 120 | |
| 121 | /** |
HIGUCHI Yuta | d9fe3a3 | 2015-11-24 18:52:25 -0800 | [diff] [blame] | 122 | * Sets annotation of Ports on which LinkDiscovery is suppressed. |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 123 | * |
| 124 | * @param annotation new key-value pair of annotation; null to clear |
| 125 | * @return self |
| 126 | */ |
| 127 | public SuppressionConfig annotation(Map<String, String> annotation) { |
| 128 | |
| 129 | // ANY_VALUE should be null in JSON |
| 130 | Map<String, String> config = Maps.transformValues(annotation, |
| 131 | v -> (v == SuppressionRules.ANY_VALUE) ? null : v); |
| 132 | |
| 133 | String jsonAnnotation = null; |
| 134 | |
| 135 | try { |
| 136 | // TODO Store annotation as a Map instead of a String (which needs NetworkConfigRegistry modification) |
| 137 | jsonAnnotation = MAPPER.writeValueAsString(config); |
| 138 | } catch (JsonProcessingException e) { |
| 139 | log.error("Failed to write JSON from: {}", annotation); |
| 140 | } |
| 141 | |
| 142 | return (SuppressionConfig) setOrClear(ANNOTATION, jsonAnnotation); |
| 143 | } |
| 144 | } |