Search in sources :

Example 36 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class WorkflowUtils method completeStateChangeWorkflow.

/**
 * Complete the lifecycle state change workflow
 *
 * @param workflowDTO Workflow DTO object
 * @throws WorkflowException Exception when completing the workflow
 */
protected static void completeStateChangeWorkflow(WorkflowDTO workflowDTO) throws WorkflowException {
    String externalWorkflowRef = workflowDTO.getExternalWorkflowReference();
    try {
        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
        Workflow workflow = apiMgtDAO.getworkflowReferenceByExternalWorkflowReference(externalWorkflowRef);
        String apiName = workflow.getMetadata("ApiName");
        String action = workflow.getMetadata("Action");
        String providerName = workflow.getMetadata("ApiProvider");
        String version = workflow.getMetadata("ApiVersion");
        String invoker = workflow.getMetadata("Invoker");
        String currentStatus = workflow.getMetadata("CurrentState");
        int tenantId = workflowDTO.getTenantId();
        try {
            // tenant flow is already started from the rest api service impl. no need to start from here
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(invoker);
            Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(invoker, tenantId);
            APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, version);
            GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
            if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
                String targetStatus;
                apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
                targetStatus = apiArtifact.getLifecycleState();
                if (!currentStatus.equals(targetStatus)) {
                    apiMgtDAO.recordAPILifeCycleEvent(apiArtifact.getId(), currentStatus.toUpperCase(), targetStatus.toUpperCase(), invoker, tenantId);
                }
                if (log.isDebugEnabled()) {
                    String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API " + "Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
                    log.debug(logMessage);
                }
            }
        } catch (RegistryException e) {
            String errorMsg = "Could not complete api state change workflow";
            log.error(errorMsg, e);
            throw new WorkflowException(errorMsg, e);
        }
    } catch (APIManagementException e) {
        String errorMsg = "Could not complete api state change workflow";
        log.error(errorMsg, e);
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Workflow(org.wso2.carbon.apimgt.api.model.Workflow) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 37 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class TenantWorkflowConfigHolder method load.

public void load() throws WorkflowException, RegistryException {
    workflowExecutorMap = new ConcurrentHashMap<>();
    InputStream in = null;
    try {
        String workFlowConfig = ServiceReferenceHolder.getInstance().getApimConfigService().getWorkFlowConfig(tenantDomain);
        if (StringUtils.isNotEmpty(workFlowConfig)) {
            in = new ByteArrayInputStream(workFlowConfig.getBytes());
            StAXOMBuilder builder = new StAXOMBuilder(in);
            secretResolver = SecretResolverFactory.create(builder.getDocumentElement(), true);
            OMElement workflowExtensionsElem = builder.getDocument().getFirstChildWithName(new QName(WorkflowConstants.WORKFLOW_EXTENSIONS));
            OMElement workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.APPLICATION_CREATION));
            String executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            Class clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            WorkflowExecutor workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION, workFlowExecutor);
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.PRODUCTION_APPLICATION_REGISTRATION));
            executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_PRODUCTION, workFlowExecutor);
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SANDBOX_APPLICATION_REGISTRATION));
            executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_SANDBOX, workFlowExecutor);
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.USER_SIGN_UP));
            executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_USER_SIGNUP, workFlowExecutor);
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_CREATION));
            executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION, workFlowExecutor);
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_UPDATE));
            if (workflowElem != null) {
                executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
                clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
                workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
                loadProperties(workflowElem, workFlowExecutor);
                workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE, workFlowExecutor);
            } else {
                executorClass = DEFAULT_SUBSCRIPTION_UPDATE_EXECUTOR_CLASS;
                clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
                workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
                workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE, workFlowExecutor);
            }
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_DELETION));
            executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION, workFlowExecutor);
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.APPLICATION_DELETION));
            executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION, workFlowExecutor);
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.API_STATE_CHANGE));
            if (workflowElem == null) {
                // TO handle migrated environment, create the default simple workflow executor
                workflowElem = OMAbstractFactory.getOMFactory().createOMElement(new QName(WorkflowConstants.API_STATE_CHANGE));
                executorClass = WorkflowConstants.DEFAULT_EXECUTOR_API_STATE_CHANGE;
                workflowElem.addAttribute(WorkflowConstants.EXECUTOR, executorClass, null);
            } else {
                executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            }
            clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_API_STATE, workFlowExecutor);
            workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.API_PRODUCT_STATE_CHANGE));
            if (workflowElem == null) {
                // To handle migration, create the default simple workflow executor
                workflowElem = OMAbstractFactory.getOMFactory().createOMElement(new QName(WorkflowConstants.API_PRODUCT_STATE_CHANGE));
                executorClass = WorkflowConstants.DEFAULT_EXECUTOR_API_PRODUCT_STATE_CHANGE;
                workflowElem.addAttribute(WorkflowConstants.EXECUTOR, executorClass, null);
            } else {
                executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
            }
            clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
            workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
            loadProperties(workflowElem, workFlowExecutor);
            workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE, workFlowExecutor);
        }
    } catch (XMLStreamException e) {
        log.error("Error building xml", e);
        handleException("Error building xml", e);
    } catch (ClassNotFoundException e) {
        log.error("Unable to find class", e);
        handleException("Unable to find class", e);
    } catch (InstantiationException e) {
        log.error("Unable to instantiate class", e);
        handleException("Unable to instantiate class", e);
    } catch (IllegalAccessException e) {
        log.error("Illegal attempt to invoke class methods", e);
        handleException("Illegal attempt to invoke class methods", e);
    } catch (WorkflowException e) {
        log.error("Unable to load workflow executor class", e);
        handleException("Unable to load workflow executor class", e);
    } catch (APIManagementException e) {
        log.error("Unable to retrieve workflow configurations", e);
        handleException("Unable to retrieve workflow configurations", e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) XMLStreamException(javax.xml.stream.XMLStreamException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder)

Example 38 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class APIStateChangeWSWorkflowExecutor method complete.

/**
 * Complete the API state change workflow process.
 */
@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Completing API State change Workflow..");
        log.debug("response: " + workflowDTO.toString());
    }
    workflowDTO.setUpdatedTime(System.currentTimeMillis());
    super.complete(workflowDTO);
    String action = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_API_LC_ACTION);
    String apiName = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APINAME);
    String providerName = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APIPROVIDER);
    String version = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APIVERSION);
    String invoker = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_INVOKER);
    String currentStatus = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APISTATE);
    int tenantId = workflowDTO.getTenantId();
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    try {
        // tenant flow is already started from the rest api service impl. no need to start from here
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(invoker);
        Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(invoker, tenantId);
        APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, version);
        GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
        if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
            String targetStatus;
            apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
            targetStatus = apiArtifact.getLifecycleState();
            if (!currentStatus.equals(targetStatus)) {
                apiMgtDAO.recordAPILifeCycleEvent(apiArtifact.getId(), currentStatus.toUpperCase(), targetStatus.toUpperCase(), invoker, tenantId);
            }
            if (log.isDebugEnabled()) {
                String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
                log.debug(logMessage);
            }
        }
    } catch (RegistryException e) {
        String errorMsg = "Could not complete api state change workflow";
        log.error(errorMsg, e);
        throw new WorkflowException(errorMsg, e);
    } catch (APIManagementException e) {
        String errorMsg = "Could not complete api state change workflow";
        log.error(errorMsg, e);
        throw new WorkflowException(errorMsg, e);
    }
    return new GeneralWorkflowResponse();
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 39 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class WorkflowExecutorFactory method getWorkflowConfigurations.

