From e63291850fd0795c5700e25e67e5dee89ba54c5f Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Tue, 1 Dec 2015 05:49:27 -0800 Subject: onos commit hash c2999f30c69e50df905a9d175ef80b3f23a98514 Change-Id: I2bb8562c4942b6d6a6d60b663db2e17540477b81 Signed-off-by: Ashlee Young --- .../provider/lldp/impl/SuppressionConfig.java | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionConfig.java (limited to 'framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionConfig.java') diff --git a/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionConfig.java b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionConfig.java new file mode 100644 index 00000000..5b10f6d2 --- /dev/null +++ b/framework/src/onos/providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionConfig.java @@ -0,0 +1,144 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.provider.lldp.impl; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; + +import org.onosproject.core.ApplicationId; +import org.onosproject.net.Device; +import org.onosproject.net.config.Config; +import org.slf4j.Logger; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES; +import static org.slf4j.LoggerFactory.getLogger; + +/** + * LinkDiscovery suppression config class. + */ +public class SuppressionConfig extends Config { + + private static final String DEVICE_TYPES = "deviceTypes"; + private static final String ANNOTATION = "annotation"; + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static final List DEFAULT_DEVICE_TYPES + = ImmutableList.copyOf(DEFAULT_RULES.getSuppressedDeviceType()); + + private final Logger log = getLogger(getClass()); + + /** + * Returns types of devices on which LinkDiscovery is suppressed. + * + * @return set of device types + */ + public Set deviceTypes() { + return ImmutableSet.copyOf(getList(DEVICE_TYPES, Device.Type::valueOf, DEFAULT_DEVICE_TYPES)); + } + + /** + * Sets types of devices on which LinkDiscovery is suppressed. + * + * @param deviceTypes new set of device types; null to clear + * @return self + */ + public SuppressionConfig deviceTypes(Set deviceTypes) { + return (SuppressionConfig) setOrClear(DEVICE_TYPES, deviceTypes); + } + + /** + * Returns annotation of Ports on which LinkDiscovery is suppressed. + * + * @return key-value pairs of annotation + */ + public Map annotation() { + ImmutableMap.Builder builder = ImmutableMap.builder(); + + String jsonAnnotation = get(ANNOTATION, null); + if (jsonAnnotation == null || jsonAnnotation.isEmpty()) { + return ImmutableMap.of(); + } + + JsonNode annotationNode; + try { + annotationNode = MAPPER.readTree(jsonAnnotation); + } catch (IOException e) { + log.error("Failed to read JSON tree from: {}", jsonAnnotation); + return ImmutableMap.of(); + } + + if (annotationNode.isObject()) { + ObjectNode obj = (ObjectNode) annotationNode; + Iterator> it = obj.fields(); + while (it.hasNext()) { + Map.Entry entry = it.next(); + final String key = entry.getKey(); + final JsonNode value = entry.getValue(); + + if (value.isValueNode()) { + if (value.isNull()) { + builder.put(key, SuppressionRules.ANY_VALUE); + } else { + builder.put(key, value.asText()); + } + } else { + log.warn("Encountered unexpected JSON field {} for annotation", entry); + } + } + } else { + log.error("Encountered unexpected JSONNode {} for annotation", annotationNode); + return ImmutableMap.of(); + } + + return builder.build(); + } + + /** + * Sets annotation of Ports on which LinkDiscovery is suppressed. + * + * @param annotation new key-value pair of annotation; null to clear + * @return self + */ + public SuppressionConfig annotation(Map annotation) { + + // ANY_VALUE should be null in JSON + Map config = Maps.transformValues(annotation, + v -> (v == SuppressionRules.ANY_VALUE) ? null : v); + + String jsonAnnotation = null; + + try { + // TODO Store annotation as a Map instead of a String (which needs NetworkConfigRegistry modification) + jsonAnnotation = MAPPER.writeValueAsString(config); + } catch (JsonProcessingException e) { + log.error("Failed to write JSON from: {}", annotation); + } + + return (SuppressionConfig) setOrClear(ANNOTATION, jsonAnnotation); + } +} -- cgit 1.2.3-korg