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;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.UUID;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.DELETE;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.HeaderParam;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MediaType;
40 import org.onap.music.datastore.PreparedQueryObject;
41 import org.onap.music.datastore.jsonobjects.JsonOnboard;
42 import org.onap.music.main.CachingUtil;
43 import org.onap.music.main.MusicCore;
44 import org.onap.music.main.MusicUtil;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import com.datastax.driver.core.DataType;
48 import com.datastax.driver.core.ResultSet;
49 import com.datastax.driver.core.Row;
50 import io.swagger.annotations.Api;
51 import io.swagger.annotations.ApiOperation;
52 import io.swagger.annotations.ApiParam;
54 @Path("/v{version: [0-9]+}/admin")
56 @Api(value = "Admin Api", hidden = true)
57 public class RestMusicAdminAPI {
58 private static EELFLogger logger = EELFManager.getInstance().getLogger(RestMusicAdminAPI.class);
61 * API to onboard an application with MUSIC. This is the mandatory first step.
65 @Path("/onboardAppWithMusic")
66 @ApiOperation(value = "Onboard application", response = String.class)
67 @Consumes(MediaType.APPLICATION_JSON)
68 @Produces(MediaType.APPLICATION_JSON)
69 public Map<String, Object> onboardAppWithMusic(JsonOnboard jsonObj,
70 @Context HttpServletResponse response) throws Exception {
71 Map<String, Object> resultMap = new HashMap<>();
72 String appName = jsonObj.getAppname();
73 String userId = jsonObj.getUserId();
74 String isAAF = jsonObj.getIsAAF();
75 String password = jsonObj.getPassword();
76 response.addHeader("X-latestVersion", MusicUtil.getVersion());
77 if (appName == null || userId == null || isAAF == null || password == null) {
78 resultMap.put("Exception",
79 "Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing.");
83 PreparedQueryObject pQuery = new PreparedQueryObject();
84 pQuery.appendQueryString(
85 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
86 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
87 ResultSet rs = MusicCore.get(pQuery);
88 if (!rs.all().isEmpty()) {
89 resultMap.put("Exception", "Your application " + appName
90 + " has already been onboarded. Please contact admin.");
94 pQuery = new PreparedQueryObject();
95 String uuid = CachingUtil.generateUUID();
96 pQuery.appendQueryString(
97 "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
98 + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)");
99 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
100 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
101 MusicUtil.DEFAULTKEYSPACENAME));
102 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
103 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
104 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
105 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
106 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
108 String returnStr = MusicCore.eventualPut(pQuery).toString();
109 if (returnStr.contains("Failure")) {
110 resultMap.put("Exception",
111 "Oops. Something wrong with onboarding process. Please retry later or contact admin.");
114 CachingUtil.updateisAAFCache(appName, isAAF);
115 resultMap.put("Success", "Your application " + appName + " has been onboarded with MUSIC.");
116 resultMap.put("Generated AID", uuid);
122 * API to onboard an application with MUSIC. This is the mandatory first step.
126 @Path("/onboardAppWithMusic")
127 @ApiOperation(value = "Onboard application", response = String.class)
128 @Consumes(MediaType.APPLICATION_JSON)
129 @Produces(MediaType.APPLICATION_JSON)
130 public Map<String, Object> getOnboardedInfo(
131 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String uuid,
132 @ApiParam(value = "Application namespace",
133 required = true) @HeaderParam("ns") String appName,
134 @Context HttpServletResponse response) throws Exception {
135 Map<String, Object> resultMap = new HashMap<>();
137 response.addHeader("X-latestVersion", MusicUtil.getVersion());
138 if (appName == null && uuid == null) {
139 resultMap.put("Exception",
140 "Please check the request parameters. Some of the required values appName(ns), aid are missing.");
144 PreparedQueryObject pQuery = new PreparedQueryObject();
146 String cql = "select uuid, keyspace_name from admin.keyspace_master where ";
148 cql = cql + "application_name = ?";
149 else if (uuid != null)
150 cql = cql + "uuid = ?";
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));
156 else if (uuid != null)
157 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
158 ResultSet rs = MusicCore.get(pQuery);
159 Iterator<Row> it = rs.iterator();
160 while (it.hasNext()) {
161 Row row = (Row) it.next();
162 resultMap.put(row.getString("keyspace_name"), row.getUUID("uuid"));
164 if (resultMap.isEmpty())
165 resultMap.put("ERROR", "Application is not onboarded. Please contact admin.");
171 @Path("/onboardAppWithMusic")
172 @ApiOperation(value = "Delete Onboard application", response = String.class)
173 @Consumes(MediaType.APPLICATION_JSON)
174 @Produces(MediaType.APPLICATION_JSON)
175 public Map<String, Object> deleteOnboardApp(JsonOnboard jsonObj,
176 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
177 @Context HttpServletResponse response) throws Exception {
178 Map<String, Object> resultMap = new HashMap<>();
179 response.addHeader("X-latestVersion", MusicUtil.getVersion());
180 String appName = jsonObj.getAppname();
181 PreparedQueryObject pQuery = new PreparedQueryObject();
183 if (appName == null && aid == null) {
184 resultMap.put("Exception", "Please make sure either appName(ns) or Aid is present");
188 pQuery.appendQueryString(
189 "select count(*) as count from admin.keyspace_master where uuid = ?");
190 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
191 UUID.fromString(aid)));
192 Row row = MusicCore.get(pQuery).one();
194 count = row.getLong(0);
198 resultMap.put("Failure", "Please verify your AID.");
201 pQuery = new PreparedQueryObject();
202 pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?");
203 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
204 UUID.fromString(aid)));
205 String result = MusicCore.eventualPut(pQuery).toString();
206 if (result.toLowerCase().contains("success")) {
207 resultMap.put("Success", "Your application has been deleted.");
210 resultMap.put("Failure", "Please verify your AID.");
216 pQuery.appendQueryString(
217 "select uuid from admin.keyspace_master where application_name = ? allow filtering");
218 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
219 ResultSet rs = MusicCore.get(pQuery);
220 List<Row> rows = rs.all();
222 if (rows.size() == 0) {
223 resultMap.put("Exception",
224 "Application not found. Please make sure Application exists.");
226 } else if (rows.size() == 1) {
227 uuid = rows.get(0).getUUID("uuid").toString();
228 pQuery = new PreparedQueryObject();
229 pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?");
230 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(),
231 UUID.fromString(uuid)));
232 MusicCore.eventualPut(pQuery);
233 resultMap.put("Success", "Your application " + appName + " has been deleted.");
236 resultMap.put("Failure", "Please provide UUID for the application.");
244 @Path("/onboardAppWithMusic")
245 @ApiOperation(value = "Update Onboard application", response = String.class)
246 @Consumes(MediaType.APPLICATION_JSON)
247 @Produces(MediaType.APPLICATION_JSON)
248 public Map<String, Object> updateOnboardApp(JsonOnboard jsonObj,
249 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
250 @Context HttpServletResponse response) throws Exception {
251 Map<String, Object> resultMap = new HashMap<>();
252 response.addHeader("X-latestVersion", MusicUtil.getVersion());
253 String appName = jsonObj.getAppname();
254 String userId = jsonObj.getUserId();
255 String isAAF = jsonObj.getIsAAF();
256 String password = jsonObj.getPassword();
257 String consistency = "eventual";
258 PreparedQueryObject pQuery = new PreparedQueryObject();
261 resultMap.put("Exception", "Please make sure Aid is present");
265 if (appName == null && userId == null && password == null && isAAF == null) {
266 resultMap.put("Exception",
267 "No parameters found to update. Please update atleast one parameter.");
271 StringBuilder preCql = new StringBuilder("UPDATE admin.keyspace_master SET ");
273 preCql.append(" application_name = ?,");
275 preCql.append(" username = ?,");
276 if (password != null)
277 preCql.append(" password = ?,");
279 preCql.append(" is_aaf = ?,");
280 preCql.deleteCharAt(preCql.length() - 1);
281 preCql.append(" WHERE uuid = ?");
282 pQuery.appendQueryString(preCql.toString());
284 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
286 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
287 if (password != null)
288 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
290 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
293 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(aid)));
294 Boolean result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
297 resultMap.put("Success", "Your application has been updated successfully");
299 resultMap.put("Exception",
300 "Oops. Spomething went wrong. Please make sure Aid is correct and application is onboarded");