2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.provider.lldp.impl;
18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static org.slf4j.LoggerFactory.getLogger;
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;
28 import org.onosproject.net.Device;
29 import org.onosproject.net.DeviceId;
30 import org.slf4j.Logger;
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;
39 import java.util.Map.Entry;
47 "deviceId" : [ "of:2222000000000000" ],
48 "deviceType" : [ "ROADM" ],
49 "annotation" : { "no-lldp" : null, "sendLLDP" : "false" }
54 * Allows for reading and writing LLDP suppression definition as a JSON file.
56 public class SuppressionRulesStore {
58 private static final String DEVICE_ID = "deviceId";
59 private static final String DEVICE_TYPE = "deviceType";
60 private static final String ANNOTATION = "annotation";
62 private final Logger log = getLogger(getClass());
64 private final File file;
67 * Creates a reader/writer of the LLDP suppression definition file.
69 * @param filePath location of the definition file
71 public SuppressionRulesStore(String filePath) {
72 file = new File(filePath);
76 * Creates a reader/writer of the LLDP suppression definition file.
78 * @param file definition file
80 public SuppressionRulesStore(File file) {
81 this.file = checkNotNull(file);
85 * Returns SuppressionRules.
87 * @return SuppressionRules
88 * @throws IOException if error occurred while reading the data
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<>();
95 ObjectMapper mapper = new ObjectMapper();
96 ObjectNode root = (ObjectNode) mapper.readTree(file);
98 for (JsonNode deviceId : root.get(DEVICE_ID)) {
99 if (deviceId.isTextual()) {
100 suppressedDevice.add(DeviceId.deviceId(deviceId.asText()));
102 log.warn("Encountered unexpected JSONNode {} for deviceId", deviceId);
106 for (JsonNode deviceType : root.get(DEVICE_TYPE)) {
107 if (deviceType.isTextual()) {
108 suppressedDeviceType.add(Device.Type.valueOf(deviceType.asText()));
110 log.warn("Encountered unexpected JSONNode {} for deviceType", deviceType);
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();
123 if (value.isValueNode()) {
124 if (value.isNull()) {
125 suppressedAnnotation.put(key, SuppressionRules.ANY_VALUE);
127 suppressedAnnotation.put(key, value.asText());
130 log.warn("Encountered unexpected JSON field {} for annotation", entry);
134 log.warn("Encountered unexpected JSONNode {} for annotation", annotation);
137 return new SuppressionRules(suppressedDevice,
138 suppressedDeviceType,
139 suppressedAnnotation);
143 * Writes the given SuppressionRules.
145 * @param rules SuppressionRules
146 * @throws IOException if error occurred while writing the data
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);
158 rules.getSuppressedDevice()
159 .forEach(deviceId -> deviceIds.add(deviceId.toString()));
161 rules.getSuppressedDeviceType()
162 .forEach(type -> deviceTypes.add(type.toString()));
164 rules.getSuppressedAnnotation().forEach((key, value) -> {
165 if (value == SuppressionRules.ANY_VALUE) {
166 annotations.putNull(key);
168 annotations.put(key, value);
171 mapper.writeTree(new JsonFactory().createGenerator(file, JsonEncoding.UTF8),