2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.rest;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
29 import java.util.UUID;
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.MediaType;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.Response.ResponseBuilder;
39 import javax.ws.rs.core.Response.Status;
41 import org.mindrot.jbcrypt.BCrypt;
42 import org.onap.music.datastore.PreparedQueryObject;
43 import org.onap.music.datastore.jsonobjects.JsonOnboard;
44 import org.onap.music.eelf.logging.EELFLoggerDelegate;
45 import org.onap.music.eelf.logging.format.AppMessages;
46 import org.onap.music.eelf.logging.format.ErrorSeverity;
47 import org.onap.music.eelf.logging.format.ErrorTypes;
48 import org.onap.music.main.CachingUtil;
49 import org.onap.music.main.MusicCore;
50 import org.onap.music.main.MusicUtil;
51 import org.onap.music.main.ResultType;
52 import com.datastax.driver.core.DataType;
53 import com.datastax.driver.core.ResultSet;
54 import com.datastax.driver.core.Row;
55 import io.swagger.annotations.Api;
56 import io.swagger.annotations.ApiOperation;
59 // @Path("/v{version: [0-9]+}/admin")
61 @Api(value = "Admin Api", hidden = true)
62 public class RestMusicAdminAPI {
63 private static EELFLoggerDelegate logger =
64 EELFLoggerDelegate.getLogger(RestMusicAdminAPI.class);
67 * API to onboard an application with MUSIC. This is the mandatory first step.
71 @Path("/onboardAppWithMusic")
72 @ApiOperation(value = "Onboard application", response = String.class)
73 @Consumes(MediaType.APPLICATION_JSON)
74 @Produces(MediaType.APPLICATION_JSON)
75 public Response onboardAppWithMusic(JsonOnboard jsonObj) throws Exception {
76 ResponseBuilder response =
77 Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
78 Map<String, Object> resultMap = new HashMap<>();
79 String appName = jsonObj.getAppname();
80 String userId = jsonObj.getUserId();
81 String isAAF = jsonObj.getIsAAF();
82 String password = jsonObj.getPassword();
83 if (appName == null || userId == null || isAAF == null || password == null) {
84 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO,
85 ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
86 resultMap.put("Exception",
87 "Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing.");
88 return Response.status(Status.UNAUTHORIZED).entity(resultMap).build();
91 PreparedQueryObject pQuery = new PreparedQueryObject();
92 pQuery.appendQueryString(
93 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
94 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
95 ResultSet rs = MusicCore.get(pQuery);
96 if (!rs.all().isEmpty()) {
97 resultMap.put("Exception", "Application " + appName
98 + " has already been onboarded. Please contact admin.");
99 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
102 pQuery = new PreparedQueryObject();
103 String uuid = CachingUtil.generateUUID();
104 pQuery.appendQueryString(
105 "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
106 + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
107 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
108 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
109 MusicUtil.DEFAULTKEYSPACENAME));
110 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
111 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
112 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt())));
113 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
114 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
116 String returnStr = MusicCore.eventualPut(pQuery).toString();
117 if (returnStr.contains("Failure")) {
118 resultMap.put("Exception",
119 "Oops. Something wrong with onboarding process. Please retry later or contact admin.");
120 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
122 CachingUtil.updateisAAFCache(appName, isAAF);
123 resultMap.put("Success", "Your application " + appName + " has been onboarded with MUSIC.");
124 resultMap.put("Generated AID", uuid);
125 return Response.status(Status.OK).entity(resultMap).build();
131 @ApiOperation(value = "Search Onboard application", response = String.class)
132 @Consumes(MediaType.APPLICATION_JSON)
133 @Produces(MediaType.APPLICATION_JSON)
134 public Response getOnboardedInfoSearch(JsonOnboard jsonObj) throws Exception {
135 Map<String, Object> resultMap = new HashMap<>();
136 ResponseBuilder response =
137 Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
138 String appName = jsonObj.getAppname();
139 String uuid = jsonObj.getAid();
140 String isAAF = jsonObj.getIsAAF();
142 if (appName == null && uuid == null && isAAF == null) {
143 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO,
144 ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
145 resultMap.put("Exception",
146 "Unauthorized: Please check the request parameters. Enter atleast one of the following parameters: appName(ns), aid, isAAF.");
147 return Response.status(Status.UNAUTHORIZED).entity(resultMap).build();
150 PreparedQueryObject pQuery = new PreparedQueryObject();
151 String cql = "select uuid, keyspace_name from admin.keyspace_master where ";
153 cql = cql + "application_name = ? AND ";
155 cql = cql + "uuid = ? AND ";
157 cql = cql + "is_aaf = ?";
159 if (cql.endsWith("AND "))
160 cql = cql.trim().substring(0, cql.length() - 4);
161 System.out.println("Query is: " + cql);
162 cql = cql + " allow filtering";
163 System.out.println("Get OnboardingInfo CQL: " + cql);
164 pQuery.appendQueryString(cql);
166 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
168 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
170 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(),
171 Boolean.parseBoolean(isAAF)));
172 ResultSet rs = MusicCore.get(pQuery);
173 Iterator<Row> it = rs.iterator();
174 while (it.hasNext()) {
175 Row row = (Row) it.next();
176 resultMap.put(row.getUUID("uuid").toString(), row.getString("keyspace_name"));
178 if (resultMap.isEmpty()) {
180 resultMap.put("Exception",
181 "Please make sure Aid is correct and application is onboarded.");
182 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
184 resultMap.put("Exception",
185 "Application is not onboarded. Please make sure all the information is correct.");
186 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
189 return Response.status(Status.OK).entity(resultMap).build();
194 @Path("/onboardAppWithMusic")
195 @ApiOperation(value = "Delete Onboard application", response = String.class)
196 @Consumes(MediaType.APPLICATION_JSON)
197 @Produces(MediaType.APPLICATION_JSON)
198 public Response deleteOnboardApp(JsonOnboard jsonObj) throws Exception {
199 Map<String, Object> resultMap = new HashMap<>();
200 ResponseBuilder response =
201 Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
202 String appName = jsonObj.getAppname();
203 String aid = jsonObj.getAid();
204 PreparedQueryObject pQuery = new PreparedQueryObject();
205 String consistency = MusicUtil.EVENTUAL;;
206 if (appName == null && aid == null) {
207 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO,
208 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
209 resultMap.put("Exception", "Please make sure either appName(ns) or Aid is present");
210 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
213 pQuery.appendQueryString(
214 "SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?");
215 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
216 UUID.fromString(aid)));
217 Row row = MusicCore.get(pQuery).one();
219 String ks = row.getString("keyspace_name");
220 if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) {
221 PreparedQueryObject queryObject = new PreparedQueryObject();
222 queryObject.appendQueryString("DROP KEYSPACE IF EXISTS " + ks + ";");
223 MusicCore.nonKeyRelatedPut(queryObject, consistency);
226 pQuery = new PreparedQueryObject();
227 pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ? IF EXISTS");
228 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
229 UUID.fromString(aid)));
230 ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
231 if (result == ResultType.SUCCESS) {
232 resultMap.put("Success", "Your application has been deleted successfully");
234 resultMap.put("Exception",
235 "Oops. Something went wrong. Please make sure Aid is correct or Application is onboarded");
236 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA,
237 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
238 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
241 return Response.status(Status.OK).entity(resultMap).build();
244 pQuery.appendQueryString(
245 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
246 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
247 ResultSet rs = MusicCore.get(pQuery);
248 List<Row> rows = rs.all();
250 if (rows.size() == 0) {
251 resultMap.put("Exception",
252 "Application not found. Please make sure Application exists.");
253 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA,
254 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
255 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
256 } else if (rows.size() == 1) {
257 uuid = rows.get(0).getUUID("uuid").toString();
258 pQuery = new PreparedQueryObject();
259 pQuery.appendQueryString(
260 "SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?");
261 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
262 UUID.fromString(uuid)));
263 Row row = MusicCore.get(pQuery).one();
264 String ks = row.getString("keyspace_name");
265 if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) {
266 PreparedQueryObject queryObject = new PreparedQueryObject();
267 queryObject.appendQueryString("DROP KEYSPACE " + ks + ";");
268 MusicCore.nonKeyRelatedPut(queryObject, consistency);
271 pQuery = new PreparedQueryObject();
272 pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?");
273 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
274 UUID.fromString(uuid)));
275 MusicCore.eventualPut(pQuery);
276 resultMap.put("Success", "Your application " + appName + " has been deleted.");
277 return Response.status(Status.OK).entity(resultMap).build();
279 resultMap.put("Failure",
280 "More than one Aid exists for this application, so please provide Aid.");
281 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MULTIPLERECORDS,
282 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
283 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
289 @Path("/onboardAppWithMusic")
290 @ApiOperation(value = "Update Onboard application", response = String.class)
291 @Consumes(MediaType.APPLICATION_JSON)
292 @Produces(MediaType.APPLICATION_JSON)
293 public Response updateOnboardApp(JsonOnboard jsonObj) throws Exception {
294 Map<String, Object> resultMap = new HashMap<>();
295 ResponseBuilder response =
296 Response.noContent().header("X-latestVersion", MusicUtil.getVersion());
297 String aid = jsonObj.getAid();
298 String appName = jsonObj.getAppname();
299 String userId = jsonObj.getUserId();
300 String isAAF = jsonObj.getIsAAF();
301 String password = jsonObj.getPassword();
302 String consistency = "eventual";
303 PreparedQueryObject pQuery;
306 resultMap.put("Exception", "Please make sure Aid is present");
307 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
308 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
309 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
312 if (appName == null && userId == null && password == null && isAAF == null) {
313 resultMap.put("Exception",
314 "No parameters found to update. Please update atleast one parameter.");
315 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
316 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
317 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
320 if (appName != null) {
321 pQuery = new PreparedQueryObject();
322 pQuery.appendQueryString(
323 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
324 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
325 ResultSet rs = MusicCore.get(pQuery);
326 if (!rs.all().isEmpty()) {
327 resultMap.put("Exception", "Application " + appName
328 + " has already been onboarded. Please contact admin.");
329 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.ALREADYEXIST,
330 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
331 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
335 pQuery = new PreparedQueryObject();
336 StringBuilder preCql = new StringBuilder("UPDATE admin.keyspace_master SET ");
338 preCql.append(" application_name = ?,");
340 preCql.append(" username = ?,");
341 if (password != null)
342 preCql.append(" password = ?,");
344 preCql.append(" is_aaf = ?,");
345 preCql.deleteCharAt(preCql.length() - 1);
346 preCql.append(" WHERE uuid = ? IF EXISTS");
347 pQuery.appendQueryString(preCql.toString());
349 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
351 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
352 if (password != null)
353 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt())));
355 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
357 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(aid)));
358 ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
360 if (result == ResultType.SUCCESS) {
361 resultMap.put("Success", "Your application has been updated successfully");
363 resultMap.put("Exception",
364 "Oops. Something went wrong. Please make sure Aid is correct and application is onboarded");
365 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA,
366 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
367 return Response.status(Status.BAD_REQUEST).entity(resultMap).build();
370 return Response.status(Status.OK).entity(resultMap).build();