blob: e0f6e5357e0c589846d6cbd26cae51f7c1ec1bfc [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;
34import javax.ws.rs.Produces;
35import javax.ws.rs.core.Context;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38import javax.ws.rs.core.UriBuilder;
39import javax.ws.rs.core.UriInfo;
40import java.io.InputStream;
41import java.util.Set;
42
43import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
44import static javax.ws.rs.core.Response.created;
45
Jian Lif65d72e2018-02-13 13:01:06 +090046/**
47 * Handles REST API call of openstack node config.
48 */
49
sanghof8164112017-07-14 14:33:16 +090050@Path("configure")
51public class OpenstackNodeWebResource extends AbstractWebResource {
52 private final Logger log = LoggerFactory.getLogger(getClass());
53
54 private static final String MESSAGE_NODE = "Received node %s request";
55 private static final String NODES = "nodes";
daniel parkde735852017-08-01 19:13:24 +090056 private static final String CREATE = "CREATE";
57 private static final String UPDATE = "UPDATE";
58 private static final String NODE_ID = "NODE_ID";
59 private static final String DELETE = "DELETE";
sanghof8164112017-07-14 14:33:16 +090060
61 private final OpenstackNodeAdminService osNodeAdminService =
Jian Lic84e3f12018-02-13 15:12:18 +090062 get(OpenstackNodeAdminService.class);
sanghof8164112017-07-14 14:33:16 +090063 private final OpenstackNodeService osNodeService =
Jian Lic84e3f12018-02-13 15:12:18 +090064 get(OpenstackNodeService.class);
sanghof8164112017-07-14 14:33:16 +090065
66 @Context
67 private UriInfo uriInfo;
68
Jian Lif65d72e2018-02-13 13:01:06 +090069 /**
70 * Creates a set of openstack nodes' config from the JSON input stream.
71 *
72 * @param input openstack nodes JSON input stream
73 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
74 * is malformed
75 * @onos.rsModel OpenstackNode
76 */
sanghof8164112017-07-14 14:33:16 +090077 @POST
78 @Consumes(MediaType.APPLICATION_JSON)
79 @Produces(MediaType.APPLICATION_JSON)
80 public Response createNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +090081 log.trace(String.format(MESSAGE_NODE, CREATE));
sanghof8164112017-07-14 14:33:16 +090082
83 readNodeConfiguration(input).forEach(osNode -> {
84 OpenstackNode existing = osNodeService.node(osNode.hostname());
85 if (existing == null) {
86 osNodeAdminService.createNode(osNode);
87 }
88 });
89
90 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
91 .path(NODES)
daniel parkde735852017-08-01 19:13:24 +090092 .path(NODE_ID);
sanghof8164112017-07-14 14:33:16 +090093
94 return created(locationBuilder.build()).build();
95 }
96
Jian Lif65d72e2018-02-13 13:01:06 +090097 /**
98 * Creates a set of openstack nodes' config from the JSON input stream.
99 *
100 * @param input openstack nodes JSON input stream
101 * @return 200 OK with the updated openstack node's config, 400 BAD_REQUEST
102 * if the JSON is malformed
103 * @onos.rsModel OpenstackNode
104 */
sanghof8164112017-07-14 14:33:16 +0900105 @PUT
106 @Consumes(MediaType.APPLICATION_JSON)
107 @Produces(MediaType.APPLICATION_JSON)
108 public Response updateNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +0900109 log.trace(String.format(MESSAGE_NODE, UPDATE));
sanghof8164112017-07-14 14:33:16 +0900110
111 Set<OpenstackNode> nodes = readNodeConfiguration(input);
112 for (OpenstackNode osNode: nodes) {
113 OpenstackNode existing = osNodeService.node(osNode.hostname());
114 if (existing == null) {
115 log.warn("There is no node configuration to update : {}", osNode.hostname());
116 return Response.notModified().build();
117 } else if (!existing.equals(osNode)) {
118 osNodeAdminService.updateNode(osNode);
119 }
120 }
121
122 return Response.ok().build();
123 }
124
Jian Lif65d72e2018-02-13 13:01:06 +0900125 /**
126 * Removes a set of openstack nodes' config from the JSON input stream.
127 *
128 * @param input openstack nodes JSON input stream
129 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed
130 * @onos.rsModel OpenstackNode
131 */
sanghof8164112017-07-14 14:33:16 +0900132 @DELETE
133 @Consumes(MediaType.APPLICATION_JSON)
134 @Produces(MediaType.APPLICATION_JSON)
135 public Response deleteNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +0900136 log.trace(String.format(MESSAGE_NODE, DELETE));
sanghof8164112017-07-14 14:33:16 +0900137
138 Set<OpenstackNode> nodes = readNodeConfiguration(input);
139 for (OpenstackNode osNode: nodes) {
140 OpenstackNode existing = osNodeService.node(osNode.hostname());
141 if (existing == null) {
142 log.warn("There is no node configuration to delete : {}", osNode.hostname());
143 return Response.notModified().build();
144 } else {
145 osNodeAdminService.removeNode(osNode.hostname());
146 }
147 }
148
149 return Response.ok().build();
150 }
151
152 private Set<OpenstackNode> readNodeConfiguration(InputStream input) {
153 Set<OpenstackNode> nodeSet = Sets.newHashSet();
154 try {
155 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
daniel parkde735852017-08-01 19:13:24 +0900156 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
sanghof8164112017-07-14 14:33:16 +0900157 nodes.forEach(node -> {
158 try {
Jian Li23c8be22018-02-13 11:34:15 +0900159 ObjectNode objectNode = node.deepCopy();
160 OpenstackNode openstackNode =
161 codec(OpenstackNode.class).decode(objectNode, this);
daniel parkb18424c2018-02-05 15:43:43 +0900162
Jian Li23c8be22018-02-13 11:34:15 +0900163 nodeSet.add(openstackNode);
sanghof8164112017-07-14 14:33:16 +0900164 } catch (Exception e) {
165 log.error(e.toString());
Jian Li23c8be22018-02-13 11:34:15 +0900166 throw new IllegalArgumentException();
sanghof8164112017-07-14 14:33:16 +0900167 }
168 });
169 } catch (Exception e) {
170 throw new IllegalArgumentException(e);
171 }
172
173 return nodeSet;
174 }
175}