blob: e960d4bbde564bb1bddaaa527ea0dc4d5b7d7bff [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;
22import org.onlab.osgi.DefaultServiceDirectory;
sanghof8164112017-07-14 14:33:16 +090023import org.onosproject.openstacknode.api.OpenstackNode;
24import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
25import org.onosproject.openstacknode.api.OpenstackNodeService;
sanghof8164112017-07-14 14:33:16 +090026import org.onosproject.rest.AbstractWebResource;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import javax.ws.rs.Consumes;
31import javax.ws.rs.DELETE;
32import javax.ws.rs.POST;
33import javax.ws.rs.PUT;
34import javax.ws.rs.Path;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.Context;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import javax.ws.rs.core.UriBuilder;
40import javax.ws.rs.core.UriInfo;
41import java.io.InputStream;
42import java.util.Set;
43
44import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
45import static javax.ws.rs.core.Response.created;
46
Jian Lif65d72e2018-02-13 13:01:06 +090047/**
48 * Handles REST API call of openstack node config.
49 */
50
sanghof8164112017-07-14 14:33:16 +090051@Path("configure")
52public 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 parkde735852017-08-01 19:13:24 +090057 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";
sanghof8164112017-07-14 14:33:16 +090061
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 Lif65d72e2018-02-13 13:01:06 +090070 /**
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 */
sanghof8164112017-07-14 14:33:16 +090078 @POST
79 @Consumes(MediaType.APPLICATION_JSON)
80 @Produces(MediaType.APPLICATION_JSON)
81 public Response createNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +090082 log.trace(String.format(MESSAGE_NODE, CREATE));
sanghof8164112017-07-14 14:33:16 +090083
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 parkde735852017-08-01 19:13:24 +090093 .path(NODE_ID);
sanghof8164112017-07-14 14:33:16 +090094
95 return created(locationBuilder.build()).build();
96 }
97
Jian Lif65d72e2018-02-13 13:01:06 +090098 /**
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 */
sanghof8164112017-07-14 14:33:16 +0900106 @PUT
107 @Consumes(MediaType.APPLICATION_JSON)
108 @Produces(MediaType.APPLICATION_JSON)
109 public Response updateNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +0900110 log.trace(String.format(MESSAGE_NODE, UPDATE));
sanghof8164112017-07-14 14:33:16 +0900111
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 Lif65d72e2018-02-13 13:01:06 +0900126 /**
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 */
sanghof8164112017-07-14 14:33:16 +0900133 @DELETE
134 @Consumes(MediaType.APPLICATION_JSON)
135 @Produces(MediaType.APPLICATION_JSON)
136 public Response deleteNodes(InputStream input) {
daniel parkde735852017-08-01 19:13:24 +0900137 log.trace(String.format(MESSAGE_NODE, DELETE));
sanghof8164112017-07-14 14:33:16 +0900138
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 parkde735852017-08-01 19:13:24 +0900157 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
sanghof8164112017-07-14 14:33:16 +0900158 nodes.forEach(node -> {
159 try {
Jian Li23c8be22018-02-13 11:34:15 +0900160 ObjectNode objectNode = node.deepCopy();
161 OpenstackNode openstackNode =
162 codec(OpenstackNode.class).decode(objectNode, this);
daniel parkb18424c2018-02-05 15:43:43 +0900163
Jian Li23c8be22018-02-13 11:34:15 +0900164 nodeSet.add(openstackNode);
sanghof8164112017-07-14 14:33:16 +0900165 } catch (Exception e) {
166 log.error(e.toString());
Jian Li23c8be22018-02-13 11:34:15 +0900167 throw new IllegalArgumentException();
sanghof8164112017-07-14 14:33:16 +0900168 }
169 });
170 } catch (Exception e) {
171 throw new IllegalArgumentException(e);
172 }
173
174 return nodeSet;
175 }
176}