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.tenantisolation;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.wordnik.swagger.annotations.Api;
25 import com.wordnik.swagger.annotations.ApiOperation;
26 import org.apache.http.HttpStatus;
27 import org.onap.so.apihandler.common.ErrorNumbers;
28 import org.onap.so.apihandler.common.ResponseBuilder;
29 import org.onap.so.apihandlerinfra.Constants;
30 import org.onap.so.apihandlerinfra.Messages;
31 import org.onap.so.apihandlerinfra.RequestsDbClient;
32 import org.onap.so.apihandlerinfra.exceptions.ApiException;
33 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
34 import org.onap.so.apihandlerinfra.logging.AlarmLoggerInfo;
35 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
36 import org.onap.so.apihandlerinfra.tenantisolationbeans.*;
37 import org.onap.so.apihandlerinfra.tenantisolationbeans.Request;
38 import org.onap.so.db.request.beans.InfraActiveRequests;
39 import org.onap.so.exceptions.ValidationException;
40 import org.onap.so.logger.MessageEnum;
41 import org.onap.so.logger.MsoAlarmLogger;
42 import org.onap.so.logger.MsoLogger;
43 import org.onap.so.utils.UUIDChecker;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Component;
47 import javax.transaction.Transactional;
49 import javax.ws.rs.core.*;
51 import java.io.IOException;
52 import java.text.SimpleDateFormat;
53 import java.util.ArrayList;
54 import java.util.List;
58 @Path("onap/so/infra/cloudResourcesRequests")
59 @Api(value="onap/so/infra/cloudResourcesRequests",description="API GET Requests for cloud resources - Tenant Isolation")
60 public class CloudResourcesOrchestration {
62 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH, CloudResourcesOrchestration.class);
65 RequestsDbClient requestDbClient;
68 private ResponseBuilder builder;
71 @Path("/{version: [vV][1]}/{requestId}/unlock")
72 @Consumes(MediaType.APPLICATION_JSON)
73 @Produces(MediaType.APPLICATION_JSON)
74 @ApiOperation(value="Unlock CloudOrchestration requests for a specified requestId")
76 public Response unlockOrchestrationRequest(String requestJSON, @PathParam("requestId") String requestId, @PathParam("version") String version) throws ApiException{
77 TenantIsolationRequest msoRequest = new TenantIsolationRequest(requestId);
78 InfraActiveRequests infraActiveRequest = null;
80 CloudOrchestrationRequest cor = null;
82 msoLogger.debug ("requestId is: " + requestId);
85 ObjectMapper mapper = new ObjectMapper();
86 cor = mapper.readValue(requestJSON, CloudOrchestrationRequest.class);
87 } catch(IOException e){
88 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,MsoLogger.ErrorCode.SchemaError).build();
90 ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
91 .cause(e).errorInfo(errorLoggerInfo).build();
92 throw validateException;
96 msoRequest.parseOrchestration(cor);
97 } catch (ValidationException e) {
98 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR,MsoLogger.ErrorCode.SchemaError).build();
99 ValidateException validateException = new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_BAD_PARAMETER)
100 .cause(e).errorInfo(errorLoggerInfo).build();
101 throw validateException;
104 infraActiveRequest = requestDbClient.getInfraActiveRequestbyRequestId(requestId);
106 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, MsoLogger.ErrorCode.AvailabilityError).build();
107 AlarmLoggerInfo alarmLoggerInfo = new AlarmLoggerInfo.Builder("MsoDatabaseAccessError", MsoAlarmLogger.CRITICAL,
108 Messages.getErrors().get (ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB)).build();
109 ValidateException validateException = new ValidateException.Builder(e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
110 .errorInfo(errorLoggerInfo).alarmInfo(alarmLoggerInfo).build();
112 throw validateException;
114 if(infraActiveRequest == null) {
116 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND,MsoLogger.ErrorCode.BusinessProcesssError).build();
117 ValidateException validateException = new ValidateException.Builder("Orchestration RequestId " + requestId + " is not found in DB", HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
118 .errorInfo(errorLoggerInfo).build();
120 throw validateException;
123 String status = infraActiveRequest.getRequestStatus();
124 if(status.equalsIgnoreCase("IN_PROGRESS") || status.equalsIgnoreCase("PENDING") || status.equalsIgnoreCase("PENDING_MANUAL_TASK")){
125 infraActiveRequest.setRequestStatus("UNLOCKED");
126 infraActiveRequest.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
127 requestDbClient.save(infraActiveRequest);
129 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND,MsoLogger.ErrorCode.DataError).build();
130 ValidateException validateException = new ValidateException.Builder("Orchestration RequestId " + requestId + " has a status of " + status + " and can not be unlocked",
131 HttpStatus.SC_BAD_REQUEST,ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
133 throw validateException;
137 return Response.status (HttpStatus.SC_NO_CONTENT).entity ("").build ();
141 @Path("/{version:[vV][1]}")
142 @Consumes(MediaType.APPLICATION_JSON)
143 @Produces(MediaType.APPLICATION_JSON)
144 @ApiOperation(value="Get status of an Operational Environment based on filter criteria",response=Response.class)
146 public Response getOperationEnvironmentStatusFilter(@Context UriInfo ui, @PathParam("version") String version ) throws ApiException{
147 MsoLogger.setServiceName ("getOperationEnvironmentStatusFilter");
148 UUIDChecker.generateUUID(msoLogger);
150 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
151 List<String> requestIdKey = queryParams.get("requestId");
152 String apiVersion = version.substring(1);
154 if(queryParams.size() == 1 && requestIdKey != null) {
155 String requestId = requestIdKey.get(0);
157 CloudOrchestrationResponse cloudOrchestrationGetResponse = new CloudOrchestrationResponse();
158 TenantIsolationRequest tenantIsolationRequest = new TenantIsolationRequest (requestId);
159 InfraActiveRequests requestDB = null;
162 requestDB = requestDbClient.getInfraActiveRequestbyRequestId(requestId);
163 } catch (Exception e) {
164 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, MsoLogger.ErrorCode.AvailabilityError).build();
165 AlarmLoggerInfo alarmLoggerInfo = new AlarmLoggerInfo.Builder("MsoDatabaseAccessError", MsoAlarmLogger.CRITICAL,
166 Messages.getErrors().get (ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB)).build();
167 ValidateException validateException = new ValidateException.Builder(e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
168 .errorInfo(errorLoggerInfo).alarmInfo(alarmLoggerInfo).build();
170 throw validateException;
171 // TODO Will need to set Status for tenantIsolationRequest
172 // tenantIsolationRequest.setStatus (org.onap.so.apihandlerinfra.vnfbeans.RequestStatusType.FAILED);
175 if(requestDB == null) {
176 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, MsoLogger.ErrorCode.BusinessProcesssError).build();
177 ValidateException validateException = new ValidateException.Builder("Orchestration RequestId " + requestId + " is not found in DB",
178 HttpStatus.SC_NO_CONTENT, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
179 .errorInfo(errorLoggerInfo).build();
181 throw validateException;
184 Request request = mapInfraActiveRequestToRequest(requestDB);
185 cloudOrchestrationGetResponse.setRequest(request);
186 return builder.buildResponse(HttpStatus.SC_OK, requestId, cloudOrchestrationGetResponse, apiVersion);
189 TenantIsolationRequest tenantIsolationRequest = new TenantIsolationRequest ();
190 List<InfraActiveRequests> activeRequests = null;
191 CloudOrchestrationRequestList orchestrationList = null;
194 Map<String, String> orchestrationMap;
196 orchestrationMap = tenantIsolationRequest.getOrchestrationFilters(queryParams);
197 }catch(ValidationException ex){
198 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.BusinessProcesssError).build();
199 ValidateException validateException = new ValidateException.Builder(ex.getMessage(),
200 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).cause(ex)
201 .errorInfo(errorLoggerInfo).build();
203 throw validateException;
206 activeRequests = requestDbClient.getCloudOrchestrationFiltersFromInfraActive(orchestrationMap);
207 orchestrationList = new CloudOrchestrationRequestList();
208 List<CloudOrchestrationResponse> requestLists = new ArrayList<CloudOrchestrationResponse>();
210 for(InfraActiveRequests infraActive : activeRequests){
212 Request request = mapInfraActiveRequestToRequest(infraActive);
213 CloudOrchestrationResponse requestList = new CloudOrchestrationResponse();
214 requestList.setRequest(request);
215 requestLists.add(requestList);
217 orchestrationList.setRequestList(requestLists);
219 return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationList, apiVersion);
223 private Request mapInfraActiveRequestToRequest(InfraActiveRequests iar) throws ApiException {
224 Request request = new Request();
225 request.setRequestId(iar.getRequestId());
226 request.setRequestScope(iar.getRequestScope());
227 request.setRequestType(iar.getRequestAction());
229 InstanceReferences ir = new InstanceReferences();
231 if(iar.getOperationalEnvId() != null)
232 ir.setOperationalEnvironmentId(iar.getOperationalEnvId());
233 if(iar.getOperationalEnvName() != null)
234 ir.setOperationalEnvName(iar.getOperationalEnvName());
235 if(iar.getRequestorId() != null)
236 ir.setRequestorId(iar.getRequestorId());
238 request.setInstanceReferences(ir);
239 String requestBody = iar.getRequestBody();
240 RequestDetails requestDetails = null;
242 if (requestBody != null) {
244 ObjectMapper mapper = new ObjectMapper();
245 requestDetails = mapper.readValue(requestBody, RequestDetails.class);
246 } catch (IOException e) {
247 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).build();
248 ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER)
249 .cause(e).errorInfo(errorLoggerInfo).build();
250 throw validateException;
254 request.setRequestDetails(requestDetails);
255 String startTimeStamp = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(iar.getStartTime()) + " GMT";
256 request.setStartTime(startTimeStamp);
258 RequestStatus status = new RequestStatus();
259 if(iar.getStatusMessage() != null){
260 status.setStatusMessage(iar.getStatusMessage());
263 if(iar.getEndTime() != null){
264 String endTimeStamp = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss").format(iar.getEndTime()) + " GMT";
265 status.setTimeStamp(endTimeStamp);
268 if(iar.getRequestStatus() != null){
269 status.setRequestState(iar.getRequestStatus());
272 if(iar.getProgress() != null){
273 status.setPercentProgress(iar.getProgress().toString());
276 request.setRequestStatus(status);