blob: 20bafb3303335656b7b20ceb8c75d447c2275070 [file] [log] [blame]
Naoki Shiota399a0b32015-11-15 20:36:13 -06001/*
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 */
16package org.onosproject.provider.lldp.impl;
17
18import com.fasterxml.jackson.core.JsonProcessingException;
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.ImmutableList;
23import com.google.common.collect.ImmutableMap;
24import com.google.common.collect.ImmutableSet;
25import com.google.common.collect.Maps;
26
27import org.onosproject.core.ApplicationId;
28import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.config.Config;
31import org.slf4j.Logger;
32
33import java.io.IOException;
34import java.util.Iterator;
35import java.util.List;
36import java.util.Map;
37import java.util.Set;
38import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES;
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * LLDP suppression config class.
43 */
44public 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}