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; |
| 31 | import javax.ws.rs.POST; |
| 32 | import javax.ws.rs.PUT; |
| 33 | import javax.ws.rs.Path; |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 34 | import javax.ws.rs.PathParam; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.core.Context; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import javax.ws.rs.core.UriBuilder; |
| 40 | import javax.ws.rs.core.UriInfo; |
| 41 | import java.io.InputStream; |
| 42 | import java.util.Set; |
| 43 | |
| 44 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 45 | import static javax.ws.rs.core.Response.created; |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 46 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 47 | import static org.onlab.util.Tools.readTreeFromStream; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 48 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 49 | /** |
| 50 | * Handles REST API call of openstack node config. |
| 51 | */ |
| 52 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 53 | @Path("configure") |
| 54 | public 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 park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 59 | 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"; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 63 | |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 64 | private static final String HOST_NAME = "hostname"; |
| 65 | private static final String ERROR_MESSAGE = " cannot be null"; |
| 66 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 67 | private final OpenstackNodeAdminService osNodeAdminService = |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 68 | get(OpenstackNodeAdminService.class); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 69 | private final OpenstackNodeService osNodeService = |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 70 | get(OpenstackNodeService.class); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 71 | |
| 72 | @Context |
| 73 | private UriInfo uriInfo; |
| 74 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 75 | /** |
| 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 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 83 | @POST |
| 84 | @Consumes(MediaType.APPLICATION_JSON) |
| 85 | @Produces(MediaType.APPLICATION_JSON) |
| 86 | public Response createNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 87 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 88 | |
| 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 park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 98 | .path(NODE_ID); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 99 | |
| 100 | return created(locationBuilder.build()).build(); |
| 101 | } |
| 102 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 103 | /** |
| 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 Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 108 | * 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] | 109 | * @onos.rsModel OpenstackNode |
| 110 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 111 | @PUT |
| 112 | @Consumes(MediaType.APPLICATION_JSON) |
| 113 | @Produces(MediaType.APPLICATION_JSON) |
| 114 | public Response updateNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 115 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 116 | |
| 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 Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 131 | /** |
| 132 | * Removes a set of openstack nodes' config from the JSON input stream. |
| 133 | * |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 134 | * @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 Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame] | 137 | * @onos.rsModel OpenstackNode |
| 138 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 139 | @DELETE |
| 140 | @Consumes(MediaType.APPLICATION_JSON) |
| 141 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 142 | @Path("{hostname}") |
| 143 | public Response deleteNodes(@PathParam("hostname") String hostname) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 144 | log.trace(String.format(MESSAGE_NODE, DELETE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 145 | |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 146 | 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); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 154 | } |
| 155 | |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame] | 156 | return Response.noContent().build(); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private Set<OpenstackNode> readNodeConfiguration(InputStream input) { |
| 160 | Set<OpenstackNode> nodeSet = Sets.newHashSet(); |
| 161 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 162 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 163 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 164 | nodes.forEach(node -> { |
| 165 | try { |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 166 | ObjectNode objectNode = node.deepCopy(); |
| 167 | OpenstackNode openstackNode = |
| 168 | codec(OpenstackNode.class).decode(objectNode, this); |
daniel park | b18424c | 2018-02-05 15:43:43 +0900 | [diff] [blame] | 169 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 170 | nodeSet.add(openstackNode); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 171 | } catch (Exception e) { |
Daniel Park | 5a6a710 | 2018-09-06 23:58:33 +0900 | [diff] [blame] | 172 | log.error("Exception occurred due to {}", e); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 173 | throw new IllegalArgumentException(); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 174 | } |
| 175 | }); |
| 176 | } catch (Exception e) { |
| 177 | throw new IllegalArgumentException(e); |
| 178 | } |
| 179 | |
| 180 | return nodeSet; |
| 181 | } |
| 182 | } |