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.servlet.http.HttpServletResponse;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import org.onap.music.datastore.PreparedQueryObject;
40 import org.onap.music.datastore.jsonobjects.JsonOnboard;
41 import org.onap.music.eelf.logging.EELFLoggerDelegate;
42 import org.onap.music.main.CachingUtil;
43 import org.onap.music.main.MusicCore;
44 import org.onap.music.main.MusicUtil;
45 import com.datastax.driver.core.DataType;
46 import com.datastax.driver.core.ResultSet;
47 import com.datastax.driver.core.Row;
48 import io.swagger.annotations.Api;
49 import io.swagger.annotations.ApiOperation;
51 @Path("/v{version: [0-9]+}/admin")
53 @Api(value = "Admin Api", hidden = true)
54 public class RestMusicAdminAPI {
55 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicAdminAPI.class);
58 * API to onboard an application with MUSIC. This is the mandatory first step.
62 @Path("/onboardAppWithMusic")
63 @ApiOperation(value = "Onboard application", response = String.class)
64 @Consumes(MediaType.APPLICATION_JSON)
65 @Produces(MediaType.APPLICATION_JSON)
66 public Map<String, Object> onboardAppWithMusic(JsonOnboard jsonObj,
67 @Context HttpServletResponse response) throws Exception {
68 Map<String, Object> resultMap = new HashMap<>();
69 String appName = jsonObj.getAppname();
70 String userId = jsonObj.getUserId();
71 String isAAF = jsonObj.getIsAAF();
72 String password = jsonObj.getPassword();
73 response.addHeader("X-latestVersion", MusicUtil.getVersion());
74 if (appName == null || userId == null || isAAF == null || password == null) {
75 resultMap.put("Exception",
76 "Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing.");
80 PreparedQueryObject pQuery = new PreparedQueryObject();
81 pQuery.appendQueryString(
82 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
83 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
84 ResultSet rs = MusicCore.get(pQuery);
85 if (!rs.all().isEmpty()) {
86 resultMap.put("Exception", "Application " + appName
87 + " has already been onboarded. Please contact admin.");
91 pQuery = new PreparedQueryObject();
92 String uuid = CachingUtil.generateUUID();
93 pQuery.appendQueryString(
94 "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
95 + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
96 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
97 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
98 MusicUtil.DEFAULTKEYSPACENAME));
99 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
100 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
101 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
102 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
103 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
105 String returnStr = MusicCore.eventualPut(pQuery).toString();
106 if (returnStr.contains("Failure")) {
107 resultMap.put("Exception",
108 "Oops. Something wrong with onboarding process. Please retry later or contact admin.");
111 CachingUtil.updateisAAFCache(appName, isAAF);
112 resultMap.put("Success", "Your application " + appName + " has been onboarded with MUSIC.");
113 resultMap.put("Generated AID", uuid);
120 @ApiOperation(value = "Search Onboard application", response = String.class)
121 @Consumes(MediaType.APPLICATION_JSON)
122 @Produces(MediaType.APPLICATION_JSON)
123 public Map<String, Object> getOnboardedInfoSearch(
125 @Context HttpServletResponse response) throws Exception {
126 Map<String, Object> resultMap = new HashMap<>();
128 response.addHeader("X-latestVersion", MusicUtil.getVersion());
129 String appName = jsonObj.getAppname();
130 String uuid = jsonObj.getAid();
131 String isAAF = jsonObj.getIsAAF();
133 if (appName == null && uuid == null && isAAF == null) {
134 resultMap.put("Exception",
135 "Please check the request parameters. Enter atleast one of the following parameters: appName(ns), aid, isAAF.");
139 PreparedQueryObject pQuery = new PreparedQueryObject();
140 String cql = "select uuid, keyspace_name from admin.keyspace_master where ";
142 cql = cql + "application_name = ? AND ";
144 cql = cql + "uuid = ? AND ";
146 cql = cql + "is_aaf = ?";
148 if(cql.endsWith("AND "))
149 cql = cql.trim().substring(0, cql.length()-4);
150 System.out.println("Query is: "+cql);
151 cql = cql + " allow filtering";
152 System.out.println("Get OnboardingInfo CQL: " + cql);
153 pQuery.appendQueryString(cql);
155 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
157 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
159 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), Boolean.parseBoolean(isAAF)));
160 ResultSet rs = MusicCore.get(pQuery);
161 Iterator<Row> it = rs.iterator();
162 while (it.hasNext()) {
163 Row row = (Row) it.next();
164 resultMap.put( row.getUUID("uuid").toString(),row.getString("keyspace_name"));
166 if (resultMap.isEmpty())
167 resultMap.put("ERROR", "Application is not onboarded. Please contact admin.");
173 @Path("/onboardAppWithMusic")
174 @ApiOperation(value = "Delete Onboard application", response = String.class)
175 @Consumes(MediaType.APPLICATION_JSON)
176 @Produces(MediaType.APPLICATION_JSON)
177 public Map<String, Object> deleteOnboardApp(JsonOnboard jsonObj,
178 @Context HttpServletResponse response) throws Exception {
179 Map<String, Object> resultMap = new HashMap<>();
180 response.addHeader("X-latestVersion", MusicUtil.getVersion());
181 String appName = jsonObj.getAppname();
182 String aid = jsonObj.getAid();
183 PreparedQueryObject pQuery = new PreparedQueryObject();
184 String consistency = MusicUtil.EVENTUAL;;
185 if (appName == null && aid == null) {
186 resultMap.put("Exception", "Please make sure either appName(ns) or Aid is present");
190 pQuery.appendQueryString("SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?");
191 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
192 UUID.fromString(aid)));
193 Row row = MusicCore.get(pQuery).one();
195 String ks = row.getString("keyspace_name");
196 if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) {
197 PreparedQueryObject queryObject = new PreparedQueryObject();
198 queryObject.appendQueryString("DROP KEYSPACE " + ks + ";");
199 MusicCore.nonKeyRelatedPut(queryObject, consistency);
202 pQuery = new PreparedQueryObject();
203 pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ? IF EXISTS");
204 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
205 UUID.fromString(aid)));
206 boolean result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
208 resultMap.put("Success", "Your application has been deleted successfully");
210 resultMap.put("Exception",
211 "Oops. Spomething went wrong. Please make sure Aid is correct or Application is onboarded");
219 pQuery.appendQueryString(
220 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
221 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
222 ResultSet rs = MusicCore.get(pQuery);
223 List<Row> rows = rs.all();
225 if (rows.size() == 0) {
226 resultMap.put("Exception",
227 "Application not found. Please make sure Application exists.");
229 } else if (rows.size() == 1) {
230 uuid = rows.get(0).getUUID("uuid").toString();
231 pQuery = new PreparedQueryObject();
232 pQuery.appendQueryString("SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?");
233 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
234 UUID.fromString(uuid)));
235 Row row = MusicCore.get(pQuery).one();
236 String ks = row.getString("keyspace_name");
237 if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) {
238 PreparedQueryObject queryObject = new PreparedQueryObject();
239 queryObject.appendQueryString("DROP KEYSPACE " + ks + ";");
240 MusicCore.nonKeyRelatedPut(queryObject, consistency);
243 pQuery = new PreparedQueryObject();
244 pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?");
245 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
246 UUID.fromString(uuid)));
247 MusicCore.eventualPut(pQuery);
248 resultMap.put("Success", "Your application " + appName + " has been deleted.");
251 resultMap.put("Failure", "More than one Aid exists for this application, so please provide Aid.");
259 @Path("/onboardAppWithMusic")
260 @ApiOperation(value = "Update Onboard application", response = String.class)
261 @Consumes(MediaType.APPLICATION_JSON)
262 @Produces(MediaType.APPLICATION_JSON)
263 public Map<String, Object> updateOnboardApp(JsonOnboard jsonObj,
264 @Context HttpServletResponse response) throws Exception {
265 Map<String, Object> resultMap = new HashMap<>();
266 response.addHeader("X-latestVersion", MusicUtil.getVersion());
267 String aid = jsonObj.getAid();
268 String appName = jsonObj.getAppname();
269 String userId = jsonObj.getUserId();
270 String isAAF = jsonObj.getIsAAF();
271 String password = jsonObj.getPassword();
272 String consistency = "eventual";
273 PreparedQueryObject pQuery;
276 resultMap.put("Exception", "Please make sure Aid is present");
280 if (appName == null && userId == null && password == null && isAAF == null) {
281 resultMap.put("Exception",
282 "No parameters found to update. Please update atleast one parameter.");
287 pQuery = new PreparedQueryObject();
288 pQuery.appendQueryString(
289 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
290 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
291 ResultSet rs = MusicCore.get(pQuery);
292 if (!rs.all().isEmpty()) {
293 resultMap.put("Exception", "Application " + appName
294 + " has already been onboarded. Please contact admin.");
299 pQuery = new PreparedQueryObject();
300 StringBuilder preCql = new StringBuilder("UPDATE admin.keyspace_master SET ");
302 preCql.append(" application_name = ?,");
304 preCql.append(" username = ?,");
305 if (password != null)
306 preCql.append(" password = ?,");
308 preCql.append(" is_aaf = ?,");
309 preCql.deleteCharAt(preCql.length() - 1);
310 preCql.append(" WHERE uuid = ? IF EXISTS");
311 pQuery.appendQueryString(preCql.toString());
313 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
315 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
316 if (password != null)
317 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
319 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
321 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(aid)));
322 boolean result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
325 resultMap.put("Success", "Your application has been updated successfully");
327 resultMap.put("Exception",
328 "Oops. Spomething went wrong. Please make sure Aid is correct and application is onboarded");