2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Modifications Copyright (c) 2018 IBM
8 * ===================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=============================================
22 * ====================================================================
25 package org.onap.music.conductor.conditionals;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.HeaderParam;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.Response.ResponseBuilder;
41 import javax.ws.rs.core.Response.Status;
43 import org.onap.music.datastore.MusicDataStoreHandle;
44 import org.onap.music.datastore.PreparedQueryObject;
45 import org.onap.music.eelf.logging.EELFLoggerDelegate;
46 import org.onap.music.eelf.logging.format.AppMessages;
47 import org.onap.music.eelf.logging.format.ErrorSeverity;
48 import org.onap.music.eelf.logging.format.ErrorTypes;
49 import org.onap.music.main.MusicUtil;
50 import org.onap.music.main.ResultType;
51 import org.onap.music.main.ReturnType;
52 import org.onap.music.response.jsonobjects.JsonResponse;
53 import org.onap.music.conductor.*;
55 import com.datastax.driver.core.DataType;
56 import com.datastax.driver.core.TableMetadata;
58 import io.swagger.annotations.Api;
59 import io.swagger.annotations.ApiParam;
61 @Path("/v2/conditional")
62 @Api(value = "Conditional Api", hidden = true)
63 public class RestMusicConditionalAPI {
64 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicConditionalAPI.class);
65 private static final String XMINORVERSION = "X-minorVersion";
66 private static final String XPATCHVERSION = "X-patchVersion";
67 private static final String NS = "ns";
68 private static final String VERSION = "v2";
71 @Path("/insert/keyspaces/{keyspace}/tables/{tablename}")
72 @Consumes(MediaType.APPLICATION_JSON)
73 @Produces(MediaType.APPLICATION_JSON)
74 public Response insertConditional(
75 @ApiParam(value = "Major Version", required = true)
76 @PathParam("version") String version,
77 @ApiParam(value = "Minor Version", required = false)
78 @HeaderParam(XMINORVERSION) String minorVersion,
79 @ApiParam(value = "Patch Version", required = false)
80 @HeaderParam(XPATCHVERSION) String patchVersion,
81 @ApiParam(value = "AID", required = true)
82 @HeaderParam("aid") String aid,
83 @ApiParam(value = "Application namespace", required = true)
84 @HeaderParam(NS) String ns,
85 @ApiParam(value = "Authorization", required = true)
86 @HeaderParam("Authorization") String authorization,
87 @ApiParam(value = "Keyspace Name", required = true)
88 @PathParam("keyspace") String keyspace,
89 @ApiParam(value = "Table Name", required = true)
90 @PathParam("tablename") String tablename,
91 JsonConditional jsonObj) throws Exception {
92 ResponseBuilder response =
93 MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
94 String primaryKey = jsonObj.getPrimaryKey();
95 String primaryKeyValue = jsonObj.getPrimaryKeyValue();
96 String casscadeColumnName = jsonObj.getCasscadeColumnName();
97 Map<String, Object> tableValues = jsonObj.getTableValues();
98 Map<String, Object> casscadeColumnData = jsonObj.getCasscadeColumnData();
99 Map<String, Map<String, String>> conditions = jsonObj.getConditions();
101 if (primaryKey == null || primaryKeyValue == null || casscadeColumnName == null
102 || tableValues.isEmpty() || casscadeColumnData.isEmpty() || conditions.isEmpty()) {
103 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL,
104 ErrorTypes.AUTHENTICATIONERROR);
105 return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE)
106 .setError(String.valueOf("One or more input values missing")).toMap()).build();
108 Map<String, Object> authMap = null;
109 Map<String, Object> valuesMap = new LinkedHashMap<>();
110 for (Map.Entry<String, Object> entry : tableValues.entrySet()) {
111 valuesMap.put(entry.getKey(), entry.getValue());
114 Map<String, String> status = new HashMap<>();
115 status.put("exists", conditions.get("exists").get("status"));
116 status.put("nonexists", conditions.get("nonexists").get("status"));
117 ReturnType out = null;
119 out = MusicConditional.conditionalInsert(keyspace, tablename,
120 casscadeColumnName, casscadeColumnData,primaryKeyValue, valuesMap, status);
121 return response.status(Status.OK).entity(new JsonResponse(
122 out.getResult()).setMessage(out.getMessage()).toMap())
127 @SuppressWarnings("unchecked")
129 @Path("/update/keyspaces/{keyspace}/tables/{tablename}")
130 @Consumes(MediaType.APPLICATION_JSON)
131 @Produces(MediaType.APPLICATION_JSON)
132 public Response updateConditional(
133 @ApiParam(value = "Major Version", required = true)
134 @PathParam("version") String version,
135 @ApiParam(value = "Minor Version", required = false)
136 @HeaderParam(XMINORVERSION) String minorVersion,
137 @ApiParam(value = "Patch Version", required = false)
138 @HeaderParam(XPATCHVERSION) String patchVersion,
139 @ApiParam(value = "AID", required = true)
140 @HeaderParam("aid") String aid,
141 @ApiParam(value = "Application namespace", required = true)
142 @HeaderParam(NS) String ns,
143 @ApiParam(value = "Authorization", required = true)
144 @HeaderParam("Authorization") String authorization,
145 @ApiParam(value = "Major Version", required = true)
146 @PathParam("keyspace") String keyspace,
147 @ApiParam(value = "Major Version", required = true)
148 @PathParam("tablename") String tablename,
149 JsonConditional upObj) throws Exception {
150 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
152 String primaryKey = upObj.getPrimaryKey();
153 String primaryKeyValue = upObj.getPrimaryKeyValue();
154 String casscadeColumnName = upObj.getCasscadeColumnName();
155 Map<String, Object> casscadeColumnData = upObj.getCasscadeColumnData();
156 Map<String, Object> tableValues = upObj.getTableValues();
158 if (primaryKey == null || primaryKeyValue == null || casscadeColumnName == null
159 || casscadeColumnData.isEmpty()) {
160 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL,
161 ErrorTypes.AUTHENTICATIONERROR);
162 return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE)
163 .setError(String.valueOf("One or more input values missing")).toMap()).build();
167 Map<String,String> casscadeColumnValueMap =
168 (Map<String, String>) casscadeColumnData.get("value");
169 TableMetadata tableInfo = null;
170 tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
171 DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType();
172 String primaryId = tableInfo.getPrimaryKey().get(0).getName();
174 PreparedQueryObject select = new PreparedQueryObject();
175 select.appendQueryString("SELECT * FROM " + keyspace + "."
176 + tablename + " where " + primaryId + " = ?");
177 select.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKeyValue));
179 PreparedQueryObject upsert =
180 MusicConditional.extractQuery(tableValues, tableInfo, tablename,
181 keyspace, primaryKey, primaryKeyValue, null, null);
182 Map<String,PreparedQueryObject> queryBank = new HashMap<>();
183 queryBank.put(MusicUtil.SELECT, select);
184 queryBank.put(MusicUtil.UPSERT, upsert);
185 String planId = casscadeColumnData.get("key").toString();
186 ReturnType result = MusicConditional.update(new UpdateDataObject().setQueryBank(queryBank)
187 .setKeyspace(keyspace)
188 .setTableName(tablename)
189 .setPrimaryKey(primaryKey)
190 .setPrimaryKeyValue(primaryKeyValue)
192 .setCascadeColumnName(casscadeColumnName)
193 .setCascadeColumnValues(casscadeColumnValueMap));
194 if (result.getResult() == ResultType.SUCCESS) {
195 return response.status(Status.OK)
196 .entity(new JsonResponse(result.getResult())
197 .setMessage(result.getMessage()).toMap()).build();
199 return response.status(Status.BAD_REQUEST)
200 .entity(new JsonResponse(result.getResult())
201 .setMessage(result.getMessage()).toMap()).build();