Committed by
Gerrit Code Review
[ONOS-3205] Migrate LLDP Link Discovery configuration to Network Configuration System
- deviceIds under suppression will be moved out to different location. (See ONOS-3461) Change-Id: I6ebe0ce7f5f2d26e7ee7175974e19305f7c17fad
Showing
11 changed files
with
316 additions
and
260 deletions
| ... | @@ -333,6 +333,26 @@ public abstract class Config<S> { | ... | @@ -333,6 +333,26 @@ public abstract class Config<S> { |
| 333 | } | 333 | } |
| 334 | 334 | ||
| 335 | /** | 335 | /** |
| 336 | + * Gets the specified array property as a list of items. | ||
| 337 | + * | ||
| 338 | + * @param name property name | ||
| 339 | + * @param function mapper from string to item | ||
| 340 | + * @param defaultValue default value if property not set | ||
| 341 | + * @param <T> type of item | ||
| 342 | + * @return list of items | ||
| 343 | + */ | ||
| 344 | + protected <T> List<T> getList(String name, Function<String, T> function, List<T> defaultValue) { | ||
| 345 | + List<T> list = Lists.newArrayList(); | ||
| 346 | + JsonNode jsonNode = object.path(name); | ||
| 347 | + if (jsonNode.isMissingNode()) { | ||
| 348 | + return defaultValue; | ||
| 349 | + } | ||
| 350 | + ArrayNode arrayNode = (ArrayNode) jsonNode; | ||
| 351 | + arrayNode.forEach(i -> list.add(function.apply(i.asText()))); | ||
| 352 | + return list; | ||
| 353 | + } | ||
| 354 | + | ||
| 355 | + /** | ||
| 336 | * Sets the specified property as an array of items in a given collection or | 356 | * Sets the specified property as an array of items in a given collection or |
| 337 | * clears it if null is given. | 357 | * clears it if null is given. |
| 338 | * | 358 | * | ... | ... |
| ... | @@ -55,5 +55,9 @@ | ... | @@ -55,5 +55,9 @@ |
| 55 | <scope>test</scope> | 55 | <scope>test</scope> |
| 56 | </dependency> | 56 | </dependency> |
| 57 | 57 | ||
| 58 | + <dependency> | ||
| 59 | + <groupId>com.fasterxml.jackson.core</groupId> | ||
| 60 | + <artifactId>jackson-databind</artifactId> | ||
| 61 | + </dependency> | ||
| 58 | </dependencies> | 62 | </dependencies> |
| 59 | </project> | 63 | </project> | ... | ... |
This diff is collapsed. Click to expand it.
| 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 | +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; | ||
| 29 | +import org.onosproject.net.DeviceId; | ||
| 30 | +import org.onosproject.net.config.Config; | ||
| 31 | +import org.slf4j.Logger; | ||
| 32 | + | ||
| 33 | +import java.io.IOException; | ||
| 34 | +import java.util.Iterator; | ||
| 35 | +import java.util.List; | ||
| 36 | +import java.util.Map; | ||
| 37 | +import java.util.Set; | ||
| 38 | +import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES; | ||
| 39 | +import static org.slf4j.LoggerFactory.getLogger; | ||
| 40 | + | ||
| 41 | +/** | ||
| 42 | + * LLDP suppression config class. | ||
| 43 | + */ | ||
| 44 | +public class SuppressionConfig extends Config<ApplicationId> { | ||
| 45 | + private static final String DEVICE_IDS = "deviceIds"; | ||
| 46 | + private static final String DEVICE_TYPES = "deviceTypes"; | ||
| 47 | + private static final String ANNOTATION = "annotation"; | ||
| 48 | + | ||
| 49 | + private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
| 50 | + private static final List<DeviceId> DEFAULT_DEVICE_IDS | ||
| 51 | + = ImmutableList.copyOf(DEFAULT_RULES.getSuppressedDevice()); | ||
| 52 | + private static final List<Device.Type> DEFAULT_DEVICE_TYPES | ||
| 53 | + = ImmutableList.copyOf(DEFAULT_RULES.getSuppressedDeviceType()); | ||
| 54 | + | ||
| 55 | + private final Logger log = getLogger(getClass()); | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * Returns device IDs on which LLDP is suppressed. | ||
| 59 | + * | ||
| 60 | + * @return Set of DeviceId objects | ||
| 61 | + */ | ||
| 62 | + @Deprecated | ||
| 63 | + public Set<DeviceId> deviceIds() { | ||
| 64 | + return ImmutableSet.copyOf(getList(DEVICE_IDS, DeviceId::deviceId, DEFAULT_DEVICE_IDS)); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * Sets device IDs on which LLDP is suppressed. | ||
| 69 | + * | ||
| 70 | + * @param deviceIds new set of device IDs; null to clear | ||
| 71 | + * @return self | ||
| 72 | + */ | ||
| 73 | + @Deprecated | ||
| 74 | + public SuppressionConfig deviceIds(Set<DeviceId> deviceIds) { | ||
| 75 | + return (SuppressionConfig) setOrClear(DEVICE_IDS, deviceIds); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * Returns types of devices on which LLDP is suppressed. | ||
| 80 | + * | ||
| 81 | + * @return set of device types | ||
| 82 | + */ | ||
| 83 | + public Set<Device.Type> deviceTypes() { | ||
| 84 | + return ImmutableSet.copyOf(getList(DEVICE_TYPES, Device.Type::valueOf, DEFAULT_DEVICE_TYPES)); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Sets types of devices on which LLDP is suppressed. | ||
| 89 | + * | ||
| 90 | + * @param deviceTypes new set of device types; null to clear | ||
| 91 | + * @return self | ||
| 92 | + */ | ||
| 93 | + public SuppressionConfig deviceTypes(Set<Device.Type> deviceTypes) { | ||
| 94 | + return (SuppressionConfig) setOrClear(DEVICE_TYPES, deviceTypes); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * Returns annotation of Ports on which LLDP is suppressed. | ||
| 99 | + * | ||
| 100 | + * @return key-value pairs of annotation | ||
| 101 | + */ | ||
| 102 | + public Map<String, String> annotation() { | ||
| 103 | + ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); | ||
| 104 | + | ||
| 105 | + String jsonAnnotation = get(ANNOTATION, null); | ||
| 106 | + if (jsonAnnotation == null || jsonAnnotation.isEmpty()) { | ||
| 107 | + return ImmutableMap.of(); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + JsonNode annotationNode; | ||
| 111 | + try { | ||
| 112 | + annotationNode = MAPPER.readTree(jsonAnnotation); | ||
| 113 | + } catch (IOException e) { | ||
| 114 | + log.error("Failed to read JSON tree from: {}", jsonAnnotation); | ||
| 115 | + return ImmutableMap.of(); | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + if (annotationNode.isObject()) { | ||
| 119 | + ObjectNode obj = (ObjectNode) annotationNode; | ||
| 120 | + Iterator<Map.Entry<String, JsonNode>> it = obj.fields(); | ||
| 121 | + while (it.hasNext()) { | ||
| 122 | + Map.Entry<String, JsonNode> entry = it.next(); | ||
| 123 | + final String key = entry.getKey(); | ||
| 124 | + final JsonNode value = entry.getValue(); | ||
| 125 | + | ||
| 126 | + if (value.isValueNode()) { | ||
| 127 | + if (value.isNull()) { | ||
| 128 | + builder.put(key, SuppressionRules.ANY_VALUE); | ||
| 129 | + } else { | ||
| 130 | + builder.put(key, value.asText()); | ||
| 131 | + } | ||
| 132 | + } else { | ||
| 133 | + log.warn("Encountered unexpected JSON field {} for annotation", entry); | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | + } else { | ||
| 137 | + log.error("Encountered unexpected JSONNode {} for annotation", annotationNode); | ||
| 138 | + return ImmutableMap.of(); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + return builder.build(); | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + /** | ||
| 145 | + * Sets annotation of Ports on which LLDP is suppressed. | ||
| 146 | + * | ||
| 147 | + * @param annotation new key-value pair of annotation; null to clear | ||
| 148 | + * @return self | ||
| 149 | + */ | ||
| 150 | + public SuppressionConfig annotation(Map<String, String> annotation) { | ||
| 151 | + | ||
| 152 | + // ANY_VALUE should be null in JSON | ||
| 153 | + Map<String, String> config = Maps.transformValues(annotation, | ||
| 154 | + v -> (v == SuppressionRules.ANY_VALUE) ? null : v); | ||
| 155 | + | ||
| 156 | + String jsonAnnotation = null; | ||
| 157 | + | ||
| 158 | + try { | ||
| 159 | + // TODO Store annotation as a Map instead of a String (which needs NetworkConfigRegistry modification) | ||
| 160 | + jsonAnnotation = MAPPER.writeValueAsString(config); | ||
| 161 | + } catch (JsonProcessingException e) { | ||
| 162 | + log.error("Failed to write JSON from: {}", annotation); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + return (SuppressionConfig) setOrClear(ANNOTATION, jsonAnnotation); | ||
| 166 | + } | ||
| 167 | +} |
| ... | @@ -18,6 +18,7 @@ package org.onosproject.provider.lldp.impl; | ... | @@ -18,6 +18,7 @@ package org.onosproject.provider.lldp.impl; |
| 18 | 18 | ||
| 19 | import java.util.Map; | 19 | import java.util.Map; |
| 20 | import java.util.Map.Entry; | 20 | import java.util.Map.Entry; |
| 21 | +import java.util.Objects; | ||
| 21 | import java.util.Set; | 22 | import java.util.Set; |
| 22 | 23 | ||
| 23 | import org.onosproject.net.Annotations; | 24 | import org.onosproject.net.Annotations; |
| ... | @@ -28,6 +29,7 @@ import org.onosproject.net.Port; | ... | @@ -28,6 +29,7 @@ import org.onosproject.net.Port; |
| 28 | 29 | ||
| 29 | import com.google.common.collect.ImmutableMap; | 30 | import com.google.common.collect.ImmutableMap; |
| 30 | import com.google.common.collect.ImmutableSet; | 31 | import com.google.common.collect.ImmutableSet; |
| 32 | +import com.google.common.base.MoreObjects; | ||
| 31 | 33 | ||
| 32 | public class SuppressionRules { | 34 | public class SuppressionRules { |
| 33 | 35 | ||
| ... | @@ -103,4 +105,34 @@ public class SuppressionRules { | ... | @@ -103,4 +105,34 @@ public class SuppressionRules { |
| 103 | Map<String, String> getSuppressedAnnotation() { | 105 | Map<String, String> getSuppressedAnnotation() { |
| 104 | return suppressedAnnotation; | 106 | return suppressedAnnotation; |
| 105 | } | 107 | } |
| 108 | + | ||
| 109 | + @Override | ||
| 110 | + public int hashCode() { | ||
| 111 | + return Objects.hash(suppressedDevice, | ||
| 112 | + suppressedDeviceType, | ||
| 113 | + suppressedAnnotation); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + @Override | ||
| 117 | + public boolean equals(Object object) { | ||
| 118 | + if (object != null && getClass() == object.getClass()) { | ||
| 119 | + SuppressionRules that = (SuppressionRules) object; | ||
| 120 | + return Objects.equals(this.suppressedDevice, | ||
| 121 | + that.suppressedDevice) | ||
| 122 | + && Objects.equals(this.suppressedDeviceType, | ||
| 123 | + that.suppressedDeviceType) | ||
| 124 | + && Objects.equals(this.suppressedAnnotation, | ||
| 125 | + that.suppressedAnnotation); | ||
| 126 | + } | ||
| 127 | + return false; | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + @Override | ||
| 131 | + public String toString() { | ||
| 132 | + return MoreObjects.toStringHelper(this) | ||
| 133 | + .add("suppressedDevice", suppressedDevice) | ||
| 134 | + .add("suppressedDeviceType", suppressedDeviceType) | ||
| 135 | + .add("suppressedAnnotation", suppressedAnnotation) | ||
| 136 | + .toString(); | ||
| 137 | + } | ||
| 106 | } | 138 | } | ... | ... |
providers/lldp/src/main/java/org/onosproject/provider/lldp/impl/SuppressionRulesStore.java
deleted
100644 → 0
| 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 | -package org.onosproject.provider.lldp.impl; | ||
| 17 | - | ||
| 18 | -import static com.google.common.base.Preconditions.checkNotNull; | ||
| 19 | -import static org.slf4j.LoggerFactory.getLogger; | ||
| 20 | - | ||
| 21 | -import com.fasterxml.jackson.core.JsonEncoding; | ||
| 22 | -import com.fasterxml.jackson.core.JsonFactory; | ||
| 23 | -import com.fasterxml.jackson.databind.JsonNode; | ||
| 24 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 25 | -import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 26 | -import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| 27 | - | ||
| 28 | -import org.onosproject.net.Device; | ||
| 29 | -import org.onosproject.net.DeviceId; | ||
| 30 | -import org.slf4j.Logger; | ||
| 31 | - | ||
| 32 | -import java.io.File; | ||
| 33 | -import java.io.IOException; | ||
| 34 | -import java.util.EnumSet; | ||
| 35 | -import java.util.HashMap; | ||
| 36 | -import java.util.HashSet; | ||
| 37 | -import java.util.Iterator; | ||
| 38 | -import java.util.Map; | ||
| 39 | -import java.util.Map.Entry; | ||
| 40 | -import java.util.Set; | ||
| 41 | - | ||
| 42 | -/* | ||
| 43 | - * JSON file example | ||
| 44 | - * | ||
| 45 | - | ||
| 46 | -{ | ||
| 47 | - "deviceId" : [ "of:2222000000000000" ], | ||
| 48 | - "deviceType" : [ "ROADM" ], | ||
| 49 | - "annotation" : { "no-lldp" : null, "sendLLDP" : "false" } | ||
| 50 | -} | ||
| 51 | - */ | ||
| 52 | - | ||
| 53 | -/** | ||
| 54 | - * Allows for reading and writing LLDP suppression definition as a JSON file. | ||
| 55 | - */ | ||
| 56 | -public class SuppressionRulesStore { | ||
| 57 | - | ||
| 58 | - private static final String DEVICE_ID = "deviceId"; | ||
| 59 | - private static final String DEVICE_TYPE = "deviceType"; | ||
| 60 | - private static final String ANNOTATION = "annotation"; | ||
| 61 | - | ||
| 62 | - private final Logger log = getLogger(getClass()); | ||
| 63 | - | ||
| 64 | - private final File file; | ||
| 65 | - | ||
| 66 | - /** | ||
| 67 | - * Creates a reader/writer of the LLDP suppression definition file. | ||
| 68 | - * | ||
| 69 | - * @param filePath location of the definition file | ||
| 70 | - */ | ||
| 71 | - public SuppressionRulesStore(String filePath) { | ||
| 72 | - file = new File(filePath); | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - /** | ||
| 76 | - * Creates a reader/writer of the LLDP suppression definition file. | ||
| 77 | - * | ||
| 78 | - * @param file definition file | ||
| 79 | - */ | ||
| 80 | - public SuppressionRulesStore(File file) { | ||
| 81 | - this.file = checkNotNull(file); | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - /** | ||
| 85 | - * Returns SuppressionRules. | ||
| 86 | - * | ||
| 87 | - * @return SuppressionRules | ||
| 88 | - * @throws IOException if error occurred while reading the data | ||
| 89 | - */ | ||
| 90 | - public SuppressionRules read() throws IOException { | ||
| 91 | - final Set<DeviceId> suppressedDevice = new HashSet<>(); | ||
| 92 | - final EnumSet<Device.Type> suppressedDeviceType = EnumSet.noneOf(Device.Type.class); | ||
| 93 | - final Map<String, String> suppressedAnnotation = new HashMap<>(); | ||
| 94 | - | ||
| 95 | - ObjectMapper mapper = new ObjectMapper(); | ||
| 96 | - ObjectNode root = (ObjectNode) mapper.readTree(file); | ||
| 97 | - | ||
| 98 | - for (JsonNode deviceId : root.get(DEVICE_ID)) { | ||
| 99 | - if (deviceId.isTextual()) { | ||
| 100 | - suppressedDevice.add(DeviceId.deviceId(deviceId.asText())); | ||
| 101 | - } else { | ||
| 102 | - log.warn("Encountered unexpected JSONNode {} for deviceId", deviceId); | ||
| 103 | - } | ||
| 104 | - } | ||
| 105 | - | ||
| 106 | - for (JsonNode deviceType : root.get(DEVICE_TYPE)) { | ||
| 107 | - if (deviceType.isTextual()) { | ||
| 108 | - suppressedDeviceType.add(Device.Type.valueOf(deviceType.asText())); | ||
| 109 | - } else { | ||
| 110 | - log.warn("Encountered unexpected JSONNode {} for deviceType", deviceType); | ||
| 111 | - } | ||
| 112 | - } | ||
| 113 | - | ||
| 114 | - JsonNode annotation = root.get(ANNOTATION); | ||
| 115 | - if (annotation.isObject()) { | ||
| 116 | - ObjectNode obj = (ObjectNode) annotation; | ||
| 117 | - Iterator<Entry<String, JsonNode>> it = obj.fields(); | ||
| 118 | - while (it.hasNext()) { | ||
| 119 | - Entry<String, JsonNode> entry = it.next(); | ||
| 120 | - final String key = entry.getKey(); | ||
| 121 | - final JsonNode value = entry.getValue(); | ||
| 122 | - | ||
| 123 | - if (value.isValueNode()) { | ||
| 124 | - if (value.isNull()) { | ||
| 125 | - suppressedAnnotation.put(key, SuppressionRules.ANY_VALUE); | ||
| 126 | - } else { | ||
| 127 | - suppressedAnnotation.put(key, value.asText()); | ||
| 128 | - } | ||
| 129 | - } else { | ||
| 130 | - log.warn("Encountered unexpected JSON field {} for annotation", entry); | ||
| 131 | - } | ||
| 132 | - } | ||
| 133 | - } else { | ||
| 134 | - log.warn("Encountered unexpected JSONNode {} for annotation", annotation); | ||
| 135 | - } | ||
| 136 | - | ||
| 137 | - return new SuppressionRules(suppressedDevice, | ||
| 138 | - suppressedDeviceType, | ||
| 139 | - suppressedAnnotation); | ||
| 140 | - } | ||
| 141 | - | ||
| 142 | - /** | ||
| 143 | - * Writes the given SuppressionRules. | ||
| 144 | - * | ||
| 145 | - * @param rules SuppressionRules | ||
| 146 | - * @throws IOException if error occurred while writing the data | ||
| 147 | - */ | ||
| 148 | - public void write(SuppressionRules rules) throws IOException { | ||
| 149 | - ObjectMapper mapper = new ObjectMapper(); | ||
| 150 | - ObjectNode root = mapper.createObjectNode(); | ||
| 151 | - ArrayNode deviceIds = mapper.createArrayNode(); | ||
| 152 | - ArrayNode deviceTypes = mapper.createArrayNode(); | ||
| 153 | - ObjectNode annotations = mapper.createObjectNode(); | ||
| 154 | - root.set(DEVICE_ID, deviceIds); | ||
| 155 | - root.set(DEVICE_TYPE, deviceTypes); | ||
| 156 | - root.set(ANNOTATION, annotations); | ||
| 157 | - | ||
| 158 | - rules.getSuppressedDevice() | ||
| 159 | - .forEach(deviceId -> deviceIds.add(deviceId.toString())); | ||
| 160 | - | ||
| 161 | - rules.getSuppressedDeviceType() | ||
| 162 | - .forEach(type -> deviceTypes.add(type.toString())); | ||
| 163 | - | ||
| 164 | - rules.getSuppressedAnnotation().forEach((key, value) -> { | ||
| 165 | - if (value == SuppressionRules.ANY_VALUE) { | ||
| 166 | - annotations.putNull(key); | ||
| 167 | - } else { | ||
| 168 | - annotations.put(key, value); | ||
| 169 | - } | ||
| 170 | - }); | ||
| 171 | - mapper.writeTree(new JsonFactory().createGenerator(file, JsonEncoding.UTF8), | ||
| 172 | - root); | ||
| 173 | - } | ||
| 174 | -} |
This diff is collapsed. Click to expand it.
providers/lldp/src/test/java/org/onosproject/provider/lldp/impl/SuppressionConfigTest.java
0 → 100644
| 1 | +package org.onosproject.provider.lldp.impl; | ||
| 2 | + | ||
| 3 | +import static org.junit.Assert.*; | ||
| 4 | + | ||
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 6 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
| 7 | +import org.junit.Before; | ||
| 8 | +import org.junit.Test; | ||
| 9 | +import org.onosproject.TestApplicationId; | ||
| 10 | +import org.onosproject.net.Device; | ||
| 11 | +import org.onosproject.net.DeviceId; | ||
| 12 | +import org.onosproject.net.config.ConfigApplyDelegate; | ||
| 13 | + | ||
| 14 | +import java.util.HashMap; | ||
| 15 | +import java.util.HashSet; | ||
| 16 | +import java.util.Map; | ||
| 17 | +import java.util.Set; | ||
| 18 | + | ||
| 19 | +public class SuppressionConfigTest { | ||
| 20 | + private static final String APP_NAME = "SuppressionConfigTest"; | ||
| 21 | + private static final TestApplicationId APP_ID = new TestApplicationId(APP_NAME); | ||
| 22 | + private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId("of:1111000000000000"); | ||
| 23 | + private static final DeviceId DEVICE_ID_2 = DeviceId.deviceId("of:2222000000000000"); | ||
| 24 | + private static final Device.Type DEVICE_TYPE_1 = Device.Type.ROADM; | ||
| 25 | + private static final Device.Type DEVICE_TYPE_2 = Device.Type.FIBER_SWITCH; | ||
| 26 | + private static final String ANNOTATION_KEY_1 = "no_lldp"; | ||
| 27 | + private static final String ANNOTATION_VALUE_1 = "true"; | ||
| 28 | + private static final String ANNOTATION_KEY_2 = "sendLLDP"; | ||
| 29 | + private static final String ANNOTATION_VALUE_2 = "false"; | ||
| 30 | + | ||
| 31 | + private SuppressionConfig cfg; | ||
| 32 | + | ||
| 33 | + @Before | ||
| 34 | + public void setUp() throws Exception { | ||
| 35 | + ConfigApplyDelegate delegate = config -> { }; | ||
| 36 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 37 | + cfg = new SuppressionConfig(); | ||
| 38 | + cfg.init(APP_ID, LldpLinkProvider.CONFIG_KEY, JsonNodeFactory.instance.objectNode(), mapper, delegate); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + @Test | ||
| 42 | + public void testDeviceIds() { | ||
| 43 | + Set<DeviceId> inputIds = new HashSet<DeviceId>() { { | ||
| 44 | + add(DEVICE_ID_1); | ||
| 45 | + add(DEVICE_ID_2); | ||
| 46 | + } }; | ||
| 47 | + | ||
| 48 | + assertNotNull(cfg.deviceIds(inputIds)); | ||
| 49 | + | ||
| 50 | + Set<DeviceId> outputIds = cfg.deviceIds(); | ||
| 51 | + assertTrue(outputIds.contains(DEVICE_ID_1)); | ||
| 52 | + assertTrue(outputIds.contains(DEVICE_ID_2)); | ||
| 53 | + assertEquals(outputIds.size(), 2); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + @Test | ||
| 57 | + public void testDeviceTypes() { | ||
| 58 | + Set<Device.Type> inputTypes = new HashSet<Device.Type>() { { | ||
| 59 | + add(DEVICE_TYPE_1); | ||
| 60 | + add(DEVICE_TYPE_2); | ||
| 61 | + } }; | ||
| 62 | + | ||
| 63 | + assertNotNull(cfg.deviceTypes(inputTypes)); | ||
| 64 | + | ||
| 65 | + Set<Device.Type> outputTypes = cfg.deviceTypes(); | ||
| 66 | + assertTrue(outputTypes.contains(DEVICE_TYPE_1)); | ||
| 67 | + assertTrue(outputTypes.contains(DEVICE_TYPE_2)); | ||
| 68 | + assertEquals(outputTypes.size(), 2); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Test | ||
| 72 | + public void testDeviceAnnotation() { | ||
| 73 | + Map<String, String> inputMap = new HashMap<String, String>() { { | ||
| 74 | + put(ANNOTATION_KEY_1, ANNOTATION_VALUE_1); | ||
| 75 | + put(ANNOTATION_KEY_2, ANNOTATION_VALUE_2); | ||
| 76 | + } }; | ||
| 77 | + | ||
| 78 | + assertNotNull(cfg.annotation(inputMap)); | ||
| 79 | + | ||
| 80 | + Map<String, String> outputMap = cfg.annotation(); | ||
| 81 | + assertEquals(outputMap.get(ANNOTATION_KEY_1), ANNOTATION_VALUE_1); | ||
| 82 | + assertEquals(outputMap.get(ANNOTATION_KEY_2), ANNOTATION_VALUE_2); | ||
| 83 | + assertEquals(outputMap.size(), 2); | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | +} |
| 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 static org.junit.Assert.*; | ||
| 20 | -import static org.onosproject.net.DeviceId.deviceId; | ||
| 21 | - | ||
| 22 | -import java.io.File; | ||
| 23 | -import java.io.IOException; | ||
| 24 | -import java.net.URISyntaxException; | ||
| 25 | -import java.nio.file.Path; | ||
| 26 | -import java.nio.file.Paths; | ||
| 27 | - | ||
| 28 | -import org.junit.Rule; | ||
| 29 | -import org.junit.Test; | ||
| 30 | -import org.junit.rules.TemporaryFolder; | ||
| 31 | -import org.onosproject.net.Device; | ||
| 32 | - | ||
| 33 | -import com.google.common.collect.ImmutableMap; | ||
| 34 | -import com.google.common.collect.ImmutableSet; | ||
| 35 | -import com.google.common.io.Resources; | ||
| 36 | - | ||
| 37 | -public class SuppressionRulesStoreTest { | ||
| 38 | - | ||
| 39 | - @Rule | ||
| 40 | - public TemporaryFolder tempFolder = new TemporaryFolder(); | ||
| 41 | - | ||
| 42 | - // "lldp_suppression.json" | ||
| 43 | - SuppressionRules testData | ||
| 44 | - = new SuppressionRules(ImmutableSet.of(deviceId("of:2222000000000000")), | ||
| 45 | - ImmutableSet.of(Device.Type.ROADM), | ||
| 46 | - ImmutableMap.of("no-lldp", SuppressionRules.ANY_VALUE, | ||
| 47 | - "sendLLDP", "false")); | ||
| 48 | - | ||
| 49 | - private static void assertRulesEqual(SuppressionRules expected, SuppressionRules actual) { | ||
| 50 | - assertEquals(expected.getSuppressedDevice(), | ||
| 51 | - actual.getSuppressedDevice()); | ||
| 52 | - assertEquals(expected.getSuppressedDeviceType(), | ||
| 53 | - actual.getSuppressedDeviceType()); | ||
| 54 | - assertEquals(expected.getSuppressedAnnotation(), | ||
| 55 | - actual.getSuppressedAnnotation()); | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - @Test | ||
| 59 | - public void testRead() throws URISyntaxException, IOException { | ||
| 60 | - Path path = Paths.get(Resources.getResource("lldp_suppression.json").toURI()); | ||
| 61 | - | ||
| 62 | - SuppressionRulesStore store = new SuppressionRulesStore(path.toString()); | ||
| 63 | - | ||
| 64 | - SuppressionRules rules = store.read(); | ||
| 65 | - | ||
| 66 | - assertRulesEqual(testData, rules); | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - @Test | ||
| 70 | - public void testWrite() throws IOException { | ||
| 71 | - File newFile = tempFolder.newFile(); | ||
| 72 | - SuppressionRulesStore store = new SuppressionRulesStore(newFile); | ||
| 73 | - store.write(testData); | ||
| 74 | - | ||
| 75 | - SuppressionRulesStore reload = new SuppressionRulesStore(newFile); | ||
| 76 | - SuppressionRules rules = reload.read(); | ||
| 77 | - | ||
| 78 | - assertRulesEqual(testData, rules); | ||
| 79 | - } | ||
| 80 | -} |
| ... | @@ -62,5 +62,12 @@ | ... | @@ -62,5 +62,12 @@ |
| 62 | ] | 62 | ] |
| 63 | } | 63 | } |
| 64 | } | 64 | } |
| 65 | + "org.onosproject.provider.lldp": { | ||
| 66 | + "suppression": { | ||
| 67 | + "deviceIds": [ "of:2222000000000000" ], | ||
| 68 | + "deviceTypes": [ "ROADM" ], | ||
| 69 | + "annotation": { "no-lldp": null, "sendLLDP" : "false" } | ||
| 70 | + } | ||
| 71 | + } | ||
| 65 | } | 72 | } |
| 66 | } | 73 | } | ... | ... |
-
Please register or login to post a comment