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 | |
| 47 | @Path("configure") |
| 48 | public class OpenstackNodeWebResource extends AbstractWebResource { |
| 49 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
| 51 | private static final String MESSAGE_NODE = "Received node %s request"; |
| 52 | private static final String NODES = "nodes"; |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 53 | private static final String CREATE = "CREATE"; |
| 54 | private static final String UPDATE = "UPDATE"; |
| 55 | private static final String NODE_ID = "NODE_ID"; |
| 56 | private static final String DELETE = "DELETE"; |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 57 | |
| 58 | private final OpenstackNodeAdminService osNodeAdminService = |
| 59 | DefaultServiceDirectory.getService(OpenstackNodeAdminService.class); |
| 60 | private final OpenstackNodeService osNodeService = |
| 61 | DefaultServiceDirectory.getService(OpenstackNodeService.class); |
| 62 | |
| 63 | @Context |
| 64 | private UriInfo uriInfo; |
| 65 | |
| 66 | @POST |
| 67 | @Consumes(MediaType.APPLICATION_JSON) |
| 68 | @Produces(MediaType.APPLICATION_JSON) |
| 69 | public Response createNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 70 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 71 | |
| 72 | readNodeConfiguration(input).forEach(osNode -> { |
| 73 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 74 | if (existing == null) { |
| 75 | osNodeAdminService.createNode(osNode); |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 80 | .path(NODES) |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 81 | .path(NODE_ID); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 82 | |
| 83 | return created(locationBuilder.build()).build(); |
| 84 | } |
| 85 | |
| 86 | @PUT |
| 87 | @Consumes(MediaType.APPLICATION_JSON) |
| 88 | @Produces(MediaType.APPLICATION_JSON) |
| 89 | public Response updateNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 90 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 91 | |
| 92 | Set<OpenstackNode> nodes = readNodeConfiguration(input); |
| 93 | for (OpenstackNode osNode: nodes) { |
| 94 | OpenstackNode existing = osNodeService.node(osNode.hostname()); |
| 95 | if (existing == null) { |
| 96 | log.warn("There is no node configuration to update : {}", osNode.hostname()); |
| 97 | return Response.notModified().build(); |
| 98 | } else if (!existing.equals(osNode)) { |
| 99 | osNodeAdminService.updateNode(osNode); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return Response.ok().build(); |
| 104 | } |
| 105 | |
| 106 | @DELETE |
| 107 | @Consumes(MediaType.APPLICATION_JSON) |
| 108 | @Produces(MediaType.APPLICATION_JSON) |
| 109 | public Response deleteNodes(InputStream input) { |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 110 | log.trace(String.format(MESSAGE_NODE, DELETE)); |
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 delete : {}", osNode.hostname()); |
| 117 | return Response.notModified().build(); |
| 118 | } else { |
| 119 | osNodeAdminService.removeNode(osNode.hostname()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return Response.ok().build(); |
| 124 | } |
| 125 | |
| 126 | private Set<OpenstackNode> readNodeConfiguration(InputStream input) { |
| 127 | Set<OpenstackNode> nodeSet = Sets.newHashSet(); |
| 128 | try { |
| 129 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
daniel park | de73585 | 2017-08-01 19:13:24 +0900 | [diff] [blame] | 130 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 131 | nodes.forEach(node -> { |
| 132 | try { |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame^] | 133 | ObjectNode objectNode = node.deepCopy(); |
| 134 | OpenstackNode openstackNode = |
| 135 | codec(OpenstackNode.class).decode(objectNode, this); |
daniel park | b18424c | 2018-02-05 15:43:43 +0900 | [diff] [blame] | 136 | |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame^] | 137 | nodeSet.add(openstackNode); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 138 | } catch (Exception e) { |
| 139 | log.error(e.toString()); |
Jian Li | 23c8be2 | 2018-02-13 11:34:15 +0900 | [diff] [blame^] | 140 | throw new IllegalArgumentException(); |
sangho | f816411 | 2017-07-14 14:33:16 +0900 | [diff] [blame] | 141 | } |
| 142 | }); |
| 143 | } catch (Exception e) { |
| 144 | throw new IllegalArgumentException(e); |
| 145 | } |
| 146 | |
| 147 | return nodeSet; |
| 148 | } |
| 149 | } |