blob: e468ce37fe60e19e5377f3fe15b582dd21bc4fa3 [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
Jian Lic4604302020-12-25 02:24:16 +090063 @Context
64 private UriInfo uriInfo;
65
66 /**
67 * Creates a set of KubeVirt API config from the JSON input stream.
68 *
69 * @param input KubeVirt API configs JSON input stream
70 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
71 * is malformed
72 * @onos.rsModel KubevirtApiConfig
73 */
74 @POST
75 @Consumes(MediaType.APPLICATION_JSON)
76 @Produces(MediaType.APPLICATION_JSON)
77 public Response createApiConfigs(InputStream input) {
78 log.trace(String.format(MESSAGE_CONFIG, CREATE));
79
80 KubevirtApiConfig config = readApiConfig(input);
Jian Lif868cd52021-02-19 10:28:01 +090081 KubevirtApiConfigAdminService service = get(KubevirtApiConfigAdminService.class);
82
Jian Lic4604302020-12-25 02:24:16 +090083 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
Jian Lif868cd52021-02-19 10:28:01 +0900106 KubevirtApiConfigAdminService service = get(KubevirtApiConfigAdminService.class);
Jian Lic4604302020-12-25 02:24:16 +0900107 KubevirtApiConfig existing = service.apiConfig();
108
109 if (existing == null) {
110 log.warn("There is no API configuration to delete");
111 return Response.notModified().build();
112 } else {
113 if (endpoint.equals(endpoint(existing))) {
114 service.removeApiConfig(endpoint);
115 } else {
116 log.warn("There is no API configuration to delete for endpoint {}", endpoint);
117 }
118 }
119
120 return Response.noContent().build();
121 }
122
123 private KubevirtApiConfig readApiConfig(InputStream input) {
124 KubevirtApiConfig config;
125 try {
126 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
127 ObjectNode objectNode = jsonTree.deepCopy();
128 config = codec(KubevirtApiConfig.class).decode(objectNode, this);
129 } catch (Exception e) {
130 throw new IllegalArgumentException(e);
131 }
132
133 return config;
134 }
135}