sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.openstacknode.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 22 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 23 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 24 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 25 | import org.onosproject.rest.AbstractWebResource; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import javax.ws.rs.Consumes; |
| 30 | import javax.ws.rs.DELETE; |
Jian Li | 901462a | 2019-02-20 18:50:30 +0900 | [diff] [blame^] | 31 | import javax.ws.rs.GET; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 32 | import javax.ws.rs.POST; |
| 33 | import javax.ws.rs.PUT; |
| 34 | import javax.ws.rs.Path; |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 35 | import javax.ws.rs.PathParam; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 36 | import javax.ws.rs.Produces; |
| 37 | import javax.ws.rs.core.Context; |
| 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
| 40 | import javax.ws.rs.core.UriBuilder; |
| 41 | import javax.ws.rs.core.UriInfo; |
| 42 | import java.io.InputStream; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 46 | import static javax.ws.rs.core.Response.created; |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 47 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 48 | import static org.onlab.util.Tools.readTreeFromStream; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 49 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 50 | /** |
| 51 | * Handles REST API call of openstack node config. |
| 52 | */ |
| 53 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 54 | @Path("configure") |
| 55 | public 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 park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 60 | 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 Li | 901462a | 2019-02-20 18:50:30 +0900 | [diff] [blame^] | 64 | private static final String QUERY = "QUERY"; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 65 | |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 66 | private static final String HOST_NAME = "hostname"; |
| 67 | private static final String ERROR_MESSAGE = " cannot be null"; |
| 68 | |
Jian Li | 901462a | 2019-02-20 18:50:30 +0900 | [diff] [blame^] | 69 | private final ObjectNode root = mapper().createObjectNode(); |
| 70 | private final ArrayNode osJsonNodes = root.putArray("nodes"); |
| 71 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 72 | private final OpenstackNodeAdminService osNodeAdminService = |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 73 | get(OpenstackNodeAdminService.class); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 74 | private final OpenstackNodeService osNodeService = |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 75 | get(OpenstackNodeService.class); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 76 | |
| 77 | @Context |
| 78 | private UriInfo uriInfo; |
| 79 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 80 | /** |
| 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 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 88 | @POST |
| 89 | @Consumes(MediaType.APPLICATION_JSON) |
| 90 | @Produces(MediaType.APPLICATION_JSON) |
| 91 | public Response createNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 92 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 93 | |
| 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 park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 103 | .path(NODE_ID); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 104 | |
| 105 | return created(locationBuilder.build()).build(); |
| 106 | } |
| 107 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 108 | /** |
Jian Li | 7fe7eaf | 2018-12-31 17:00:33 +0900 | [diff] [blame] | 109 | * Updates a set of openstack nodes' config from the JSON input stream. |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 110 | * |
| 111 | * @param input openstack nodes JSON input stream |
| 112 | * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 113 | * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 114 | * @onos.rsModel OpenstackNode |
| 115 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 116 | @PUT |
| 117 | @Consumes(MediaType.APPLICATION_JSON) |
| 118 | @Produces(MediaType.APPLICATION_JSON) |
| 119 | public Response updateNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 120 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 121 | |
| 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 Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 136 | /** |
| 137 | * Removes a set of openstack nodes' config from the JSON input stream. |
| 138 | * |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 139 | * @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 Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 142 | * @onos.rsModel OpenstackNode |
| 143 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 144 | @DELETE |
| 145 | @Consumes(MediaType.APPLICATION_JSON) |
| 146 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 147 | @Path("{hostname}") |
| 148 | public Response deleteNodes(@PathParam("hostname") String hostname) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 149 | log.trace(String.format(MESSAGE_NODE, DELETE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 150 | |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 151 | 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); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 159 | } |
| 160 | |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 161 | return Response.noContent().build(); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Jian Li | 901462a | 2019-02-20 18:50:30 +0900 | [diff] [blame^] | 164 | /** |
| 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 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 183 | private Set<OpenstackNode> readNodeConfiguration(InputStream input) { |
| 184 | Set<OpenstackNode> nodeSet = Sets.newHashSet(); |
| 185 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 186 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 187 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 188 | nodes.forEach(node -> { |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 189 | ObjectNode objectNode = node.deepCopy(); |
| 190 | OpenstackNode openstackNode = |
| 191 | codec(OpenstackNode.class).decode(objectNode, this); |
daniel park | b18424c | 2018-02-05 15:43:43 +0900 | [diff] [blame] | 192 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 193 | nodeSet.add(openstackNode); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 194 | }); |
| 195 | } catch (Exception e) { |
| 196 | throw new IllegalArgumentException(e); |
| 197 | } |
| 198 | |
| 199 | return nodeSet; |
| 200 | } |
| 201 | } |