blob: 1c6985bb26a9c3b35a040955e2f4a4678f5a3098 [file] [log] [blame]
Jian Lic4604302020-12-25 02:24:16 +09001/*
2 * Copyright 2020-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.kubevirtnode.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
21import org.onosproject.kubevirtnode.api.KubevirtApiConfigAdminService;
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.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.Context;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import javax.ws.rs.core.UriBuilder;
36import javax.ws.rs.core.UriInfo;
37import java.io.InputStream;
38
39import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
40import static javax.ws.rs.core.Response.created;
41import static org.onlab.util.Tools.readTreeFromStream;
42import static org.onosproject.kubevirtnode.util.KubevirtNodeUtil.endpoint;
43
44/**
45 * Handles REST API call of kubernetes node config.
46 */
47@Path("api-config")
48public class KubevirtApiConfigWebResource extends AbstractWebResource {
49
50 private final Logger log = LoggerFactory.getLogger(getClass());
51
52 private static final String MESSAGE_CONFIG = "Received API config %s request";
53
54 private static final String CREATE = "CREATE";
55 private static final String UPDATE = "UPDATE";
56 private static final String REMOVE = "REMOVE";
57 private static final String QUERY = "QUERY";
58 private static final String API_CONFIG = "apiConfig";
59
60 private static final String ENDPOINT = "endpoint";
61 private static final String ERROR_MESSAGE = " cannot be null";
62
63 private final KubevirtApiConfigAdminService service = get(KubevirtApiConfigAdminService.class);
64
65 @Context
66 private UriInfo uriInfo;
67
68 /**
69 * Creates a set of KubeVirt API config from the JSON input stream.
70 *
71 * @param input KubeVirt API configs JSON input stream
72 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
73 * is malformed
74 * @onos.rsModel KubevirtApiConfig
75 */
76 @POST
77 @Consumes(MediaType.APPLICATION_JSON)
78 @Produces(MediaType.APPLICATION_JSON)
79 public Response createApiConfigs(InputStream input) {
80 log.trace(String.format(MESSAGE_CONFIG, CREATE));
81
82 KubevirtApiConfig config = readApiConfig(input);
83 if (config != null) {
84 service.createApiConfig(config);
85 }
86
87 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
88 .path(API_CONFIG);
89
90 return created(locationBuilder.build()).build();
91 }
92
93 /**
94 * Removes a KubeVirt API config.
95 *
96 * @param endpoint KubeVirt API endpoint
97 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed
98 */
99 @DELETE
100 @Path("{endpoint : .+}")
101 @Consumes(MediaType.APPLICATION_JSON)
102 @Produces(MediaType.APPLICATION_JSON)
103 public Response deleteApiConfig(@PathParam("endpoint") String endpoint) {
104 log.trace(String.format(MESSAGE_CONFIG, REMOVE));
105
106 KubevirtApiConfig existing = service.apiConfig();
107
108 if (existing == null) {
109 log.warn("There is no API configuration to delete");
110 return Response.notModified().build();
111 } else {
112 if (endpoint.equals(endpoint(existing))) {
113 service.removeApiConfig(endpoint);
114 } else {
115 log.warn("There is no API configuration to delete for endpoint {}", endpoint);
116 }
117 }
118
119 return Response.noContent().build();
120 }
121
122 private KubevirtApiConfig readApiConfig(InputStream input) {
123 KubevirtApiConfig config;
124 try {
125 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
126 ObjectNode objectNode = jsonTree.deepCopy();
127 config = codec(KubevirtApiConfig.class).decode(objectNode, this);
128 } catch (Exception e) {
129 throw new IllegalArgumentException(e);
130 }
131
132 return config;
133 }
134}