blob: c64cab72b52aab4777122564378ab63974b8fac9 [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 Li65cb23d2019-03-13 19:31:27 +090050import static org.onosproject.openstacknode.api.NodeState.INCOMPLETE;
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";
sanghof8164112017-07-14 14:33:16 +090068
Jian Lib959d012018-02-19 15:27:58 +090069 private static final String HOST_NAME = "hostname";
70 private static final String ERROR_MESSAGE = " cannot be null";
71
Jian Li901462a2019-02-20 18:50:30 +090072 private final ObjectNode root = mapper().createObjectNode();
73 private final ArrayNode osJsonNodes = root.putArray("nodes");
74
sanghof8164112017-07-14 14:33:16 +090075 private final OpenstackNodeAdminService osNodeAdminService =
Jian Lic84e3f12018-02-13 15:12:18 +090076 get(OpenstackNodeAdminService.class);
sanghof8164112017-07-14 14:33:16 +090077 private final OpenstackNodeService osNodeService =
Jian Lic84e3f12018-02-13 15:12:18 +090078 get(OpenstackNodeService.class);
sanghof8164112017-07-14 14:33:16 +090079
80 @Context
81 private UriInfo uriInfo;
82
Jian Lif65d72e2018-02-13 13:01:06 +090083 /**
84 * Creates a set of openstack nodes' config from the JSON input stream.
85 *
86 * @param input openstack nodes JSON input stream
87 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
88 * is malformed
89 * @onos.rsModel OpenstackNode
90 */
sanghof8164112017-07-14 14:33:16 +090091 @POST
92 @Consumes(MediaType.APPLICATION_JSON)
93 @Produces(MediaType.APPLICATION_JSON)
94 public Response createNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +090095 log.trace(String.format(MESSAGE_NODE, CREATE));
sanghof8164112017-07-14 14:33:16 +090096
97 readNodeConfiguration(input).forEach(osNode -> {
98 OpenstackNode existing = osNodeService.node(osNode.hostname());
99 if (existing == null) {
100 osNodeAdminService.createNode(osNode);
101 }
102 });
103
104 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
105 .path(NODES)
daniel parkde735852017-08-01 19:13:24 +0900106 .path(NODE_ID);
sanghof8164112017-07-14 14:33:16 +0900107
108 return created(locationBuilder.build()).build();
109 }
110
Jian Lif65d72e2018-02-13 13:01:06 +0900111 /**
Jian Li7fe7eaf2018-12-31 17:00:33 +0900112 * Updates a set of openstack nodes' config from the JSON input stream.
Jian Lif65d72e2018-02-13 13:01:06 +0900113 *
114 * @param input openstack nodes JSON input stream
115 * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST
Jian Lib959d012018-02-19 15:27:58 +0900116 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900117 * @onos.rsModel OpenstackNode
118 */
sanghof8164112017-07-14 14:33:16 +0900119 @PUT
120 @Consumes(MediaType.APPLICATION_JSON)
121 @Produces(MediaType.APPLICATION_JSON)
122 public Response updateNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +0900123 log.trace(String.format(MESSAGE_NODE, UPDATE));
sanghof8164112017-07-14 14:33:16 +0900124
125 Set<OpenstackNode> nodes = readNodeConfiguration(input);
126 for (OpenstackNode osNode: nodes) {
127 OpenstackNode existing = osNodeService.node(osNode.hostname());
128 if (existing == null) {
129 log.warn("There is no node configuration to update : {}", osNode.hostname());
130 return Response.notModified().build();
131 } else if (!existing.equals(osNode)) {
132 osNodeAdminService.updateNode(osNode);
133 }
134 }
135
136 return Response.ok().build();
137 }
138
Jian Lif65d72e2018-02-13 13:01:06 +0900139 /**
140 * Removes a set of openstack nodes' config from the JSON input stream.
141 *
Jian Lib959d012018-02-19 15:27:58 +0900142 * @param hostname host name contained in openstack nodes configuration
143 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
144 * 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900145 * @onos.rsModel OpenstackNode
146 */
sanghof8164112017-07-14 14:33:16 +0900147 @DELETE
148 @Consumes(MediaType.APPLICATION_JSON)
149 @Produces(MediaType.APPLICATION_JSON)
Jian Lib959d012018-02-19 15:27:58 +0900150 @Path("{hostname}")
151 public Response deleteNodes(@PathParam("hostname") String hostname) {
daniel parkde735852017-08-01 19:13:24 +0900152 log.trace(String.format(MESSAGE_NODE, DELETE));
sanghof8164112017-07-14 14:33:16 +0900153
Jian Lib959d012018-02-19 15:27:58 +0900154 OpenstackNode existing =
155 osNodeService.node(nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
156
157 if (existing == null) {
158 log.warn("There is no node configuration to delete : {}", hostname);
159 return Response.notModified().build();
160 } else {
161 osNodeAdminService.removeNode(hostname);
sanghof8164112017-07-14 14:33:16 +0900162 }
163
Jian Lib959d012018-02-19 15:27:58 +0900164 return Response.noContent().build();
sanghof8164112017-07-14 14:33:16 +0900165 }
166
Jian Li901462a2019-02-20 18:50:30 +0900167 /**
168 * Obtains a set of openstack node's.
169 *
170 * @return 200 OK with array of all the openstack nodes stored in the system
171 * @onos.rsModel OpenstackNode
172 */
173 @GET
174 @Produces(MediaType.APPLICATION_JSON)
175 public Response nodes() {
176 log.trace(String.format(MESSAGE_NODE, QUERY));
177
178 Set<OpenstackNode> osNodes = osNodeService.nodes();
179
180 for (OpenstackNode osNode : osNodes) {
181 osJsonNodes.add(codec(OpenstackNode.class).encode(osNode, this));
182 }
183 return ok(root).build();
184 }
185
Jian Li65cb23d2019-03-13 19:31:27 +0900186 /**
187 * Initializes openstack node.
188 *
189 * @param hostname hostname of openstack node
190 * @return 200 OK with init result, 404 not found, 500 server error
191 */
192 @GET
193 @Produces(MediaType.APPLICATION_JSON)
194 @Path("init/node/{hostname}")
195 public Response initNode(@PathParam("hostname") String hostname) {
196 log.trace(String.format(MESSAGE_NODE, QUERY));
197
198 OpenstackNode osNode = osNodeService.node(hostname);
199 if (osNode == null) {
200 log.error("Given node {} does not exist", hostname);
201 return Response.serverError().build();
202 }
203 OpenstackNode updated = osNode.updateState(NodeState.INIT);
204 osNodeAdminService.updateNode(updated);
205 return ok(mapper().createObjectNode()).build();
206 }
207
208 /**
209 * Initializes all openstack nodes.
210 *
211 * @return 200 OK with init result, 500 server error
212 */
213 @GET
214 @Produces(MediaType.APPLICATION_JSON)
215 @Path("init/all")
216 public Response initAllNodes() {
217 log.trace(String.format(MESSAGE_NODE, QUERY));
218
219 osNodeService.nodes()
220 .forEach(n -> {
221 OpenstackNode updated = n.updateState(NodeState.INIT);
222 osNodeAdminService.updateNode(updated);
223 });
224
225 return ok(mapper().createObjectNode()).build();
226 }
227
228 /**
229 * Initializes incomplete openstack nodes.
230 *
231 * @return 200 OK with init result, 500 server error
232 */
233 @GET
234 @Produces(MediaType.APPLICATION_JSON)
235 @Path("init/incomplete")
236 public Response initIncompleteNodes() {
237 log.trace(String.format(MESSAGE_NODE, QUERY));
238
239 osNodeService.nodes().stream()
240 .filter(n -> n.state() == INCOMPLETE)
241 .forEach(n -> {
242 OpenstackNode updated = n.updateState(NodeState.INIT);
243 osNodeAdminService.updateNode(updated);
244 });
245
246 return ok(mapper().createObjectNode()).build();
247 }
248
sanghof8164112017-07-14 14:33:16 +0900249 private Set<OpenstackNode> readNodeConfiguration(InputStream input) {
250 Set<OpenstackNode> nodeSet = Sets.newHashSet();
251 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700252 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
daniel parkde735852017-08-01 19:13:24 +0900253 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
sanghof8164112017-07-14 14:33:16 +0900254 nodes.forEach(node -> {
Jian Li23c8be22018-02-13 11:34:15 +0900255 ObjectNode objectNode = node.deepCopy();
256 OpenstackNode openstackNode =
257 codec(OpenstackNode.class).decode(objectNode, this);
daniel parkb18424c2018-02-05 15:43:43 +0900258
Jian Li23c8be22018-02-13 11:34:15 +0900259 nodeSet.add(openstackNode);
sanghof8164112017-07-14 14:33:16 +0900260 });
261 } catch (Exception e) {
262 throw new IllegalArgumentException(e);
263 }
264
265 return nodeSet;
266 }
267}