blob: 4e8f5dfd4783426c7ff45e55a5bdc69d4d6c9fce [file] [log] [blame]
Jian Li260231e2021-01-13 18:05:00 +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.kubevirtnetworking.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
21import org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService;
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.POST;
30import javax.ws.rs.PUT;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import java.io.IOException;
37import java.io.InputStream;
38import java.net.URI;
39import java.net.URISyntaxException;
40
41import static org.onlab.util.Tools.nullIsNotFound;
42import static org.onlab.util.Tools.readTreeFromStream;
43
44/**
45 * Handles REST API call from CNI plugin.
46 */
47@Path("network")
48public class KubevirtNetworkWebResource extends AbstractWebResource {
49
50 protected final Logger log = LoggerFactory.getLogger(getClass());
51
52 private static final String MESSAGE = "Received network %s request";
53 private static final String NETWORK_NOT_FOUND = "Network is not found for";
54 private static final String NETWORK_INVALID = "Invalid networkId in network update request";
55
56 private static final String RESULT = "result";
57
Jian Li260231e2021-01-13 18:05:00 +090058 /**
59 * Creates a network from the JSON input stream.
60 *
61 * @param input network JSON input stream
62 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
63 * is invalid or duplicated network already exists
64 * @onos.rsModel KubevirtNetwork
65 */
66 @POST
67 @Consumes(MediaType.APPLICATION_JSON)
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response createNetwork(InputStream input) {
70 log.trace(String.format(MESSAGE, "CREATE"));
Jian Lif868cd52021-02-19 10:28:01 +090071 KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
Jian Li260231e2021-01-13 18:05:00 +090072 URI location;
73
74 try {
75 ObjectNode jsonTree = readTreeFromStream(mapper(), input);
76 final KubevirtNetwork network =
77 codec(KubevirtNetwork.class).decode(jsonTree, this);
Jian Lif868cd52021-02-19 10:28:01 +090078 service.createNetwork(network);
Jian Li260231e2021-01-13 18:05:00 +090079 location = new URI(network.networkId());
80 } catch (IOException | URISyntaxException e) {
81 throw new IllegalArgumentException(e);
82 }
83
84 return Response.created(location).build();
85 }
86
87 /**
88 * Updates the network with the specified identifier.
89 *
90 * @param id network identifier
91 * @param input network JSON input stream
92 * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
93 * network does not exist
94 * @onos.rsModel KubevirtNetwork
95 */
96 @PUT
97 @Path("{id}")
98 @Consumes(MediaType.APPLICATION_JSON)
99 @Produces(MediaType.APPLICATION_JSON)
100 public Response updateNetwork(@PathParam("id") String id, InputStream input) {
101 log.trace(String.format(MESSAGE, "UPDATED"));
Jian Lif868cd52021-02-19 10:28:01 +0900102 KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
Jian Li260231e2021-01-13 18:05:00 +0900103
104 try {
105 ObjectNode jsonTree = readTreeFromStream(mapper(), input);
106 JsonNode specifiedNetworkId = jsonTree.get("networkId");
107
108 if (specifiedNetworkId != null && !specifiedNetworkId.asText().equals(id)) {
109 throw new IllegalArgumentException(NETWORK_INVALID);
110 }
111
112 final KubevirtNetwork network =
113 codec(KubevirtNetwork.class).decode(jsonTree, this);
Jian Lif868cd52021-02-19 10:28:01 +0900114 service.updateNetwork(network);
Jian Li260231e2021-01-13 18:05:00 +0900115 } catch (IOException e) {
116 throw new IllegalArgumentException(e);
117 }
118
119 return Response.ok().build();
120 }
121
122 /**
123 * Removes the network with the given id.
124 *
125 * @param id network identifier
126 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist
127 */
128 @DELETE
129 @Path("{id}")
130 public Response removeNetwork(@PathParam("id") String id) {
131 log.trace(String.format(MESSAGE, "DELETE " + id));
Jian Lif868cd52021-02-19 10:28:01 +0900132 KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
Jian Li260231e2021-01-13 18:05:00 +0900133
Jian Lif868cd52021-02-19 10:28:01 +0900134 service.removeNetwork(id);
Jian Li260231e2021-01-13 18:05:00 +0900135 return Response.noContent().build();
136 }
137
138 /**
139 * Checks whether the network exists with given network id.
140 *
141 * @param id network identifier
142 * @return 200 OK with true/false result
143 */
144 @GET
145 @Path("exist/{id}")
146 public Response hasNetwork(@PathParam("id") String id) {
147 log.trace(String.format(MESSAGE, "QUERY " + id));
148
Jian Lif868cd52021-02-19 10:28:01 +0900149 KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
150
Jian Li260231e2021-01-13 18:05:00 +0900151 ObjectNode root = mapper().createObjectNode();
Jian Lif868cd52021-02-19 10:28:01 +0900152 KubevirtNetwork network = service.network(id);
Jian Li260231e2021-01-13 18:05:00 +0900153
154 if (network == null) {
155 root.put(RESULT, false);
156 } else {
157 root.put(RESULT, true);
158 }
159
160 return Response.ok(root).build();
161 }
162
163 /**
164 * Returns set of all networks.
165 *
166 * @return 200 OK with set of all networks
167 * @onos.rsModel KubevirtNetworks
168 */
169 @GET
170 @Produces(MediaType.APPLICATION_JSON)
171 public Response getNetworks() {
Jian Lif868cd52021-02-19 10:28:01 +0900172 KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
173 final Iterable<KubevirtNetwork> networks = service.networks();
Jian Li260231e2021-01-13 18:05:00 +0900174 return ok(encodeArray(KubevirtNetwork.class, "networks", networks)).build();
175 }
176
177 /**
178 * Returns the network with the specified identifier.
179 *
180 * @param id network identifier
181 * @return 200 OK with a network, 404 not found
182 * @onos.rsModel KubevirtNetwork
183 */
184 @GET
185 @Produces(MediaType.APPLICATION_JSON)
186 @Path("{id}")
187 public Response getNetworkById(@PathParam("id") String id) {
Jian Lif868cd52021-02-19 10:28:01 +0900188 KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
189 final KubevirtNetwork network = nullIsNotFound(service.network(id),
Jian Li260231e2021-01-13 18:05:00 +0900190 NETWORK_NOT_FOUND + id);
191 return ok(codec(KubevirtNetwork.class).encode(network, this)).build();
192 }
193}