blob: 7484d04f6c7843cbad0f84775929c11356305c1a [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;
41
42/**
43 * Handles REST API call of openstack telemetry configuration.
44 */
45@Path("config")
46public class OpenstackTelemetryConfigWebResource extends AbstractWebResource {
47
48 private final Logger log = LoggerFactory.getLogger(getClass());
49
50 private static final String MESSAGE_CONFIG = "Received config %s request";
51 private static final String CONFIG = "config";
52 private static final String ADDRESS = "address";
53 private static final String QUERY = "QUERY";
54 private static final String UPDATE = "UPDATE";
55 private static final String DELETE = "DELETE";
56 private static final String CONFIG_NAME = "config name";
57 private static final String NOT_NULL_MESSAGE = " cannot be null";
58 private static final String CONFIG_NOT_FOUND = "Config is not found";
59
60 private final TelemetryConfigAdminService configService =
61 get(TelemetryConfigAdminService.class);
62
63 @Context
64 private UriInfo uriInfo;
65
66 /**
67 * Updates the telemetry configuration address from the JSON input stream.
68 *
69 * @param configName telemetry config name
70 * @param address telemetry config address
71 * @return 200 OK with the updated telemetry config, 400 BAD_REQUEST
72 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
73 * due to incorrect configuration name so that we cannot find the existing config
74 */
75 @PUT
76 @Path("address/{name}/{address}")
77 @Consumes(MediaType.APPLICATION_JSON)
78 @Produces(MediaType.APPLICATION_JSON)
79 public Response updateConfigAddress(@PathParam("name") String configName,
80 @PathParam("address") String address) {
81 log.trace(String.format(MESSAGE_CONFIG, UPDATE));
82
83 try {
84 TelemetryConfig config = configService.getConfig(
85 nullIsIllegal(configName, CONFIG_NAME + NOT_NULL_MESSAGE));
86
87 if (config == null) {
88 log.warn("There is no config found to modify for {}", configName);
89 return Response.notModified().build();
90 } else {
91 Map<String, String> updatedProperties =
92 Maps.newHashMap(config.properties());
93 updatedProperties.put(ADDRESS,
94 nullIsIllegal(address, ADDRESS + NOT_NULL_MESSAGE));
95 TelemetryConfig updatedConfig =
96 config.updateProperties(updatedProperties);
97
98 configService.updateTelemetryConfig(updatedConfig);
99 return Response.ok().build();
100 }
101
102 } catch (Exception e) {
103 throw new IllegalArgumentException(e);
104 }
105 }
106
107 /**
108 * Deletes the telemetry configuration by referring to configuration name.
109 *
110 * @param configName telemetry configuration name
111 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed,
112 * and 304 NOT_MODIFIED without removing config, due to incorrect
113 * configuration name so that we cannot find the existing config
114 */
115 @DELETE
116 @Path("{name}")
117 @Consumes(MediaType.APPLICATION_JSON)
118 @Produces(MediaType.APPLICATION_JSON)
119 public Response deleteTelemetryConfig(@PathParam("name") String configName) {
120 log.trace(String.format(MESSAGE_CONFIG, DELETE));
121
122 TelemetryConfig config = configService.getConfig(
123 nullIsIllegal(configName, CONFIG_NAME + NOT_NULL_MESSAGE));
124
125 if (config == null) {
126 log.warn("There is no config found to delete for {}", configName);
127 return Response.notModified().build();
128 } else {
129 configService.removeTelemetryConfig(configName);
130 return Response.noContent().build();
131 }
132 }
133
134 /**
135 * Get details of telemetry config.
136 * Returns detailed properties of the specified telemetry config.
137 *
138 * @param configName telemetry configName
139 * @return 200 OK with detailed properties of the specific telemetry config
140 * @onos.rsModel TelemetryConfig
141 */
142 @GET
143 @Path("{name}")
144 @Consumes(MediaType.APPLICATION_JSON)
145 @Produces(MediaType.APPLICATION_JSON)
146 public Response getConfig(@PathParam("name") String configName) {
147 log.trace(String.format(MESSAGE_CONFIG, QUERY));
148
149 final TelemetryConfig config =
150 nullIsNotFound(configService.getConfig(configName), CONFIG_NOT_FOUND);
151 final ObjectNode root = codec(TelemetryConfig.class).encode(config, this);
152 return ok(root).build();
153 }
154
155 /**
156 * Enables the telemetry configuration with the given config name.
157 *
158 * @param configName telemetry configuration name
159 * @return 200 OK with the enabled telemetry config,
160 * 400 BAD_REQUEST if the JSON is malformed,
161 * and 304 NOT_MODIFIED without removing config, due to incorrect
162 * configuration name so that we cannot find the existing config
163 */
164 @PUT
165 @Path("enable/{name}")
166 @Consumes(MediaType.APPLICATION_JSON)
167 @Produces(MediaType.APPLICATION_JSON)
168 public Response enableConfig(@PathParam("name") String configName) {
169 log.trace(String.format(MESSAGE_CONFIG, UPDATE));
170
171 TelemetryConfig config = configService.getConfig(
172 nullIsIllegal(configName, CONFIG_NAME + NOT_NULL_MESSAGE));
173
174 if (config == null) {
175 log.warn("There is no config found to enable for {}", configName);
176 return Response.notModified().build();
177 } else {
178 TelemetryConfig updatedConfig = config.updateEnabled(true);
179 configService.updateTelemetryConfig(updatedConfig);
180 return Response.ok().build();
181 }
182 }
183
184 /**
185 * Disables the telemetry configuration with the given config name.
186 *
187 * @param configName telemetry configuration name
188 * @return 200 OK with the disabled telemetry config
189 * 400 BAD_REQUEST if the JSON is malformed,
190 * and 304 NOT_MODIFIED without removing config, due to incorrect
191 * configuration name so that we cannot find the existing config
192 */
193 @PUT
194 @Path("disable/{name}")
195 @Consumes(MediaType.APPLICATION_JSON)
196 @Produces(MediaType.APPLICATION_JSON)
197 public Response disableConfig(@PathParam("name") String configName) {
198 log.trace(String.format(MESSAGE_CONFIG, UPDATE));
199
200 TelemetryConfig config = configService.getConfig(
201 nullIsIllegal(configName, CONFIG_NAME + NOT_NULL_MESSAGE));
202
203 if (config == null) {
204 log.warn("There is no config found to disable for {}", configName);
205 return Response.notModified().build();
206 } else {
207 TelemetryConfig updatedConfig = config.updateEnabled(false);
208 configService.updateTelemetryConfig(updatedConfig);
209 return Response.ok().build();
210 }
211 }
212}