Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snetworking.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.k8snetworking.api.K8sNetwork; |
| 21 | import org.onosproject.k8snetworking.api.K8sNetworkAdminService; |
| 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; |
Jian Li | ec0f748 | 2019-02-12 13:14:29 +0900 | [diff] [blame] | 28 | import javax.ws.rs.GET; |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 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.readTreeFromStream; |
| 42 | |
| 43 | /** |
| 44 | * Handles REST API call from CNI plugin. |
| 45 | */ |
| 46 | @Path("network") |
| 47 | public class K8sNetworkWebResource extends AbstractWebResource { |
| 48 | |
| 49 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
| 51 | private static final String MESSAGE = "Received network %s request"; |
| 52 | private static final String NETWORK_INVALID = "Invalid networkId in network update request"; |
| 53 | |
Jian Li | ec0f748 | 2019-02-12 13:14:29 +0900 | [diff] [blame] | 54 | private static final String RESULT = "result"; |
| 55 | |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 56 | private final K8sNetworkAdminService adminService = get(K8sNetworkAdminService.class); |
| 57 | |
| 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 K8sNetwork |
| 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")); |
| 71 | URI location; |
| 72 | |
| 73 | try { |
| 74 | ObjectNode jsonTree = readTreeFromStream(mapper(), input); |
| 75 | final K8sNetwork network = codec(K8sNetwork.class).decode(jsonTree, this); |
| 76 | adminService.createNetwork(network); |
| 77 | location = new URI(network.networkId()); |
| 78 | } catch (IOException | URISyntaxException e) { |
| 79 | throw new IllegalArgumentException(e); |
| 80 | } |
| 81 | |
| 82 | return Response.created(location).build(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Updates the network with the specified identifier. |
| 87 | * |
| 88 | * @param id network identifier |
| 89 | * @param input network JSON input stream |
| 90 | * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested |
| 91 | * network does not exist |
| 92 | * @onos.rsModel K8sNetwork |
| 93 | */ |
| 94 | @PUT |
| 95 | @Path("{id}") |
| 96 | @Consumes(MediaType.APPLICATION_JSON) |
| 97 | @Produces(MediaType.APPLICATION_JSON) |
| 98 | public Response updateNetwork(@PathParam("id") String id, InputStream input) { |
| 99 | log.trace(String.format(MESSAGE, "UPDATED")); |
| 100 | |
| 101 | try { |
| 102 | ObjectNode jsonTree = readTreeFromStream(mapper(), input); |
| 103 | JsonNode specifiedNetworkId = jsonTree.get("networkId"); |
| 104 | |
| 105 | if (specifiedNetworkId != null && !specifiedNetworkId.asText().equals(id)) { |
| 106 | throw new IllegalArgumentException(NETWORK_INVALID); |
| 107 | } |
| 108 | |
| 109 | final K8sNetwork network = codec(K8sNetwork.class).decode(jsonTree, this); |
| 110 | adminService.updateNetwork(network); |
| 111 | } catch (IOException e) { |
| 112 | throw new IllegalArgumentException(e); |
| 113 | } |
| 114 | |
| 115 | return Response.ok().build(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Removes the network with the given id. |
| 120 | * |
| 121 | * @param id network identifier |
| 122 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist |
| 123 | */ |
| 124 | @DELETE |
| 125 | @Path("{id}") |
| 126 | public Response removeNetwork(@PathParam("id") String id) { |
| 127 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 128 | |
| 129 | adminService.removeNetwork(id); |
| 130 | return Response.noContent().build(); |
| 131 | } |
Jian Li | ec0f748 | 2019-02-12 13:14:29 +0900 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Checks whether the network exists with given network id. |
| 135 | * |
| 136 | * @param id network identifier |
| 137 | * @return 200 OK with true/false result |
| 138 | */ |
| 139 | @GET |
| 140 | @Path("exist/{id}") |
| 141 | public Response hasNetwork(@PathParam("id") String id) { |
| 142 | log.trace(String.format(MESSAGE, "QUERY " + id)); |
| 143 | |
| 144 | ObjectNode root = mapper().createObjectNode(); |
| 145 | K8sNetwork network = adminService.network(id); |
| 146 | |
| 147 | if (network == null) { |
| 148 | root.put(RESULT, false); |
| 149 | } else { |
| 150 | root.put(RESULT, true); |
| 151 | } |
| 152 | |
| 153 | return Response.ok(root).build(); |
| 154 | } |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 155 | } |