blob: dd9ac56e21ceeadee73f73ea988f7424394dd46f [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;
Jian Li65cb23d2019-03-13 19:31:27 +090022import org.onosproject.openstacknode.api.NodeState;
sanghof8164112017-07-14 14:33:16 +090023import org.onosproject.openstacknode.api.OpenstackNode;
24import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
25import org.onosproject.openstacknode.api.OpenstackNodeService;
sanghof8164112017-07-14 14:33:16 +090026import org.onosproject.rest.AbstractWebResource;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import javax.ws.rs.Consumes;
31import javax.ws.rs.DELETE;
Jian Li901462a2019-02-20 18:50:30 +090032import javax.ws.rs.GET;
sanghof8164112017-07-14 14:33:16 +090033import javax.ws.rs.POST;
34import javax.ws.rs.PUT;
35import javax.ws.rs.Path;
Jian Lib959d012018-02-19 15:27:58 +090036import javax.ws.rs.PathParam;
sanghof8164112017-07-14 14:33:16 +090037import javax.ws.rs.Produces;
38import javax.ws.rs.core.Context;
39import javax.ws.rs.core.MediaType;
40import javax.ws.rs.core.Response;
41import javax.ws.rs.core.UriBuilder;
42import javax.ws.rs.core.UriInfo;
43import java.io.InputStream;
44import java.util.Set;
45
46import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
47import static javax.ws.rs.core.Response.created;
Jian Lib959d012018-02-19 15:27:58 +090048import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkey86ee5e82018-04-02 15:33:07 -070049import static org.onlab.util.Tools.readTreeFromStream;
Jian Lid30295e2019-04-12 11:50:32 +090050import static org.onosproject.openstacknode.api.NodeState.COMPLETE;
sanghof8164112017-07-14 14:33:16 +090051
Jian Lif65d72e2018-02-13 13:01:06 +090052/**
53 * Handles REST API call of openstack node config.
54 */
55
sanghof8164112017-07-14 14:33:16 +090056@Path("configure")
57public class OpenstackNodeWebResource extends AbstractWebResource {
58 private final Logger log = LoggerFactory.getLogger(getClass());
59
60 private static final String MESSAGE_NODE = "Received node %s request";
61 private static final String NODES = "nodes";
daniel parkde735852017-08-01 19:13:24 +090062 private static final String CREATE = "CREATE";
63 private static final String UPDATE = "UPDATE";
64 private static final String NODE_ID = "NODE_ID";
65 private static final String DELETE = "DELETE";
Jian Li901462a2019-02-20 18:50:30 +090066 private static final String QUERY = "QUERY";
Jian Li65cb23d2019-03-13 19:31:27 +090067 private static final String INIT = "INIT";
Daniel Park0ba22e42019-04-11 18:22:50 +090068 private static final String NOT_EXIST = "Not exist";
69 private static final String STATE = "State";
sanghof8164112017-07-14 14:33:16 +090070
Jian Lib959d012018-02-19 15:27:58 +090071 private static final String HOST_NAME = "hostname";
72 private static final String ERROR_MESSAGE = " cannot be null";
73
Daniel Park0ba22e42019-04-11 18:22:50 +090074 private final ObjectNode root = mapper().createObjectNode();
Jian Li901462a2019-02-20 18:50:30 +090075 private final ArrayNode osJsonNodes = root.putArray("nodes");
76
sanghof8164112017-07-14 14:33:16 +090077 private final OpenstackNodeAdminService osNodeAdminService =
Jian Lic84e3f12018-02-13 15:12:18 +090078 get(OpenstackNodeAdminService.class);
sanghof8164112017-07-14 14:33:16 +090079 private final OpenstackNodeService osNodeService =
Jian Lic84e3f12018-02-13 15:12:18 +090080 get(OpenstackNodeService.class);
sanghof8164112017-07-14 14:33:16 +090081
82 @Context
83 private UriInfo uriInfo;
84
Jian Lif65d72e2018-02-13 13:01:06 +090085 /**
86 * Creates a set of openstack nodes' config from the JSON input stream.
87 *
88 * @param input openstack nodes JSON input stream
89 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
90 * is malformed
91 * @onos.rsModel OpenstackNode
92 */
sanghof8164112017-07-14 14:33:16 +090093 @POST
94 @Consumes(MediaType.APPLICATION_JSON)
95 @Produces(MediaType.APPLICATION_JSON)
96 public Response createNodes(InputStream input) {
Jian Li3423ba12019-09-24 01:01:24 +090097 log.info(String.format(MESSAGE_NODE, CREATE));
sanghof8164112017-07-14 14:33:16 +090098
99 readNodeConfiguration(input).forEach(osNode -> {
100 OpenstackNode existing = osNodeService.node(osNode.hostname());
101 if (existing == null) {
102 osNodeAdminService.createNode(osNode);
103 }
104 });
105
106 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
107 .path(NODES)
daniel parkde735852017-08-01 19:13:24 +0900108 .path(NODE_ID);
sanghof8164112017-07-14 14:33:16 +0900109
110 return created(locationBuilder.build()).build();
111 }
112
Jian Lif65d72e2018-02-13 13:01:06 +0900113 /**
Jian Li7fe7eaf2018-12-31 17:00:33 +0900114 * Updates a set of openstack nodes' config from the JSON input stream.
Jian Lif65d72e2018-02-13 13:01:06 +0900115 *
116 * @param input openstack nodes JSON input stream
117 * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST
Jian Lib959d012018-02-19 15:27:58 +0900118 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900119 * @onos.rsModel OpenstackNode
120 */
sanghof8164112017-07-14 14:33:16 +0900121 @PUT
122 @Consumes(MediaType.APPLICATION_JSON)
123 @Produces(MediaType.APPLICATION_JSON)
124 public Response updateNodes(InputStream input) {
Jian Li3423ba12019-09-24 01:01:24 +0900125 log.info(String.format(MESSAGE_NODE, UPDATE));
sanghof8164112017-07-14 14:33:16 +0900126
127 Set<OpenstackNode> nodes = readNodeConfiguration(input);
128 for (OpenstackNode osNode: nodes) {
129 OpenstackNode existing = osNodeService.node(osNode.hostname());
130 if (existing == null) {
131 log.warn("There is no node configuration to update : {}", osNode.hostname());
132 return Response.notModified().build();
133 } else if (!existing.equals(osNode)) {
134 osNodeAdminService.updateNode(osNode);
135 }
136 }
137
138 return Response.ok().build();
139 }
140
Jian Lif65d72e2018-02-13 13:01:06 +0900141 /**
142 * Removes a set of openstack nodes' config from the JSON input stream.
143 *
Jian Lib959d012018-02-19 15:27:58 +0900144 * @param hostname host name contained in openstack nodes configuration
145 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
146 * 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900147 * @onos.rsModel OpenstackNode
148 */
sanghof8164112017-07-14 14:33:16 +0900149 @DELETE
150 @Consumes(MediaType.APPLICATION_JSON)
151 @Produces(MediaType.APPLICATION_JSON)
Jian Lib959d012018-02-19 15:27:58 +0900152 @Path("{hostname}")
153 public Response deleteNodes(@PathParam("hostname") String hostname) {
Jian Li3423ba12019-09-24 01:01:24 +0900154 log.info(String.format(MESSAGE_NODE, DELETE));
sanghof8164112017-07-14 14:33:16 +0900155
Jian Lib959d012018-02-19 15:27:58 +0900156 OpenstackNode existing =
157 osNodeService.node(nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
158
159 if (existing == null) {
160 log.warn("There is no node configuration to delete : {}", hostname);
161 return Response.notModified().build();
162 } else {
163 osNodeAdminService.removeNode(hostname);
sanghof8164112017-07-14 14:33:16 +0900164 }
165
Jian Lib959d012018-02-19 15:27:58 +0900166 return Response.noContent().build();
sanghof8164112017-07-14 14:33:16 +0900167 }
168
Jian Li901462a2019-02-20 18:50:30 +0900169 /**
170 * Obtains a set of openstack node's.
171 *
172 * @return 200 OK with array of all the openstack nodes stored in the system
173 * @onos.rsModel OpenstackNode
174 */
175 @GET
176 @Produces(MediaType.APPLICATION_JSON)
177 public Response nodes() {
178 log.trace(String.format(MESSAGE_NODE, QUERY));
179
180 Set<OpenstackNode> osNodes = osNodeService.nodes();
181
182 for (OpenstackNode osNode : osNodes) {
183 osJsonNodes.add(codec(OpenstackNode.class).encode(osNode, this));
184 }
185 return ok(root).build();
186 }
187
Jian Li65cb23d2019-03-13 19:31:27 +0900188 /**
Daniel Park0ba22e42019-04-11 18:22:50 +0900189 * Obtains the state of the openstack node.
190 *
191 * @param hostname hostname of the openstack
192 * @return the state of the openstack node in Json
193 */
194 @GET
195 @Produces(MediaType.APPLICATION_JSON)
196 @Path("state/{hostname}")
197 public Response stateOfNode(@PathParam("hostname") String hostname) {
198 log.trace(String.format(MESSAGE_NODE, QUERY));
199
200 OpenstackNode osNode = osNodeService.node(hostname);
201 String nodeState = osNode != null ? osNode.state().toString() : NOT_EXIST;
202
203 return ok(mapper().createObjectNode().put(STATE, nodeState)).build();
204 }
205
206 /**
Jian Li65cb23d2019-03-13 19:31:27 +0900207 * Initializes openstack node.
208 *
209 * @param hostname hostname of openstack node
210 * @return 200 OK with init result, 404 not found, 500 server error
211 */
212 @GET
213 @Produces(MediaType.APPLICATION_JSON)
214 @Path("init/node/{hostname}")
215 public Response initNode(@PathParam("hostname") String hostname) {
Jian Li3423ba12019-09-24 01:01:24 +0900216 log.info(String.format(MESSAGE_NODE, INIT));
Jian Li65cb23d2019-03-13 19:31:27 +0900217
218 OpenstackNode osNode = osNodeService.node(hostname);
219 if (osNode == null) {
220 log.error("Given node {} does not exist", hostname);
221 return Response.serverError().build();
222 }
223 OpenstackNode updated = osNode.updateState(NodeState.INIT);
224 osNodeAdminService.updateNode(updated);
225 return ok(mapper().createObjectNode()).build();
226 }
227
228 /**
229 * Initializes all openstack nodes.
230 *
231 * @return 200 OK with init result, 500 server error
232 */
233 @GET
234 @Produces(MediaType.APPLICATION_JSON)
235 @Path("init/all")
236 public Response initAllNodes() {
Jian Li3423ba12019-09-24 01:01:24 +0900237 log.info(String.format(MESSAGE_NODE, INIT));
Jian Li65cb23d2019-03-13 19:31:27 +0900238
239 osNodeService.nodes()
240 .forEach(n -> {
241 OpenstackNode updated = n.updateState(NodeState.INIT);
242 osNodeAdminService.updateNode(updated);
243 });
244
245 return ok(mapper().createObjectNode()).build();
246 }
247
248 /**
Jian Lid30295e2019-04-12 11:50:32 +0900249 * Initializes openstack nodes which are in the stats other than COMPLETE.
Jian Li65cb23d2019-03-13 19:31:27 +0900250 *
251 * @return 200 OK with init result, 500 server error
252 */
253 @GET
254 @Produces(MediaType.APPLICATION_JSON)
255 @Path("init/incomplete")
256 public Response initIncompleteNodes() {
Jian Li3423ba12019-09-24 01:01:24 +0900257 log.info(String.format(MESSAGE_NODE, INIT));
Jian Li65cb23d2019-03-13 19:31:27 +0900258
259 osNodeService.nodes().stream()
Jian Lid30295e2019-04-12 11:50:32 +0900260 .filter(n -> n.state() != COMPLETE)
Jian Li65cb23d2019-03-13 19:31:27 +0900261 .forEach(n -> {
Daniel Parkf8f4f202019-09-15 16:38:06 +0900262 log.info("Node {} isn't COMPLETE state so performs initialization again.",
263 n.hostname());
Jian Li65cb23d2019-03-13 19:31:27 +0900264 OpenstackNode updated = n.updateState(NodeState.INIT);
265 osNodeAdminService.updateNode(updated);
266 });
267
268 return ok(mapper().createObjectNode()).build();
269 }
270
sanghof8164112017-07-14 14:33:16 +0900271 private Set<OpenstackNode> readNodeConfiguration(InputStream input) {
272 Set<OpenstackNode> nodeSet = Sets.newHashSet();
273 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700274 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
daniel parkde735852017-08-01 19:13:24 +0900275 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
sanghof8164112017-07-14 14:33:16 +0900276 nodes.forEach(node -> {
Jian Li23c8be22018-02-13 11:34:15 +0900277 ObjectNode objectNode = node.deepCopy();
278 OpenstackNode openstackNode =
279 codec(OpenstackNode.class).decode(objectNode, this);
daniel parkb18424c2018-02-05 15:43:43 +0900280
Jian Li23c8be22018-02-13 11:34:15 +0900281 nodeSet.add(openstackNode);
sanghof8164112017-07-14 14:33:16 +0900282 });
283 } catch (Exception e) {
284 throw new IllegalArgumentException(e);
285 }
286
287 return nodeSet;
288 }
289}