blob: 1ae30e0975bec0b6755ec7b3b3ea50e4fb92c169 [file] [log] [blame]
Jian Li7fe7eaf2018-12-31 17:00:33 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.openstacktelemetry.web;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.Maps;
20import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService;
21import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
22import org.onosproject.rest.AbstractWebResource;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.GET;
29import javax.ws.rs.PUT;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.Context;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import javax.ws.rs.core.UriInfo;
37import java.util.Map;
38
39import static org.onlab.util.Tools.nullIsIllegal;
40import static org.onlab.util.Tools.nullIsNotFound;
Jian Li667c6eb2019-01-07 23:01:12 +090041import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.Status.DISABLED;
42import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.Status.ENABLED;
Jian Li7fe7eaf2018-12-31 17:00:33 +090043
44/**
45 * Handles REST API call of openstack telemetry configuration.
46 */
47@Path("config")
48public class OpenstackTelemetryConfigWebResource extends AbstractWebResource {
49
50 private final Logger log = LoggerFactory.getLogger(getClass());
51
52 private static final String MESSAGE_CONFIG = "Received config %s request";
53 private static final String CONFIG = "config";
54 private static final String ADDRESS = "address";
55 private static final String QUERY = "QUERY";
56 private static final String UPDATE = "UPDATE";
57 private static final String DELETE = "DELETE";
58 private static final String CONFIG_NAME = "config name";
59 private static final String NOT_NULL_MESSAGE = " cannot be null";
60 private static final String CONFIG_NOT_FOUND = "Config is not found";
61
62 private final TelemetryConfigAdminService configService =
63 get(TelemetryConfigAdminService.class);
64
65 @Context
66 private UriInfo uriInfo;
67
68 /**
69 * Updates the telemetry configuration address from the JSON input stream.
70 *
71 * @param configName telemetry config name
72 * @param address telemetry config address
73 * @return 200 OK with the updated telemetry config, 400 BAD_REQUEST
74 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
75 * due to incorrect configuration name so that we cannot find the existing config
76 */
77 @PUT
78 @Path("address/{name}/{address}")
79 @Consumes(MediaType.APPLICATION_JSON)
80 @Produces(MediaType.APPLICATION_JSON)
81 public Response updateConfigAddress(@PathParam("name") String configName,
82 @PathParam("address") String address) {
83 log.trace(String.format(MESSAGE_CONFIG, UPDATE));
84
85 try {
86 TelemetryConfig config = configService.getConfig(
87 nullIsIllegal(configName, CONFIG_NAME + NOT_NULL_MESSAGE));
88
89 if (config == null) {
90 log.warn("There is no config found to modify for {}", configName);
91 return Response.notModified().build();
92 } else {
93 Map<String, String> updatedProperties =
94 Maps.newHashMap(config.properties());
95 updatedProperties.put(ADDRESS,
96 nullIsIllegal(address, ADDRESS + NOT_NULL_MESSAGE));
97 TelemetryConfig updatedConfig =
98 config.updateProperties(updatedProperties);
99
100 configService.updateTelemetryConfig(updatedConfig);
101 return Response.ok().build();
102 }
103
104 } catch (Exception e) {
105 throw new IllegalArgumentException(e);
106 }
107 }
108
109 /**
110 * Deletes the telemetry configuration by referring to configuration name.
111 *
112 * @param configName telemetry configuration name
113 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed,
114 * and 304 NOT_MODIFIED without removing config, due to incorrect
115 * configuration name so that we cannot find the existing config
116 */
117 @DELETE
118 @Path("{name}")
119 @Consumes(MediaType.APPLICATION_JSON)
120 @Produces(MediaType.APPLICATION_JSON)
121 public Response deleteTelemetryConfig(@PathParam("name") String configName) {
122 log.trace(String.format(MESSAGE_CONFIG, DELETE));
123
124 TelemetryConfig config = configService.getConfig(
125 nullIsIllegal(configName, CONFIG_NAME + NOT_NULL_MESSAGE));
126
127 if (config == null) {
128 log.warn("There is no config found to delete for {}", configName);
129 return Response.notModified().build();
130 } else {
131 configService.removeTelemetryConfig(configName);
132 return Response.noContent().build();
133 }
134 }
135
136 /**
137 * Get details of telemetry config.
138 * Returns detailed properties of the specified telemetry config.
139 *
140 * @param configName telemetry configName
141 * @return 200 OK with detailed properties of the specific telemetry config
142 * @onos.rsModel TelemetryConfig
143 */
144 @GET
145 @Path("{name}")
146 @Consumes(MediaType.APPLICATION_JSON)
147 @Produces(MediaType.APPLICATION_JSON)
148 public Response getConfig(@PathParam("name") String configName) {
149 log.trace(String.format(MESSAGE_CONFIG, QUERY));
150
151 final TelemetryConfig config =
152 nullIsNotFound(configService.getConfig(configName), CONFIG_NOT_FOUND);
153 final ObjectNode root = codec(TelemetryConfig.class).encode(config, this);
154 return ok(root).build();
155 }
156
157 /**
158 * Enables the telemetry configuration with the given config name.
159 *
160 * @param configName telemetry configuration name
161 * @return 200 OK with the enabled telemetry config,
162 * 400 BAD_REQUEST if the JSON is malformed,
163 * and 304 NOT_MODIFIED without removing config, due to incorrect
164 * configuration name so that we cannot find the existing config
165 */
166 @PUT
167 @Path("enable/{name}")
168 @Consumes(MediaType.APPLICATION_JSON)
169 @Produces(MediaType.APPLICATION_JSON)
170 public Response enableConfig(@PathParam("name") String configName) {
171 log.trace(String.format(MESSAGE_CONFIG, UPDATE));
172
173 TelemetryConfig config = configService.getConfig(
174 nullIsIllegal(configName, CONFIG_NAME + NOT_NULL_MESSAGE));
175
176 if (config == null) {
177 log.warn("There is no config found to enable for {}", configName);
178 return Response.notModified().build();
179 } else {
Jian Li667c6eb2019-01-07 23:01:12 +0900180 TelemetryConfig updatedConfig = config.updateStatus(ENABLED);
Jian Li7fe7eaf2018-12-31 17:00:33 +0900181 configService.updateTelemetryConfig(updatedConfig);
182 return Response.ok().build();
183 }
184 }
185
186 /**
187 * Disables the telemetry configuration with the given config name.
188 *
189 * @param configName telemetry configuration name
190 * @return 200 OK with the disabled telemetry config
191 * 400 BAD_REQUEST if the JSON is malformed,
192 * and 304 NOT_MODIFIED without removing config, due to incorrect
193 * configuration name so that we cannot find the existing config
194 */
195 @PUT
196 @Path("disable/{name}")
197 @Consumes(MediaType.APPLICATION_JSON)
198 @Produces(MediaType.APPLICATION_JSON)
199 public Response disableConfig(@PathParam("name") String configName) {
200 log.trace(String.format(MESSAGE_CONFIG, UPDATE));
201
202 TelemetryConfig config = configService.getConfig(
203 nullIsIllegal(configName, CONFIG_NAME + NOT_NULL_MESSAGE));
204
205 if (config == null) {
206 log.warn("There is no config found to disable for {}", configName);
207 return Response.notModified().build();
208 } else {
Jian Li667c6eb2019-01-07 23:01:12 +0900209 TelemetryConfig updatedConfig = config.updateStatus(DISABLED);
Jian Li7fe7eaf2018-12-31 17:00:33 +0900210 configService.updateTelemetryConfig(updatedConfig);
211 return Response.ok().build();
212 }
213 }
214}