2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.apihandlerinfra;
23 import java.io.IOException;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.List;
29 import javax.transaction.Transactional;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriInfo;
42 import org.apache.commons.lang.StringUtils;
43 import org.apache.http.HttpStatus;
44 import org.onap.so.apihandler.common.ErrorNumbers;
45 import org.onap.so.apihandler.common.ResponseBuilder;
46 import org.onap.so.apihandlerinfra.exceptions.ApiException;
47 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
48 import org.onap.so.apihandlerinfra.logging.AlarmLoggerInfo;
49 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
50 import org.onap.so.db.request.beans.InfraActiveRequests;
51 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
52 import org.onap.so.exceptions.ValidationException;
53 import org.onap.so.logger.MessageEnum;
54 import org.onap.so.logger.MsoAlarmLogger;
55 import org.onap.so.logger.MsoLogger;
56 import org.onap.so.serviceinstancebeans.GetOrchestrationListResponse;
57 import org.onap.so.serviceinstancebeans.GetOrchestrationResponse;
58 import org.onap.so.serviceinstancebeans.InstanceReferences;
59 import org.onap.so.serviceinstancebeans.Request;
60 import org.onap.so.serviceinstancebeans.RequestDetails;
61 import org.onap.so.serviceinstancebeans.RequestList;
62 import org.onap.so.serviceinstancebeans.RequestStatus;
63 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.stereotype.Component;
67 import com.fasterxml.jackson.databind.ObjectMapper;
69 import io.swagger.annotations.Api;
70 import io.swagger.annotations.ApiOperation;
72 @Path("onap/so/infra/orchestrationRequests")
73 @Api(value="onap/so/infra/orchestrationRequests",description="API Requests for Orchestration requests")
75 public class OrchestrationRequests {
77 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH, OrchestrationRequests.class);
79 private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger ();
82 private InfraActiveRequestsRepository infraActiveRequestsRepository;
85 private MsoRequest msoRequest;
88 private ResponseBuilder builder;
91 @Path("/{version:[vV][4-7]}/{requestId}")
92 @ApiOperation(value="Find Orchestrated Requests for a given requestId",response=Response.class)
93 @Produces(MediaType.APPLICATION_JSON)
95 public Response getOrchestrationRequest(@PathParam("requestId") String requestId, @PathParam("version") String version) throws ApiException{
97 String apiVersion = version.substring(1);
98 GetOrchestrationResponse orchestrationResponse = new GetOrchestrationResponse();
101 InfraActiveRequests requestDB = null;
104 requestDB = infraActiveRequestsRepository.findOneByRequestIdOrClientRequestId(requestId, requestId);
105 } catch (Exception e) {
107 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, MsoLogger.ErrorCode.AvailabilityError).build();
108 AlarmLoggerInfo alarmLoggerInfo = new AlarmLoggerInfo.Builder("MsoDatabaseAccessError", MsoAlarmLogger.CRITICAL, Messages.errors.get(ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB)).build();
112 ValidateException validateException = new ValidateException.Builder("Exception while communciate with Request DB - Infra Request Lookup",
113 HttpStatus.SC_NOT_FOUND,ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB).cause(e).errorInfo(errorLoggerInfo).alarmInfo(alarmLoggerInfo).build();
115 throw validateException;
119 if(requestDB == null) {
121 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, MsoLogger.ErrorCode.BusinessProcesssError).build();
124 ValidateException validateException = new ValidateException.Builder("Orchestration RequestId " + requestId + " is not found in DB",
125 HttpStatus.SC_NO_CONTENT, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
127 throw validateException;
130 Request request = mapInfraActiveRequestToRequest(requestDB);
132 orchestrationResponse.setRequest(request);
134 return builder.buildResponse(HttpStatus.SC_OK, requestId, orchestrationResponse, apiVersion);
138 @Path("/{version:[vV][4-7]}")
139 @ApiOperation(value="Find Orchestrated Requests for a URI Information",response=Response.class)
140 @Produces(MediaType.APPLICATION_JSON)
142 public Response getOrchestrationRequest(@Context UriInfo ui, @PathParam("version") String version) throws ApiException{
144 long startTime = System.currentTimeMillis ();
146 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
148 List<InfraActiveRequests> activeRequests = null;
150 GetOrchestrationListResponse orchestrationList = null;
151 Map<String, List<String>> orchestrationMap;
152 String apiVersion = version.substring(1);
155 orchestrationMap = msoRequest.getOrchestrationFilters(queryParams);
156 if (orchestrationMap.isEmpty()) {
157 throw new ValidationException("At least one filter query param must be specified");
159 }catch(ValidationException ex){
160 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, MsoLogger.ErrorCode.DataError).build();
163 ValidateException validateException = new ValidateException.Builder(ex.getMessage(),
164 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).cause(ex).errorInfo(errorLoggerInfo).build();
166 throw validateException;
170 activeRequests = infraActiveRequestsRepository.getOrchestrationFiltersFromInfraActive(orchestrationMap);
172 orchestrationList = new GetOrchestrationListResponse();
173 List<RequestList> requestLists = new ArrayList<>();
175 for(InfraActiveRequests infraActive : activeRequests){
176 RequestList requestList = new RequestList();
177 Request request = mapInfraActiveRequestToRequest(infraActive);
178 requestList.setRequest(request);
179 requestLists.add(requestList);
182 orchestrationList.setRequestList(requestLists);
183 return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationList, apiVersion);
188 @Path("/{version: [vV][4-7]}/{requestId}/unlock")
189 @Consumes(MediaType.APPLICATION_JSON)
190 @Produces(MediaType.APPLICATION_JSON)
191 @ApiOperation(value="Unlock Orchestrated Requests for a given requestId",response=Response.class)
193 public Response unlockOrchestrationRequest(String requestJSON, @PathParam("requestId") String requestId, @PathParam("version") String version) throws ApiException{
195 long startTime = System.currentTimeMillis ();
196 msoLogger.debug ("requestId is: " + requestId);
197 ServiceInstancesRequest sir = null;
199 InfraActiveRequests infraActiveRequest = null;
200 Request request = null;
203 ObjectMapper mapper = new ObjectMapper();
204 sir = mapper.readValue(requestJSON, ServiceInstancesRequest.class);
205 } catch(IOException e){
207 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).build();
210 ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed : " + e.getMessage(),
211 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
213 throw validateException;
217 msoRequest.parseOrchestration(sir);
218 } catch (Exception e) {
219 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).build();
220 ValidateException validateException = new ValidateException.Builder("Error parsing request: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
221 .errorInfo(errorLoggerInfo).build();
222 throw validateException;
225 infraActiveRequest = infraActiveRequestsRepository.findOneByRequestIdOrClientRequestId(requestId, requestId);
226 if(infraActiveRequest == null) {
227 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, MsoLogger.ErrorCode.BusinessProcesssError).build();
230 ValidateException validateException = new ValidateException.Builder("Null response from RequestDB when searching by RequestId",
231 HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
233 throw validateException;
236 String status = infraActiveRequest.getRequestStatus();
237 if(status.equalsIgnoreCase("IN_PROGRESS") || status.equalsIgnoreCase("PENDING") || status.equalsIgnoreCase("PENDING_MANUAL_TASK")){
238 infraActiveRequest.setRequestStatus("UNLOCKED");
239 infraActiveRequest.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
240 infraActiveRequestsRepository.save(infraActiveRequest);
243 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, MsoLogger.ErrorCode.DataError).build();
246 ValidateException validateException = new ValidateException.Builder("Orchestration RequestId " + requestId + " has a status of " + status + " and can not be unlocked",
247 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
249 throw validateException;
252 return Response.status (HttpStatus.SC_NO_CONTENT).entity ("").build ();
255 private Request mapInfraActiveRequestToRequest(InfraActiveRequests iar) throws ApiException{
257 String requestBody = iar.getRequestBody();
258 Request request = new Request();
260 ObjectMapper mapper = new ObjectMapper();
262 request.setRequestId(iar.getRequestId());
263 request.setRequestScope(iar.getRequestScope());
264 request.setRequestType(iar.getRequestAction());
266 InstanceReferences ir = new InstanceReferences();
267 if(iar.getNetworkId() != null)
268 ir.setNetworkInstanceId(iar.getNetworkId());
269 if(iar.getNetworkName() != null)
270 ir.setNetworkInstanceName(iar.getNetworkName());
271 if(iar.getServiceInstanceId() != null)
272 ir.setServiceInstanceId(iar.getServiceInstanceId());
273 if(iar.getServiceInstanceName() != null)
274 ir.setServiceInstanceName(iar.getServiceInstanceName());
275 if(iar.getVfModuleId() != null)
276 ir.setVfModuleInstanceId(iar.getVfModuleId());
277 if(iar.getVfModuleName() != null)
278 ir.setVfModuleInstanceName(iar.getVfModuleName());
279 if(iar.getVnfId() != null)
280 ir.setVnfInstanceId(iar.getVnfId());
281 if(iar.getVnfName() != null)
282 ir.setVnfInstanceName(iar.getVnfName());
283 if(iar.getVolumeGroupId() != null)
284 ir.setVolumeGroupInstanceId(iar.getVolumeGroupId());
285 if(iar.getVolumeGroupName() != null)
286 ir.setVolumeGroupInstanceName(iar.getVolumeGroupName());
287 if(iar.getRequestorId() != null)
288 ir.setRequestorId(iar.getRequestorId());
291 request.setInstanceReferences(ir);
293 RequestDetails requestDetails = null;
295 if(StringUtils.isNotBlank(requestBody)) {
297 if(requestBody.contains("\"requestDetails\":")){
298 ServiceInstancesRequest sir = mapper.readValue(requestBody, ServiceInstancesRequest.class);
299 requestDetails = sir.getRequestDetails();
301 requestDetails = mapper.readValue(requestBody, RequestDetails.class);
303 } catch (IOException e) {
305 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).build();
306 ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed : ",
307 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
309 throw validateException;
312 request.setRequestDetails(requestDetails);
314 if(iar.getStartTime() != null) {
315 String startTimeStamp = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(iar.getStartTime()) + " GMT";
316 request.setStartTime(startTimeStamp);
319 RequestStatus status = new RequestStatus();
320 if(iar.getStatusMessage() != null){
321 status.setStatusMessage(iar.getStatusMessage());
324 if(iar.getEndTime() != null){
325 String endTimeStamp = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(iar.getEndTime()) + " GMT";
326 status.setFinishTime(endTimeStamp);
330 if(iar.getRequestStatus() != null){
331 status.setRequestState(iar.getRequestStatus());
334 if(iar.getProgress() != null){
335 status.setPercentProgress(iar.getProgress().intValue());
338 request.setRequestStatus(status);