blob: 8ab531f3a0dfb755a234214cb4654d519cd74521 [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
58 private final KubevirtNetworkAdminService adminService =
59 get(KubevirtNetworkAdminService.class);
60
61 /**
62 * Creates a network from the JSON input stream.
63 *
64 * @param input network JSON input stream
65 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
66 * is invalid or duplicated network already exists
67 * @onos.rsModel KubevirtNetwork
68 */
69 @POST
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
72 public Response createNetwork(InputStream input) {
73 log.trace(String.format(MESSAGE, "CREATE"));
74 URI location;
75
76 try {
77 ObjectNode jsonTree = readTreeFromStream(mapper(), input);
78 final KubevirtNetwork network =
79 codec(KubevirtNetwork.class).decode(jsonTree, this);
80 adminService.createNetwork(network);
81 location = new URI(network.networkId());
82 } catch (IOException | URISyntaxException e) {
83 throw new IllegalArgumentException(e);
84 }
85
86 return Response.created(location).build();
87 }
88
89 /**
90 * Updates the network with the specified identifier.
91 *
92 * @param id network identifier
93 * @param input network JSON input stream
94 * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
95 * network does not exist
96 * @onos.rsModel KubevirtNetwork
97 */
98 @PUT
99 @Path("{id}")
100 @Consumes(MediaType.APPLICATION_JSON)
101 @Produces(MediaType.APPLICATION_JSON)
102 public Response updateNetwork(@PathParam("id") String id, InputStream input) {
103 log.trace(String.format(MESSAGE, "UPDATED"));
104
105 try {
106 ObjectNode jsonTree = readTreeFromStream(mapper(), input);
107 JsonNode specifiedNetworkId = jsonTree.get("networkId");
108
109 if (specifiedNetworkId != null && !specifiedNetworkId.asText().equals(id)) {
110 throw new IllegalArgumentException(NETWORK_INVALID);
111 }
112
113 final KubevirtNetwork network =
114 codec(KubevirtNetwork.class).decode(jsonTree, this);
115 adminService.updateNetwork(network);
116 } catch (IOException e) {
117 throw new IllegalArgumentException(e);
118 }
119
120 return Response.ok().build();
121 }
122
123 /**
124 * Removes the network with the given id.
125 *
126 * @param id network identifier
127 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist
128 */
129 @DELETE
130 @Path("{id}")
131 public Response removeNetwork(@PathParam("id") String id) {
132 log.trace(String.format(MESSAGE, "DELETE " + id));
133
134 adminService.removeNetwork(id);
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
149 ObjectNode root = mapper().createObjectNode();
150 KubevirtNetwork network = adminService.network(id);
151
152 if (network == null) {
153 root.put(RESULT, false);
154 } else {
155 root.put(RESULT, true);
156 }
157
158 return Response.ok(root).build();
159 }
160
161 /**
162 * Returns set of all networks.
163 *
164 * @return 200 OK with set of all networks
165 * @onos.rsModel KubevirtNetworks
166 */
167 @GET
168 @Produces(MediaType.APPLICATION_JSON)
169 public Response getNetworks() {
170 final Iterable<KubevirtNetwork> networks = adminService.networks();
171 return ok(encodeArray(KubevirtNetwork.class, "networks", networks)).build();
172 }
173
174 /**
175 * Returns the network with the specified identifier.
176 *
177 * @param id network identifier
178 * @return 200 OK with a network, 404 not found
179 * @onos.rsModel KubevirtNetwork
180 */
181 @GET
182 @Produces(MediaType.APPLICATION_JSON)
183 @Path("{id}")
184 public Response getNetworkById(@PathParam("id") String id) {
185 final KubevirtNetwork network = nullIsNotFound(adminService.network(id),
186 NETWORK_NOT_FOUND + id);
187 return ok(codec(KubevirtNetwork.class).encode(network, this)).build();
188 }
189}