blob: 360bebd2cdfb75452d958608e5d527f881276886 [file] [log] [blame]
Yuta HIGUCHI41289382014-12-19 17:47:12 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Yuta HIGUCHI41289382014-12-19 17:47:12 -08003 *
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 */
16package org.onosproject.provider.lldp.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import com.fasterxml.jackson.core.JsonEncoding;
22import com.fasterxml.jackson.core.JsonFactory;
23import com.fasterxml.jackson.databind.JsonNode;
24import com.fasterxml.jackson.databind.ObjectMapper;
25import com.fasterxml.jackson.databind.node.ArrayNode;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27
28import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.slf4j.Logger;
31
32import java.io.File;
33import java.io.IOException;
34import java.util.EnumSet;
35import java.util.HashMap;
36import java.util.HashSet;
37import java.util.Iterator;
38import java.util.Map;
39import java.util.Map.Entry;
40import 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 */
56public 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
Charles M.C. Chanbc0e84d2015-01-13 23:53:09 +080088 * @throws IOException if error occurred while reading the data
Yuta HIGUCHI41289382014-12-19 17:47:12 -080089 */
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
Charles M.C. Chanbc0e84d2015-01-13 23:53:09 +0800146 * @throws IOException if error occurred while writing the data
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800147 */
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}