Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnetworking.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 21 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.PUT; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.net.URI; |
| 39 | import java.net.URISyntaxException; |
| 40 | |
| 41 | import static org.onlab.util.Tools.nullIsNotFound; |
| 42 | import static org.onlab.util.Tools.readTreeFromStream; |
| 43 | |
| 44 | /** |
| 45 | * Handles REST API call from CNI plugin. |
| 46 | */ |
| 47 | @Path("network") |
| 48 | public 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 Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 58 | /** |
| 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 Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 71 | KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class); |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 72 | URI location; |
| 73 | |
| 74 | try { |
| 75 | ObjectNode jsonTree = readTreeFromStream(mapper(), input); |
| 76 | final KubevirtNetwork network = |
| 77 | codec(KubevirtNetwork.class).decode(jsonTree, this); |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 78 | service.createNetwork(network); |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 79 | 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 Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 102 | KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class); |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 103 | |
| 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 Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 114 | service.updateNetwork(network); |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 115 | } 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 Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 132 | KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class); |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 133 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 134 | service.removeNetwork(id); |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 135 | 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 Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 149 | KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class); |
| 150 | |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 151 | ObjectNode root = mapper().createObjectNode(); |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 152 | KubevirtNetwork network = service.network(id); |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 153 | |
| 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 Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 172 | KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class); |
| 173 | final Iterable<KubevirtNetwork> networks = service.networks(); |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 174 | 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 Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 188 | KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class); |
| 189 | final KubevirtNetwork network = nullIsNotFound(service.network(id), |
Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 190 | NETWORK_NOT_FOUND + id); |
| 191 | return ok(codec(KubevirtNetwork.class).encode(network, this)).build(); |
| 192 | } |
| 193 | } |