public TenantWorkflowConfigHolder getWorkflowConfigurations() throws WorkflowException {
    String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    String cacheName = tenantDomain + "_" + APIConstants.WORKFLOW_CACHE_NAME;
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    // synchronized (cacheName.intern()){
    Cache workflowCache = Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER).getCache(APIConstants.WORKFLOW_CACHE_NAME);
    TenantWorkflowConfigHolder workflowConfig = (TenantWorkflowConfigHolder) workflowCache.get(cacheName);
    if (workflowConfig != null) {
        return workflowConfig;
    } else {
        TenantWorkflowConfigHolder configHolder = new TenantWorkflowConfigHolder(tenantDomain, tenantId);
        try {
            configHolder.load();
            workflowCache.put(cacheName, configHolder);
            return configHolder;
        } catch (WorkflowException e) {
            handleException("Error occurred while creating workflow configurations for tenant " + tenantDomain, e);
        } catch (RegistryException e) {
            handleException("Error occurred while creating workflow configurations for tenant " + tenantDomain, e);
        }
    }
    return null;
}
Also used : RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Cache(javax.cache.Cache)

Example 40 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetTopRatedAPIs.

@Test
public void testGetTopRatedAPIs() throws APIManagementException, RegistryException {
    Registry userRegistry = Mockito.mock(Registry.class);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
    GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
    GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
    GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
    Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
    Mockito.when(artifact.getAttribute(Mockito.anyString())).thenReturn("PUBLISHED");
    Mockito.when(artifact.getPath()).thenReturn("testPath");
    Mockito.when(userRegistry.getAverageRating("testPath")).thenReturn((float) 20.0);
    APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
    API api = new API(apiId1);
    Mockito.when(APIUtil.getAPI(artifact, userRegistry)).thenReturn(api);
    assertNotNull(apiConsumer.getTopRatedAPIs(10));
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)235 Resource (org.wso2.carbon.registry.core.Resource)196 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)167 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)145 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)104 Registry (org.wso2.carbon.registry.core.Registry)95 Test (org.junit.Test)81 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)81 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)67 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)61 UserStoreException (org.wso2.carbon.user.api.UserStoreException)60 API (org.wso2.carbon.apimgt.api.model.API)58 IOException (java.io.IOException)57 ArrayList (java.util.ArrayList)55 QName (javax.xml.namespace.QName)42 APIResource (org.wso2.carbon.apimgt.api.doc.model.APIResource)40 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)40 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)40