blob: 1b69f30e1d4f494b563587c309913cd4754e8e39 [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;
Ray Milkey86ee5e82018-04-02 15:33:07 -070047import static org.onlab.util.Tools.readTreeFromStream;
sanghof8164112017-07-14 14:33:16 +090048
Jian Lif65d72e2018-02-13 13:01:06 +090049/**
50 * Handles REST API call of openstack node config.
51 */
52
sanghof8164112017-07-14 14:33:16 +090053@Path("configure")
54public class OpenstackNodeWebResource extends AbstractWebResource {
55 private final Logger log = LoggerFactory.getLogger(getClass());
56
57 private static final String MESSAGE_NODE = "Received node %s request";
58 private static final String NODES = "nodes";
daniel parkde735852017-08-01 19:13:24 +090059 private static final String CREATE = "CREATE";
60 private static final String UPDATE = "UPDATE";
61 private static final String NODE_ID = "NODE_ID";
62 private static final String DELETE = "DELETE";
sanghof8164112017-07-14 14:33:16 +090063
Jian Lib959d012018-02-19 15:27:58 +090064 private static final String HOST_NAME = "hostname";
65 private static final String ERROR_MESSAGE = " cannot be null";
66
sanghof8164112017-07-14 14:33:16 +090067 private final OpenstackNodeAdminService osNodeAdminService =
Jian Lic84e3f12018-02-13 15:12:18 +090068 get(OpenstackNodeAdminService.class);
sanghof8164112017-07-14 14:33:16 +090069 private final OpenstackNodeService osNodeService =
Jian Lic84e3f12018-02-13 15:12:18 +090070 get(OpenstackNodeService.class);
sanghof8164112017-07-14 14:33:16 +090071
72 @Context
73 private UriInfo uriInfo;
74
Jian Lif65d72e2018-02-13 13:01:06 +090075 /**
76 * Creates a set of openstack nodes' config from the JSON input stream.
77 *
78 * @param input openstack nodes JSON input stream
79 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
80 * is malformed
81 * @onos.rsModel OpenstackNode
82 */
sanghof8164112017-07-14 14:33:16 +090083 @POST
84 @Consumes(MediaType.APPLICATION_JSON)
85 @Produces(MediaType.APPLICATION_JSON)
86 public Response createNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +090087 log.trace(String.format(MESSAGE_NODE, CREATE));
sanghof8164112017-07-14 14:33:16 +090088
89 readNodeConfiguration(input).forEach(osNode -> {
90 OpenstackNode existing = osNodeService.node(osNode.hostname());
91 if (existing == null) {
92 osNodeAdminService.createNode(osNode);
93 }
94 });
95
96 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
97 .path(NODES)
daniel parkde735852017-08-01 19:13:24 +090098 .path(NODE_ID);
sanghof8164112017-07-14 14:33:16 +090099
100 return created(locationBuilder.build()).build();
101 }
102
Jian Lif65d72e2018-02-13 13:01:06 +0900103 /**
104 * Creates a set of openstack nodes' config from the JSON input stream.
105 *
106 * @param input openstack nodes JSON input stream
107 * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST
Jian Lib959d012018-02-19 15:27:58 +0900108 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900109 * @onos.rsModel OpenstackNode
110 */
sanghof8164112017-07-14 14:33:16 +0900111 @PUT
112 @Consumes(MediaType.APPLICATION_JSON)
113 @Produces(MediaType.APPLICATION_JSON)
114 public Response updateNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +0900115 log.trace(String.format(MESSAGE_NODE, UPDATE));
sanghof8164112017-07-14 14:33:16 +0900116
117 Set<OpenstackNode> nodes = readNodeConfiguration(input);
118 for (OpenstackNode osNode: nodes) {
119 OpenstackNode existing = osNodeService.node(osNode.hostname());
120 if (existing == null) {
121 log.warn("There is no node configuration to update : {}", osNode.hostname());
122 return Response.notModified().build();
123 } else if (!existing.equals(osNode)) {
124 osNodeAdminService.updateNode(osNode);
125 }
126 }
127
128 return Response.ok().build();
129 }
130
Jian Lif65d72e2018-02-13 13:01:06 +0900131 /**
132 * Removes a set of openstack nodes' config from the JSON input stream.
133 *
Jian Lib959d012018-02-19 15:27:58 +0900134 * @param hostname host name contained in openstack nodes configuration
135 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
136 * 304 NOT_MODIFIED without the updated config
Jian Lif65d72e2018-02-13 13:01:06 +0900137 * @onos.rsModel OpenstackNode
138 */
sanghof8164112017-07-14 14:33:16 +0900139 @DELETE
140 @Consumes(MediaType.APPLICATION_JSON)
141 @Produces(MediaType.APPLICATION_JSON)
Jian Lib959d012018-02-19 15:27:58 +0900142 @Path("{hostname}")
143 public Response deleteNodes(@PathParam("hostname") String hostname) {
daniel parkde735852017-08-01 19:13:24 +0900144 log.trace(String.format(MESSAGE_NODE, DELETE));
sanghof8164112017-07-14 14:33:16 +0900145
Jian Lib959d012018-02-19 15:27:58 +0900146 OpenstackNode existing =
147 osNodeService.node(nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
148
149 if (existing == null) {
150 log.warn("There is no node configuration to delete : {}", hostname);
151 return Response.notModified().build();
152 } else {
153 osNodeAdminService.removeNode(hostname);
sanghof8164112017-07-14 14:33:16 +0900154 }
155
Jian Lib959d012018-02-19 15:27:58 +0900156 return Response.noContent().build();
sanghof8164112017-07-14 14:33:16 +0900157 }
158
159 private Set<OpenstackNode> readNodeConfiguration(InputStream input) {
160 Set<OpenstackNode> nodeSet = Sets.newHashSet();
161 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700162 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
daniel parkde735852017-08-01 19:13:24 +0900163 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
sanghof8164112017-07-14 14:33:16 +0900164 nodes.forEach(node -> {
165 try {
Jian Li23c8be22018-02-13 11:34:15 +0900166 ObjectNode objectNode = node.deepCopy();
167 OpenstackNode openstackNode =
168 codec(OpenstackNode.class).decode(objectNode, this);
daniel parkb18424c2018-02-05 15:43:43 +0900169
Jian Li23c8be22018-02-13 11:34:15 +0900170 nodeSet.add(openstackNode);
sanghof8164112017-07-14 14:33:16 +0900171 } catch (Exception e) {
Daniel Park5a6a7102018-09-06 23:58:33 +0900172 log.error("Exception occurred due to {}", e);
Jian Li23c8be22018-02-13 11:34:15 +0900173 throw new IllegalArgumentException();
sanghof8164112017-07-14 14:33:16 +0900174 }
175 });
176 } catch (Exception e) {
177 throw new IllegalArgumentException(e);
178 }
179
180 return nodeSet;
181 }
182}