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; |
| 22 | import org.onlab.osgi.DefaultServiceDirectory; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 24 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 25 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 26 | import org.onosproject.rest.AbstractWebResource; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import javax.ws.rs.Consumes; |
| 31 | import javax.ws.rs.DELETE; |
| 32 | import javax.ws.rs.POST; |
| 33 | import javax.ws.rs.PUT; |
| 34 | import javax.ws.rs.Path; |
| 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; |
| 46 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame^] | 47 | /** |
| 48 | * Handles REST API call of openstack node config. |
| 49 | */ |
| 50 | |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 51 | @Path("configure") |
| 52 | public class OpenstackNodeWebResource extends AbstractWebResource { |
| 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
| 55 | private static final String MESSAGE_NODE = "Received node %s request"; |
| 56 | private static final String NODES = "nodes"; |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 57 | private static final String CREATE = "CREATE"; |
| 58 | private static final String UPDATE = "UPDATE"; |
| 59 | private static final String NODE_ID = "NODE_ID"; |
| 60 | private static final String DELETE = "DELETE"; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 61 | |
| 62 | private final OpenstackNodeAdminService osNodeAdminService = |
| 63 | DefaultServiceDirectory.getService(OpenstackNodeAdminService.class); |
| 64 | private final OpenstackNodeService osNodeService = |
| 65 | DefaultServiceDirectory.getService(OpenstackNodeService.class); |
| 66 | |
| 67 | @Context |
| 68 | private UriInfo uriInfo; |
| 69 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame^] | 70 | /** |
| 71 | * Creates a set of openstack nodes' config from the JSON input stream. |
| 72 | * |
| 73 | * @param input openstack nodes JSON input stream |
| 74 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 75 | * is malformed |
| 76 | * @onos.rsModel OpenstackNode |
| 77 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 78 | @POST |
| 79 | @Consumes(MediaType.APPLICATION_JSON) |
| 80 | @Produces(MediaType.APPLICATION_JSON) |
| 81 | public Response createNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 82 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 83 | |
| 84 | readNodeConfiguration(input).forEach(osNode -> { |
| 85 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 86 | if (existing == null) { |
| 87 | osNodeAdminService.createNode(osNode); |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 92 | .path(NODES) |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 93 | .path(NODE_ID); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 94 | |
| 95 | return created(locationBuilder.build()).build(); |
| 96 | } |
| 97 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame^] | 98 | /** |
| 99 | * Creates a set of openstack nodes' config from the JSON input stream. |
| 100 | * |
| 101 | * @param input openstack nodes JSON input stream |
| 102 | * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST |
| 103 | * if the JSON is malformed |
| 104 | * @onos.rsModel OpenstackNode |
| 105 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 106 | @PUT |
| 107 | @Consumes(MediaType.APPLICATION_JSON) |
| 108 | @Produces(MediaType.APPLICATION_JSON) |
| 109 | public Response updateNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 110 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 111 | |
| 112 | Set<OpenstackNode> nodes = readNodeConfiguration(input); |
| 113 | for (OpenstackNode osNode: nodes) { |
| 114 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 115 | if (existing == null) { |
| 116 | log.warn("There is no node configuration to update : {}", osNode.hostname()); |
| 117 | return Response.notModified().build(); |
| 118 | } else if (!existing.equals(osNode)) { |
| 119 | osNodeAdminService.updateNode(osNode); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return Response.ok().build(); |
| 124 | } |
| 125 | |
Jian Li | f65d72e | 2018-02-13 13:01:06 +0900 | [diff] [blame^] | 126 | /** |
| 127 | * Removes a set of openstack nodes' config from the JSON input stream. |
| 128 | * |
| 129 | * @param input openstack nodes JSON input stream |
| 130 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed |
| 131 | * @onos.rsModel OpenstackNode |
| 132 | */ |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 133 | @DELETE |
| 134 | @Consumes(MediaType.APPLICATION_JSON) |
| 135 | @Produces(MediaType.APPLICATION_JSON) |
| 136 | public Response deleteNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 137 | log.trace(String.format(MESSAGE_NODE, DELETE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 138 | |
| 139 | Set<OpenstackNode> nodes = readNodeConfiguration(input); |
| 140 | for (OpenstackNode osNode: nodes) { |
| 141 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 142 | if (existing == null) { |
| 143 | log.warn("There is no node configuration to delete : {}", osNode.hostname()); |
| 144 | return Response.notModified().build(); |
| 145 | } else { |
| 146 | osNodeAdminService.removeNode(osNode.hostname()); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return Response.ok().build(); |
| 151 | } |
| 152 | |
| 153 | private Set<OpenstackNode> readNodeConfiguration(InputStream input) { |
| 154 | Set<OpenstackNode> nodeSet = Sets.newHashSet(); |
| 155 | try { |
| 156 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 157 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 158 | nodes.forEach(node -> { |
| 159 | try { |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 160 | ObjectNode objectNode = node.deepCopy(); |
| 161 | OpenstackNode openstackNode = |
| 162 | codec(OpenstackNode.class).decode(objectNode, this); |
daniel park | b18424c | 2018-02-05 15:43:43 +0900 | [diff] [blame] | 163 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 164 | nodeSet.add(openstackNode); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 165 | } catch (Exception e) { |
| 166 | log.error(e.toString()); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame] | 167 | throw new IllegalArgumentException(); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 168 | } |
| 169 | }); |
| 170 | } catch (Exception e) { |
| 171 | throw new IllegalArgumentException(e); |
| 172 | } |
| 173 | |
| 174 | return nodeSet; |
| 175 | } |
| 176 | } |