blob: 41d3f33ec28d52540b094721ced218ff609b9a9a [file] [log] [blame]
sanghof8164112017-07-14 14:33:16 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
sanghof8164112017-07-14 14:33:16 +09003 *
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.openstacknode.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Li23c8be22018-02-13 11:34:15 +090020import com.fasterxml.jackson.databind.node.ObjectNode;
sanghof8164112017-07-14 14:33:16 +090021import com.google.common.collect.Sets;
sanghof8164112017-07-14 14:33:16 +090022import org.onosproject.openstacknode.api.OpenstackNode;
23import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
24import org.onosproject.openstacknode.api.OpenstackNodeService;
sanghof8164112017-07-14 14:33:16 +090025import org.onosproject.rest.AbstractWebResource;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import javax.ws.rs.Consumes;
30import javax.ws.rs.DELETE;
Jian Li901462a2019-02-20 18:50:30 +090031import javax.ws.rs.GET;
sanghof8164112017-07-14 14:33:16 +090032import javax.ws.rs.POST;
33import javax.ws.rs.PUT;
34import javax.ws.rs.Path;
Jian Lib959d012018-02-19 15:27:58 +090035import javax.ws.rs.PathParam;
sanghof8164112017-07-14 14:33:16 +090036import javax.ws.rs.Produces;
37import javax.ws.rs.core.Context;
38import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
40import javax.ws.rs.core.UriBuilder;
41import javax.ws.rs.core.UriInfo;
42import java.io.InputStream;
43import java.util.Set;
44
45import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
46import static javax.ws.rs.core.Response.created;
Jian Lib959d012018-02-19 15:27:58 +090047import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkey86ee5e82018-04-02 15:33:07 -070048import static org.onlab.util.Tools.readTreeFromStream;
sanghof8164112017-07-14 14:33:16 +090049
Jian Lif65d72e2018-02-13 13:01:06 +090050/**
51 * Handles REST API call of openstack node config.
52 */
53
sanghof8164112017-07-14 14:33:16 +090054@Path("configure")
55public class OpenstackNodeWebResource extends AbstractWebResource {
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
58 private static final String MESSAGE_NODE = "Received node %s request";
59 private static final String NODES = "nodes";
daniel parkde735852017-08-01 19:13:24 +090060 private static final String CREATE = "CREATE";
61 private static final String UPDATE = "UPDATE";
62 private static final String NODE_ID = "NODE_ID";
63 private static final String DELETE = "DELETE";
Jian Li901462a2019-02-20 18:50:30 +090064 private static final String QUERY = "QUERY";
sanghof8164112017-07-14 14:33:16 +090065
Jian Lib959d012018-02-19 15:27:58 +090066 private static final String HOST_NAME = "hostname";
67 private static final String ERROR_MESSAGE = " cannot be null";
68
Jian Li901462a2019-02-20 18:50:30 +090069 private final ObjectNode root = mapper().createObjectNode();
70 private final ArrayNode osJsonNodes = root.putArray("nodes");
71
sanghof8164112017-07-14 14:33:16 +090072 private final OpenstackNodeAdminService osNodeAdminService =
Jian Lic84e3f12018-02-13 15:12:18 +090073 get(OpenstackNodeAdminService.class);
sanghof8164112017-07-14 14:33:16 +090074 private final OpenstackNodeService osNodeService =
Jian Lic84e3f12018-02-13 15:12:18 +090075 get(OpenstackNodeService.class);
sanghof8164112017-07-14 14:33:16 +090076
77 @Context
78 private UriInfo uriInfo;
79
Jian Lif65d72e2018-02-13 13:01:06 +090080 /**
81 * Creates a set of openstack nodes' config from the JSON input stream.
82 *
83 * @param input openstack nodes JSON input stream
84 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
85 * is malformed
86 * @onos.rsModel OpenstackNode
87 */
sanghof8164112017-07-14 14:33:16 +090088 @POST
89 @Consumes(MediaType.APPLICATION_JSON)
90 @Produces(MediaType.APPLICATION_JSON)
91 public Response createNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +090092 log.trace(String.format(MESSAGE_NODE, CREATE));
sanghof8164112017-07-14 14:33:16 +090093
94 readNodeConfiguration(input).forEach(osNode -> {
95 OpenstackNode existing = osNodeService.node(osNode.hostname());
96 if (existing == null) {
97 osNodeAdminService.createNode(osNode);
98 }
99 });
100
101 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
102 .path(NODES)
daniel parkde735852017-08-01 19:13:24 +0900103 .path(NODE_ID);
sanghof8164112017-07-14 14:33:16 +0900104
105 return created(locationBuilder.build()).build();
106 }
107
Jian Lif65d72e2018-02-13 13:01:06 +0900108 /**
Jian Li7fe7eaf2018-12-31 17:00:33 +0900109 * Updates a set of openstack nodes' config from the JSON input stream.
Jian Lif65d72e2018-02-13 13:01:06 +0900110 *
111 * @param input openstack nodes JSON input stream
112 * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST
Jian Lib959d012018-02-19 15:27:58 +0900113 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900114 * @onos.rsModel OpenstackNode
115 */
sanghof8164112017-07-14 14:33:16 +0900116 @PUT
117 @Consumes(MediaType.APPLICATION_JSON)
118 @Produces(MediaType.APPLICATION_JSON)
119 public Response updateNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +0900120 log.trace(String.format(MESSAGE_NODE, UPDATE));
sanghof8164112017-07-14 14:33:16 +0900121
122 Set<OpenstackNode> nodes = readNodeConfiguration(input);
123 for (OpenstackNode osNode: nodes) {
124 OpenstackNode existing = osNodeService.node(osNode.hostname());
125 if (existing == null) {
126 log.warn("There is no node configuration to update : {}", osNode.hostname());
127 return Response.notModified().build();
128 } else if (!existing.equals(osNode)) {
129 osNodeAdminService.updateNode(osNode);
130 }
131 }
132
133 return Response.ok().build();
134 }
135
Jian Lif65d72e2018-02-13 13:01:06 +0900136 /**
137 * Removes a set of openstack nodes' config from the JSON input stream.
138 *
Jian Lib959d012018-02-19 15:27:58 +0900139 * @param hostname host name contained in openstack nodes configuration
140 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
141 * 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900142 * @onos.rsModel OpenstackNode
143 */
sanghof8164112017-07-14 14:33:16 +0900144 @DELETE
145 @Consumes(MediaType.APPLICATION_JSON)
146 @Produces(MediaType.APPLICATION_JSON)
Jian Lib959d012018-02-19 15:27:58 +0900147 @Path("{hostname}")
148 public Response deleteNodes(@PathParam("hostname") String hostname) {
daniel parkde735852017-08-01 19:13:24 +0900149 log.trace(String.format(MESSAGE_NODE, DELETE));
sanghof8164112017-07-14 14:33:16 +0900150
Jian Lib959d012018-02-19 15:27:58 +0900151 OpenstackNode existing =
152 osNodeService.node(nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
153
154 if (existing == null) {
155 log.warn("There is no node configuration to delete : {}", hostname);
156 return Response.notModified().build();
157 } else {
158 osNodeAdminService.removeNode(hostname);
sanghof8164112017-07-14 14:33:16 +0900159 }
160
Jian Lib959d012018-02-19 15:27:58 +0900161 return Response.noContent().build();
sanghof8164112017-07-14 14:33:16 +0900162 }
163
Jian Li901462a2019-02-20 18:50:30 +0900164 /**
165 * Obtains a set of openstack node's.
166 *
167 * @return 200 OK with array of all the openstack nodes stored in the system
168 * @onos.rsModel OpenstackNode
169 */
170 @GET
171 @Produces(MediaType.APPLICATION_JSON)
172 public Response nodes() {
173 log.trace(String.format(MESSAGE_NODE, QUERY));
174
175 Set<OpenstackNode> osNodes = osNodeService.nodes();
176
177 for (OpenstackNode osNode : osNodes) {
178 osJsonNodes.add(codec(OpenstackNode.class).encode(osNode, this));
179 }
180 return ok(root).build();
181 }
182
sanghof8164112017-07-14 14:33:16 +0900183 private Set<OpenstackNode> readNodeConfiguration(InputStream input) {
184 Set<OpenstackNode> nodeSet = Sets.newHashSet();
185 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700186 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
daniel parkde735852017-08-01 19:13:24 +0900187 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
sanghof8164112017-07-14 14:33:16 +0900188 nodes.forEach(node -> {
Jian Li23c8be22018-02-13 11:34:15 +0900189 ObjectNode objectNode = node.deepCopy();
190 OpenstackNode openstackNode =
191 codec(OpenstackNode.class).decode(objectNode, this);
daniel parkb18424c2018-02-05 15:43:43 +0900192
Jian Li23c8be22018-02-13 11:34:15 +0900193 nodeSet.add(openstackNode);
sanghof8164112017-07-14 14:33:16 +0900194 });
195 } catch (Exception e) {
196 throw new IllegalArgumentException(e);
197 }
198
199 return nodeSet;
200 }
201}