blob: f636b1a14fb22ce2f42243df67052488e60aab52 [file] [log] [blame]
Jian Lia80b1582019-01-25 12:47:42 +09001/*
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 */
16package org.onosproject.k8snetworking.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
21import org.onosproject.k8snetworking.api.K8sPort;
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.PUT;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.io.IOException;
36import java.io.InputStream;
37import java.net.URI;
38import java.net.URISyntaxException;
39
40import static org.onlab.util.Tools.readTreeFromStream;
41
42/**
43 * Handles port related REST API call from CNI plugin.
44 */
45@Path("port")
46public class K8sPortWebResource extends AbstractWebResource {
47
48 protected final Logger log = LoggerFactory.getLogger(getClass());
49
50 private static final String MESSAGE = "Received port %s request";
51 private static final String PORT_INVALID = "Invalid portId in port update request";
52
53 private final K8sNetworkAdminService adminService = get(K8sNetworkAdminService.class);
54
55 /**
56 * Creates a port from the JSON input stream.
57 *
58 * @param input port JSON input stream
59 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
60 * is invalid or duplicated port already exists
61 * @onos.rsModel K8sPort
62 */
63 @POST
64 @Consumes(MediaType.APPLICATION_JSON)
65 @Produces(MediaType.APPLICATION_JSON)
66 public Response createPort(InputStream input) {
67 log.trace(String.format(MESSAGE, "CREATE"));
68 URI location;
69
70 try {
71 ObjectNode jsonTree = readTreeFromStream(mapper(), input);
72 final K8sPort port = codec(K8sPort.class).decode(jsonTree, this);
73 adminService.createPort(port);
74 location = new URI(port.portId());
75 } catch (IOException | URISyntaxException e) {
76 throw new IllegalArgumentException(e);
77 }
78
79 return Response.created(location).build();
80 }
81
82 /**
83 * Updates the port with the specified identifier.
84 *
85 * @param id port identifier
86 * @param input port JSON input stream
87 * @return 200 OK with the updated port, 400 BAD_REQUEST if the requested
88 * port does not exist
89 * @onos.rsModel K8sPort
90 */
91 @PUT
92 @Path("{id}")
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces(MediaType.APPLICATION_JSON)
95 public Response updatePort(@PathParam("id") String id, InputStream input) {
96 log.trace(String.format(MESSAGE, "UPDATED"));
97
98 try {
99 ObjectNode jsonTree = readTreeFromStream(mapper(), input);
100 JsonNode specifiedPortId = jsonTree.get("portId");
101
102 if (specifiedPortId != null && !specifiedPortId.asText().equals(id)) {
103 throw new IllegalArgumentException(PORT_INVALID);
104 }
105
106 final K8sPort port = codec(K8sPort.class).decode(jsonTree, this);
107 adminService.updatePort(port);
108 } catch (IOException e) {
109 throw new IllegalArgumentException(e);
110 }
111
112 return Response.ok().build();
113 }
114
115 /**
116 * Removes the port with the given id.
117 *
118 * @param id port identifier
119 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist
120 */
121 @DELETE
122 @Path("{id}")
123 public Response removePort(@PathParam("id") String id) {
124 log.trace(String.format(MESSAGE, "DELETE " + id));
125
126 adminService.removePort(id);
127 return Response.noContent().build();
128 }
Jian Lia80b1582019-01-25 12:47:42 +0900129}