blob: 1c7d3ebac5782e129ede613a2cc2fd53e0f588e1 [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;
31import javax.ws.rs.POST;
32import javax.ws.rs.PUT;
33import javax.ws.rs.Path;
Jian Lib959d012018-02-19 15:27:58 +090034import javax.ws.rs.PathParam;
sanghof8164112017-07-14 14:33:16 +090035import javax.ws.rs.Produces;
36import javax.ws.rs.core.Context;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import javax.ws.rs.core.UriBuilder;
40import javax.ws.rs.core.UriInfo;
41import java.io.InputStream;
42import java.util.Set;
43
44import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
45import static javax.ws.rs.core.Response.created;
Jian Lib959d012018-02-19 15:27:58 +090046import static org.onlab.util.Tools.nullIsIllegal;
sanghof8164112017-07-14 14:33:16 +090047
Jian Lif65d72e2018-02-13 13:01:06 +090048/**
49 * Handles REST API call of openstack node config.
50 */
51
sanghof8164112017-07-14 14:33:16 +090052@Path("configure")
53public class OpenstackNodeWebResource extends AbstractWebResource {
54 private final Logger log = LoggerFactory.getLogger(getClass());
55
56 private static final String MESSAGE_NODE = "Received node %s request";
57 private static final String NODES = "nodes";
daniel parkde735852017-08-01 19:13:24 +090058 private static final String CREATE = "CREATE";
59 private static final String UPDATE = "UPDATE";
60 private static final String NODE_ID = "NODE_ID";
61 private static final String DELETE = "DELETE";
sanghof8164112017-07-14 14:33:16 +090062
Jian Lib959d012018-02-19 15:27:58 +090063 private static final String HOST_NAME = "hostname";
64 private static final String ERROR_MESSAGE = " cannot be null";
65
sanghof8164112017-07-14 14:33:16 +090066 private final OpenstackNodeAdminService osNodeAdminService =
Jian Lic84e3f12018-02-13 15:12:18 +090067 get(OpenstackNodeAdminService.class);
sanghof8164112017-07-14 14:33:16 +090068 private final OpenstackNodeService osNodeService =
Jian Lic84e3f12018-02-13 15:12:18 +090069 get(OpenstackNodeService.class);
sanghof8164112017-07-14 14:33:16 +090070
71 @Context
72 private UriInfo uriInfo;
73
Jian Lif65d72e2018-02-13 13:01:06 +090074 /**
75 * Creates a set of openstack nodes' config from the JSON input stream.
76 *
77 * @param input openstack nodes JSON input stream
78 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
79 * is malformed
80 * @onos.rsModel OpenstackNode
81 */
sanghof8164112017-07-14 14:33:16 +090082 @POST
83 @Consumes(MediaType.APPLICATION_JSON)
84 @Produces(MediaType.APPLICATION_JSON)
85 public Response createNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +090086 log.trace(String.format(MESSAGE_NODE, CREATE));
sanghof8164112017-07-14 14:33:16 +090087
88 readNodeConfiguration(input).forEach(osNode -> {
89 OpenstackNode existing = osNodeService.node(osNode.hostname());
90 if (existing == null) {
91 osNodeAdminService.createNode(osNode);
92 }
93 });
94
95 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
96 .path(NODES)
daniel parkde735852017-08-01 19:13:24 +090097 .path(NODE_ID);
sanghof8164112017-07-14 14:33:16 +090098
99 return created(locationBuilder.build()).build();
100 }
101
Jian Lif65d72e2018-02-13 13:01:06 +0900102 /**
103 * Creates a set of openstack nodes' config from the JSON input stream.
104 *
105 * @param input openstack nodes JSON input stream
106 * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST
Jian Lib959d012018-02-19 15:27:58 +0900107 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900108 * @onos.rsModel OpenstackNode
109 */
sanghof8164112017-07-14 14:33:16 +0900110 @PUT
111 @Consumes(MediaType.APPLICATION_JSON)
112 @Produces(MediaType.APPLICATION_JSON)
113 public Response updateNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +0900114 log.trace(String.format(MESSAGE_NODE, UPDATE));
sanghof8164112017-07-14 14:33:16 +0900115
116 Set<OpenstackNode> nodes = readNodeConfiguration(input);
117 for (OpenstackNode osNode: nodes) {
118 OpenstackNode existing = osNodeService.node(osNode.hostname());
119 if (existing == null) {
120 log.warn("There is no node configuration to update : {}", osNode.hostname());
121 return Response.notModified().build();
122 } else if (!existing.equals(osNode)) {
123 osNodeAdminService.updateNode(osNode);
124 }
125 }
126
127 return Response.ok().build();
128 }
129
Jian Lif65d72e2018-02-13 13:01:06 +0900130 /**
131 * Removes a set of openstack nodes' config from the JSON input stream.
132 *
Jian Lib959d012018-02-19 15:27:58 +0900133 * @param hostname host name contained in openstack nodes configuration
134 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
135 * 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900136 * @onos.rsModel OpenstackNode
137 */
sanghof8164112017-07-14 14:33:16 +0900138 @DELETE
139 @Consumes(MediaType.APPLICATION_JSON)
140 @Produces(MediaType.APPLICATION_JSON)
Jian Lib959d012018-02-19 15:27:58 +0900141 @Path("{hostname}")
142 public Response deleteNodes(@PathParam("hostname") String hostname) {
daniel parkde735852017-08-01 19:13:24 +0900143 log.trace(String.format(MESSAGE_NODE, DELETE));
sanghof8164112017-07-14 14:33:16 +0900144
Jian Lib959d012018-02-19 15:27:58 +0900145 OpenstackNode existing =
146 osNodeService.node(nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
147
148 if (existing == null) {
149 log.warn("There is no node configuration to delete : {}", hostname);
150 return Response.notModified().build();
151 } else {
152 osNodeAdminService.removeNode(hostname);
sanghof8164112017-07-14 14:33:16 +0900153 }
154
Jian Lib959d012018-02-19 15:27:58 +0900155 return Response.noContent().build();
sanghof8164112017-07-14 14:33:16 +0900156 }
157
158 private Set<OpenstackNode> readNodeConfiguration(InputStream input) {
159 Set<OpenstackNode> nodeSet = Sets.newHashSet();
160 try {
161 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
daniel parkde735852017-08-01 19:13:24 +0900162 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
sanghof8164112017-07-14 14:33:16 +0900163 nodes.forEach(node -> {
164 try {
Jian Li23c8be22018-02-13 11:34:15 +0900165 ObjectNode objectNode = node.deepCopy();
166 OpenstackNode openstackNode =
167 codec(OpenstackNode.class).decode(objectNode, this);
daniel parkb18424c2018-02-05 15:43:43 +0900168
Jian Li23c8be22018-02-13 11:34:15 +0900169 nodeSet.add(openstackNode);
sanghof8164112017-07-14 14:33:16 +0900170 } catch (Exception e) {
171 log.error(e.toString());
Jian Li23c8be22018-02-13 11:34:15 +0900172 throw new IllegalArgumentException();
sanghof8164112017-07-14 14:33:16 +0900173 }
174 });
175 } catch (Exception e) {
176 throw new IllegalArgumentException(e);
177 }
178
179 return nodeSet;
180 }
181}