blob: f861159e54257e150b3dc4b64a896b0ceb5e9e1b [file] [log] [blame]
Naoki Shiota399a0b32015-11-15 20:36:13 -06001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Naoki Shiota399a0b32015-11-15 20:36:13 -06003 *
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;
Naoki Shiota399a0b32015-11-15 20:36:13 -060029import org.onosproject.net.config.Config;
30import org.slf4j.Logger;
31
32import java.io.IOException;
33import java.util.Iterator;
34import java.util.List;
35import java.util.Map;
36import java.util.Set;
37import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES;
38import static org.slf4j.LoggerFactory.getLogger;
39
40/**
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080041 * LinkDiscovery suppression config class.
Naoki Shiota399a0b32015-11-15 20:36:13 -060042 */
43public class SuppressionConfig extends Config<ApplicationId> {
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080044
Naoki Shiota399a0b32015-11-15 20:36:13 -060045 private static final String DEVICE_TYPES = "deviceTypes";
46 private static final String ANNOTATION = "annotation";
47
48 private static final ObjectMapper MAPPER = new ObjectMapper();
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080049
Naoki Shiota399a0b32015-11-15 20:36:13 -060050 private static final List<Device.Type> DEFAULT_DEVICE_TYPES
51 = ImmutableList.copyOf(DEFAULT_RULES.getSuppressedDeviceType());
52
53 private final Logger log = getLogger(getClass());
54
55 /**
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080056 * Returns types of devices on which LinkDiscovery is suppressed.
Naoki Shiota399a0b32015-11-15 20:36:13 -060057 *
58 * @return set of device types
59 */
60 public Set<Device.Type> deviceTypes() {
61 return ImmutableSet.copyOf(getList(DEVICE_TYPES, Device.Type::valueOf, DEFAULT_DEVICE_TYPES));
62 }
63
64 /**
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080065 * Sets types of devices on which LinkDiscovery is suppressed.
Naoki Shiota399a0b32015-11-15 20:36:13 -060066 *
67 * @param deviceTypes new set of device types; null to clear
68 * @return self
69 */
70 public SuppressionConfig deviceTypes(Set<Device.Type> deviceTypes) {
71 return (SuppressionConfig) setOrClear(DEVICE_TYPES, deviceTypes);
72 }
73
74 /**
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -080075 * Returns annotation of Ports on which LinkDiscovery is suppressed.
Naoki Shiota399a0b32015-11-15 20:36:13 -060076 *
77 * @return key-value pairs of annotation
78 */
79 public Map<String, String> annotation() {
80 ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
81
82 String jsonAnnotation = get(ANNOTATION, null);
83 if (jsonAnnotation == null || jsonAnnotation.isEmpty()) {
84 return ImmutableMap.of();
85 }
86
87 JsonNode annotationNode;
88 try {
89 annotationNode = MAPPER.readTree(jsonAnnotation);
90 } catch (IOException e) {
91 log.error("Failed to read JSON tree from: {}", jsonAnnotation);
92 return ImmutableMap.of();
93 }
94
95 if (annotationNode.isObject()) {
96 ObjectNode obj = (ObjectNode) annotationNode;
97 Iterator<Map.Entry<String, JsonNode>> it = obj.fields();
98 while (it.hasNext()) {
99 Map.Entry<String, JsonNode> entry = it.next();
100 final String key = entry.getKey();
101 final JsonNode value = entry.getValue();
102
103 if (value.isValueNode()) {
104 if (value.isNull()) {
105 builder.put(key, SuppressionRules.ANY_VALUE);
106 } else {
107 builder.put(key, value.asText());
108 }
109 } else {
110 log.warn("Encountered unexpected JSON field {} for annotation", entry);
111 }
112 }
113 } else {
114 log.error("Encountered unexpected JSONNode {} for annotation", annotationNode);
115 return ImmutableMap.of();
116 }
117
118 return builder.build();
119 }
120
121 /**
HIGUCHI Yutad9fe3a32015-11-24 18:52:25 -0800122 * Sets annotation of Ports on which LinkDiscovery is suppressed.
Naoki Shiota399a0b32015-11-15 20:36:13 -0600123 *
124 * @param annotation new key-value pair of annotation; null to clear
125 * @return self
126 */
127 public SuppressionConfig annotation(Map<String, String> annotation) {
128
129 // ANY_VALUE should be null in JSON
130 Map<String, String> config = Maps.transformValues(annotation,
131 v -> (v == SuppressionRules.ANY_VALUE) ? null : v);
132
133 String jsonAnnotation = null;
134
135 try {
136 // TODO Store annotation as a Map instead of a String (which needs NetworkConfigRegistry modification)
137 jsonAnnotation = MAPPER.writeValueAsString(config);
138 } catch (JsonProcessingException e) {
139 log.error("Failed to write JSON from: {}", annotation);
140 }
141
142 return (SuppressionConfig) setOrClear(ANNOTATION, jsonAnnotation);
143 }
144}