2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
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.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.apihandlerinfra.tenantisolation;
25 import java.io.IOException;
26 import java.text.SimpleDateFormat;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.transaction.Transactional;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.MultivaluedMap;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriInfo;
42 import org.apache.http.HttpStatus;
43 import org.onap.so.apihandler.common.ErrorNumbers;
44 import org.onap.so.apihandler.common.ResponseBuilder;
45 import org.onap.so.apihandlerinfra.Constants;
46 import org.onap.so.apihandlerinfra.exceptions.ApiException;
47 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
48 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
49 import org.onap.so.apihandlerinfra.tenantisolationbeans.CloudOrchestrationRequestList;
50 import org.onap.so.apihandlerinfra.tenantisolationbeans.CloudOrchestrationResponse;
51 import org.onap.so.apihandlerinfra.tenantisolationbeans.InstanceReferences;
52 import org.onap.so.apihandlerinfra.tenantisolationbeans.Request;
53 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
54 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestStatus;
55 import org.onap.so.db.request.beans.InfraActiveRequests;
56 import org.onap.so.db.request.client.RequestsDbClient;
57 import org.onap.so.exceptions.ValidationException;
58 import org.onap.so.logger.ErrorCode;
59 import org.onap.so.logger.MessageEnum;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.stereotype.Component;
64 import com.fasterxml.jackson.databind.ObjectMapper;
65 import io.swagger.annotations.Api;
66 import io.swagger.annotations.ApiOperation;
69 @Path("onap/so/infra/cloudResourcesRequests")
70 @Api(value = "onap/so/infra/cloudResourcesRequests",
71 description = "API GET Requests for cloud resources - Tenant Isolation")
72 public class CloudResourcesOrchestration {
74 private static Logger logger = LoggerFactory.getLogger(CloudResourcesOrchestration.class);
77 RequestsDbClient requestDbClient;
80 private ResponseBuilder builder;
83 @Path("/{version: [vV][1]}/{requestId}/unlock")
84 @Consumes(MediaType.APPLICATION_JSON)
85 @Produces(MediaType.APPLICATION_JSON)
86 @ApiOperation(value = "Unlock CloudOrchestration requests for a specified requestId")
88 public Response unlockOrchestrationRequest(String requestJSON, @PathParam("requestId") String requestId,
89 @PathParam("version") String version) throws ApiException {
90 TenantIsolationRequest msoRequest = new TenantIsolationRequest(requestId);
91 InfraActiveRequests infraActiveRequest;
93 CloudOrchestrationRequest cor;
95 logger.debug("requestId is: {}", requestId);
98 ObjectMapper mapper = new ObjectMapper();
99 cor = mapper.readValue(requestJSON, CloudOrchestrationRequest.class);
100 } catch (IOException e) {
101 ErrorLoggerInfo errorLoggerInfo =
102 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
105 ValidateException validateException =
106 new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(),
107 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
108 .errorInfo(errorLoggerInfo).build();
109 throw validateException;
113 msoRequest.parseOrchestration(cor);
114 } catch (ValidationException e) {
115 ErrorLoggerInfo errorLoggerInfo =
116 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
118 ValidateException validateException =
119 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
120 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
121 throw validateException;
124 infraActiveRequest = requestDbClient.getInfraActiveRequestbyRequestId(requestId);
125 } catch (Exception e) {
126 ValidateException validateException = new ValidateException.Builder(e.getMessage(),
127 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).build();
128 throw validateException;
130 if (infraActiveRequest == null) {
132 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND,
133 ErrorCode.BusinessProcesssError).build();
134 ValidateException validateException =
135 new ValidateException.Builder("Orchestration RequestId " + requestId + " is not found in DB",
136 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
137 .errorInfo(errorLoggerInfo).build();
139 throw validateException;
142 String status = infraActiveRequest.getRequestStatus();
143 if ("IN_PROGRESS".equalsIgnoreCase(status) || "PENDING".equalsIgnoreCase(status)
144 || "PENDING_MANUAL_TASK".equalsIgnoreCase(status)) {
145 infraActiveRequest.setRequestStatus("UNLOCKED");
146 infraActiveRequest.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
147 infraActiveRequest.setRequestId(requestId);
148 requestDbClient.save(infraActiveRequest);
150 ErrorLoggerInfo errorLoggerInfo =
151 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError)
153 ValidateException validateException = new ValidateException.Builder(
154 "Orchestration RequestId " + requestId + " has a status of " + status
155 + " and can not be unlocked",
156 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo)
159 throw validateException;
163 return Response.status(HttpStatus.SC_NO_CONTENT).entity("").build();
167 @Path("/{version:[vV][1]}")
168 @Consumes(MediaType.APPLICATION_JSON)
169 @Produces(MediaType.APPLICATION_JSON)
170 @ApiOperation(value = "Get status of an Operational Environment based on filter criteria",
171 response = Response.class)
173 public Response getOperationEnvironmentStatusFilter(@Context UriInfo ui, @PathParam("version") String version)
174 throws ApiException {
176 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
177 List<String> requestIdKey = queryParams.get("requestId");
178 String apiVersion = version.substring(1);
180 if (queryParams.size() == 1 && requestIdKey != null) {
181 String requestId = requestIdKey.get(0);
183 CloudOrchestrationResponse cloudOrchestrationGetResponse = new CloudOrchestrationResponse();
184 InfraActiveRequests requestDB;
187 requestDB = requestDbClient.getInfraActiveRequestbyRequestId(requestId);
188 } catch (Exception e) {
189 ErrorLoggerInfo errorLoggerInfo =
190 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.AvailabilityError)
192 ValidateException validateException =
193 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR,
194 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e).errorInfo(errorLoggerInfo).build();
195 throw validateException;
198 if (requestDB == null) {
199 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR,
200 ErrorCode.BusinessProcesssError).build();
201 ValidateException validateException =
202 new ValidateException.Builder("Orchestration RequestId " + requestId + " is not found in DB",
203 HttpStatus.SC_NO_CONTENT, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
204 .errorInfo(errorLoggerInfo).build();
206 throw validateException;
209 Request request = mapInfraActiveRequestToRequest(requestDB);
210 cloudOrchestrationGetResponse.setRequest(request);
211 return builder.buildResponse(HttpStatus.SC_OK, requestId, cloudOrchestrationGetResponse, apiVersion);
214 TenantIsolationRequest tenantIsolationRequest = new TenantIsolationRequest();
215 List<InfraActiveRequests> activeRequests;
216 CloudOrchestrationRequestList orchestrationList;
219 Map<String, String> orchestrationMap;
221 orchestrationMap = tenantIsolationRequest.getOrchestrationFilters(queryParams);
222 } catch (ValidationException ex) {
223 ErrorLoggerInfo errorLoggerInfo =
224 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcesssError)
226 ValidateException validateException =
227 new ValidateException.Builder(ex.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR,
228 ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).cause(ex).errorInfo(errorLoggerInfo).build();
230 throw validateException;
233 activeRequests = requestDbClient.getCloudOrchestrationFiltersFromInfraActive(orchestrationMap);
234 orchestrationList = new CloudOrchestrationRequestList();
235 List<CloudOrchestrationResponse> requestLists = new ArrayList<>();
237 for (InfraActiveRequests infraActive : activeRequests) {
239 Request request = mapInfraActiveRequestToRequest(infraActive);
240 CloudOrchestrationResponse requestList = new CloudOrchestrationResponse();
241 requestList.setRequest(request);
242 requestLists.add(requestList);
244 orchestrationList.setRequestList(requestLists);
246 return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationList, apiVersion);
250 private Request mapInfraActiveRequestToRequest(InfraActiveRequests iar) throws ApiException {
251 Request request = new Request();
252 request.setRequestId(iar.getRequestId());
253 request.setRequestScope(iar.getRequestScope());
254 request.setRequestType(iar.getRequestAction());
256 InstanceReferences ir = new InstanceReferences();
258 if (iar.getOperationalEnvId() != null)
259 ir.setOperationalEnvironmentId(iar.getOperationalEnvId());
260 if (iar.getOperationalEnvName() != null)
261 ir.setOperationalEnvName(iar.getOperationalEnvName());
262 if (iar.getRequestorId() != null)
263 ir.setRequestorId(iar.getRequestorId());
265 request.setInstanceReferences(ir);
266 String requestBody = iar.getRequestBody();
267 RequestDetails requestDetails = null;
269 if (requestBody != null) {
271 ObjectMapper mapper = new ObjectMapper();
272 requestDetails = mapper.readValue(requestBody, RequestDetails.class);
273 } catch (IOException e) {
274 ErrorLoggerInfo errorLoggerInfo =
275 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
277 ValidateException validateException =
278 new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(),
279 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
280 .errorInfo(errorLoggerInfo).build();
281 throw validateException;
285 request.setRequestDetails(requestDetails);
286 String startTimeStamp = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(iar.getStartTime()) + " GMT";
287 request.setStartTime(startTimeStamp);
289 RequestStatus status = new RequestStatus();
290 if (iar.getStatusMessage() != null) {
291 status.setStatusMessage(iar.getStatusMessage());
294 if (iar.getEndTime() != null) {
295 String endTimeStamp = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(iar.getEndTime()) + " GMT";
296 status.setTimeStamp(endTimeStamp);
299 if (iar.getRequestStatus() != null) {
300 status.setRequestState(iar.getRequestStatus());
303 if (iar.getProgress() != null) {
304 status.setPercentProgress(iar.getProgress().toString());
307 request.setRequestStatus(status);