use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.
the class PublisherCommonUtils method changeApiOrApiProductLifecycle.
/**
* Change the lifecycle state of an API or API Product identified by UUID
*
* @param action LC state change action
* @param apiTypeWrapper API Type Wrapper (API or API Product)
* @param lcChecklist LC state change check list
* @param organization Organization of logged-in user
* @return APIStateChangeResponse
* @throws APIManagementException Exception if there is an error when changing the LC state of API or API Product
*/
public static APIStateChangeResponse changeApiOrApiProductLifecycle(String action, ApiTypeWrapper apiTypeWrapper, String lcChecklist, String organization) throws APIManagementException {
String[] checkListItems = lcChecklist != null ? lcChecklist.split(APIConstants.DELEM_COMMA) : new String[0];
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
Map<String, Object> apiLCData = apiProvider.getAPILifeCycleData(apiTypeWrapper.getUuid(), organization);
String[] nextAllowedStates = (String[]) apiLCData.get(APIConstants.LC_NEXT_STATES);
if (!ArrayUtils.contains(nextAllowedStates, action)) {
throw new APIManagementException("Action '" + action + "' is not allowed. Allowed actions are " + Arrays.toString(nextAllowedStates), ExceptionCodes.from(ExceptionCodes.UNSUPPORTED_LIFECYCLE_ACTION, action));
}
// check and set lifecycle check list items including "Deprecate Old Versions" and "Require Re-Subscription".
Map<String, Boolean> lcMap = new HashMap<>();
for (String checkListItem : checkListItems) {
String[] attributeValPair = checkListItem.split(APIConstants.DELEM_COLON);
if (attributeValPair.length == 2) {
String checkListItemName = attributeValPair[0].trim();
boolean checkListItemValue = Boolean.parseBoolean(attributeValPair[1].trim());
lcMap.put(checkListItemName, checkListItemValue);
}
}
try {
return apiProvider.changeLifeCycleStatus(organization, apiTypeWrapper, action, lcMap);
} catch (FaultGatewaysException e) {
throw new APIManagementException("Error while change the state of artifact with name - " + apiTypeWrapper.getName(), e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.
the class APIMappingUtil method toWorkflowResponseDTO.
/**
* Returns workflow state DTO from the provided information.
*
* @param lifecycleStateDTO Lifecycle state DTO
* @param stateChangeResponse workflow response from API lifecycle change
* @return workflow state DTO
*/
public static WorkflowResponseDTO toWorkflowResponseDTO(LifecycleStateDTO lifecycleStateDTO, APIStateChangeResponse stateChangeResponse) {
WorkflowResponseDTO workflowResponseDTO = new WorkflowResponseDTO();
if (WorkflowStatus.APPROVED.toString().equals(stateChangeResponse.getStateChangeStatus())) {
workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.APPROVED);
} else if (WorkflowStatus.CREATED.toString().equals(stateChangeResponse.getStateChangeStatus())) {
workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.CREATED);
} else if ((WorkflowStatus.REGISTERED.toString().equals(stateChangeResponse.getStateChangeStatus()))) {
workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.REGISTERED);
} else if ((WorkflowStatus.REJECTED.toString().equals(stateChangeResponse.getStateChangeStatus()))) {
workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.REJECTED);
} else {
log.error("Unrecognized state : " + stateChangeResponse.getStateChangeStatus());
workflowResponseDTO.setWorkflowStatus(WorkflowResponseDTO.WorkflowStatusEnum.CREATED);
}
workflowResponseDTO.setLifecycleState(lifecycleStateDTO);
return workflowResponseDTO;
}
use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method changeAPILifecycle.
@Override
public Response changeAPILifecycle(String action, String apiId, String lifecycleChecklist, String ifMatch, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
ApiTypeWrapper apiWrapper = new ApiTypeWrapper(apiProvider.getAPIbyUUID(apiId, organization));
APIStateChangeResponse stateChangeResponse = PublisherCommonUtils.changeApiOrApiProductLifecycle(action, apiWrapper, lifecycleChecklist, organization);
// returns the current lifecycle state
// todo try to prevent this call
LifecycleStateDTO stateDTO = getLifecycleState(apiId, organization);
WorkflowResponseDTO workflowResponseDTO = APIMappingUtil.toWorkflowResponseDTO(stateDTO, stateChangeResponse);
return Response.ok().entity(workflowResponseDTO).build();
} catch (APIManagementException e) {
// Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while updating the lifecycle of API " + apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Error while updating lifecycle of API " + apiId, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method changeAPIProductLifecycle.
@Override
public Response changeAPIProductLifecycle(String action, String apiProductId, String lifecycleChecklist, String ifMatch, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
ApiTypeWrapper productWrapper = new ApiTypeWrapper(apiProvider.getAPIProductbyUUID(apiProductId, organization));
APIStateChangeResponse stateChangeResponse = PublisherCommonUtils.changeApiOrApiProductLifecycle(action, productWrapper, lifecycleChecklist, organization);
LifecycleStateDTO stateDTO = getLifecycleState(apiProductId, organization);
WorkflowResponseDTO workflowResponseDTO = APIMappingUtil.toWorkflowResponseDTO(stateDTO, stateChangeResponse);
return Response.ok().entity(workflowResponseDTO).build();
}
use of org.wso2.carbon.apimgt.api.model.APIStateChangeResponse in project carbon-apimgt by wso2.
the class APIProviderImpl method changeLifeCycleStatus.
/**
* This method is to change registry lifecycle states for an API or API Product artifact
*
* @param orgId UUID of the organization
* @param apiTypeWrapper API Type Wrapper
* @param action Action which need to execute from registry lifecycle
* @param checklist checklist items
* @return APIStateChangeResponse API workflow state and WorkflowResponse
*/
@Override
public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper apiTypeWrapper, String action, Map<String, Boolean> checklist) throws APIManagementException, FaultGatewaysException {
APIStateChangeResponse response = new APIStateChangeResponse();
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
String targetStatus;
String providerName;
String apiName;
String apiContext;
String apiType;
String apiVersion;
String currentStatus;
String uuid;
int apiOrApiProductId;
boolean isApiProduct = apiTypeWrapper.isAPIProduct();
String workflowType;
if (isApiProduct) {
APIProduct apiProduct = apiTypeWrapper.getApiProduct();
providerName = apiProduct.getId().getProviderName();
apiName = apiProduct.getId().getName();
apiContext = apiProduct.getContext();
apiType = apiProduct.getType();
apiVersion = apiProduct.getId().getVersion();
currentStatus = apiProduct.getState();
uuid = apiProduct.getUuid();
apiOrApiProductId = apiMgtDAO.getAPIProductId(apiTypeWrapper.getApiProduct().getId());
workflowType = WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE;
} else {
API api = apiTypeWrapper.getApi();
providerName = api.getId().getProviderName();
apiName = api.getId().getApiName();
apiContext = api.getContext();
apiType = api.getType();
apiVersion = api.getId().getVersion();
currentStatus = api.getStatus();
uuid = api.getUuid();
apiOrApiProductId = apiMgtDAO.getAPIID(uuid);
workflowType = WorkflowConstants.WF_TYPE_AM_API_STATE;
}
String gatewayVendor = apiMgtDAO.getGatewayVendorByAPIUUID(uuid);
WorkflowStatus apiWFState = null;
WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
if (wfDTO != null) {
apiWFState = wfDTO.getStatus();
}
// if the workflow has started, then executor should not fire again
if (!WorkflowStatus.CREATED.equals(apiWFState)) {
response = executeStateChangeWorkflow(currentStatus, action, apiName, apiContext, apiType, apiVersion, providerName, apiOrApiProductId, uuid, gatewayVendor, workflowType);
// get the workflow state once the executor is executed.
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
if (wfDTO != null) {
apiWFState = wfDTO.getStatus();
response.setStateChangeStatus(apiWFState.toString());
} else {
response.setStateChangeStatus(WorkflowStatus.APPROVED.toString());
}
}
// apiWFState is null when simple wf executor is used because wf state is not stored in the db.
if (WorkflowStatus.APPROVED.equals(apiWFState) || apiWFState == null) {
targetStatus = LCManagerFactory.getInstance().getLCManager().getStateForTransition(action);
apiPersistenceInstance.changeAPILifeCycle(new Organization(orgId), uuid, targetStatus);
sendLCStateChangeNotification(apiName, apiType, apiContext, apiVersion, targetStatus, providerName, apiOrApiProductId, uuid);
if (!isApiProduct) {
API api = apiTypeWrapper.getApi();
api.setOrganization(orgId);
changeLifeCycle(api, currentStatus, targetStatus, checklist);
// Sending Notifications to existing subscribers
if (APIConstants.PUBLISHED.equals(targetStatus)) {
sendEmailNotification(api);
}
} else {
APIProduct apiProduct = apiTypeWrapper.getApiProduct();
apiProduct.setOrganization(orgId);
changeLifecycle(apiProduct, currentStatus, targetStatus);
}
addLCStateChangeInDatabase(currentStatus, targetStatus, uuid);
if (log.isDebugEnabled()) {
String logMessage = "LC Status changed successfully for artifact with name: " + apiName + ", version " + apiVersion + ", New Status : " + targetStatus;
log.debug(logMessage);
}
extractRecommendationDetails(apiTypeWrapper);
return response;
}
} catch (APIPersistenceException e) {
handleException("Error while accessing persistence layer", e);
} catch (PersistenceException e) {
handleException("Error while accessing lifecycle information ", e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return response;
}
Aggregations