blob: 7c4b3bef0847296d1637aa058361aa9464dd2a8a [file] [log] [blame]
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -08001/*
2 * Copyright 2017-present 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.net.config.basics;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.HashMap;
21import java.util.Map;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.config.BaseConfig;
24import org.slf4j.Logger;
25
26import com.fasterxml.jackson.core.JsonProcessingException;
27import com.fasterxml.jackson.databind.JsonNode;
28import com.fasterxml.jackson.databind.ObjectMapper;
29import com.fasterxml.jackson.databind.node.ObjectNode;
30
31/**
32 * Configuration to add extra annotations to a port via netcfg subsystem.
33 */
34public class PortAnnotationConfig
35 extends BaseConfig<ConnectPoint> {
36
37 /**
38 * {@value #CONFIG_KEY} : a netcfg ConfigKey for {@link PortAnnotationConfig}.
39 */
40 public static final String CONFIG_KEY = "annotations";
41
42 /**
43 * JSON key for annotation entries.
44 * Value is a JSON object.
45 */
46 private static final String ENTRIES = "entries";
47
48 private final Logger log = getLogger(getClass());
49
50 @Override
51 public boolean isValid() {
52 return hasField(ENTRIES) && object.get(ENTRIES).isObject();
53 }
54
55 /**
56 * Returns annotations to add to a Port.
57 *
58 * @return annotations as a map. null value represent key removal request
59 */
60 public Map<String, String> annotations() {
61 Map<String, String> map = new HashMap<>();
62
63 JsonNode jsonNode = object.path(ENTRIES);
64 if (!jsonNode.isObject()) {
65 return map;
66 }
67
68 jsonNode.fields().forEachRemaining(entry -> {
69 String key = entry.getKey();
70 JsonNode value = entry.getValue();
71 if (value.isTextual()) {
72 map.put(key, value.asText());
73 } else if (value.isNull()) {
74 map.put(key, null);
75 } else {
76 try {
77 map.put(key, mapper().writeValueAsString(value));
78 } catch (JsonProcessingException e) {
79 log.warn("Error processing JSON value for {}.", key, e);
80 }
81 }
82 });
83 return map;
84 }
85
86 /**
87 * Sets annotations to add to a Port.
88 *
89 * @param replace annotations to be added by this configuration.
90 * null value represent key removal request
91 * @return self
92 */
93 public PortAnnotationConfig annotations(Map<String, String> replace) {
94 ObjectNode anns = object.objectNode();
95 if (replace != null) {
96 replace.forEach((k, v) -> {
97 anns.put(k, v);
98 });
99 }
100 object.set(ENTRIES, anns);
101 return this;
102 }
103
104 /**
105 * Add configuration to set or remove annotation entry.
106 *
107 * @param key annotations key
108 * @param value annotations value. specifying null removes the entry.
109 * @return self
110 */
111 public PortAnnotationConfig annotation(String key, String value) {
112 JsonNode ent = object.path(ENTRIES);
113 ObjectNode obj = (ent.isObject()) ? (ObjectNode) ent : object.objectNode();
114
115 obj.put(key, value);
116
117 object.set(ENTRIES, obj);
118 return this;
119 }
120
121 /**
122 * Remove configuration about specified key.
123 *
124 * @param key annotations key
125 * @return self
126 */
127 public PortAnnotationConfig annotation(String key) {
128 JsonNode ent = object.path(ENTRIES);
129 ObjectNode obj = (ent.isObject()) ? (ObjectNode) ent : object.objectNode();
130
131 obj.remove(key);
132
133 object.set(ENTRIES, obj);
134 return this;
135 }
136
137 /**
138 * Create a detached {@link PortAnnotationConfig}.
139 * <p>
140 * Note: created instance needs to be initialized by #init(..) before using.
141 */
142 public PortAnnotationConfig() {
143 super();
144 }
145
146 /**
147 * Create a detached {@link PortAnnotationConfig} for specified port.
148 * <p>
149 * Note: created instance is not bound to NetworkConfigService,
150 * thus cannot use {@link #apply()}. Must be passed to the service
151 * using NetworkConfigService#applyConfig
152 *
153 * @param cp ConnectPoint
154 */
155 public PortAnnotationConfig(ConnectPoint cp) {
156 ObjectMapper mapper = new ObjectMapper();
157 init(cp, CONFIG_KEY, mapper.createObjectNode(), mapper, null);
158 }
159}