use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-mediation by wso2.
the class MediationStatisticsComponent method createStores.
/**
* Create the observers store using the synapse environment and configuration context.
*
* @param synEnvService information about synapse runtime
*/
private void createStores(SynapseEnvironmentService synEnvService) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
MessageFlowObserverStore observerStore = new MessageFlowObserverStore();
MessageFlowReporterThread reporterThread = null;
ServerConfiguration serverConf = ServerConfiguration.getInstance();
// Set a custom interval value if required
String interval = serverConf.getFirstProperty(AnalyticsDataPublisherConstants.FLOW_STATISTIC_WORKER_IDLE_INTERVAL);
long delay = AnalyticsDataPublisherConstants.FLOW_STATISTIC_WORKER_IDLE_INTERVAL_DEFAULT;
if (interval != null) {
try {
delay = Long.parseLong(interval);
} catch (NumberFormatException ignored) {
if (log.isDebugEnabled()) {
log.debug("Invalid delay time for mediation-flow-tracer thread. It will use default value - " + AnalyticsDataPublisherConstants.FLOW_STATISTIC_WORKER_IDLE_INTERVAL_DEFAULT);
}
delay = AnalyticsDataPublisherConstants.FLOW_STATISTIC_WORKER_IDLE_INTERVAL_DEFAULT;
}
}
String workerCountString = serverConf.getFirstProperty(AnalyticsDataPublisherConstants.FLOW_STATISTIC_WORKER_COUNT);
int workerCount = AnalyticsDataPublisherConstants.FLOW_STATISTIC_WORKER_COUNT_DEFAULT;
if (workerCountString != null) {
try {
workerCount = Integer.parseInt(workerCountString);
} catch (NumberFormatException ignored) {
if (log.isDebugEnabled()) {
log.debug("Invalid StatisticWorkerCount. It will use default value - " + AnalyticsDataPublisherConstants.FLOW_STATISTIC_WORKER_COUNT_DEFAULT);
}
workerCount = AnalyticsDataPublisherConstants.FLOW_STATISTIC_WORKER_COUNT_DEFAULT;
}
}
List<MessageFlowReporterThread> messageFlowReporterThreadList = new ArrayList<>();
for (int i = 0; i < workerCount; i++) {
reporterThread = new MessageFlowReporterThread(synEnvService, observerStore);
reporterThread.setName("message-flow-reporter-" + i + "-tenant-" + tenantId);
reporterThread.setDelay(delay);
reporterThread.start();
messageFlowReporterThreadList.add(reporterThread);
}
reporterThreads.put(tenantId, messageFlowReporterThreadList);
String disableJmxStr = serverConf.getFirstProperty(AnalyticsDataPublisherConstants.FLOW_STATISTIC_JMX_PUBLISHING);
boolean enableJmxPublishing = !Boolean.parseBoolean(disableJmxStr);
if (enableJmxPublishing) {
JMXMediationFlowObserver jmxObserver = new JMXMediationFlowObserver(tenantId);
observerStore.registerObserver(jmxObserver);
log.info("JMX mediation statistic publishing enabled for tenant: " + tenantId);
}
String disableAnalyticStr = serverConf.getFirstProperty(AnalyticsDataPublisherConstants.FLOW_STATISTIC_ANALYTICS_PUBLISHING);
boolean enableAnalyticsPublishing = !Boolean.parseBoolean(disableAnalyticStr);
if (enableAnalyticsPublishing) {
AnalyticsMediationFlowObserver dasObserver = new AnalyticsMediationFlowObserver();
observerStore.registerObserver(dasObserver);
dasObserver.setTenantId(tenantId);
log.info("DAS mediation statistic publishing enabled for tenant: " + tenantId);
}
// Engage custom observer implementations (user written extensions)
String observers = serverConf.getFirstProperty(AnalyticsDataPublisherConstants.STAT_OBSERVERS);
if (observers != null && !"".equals(observers)) {
String[] classNames = observers.split(",");
for (String className : classNames) {
try {
Class clazz = this.getClass().getClassLoader().loadClass(className.trim());
MessageFlowObserver o = (MessageFlowObserver) clazz.newInstance();
observerStore.registerObserver(o);
if (o instanceof TenantInformation) {
TenantInformation tenantInformation = (TenantInformation) o;
tenantInformation.setTenantId(synEnvService.getTenantId());
}
} catch (Exception e) {
log.error("Error while initializing the mediation statistics observer : " + className, e);
}
}
}
// 'MediationStat service' will be deployed per tenant (cardinality="1..n")
if (log.isDebugEnabled()) {
log.debug("Registering Observer for tenant: " + tenantId);
}
stores.put(tenantId, observerStore);
// Adding configuration reporting thread
MediationConfigReporterThread configReporterThread = new MediationConfigReporterThread(synEnvService);
configReporterThread.setName("mediation-config-reporter-" + tenantId);
configReporterThread.setTenantId(tenantId);
configReporterThread.setPublishingAnalyticESB(enableAnalyticsPublishing);
configReporterThread.start();
if (log.isDebugEnabled()) {
log.debug("Registering the new mediation configuration reporter thread");
}
configReporterThreads.put(tenantId, configReporterThread);
}
use of org.wso2.carbon.apimgt.api.model.Environment in project product-is by wso2.
the class RESTTestBase method init.
/**
* Initialize the RestAssured environment and create SwaggerRequestResponseValidator with the swagger definition
*
* @param swaggerDefinition swagger definition name
* @param basePathInSwagger basepath that is defined in the swagger definition (ex: /api/users/v1)
* @param basePath basepath of the current test run (ex: /t/carbon.super/api/users/v1)
* @throws IOException
* @throws XPathExpressionException
*/
protected void init(String swaggerDefinition, String basePathInSwagger, String basePath) throws RemoteException {
this.basePath = basePath;
this.swaggerDefinition = swaggerDefinition;
RestAssured.baseURI = backendURL.replace(SERVICES, "");
String swagger = replaceInSwaggerDefinition(swaggerDefinition, basePathInSwagger, basePath);
OpenApiInteractionValidator openAPIValidator = OpenApiInteractionValidator.createForInlineApiSpecification(swagger).withLevelResolver(LevelResolverFactory.withAdditionalPropertiesIgnored()).build();
validationFilter = new OpenApiValidationFilter(openAPIValidator);
remoteUSMServiceClient = new RemoteUserStoreManagerServiceClient(backendURL, sessionCookie);
userProfileMgtServiceClient = new UserProfileMgtServiceClient(backendURL, sessionCookie);
identityProviderMgtServiceClient = new IdentityProviderMgtServiceClient(sessionCookie, backendURL);
}
use of org.wso2.carbon.apimgt.api.model.Environment in project product-is by wso2.
the class CypressTestContainer method addOrOverwriteTestConfigProperty.
/**
* Add or overwrite the environment configurations of cypress test suite.
*
* @param filePath File path of the cypress.env.json file.
* @param propertyName Name of the cypress environment property.
* @param propertyValue Value of the cypress environment property.
*/
public void addOrOverwriteTestConfigProperty(Path filePath, String propertyName, String propertyValue) throws CypressContainerException {
if (StringUtils.isBlank(propertyName)) {
throw new CypressContainerException("Invalid config element propertyName.");
}
Gson gson = new Gson();
try (Reader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) {
JsonObject envConfigJSON = gson.fromJson(reader, JsonObject.class);
if (envConfigJSON.get(propertyName) != null) {
envConfigJSON.remove(propertyName);
}
envConfigJSON.addProperty(propertyName, propertyValue);
try (Writer writer = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8)) {
gson.toJson(envConfigJSON, writer);
}
LOG.info("Updated " + CypressTestConstants.CYPRESS_ENV_CONFIG_FILE + " file with key: " + propertyName + ", value: " + propertyValue);
} catch (IOException e) {
throw new RuntimeException("Failed to update " + CypressTestConstants.CYPRESS_ENV_CONFIG_FILE + " with key: " + propertyName + ", value: " + propertyValue);
}
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class AbstractAPIManager method populateAPIInformation.
protected void populateAPIInformation(String uuid, String organization, API api) throws APIManagementException, OASPersistenceException, ParseException, AsyncSpecPersistenceException {
Organization org = new Organization(organization);
// UUID
if (api.getUuid() == null) {
api.setUuid(uuid);
}
api.setOrganization(organization);
// environment
String environmentString = null;
if (api.getEnvironments() != null) {
environmentString = String.join(",", api.getEnvironments());
}
api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
// workflow status
APIIdentifier apiId = api.getId();
WorkflowDTO workflow;
String currentApiUuid = uuid;
if (api.isRevision() && api.getRevisionedApiId() != null) {
currentApiUuid = api.getRevisionedApiId();
}
workflow = APIUtil.getAPIWorkflowStatus(currentApiUuid, WF_TYPE_AM_API_STATE);
if (workflow != null) {
WorkflowStatus status = workflow.getStatus();
api.setWorkflowStatus(status.toString());
}
// TODO try to use a single query to get info from db
int internalId = apiMgtDAO.getAPIID(currentApiUuid);
apiId.setId(internalId);
apiMgtDAO.setServiceStatusInfoToAPI(api, internalId);
// api level tier
String apiLevelTier;
if (api.isRevision()) {
apiLevelTier = apiMgtDAO.getAPILevelTier(api.getRevisionedApiId(), api.getUuid());
} else {
apiLevelTier = apiMgtDAO.getAPILevelTier(internalId);
}
api.setApiLevelPolicy(apiLevelTier);
// available tier
String tiers = null;
Set<Tier> tiersSet = api.getAvailableTiers();
Set<String> tierNameSet = new HashSet<String>();
for (Tier t : tiersSet) {
tierNameSet.add(t.getName());
}
if (api.getAvailableTiers() != null) {
tiers = String.join("||", tierNameSet);
}
Map<String, Tier> definedTiers = APIUtil.getTiers(APIUtil.getInternalOrganizationId(organization));
Set<Tier> availableTier = APIUtil.getAvailableTiers(definedTiers, tiers, api.getId().getApiName());
api.setAvailableTiers(availableTier);
// Scopes
Map<String, Scope> scopeToKeyMapping = APIUtil.getAPIScopes(currentApiUuid, organization);
api.setScopes(new LinkedHashSet<>(scopeToKeyMapping.values()));
// templates
String resourceConfigsString;
if (api.getSwaggerDefinition() != null) {
resourceConfigsString = api.getSwaggerDefinition();
} else {
resourceConfigsString = apiPersistenceInstance.getOASDefinition(org, uuid);
}
api.setSwaggerDefinition(resourceConfigsString);
if (resourceConfigsString == null) {
if (api.getAsyncApiDefinition() != null) {
resourceConfigsString = api.getAsyncApiDefinition();
} else {
resourceConfigsString = apiPersistenceInstance.getAsyncDefinition(org, uuid);
}
api.setAsyncApiDefinition(resourceConfigsString);
}
if (api.getType() != null && APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) {
api.setGraphQLSchema(getGraphqlSchemaDefinition(uuid, organization));
}
JSONParser jsonParser = new JSONParser();
JSONObject paths = null;
if (resourceConfigsString != null) {
JSONObject resourceConfigsJSON = (JSONObject) jsonParser.parse(resourceConfigsString);
paths = (JSONObject) resourceConfigsJSON.get(APIConstants.SWAGGER_PATHS);
}
Set<URITemplate> uriTemplates = apiMgtDAO.getURITemplatesOfAPI(api.getUuid());
for (URITemplate uriTemplate : uriTemplates) {
String uTemplate = uriTemplate.getUriTemplate();
String method = uriTemplate.getHTTPVerb();
List<Scope> oldTemplateScopes = uriTemplate.retrieveAllScopes();
List<Scope> newTemplateScopes = new ArrayList<>();
if (!oldTemplateScopes.isEmpty()) {
for (Scope templateScope : oldTemplateScopes) {
Scope scope = scopeToKeyMapping.get(templateScope.getKey());
newTemplateScopes.add(scope);
}
}
uriTemplate.addAllScopes(newTemplateScopes);
uriTemplate.setResourceURI(api.getUrl());
uriTemplate.setResourceSandboxURI(api.getSandboxUrl());
// AWS Lambda: set arn & timeout to URI template
if (paths != null) {
JSONObject path = (JSONObject) paths.get(uTemplate);
if (path != null) {
JSONObject operation = (JSONObject) path.get(method.toLowerCase());
if (operation != null) {
if (operation.containsKey(APIConstants.SWAGGER_X_AMZN_RESOURCE_NAME)) {
uriTemplate.setAmznResourceName((String) operation.get(APIConstants.SWAGGER_X_AMZN_RESOURCE_NAME));
}
if (operation.containsKey(APIConstants.SWAGGER_X_AMZN_RESOURCE_TIMEOUT)) {
uriTemplate.setAmznResourceTimeout(((Long) operation.get(APIConstants.SWAGGER_X_AMZN_RESOURCE_TIMEOUT)).intValue());
}
}
}
}
}
if (APIConstants.IMPLEMENTATION_TYPE_INLINE.equalsIgnoreCase(api.getImplementation())) {
for (URITemplate template : uriTemplates) {
template.setMediationScript(template.getAggregatedMediationScript());
}
}
api.setUriTemplates(uriTemplates);
// CORS . if null is returned, set default config from the configuration
if (api.getCorsConfiguration() == null) {
api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
}
// set category
List<APICategory> categories = api.getApiCategories();
if (categories != null) {
List<String> categoriesOfAPI = new ArrayList<String>();
for (APICategory apiCategory : categories) {
categoriesOfAPI.add(apiCategory.getName());
}
List<APICategory> categoryList = new ArrayList<>();
if (!categoriesOfAPI.isEmpty()) {
// category array retrieved from artifact has only the category name, therefore we need to fetch
// categories
// and fill out missing attributes before attaching the list to the api
List<APICategory> allCategories = APIUtil.getAllAPICategoriesOfOrganization(organization);
// todo-category: optimize this loop with breaks
for (String categoryName : categoriesOfAPI) {
for (APICategory category : allCategories) {
if (categoryName.equals(category.getName())) {
categoryList.add(category);
break;
}
}
}
}
api.setApiCategories(categoryList);
}
}
use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.
the class ApiMgtDAO method addAPIRevisionDeployment.
/**
* Adds an API revision Deployment mapping record to the database
*
* @param apiRevisionId uuid of the revision
* @param apiRevisionDeployments content of the revision deployment mapping objects
* @throws APIManagementException if an error occurs when adding a new API revision
*/
public void addAPIRevisionDeployment(String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeployments) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
// Adding to AM_DEPLOYMENT_REVISION_MAPPING table
PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.ADD_API_REVISION_DEPLOYMENT_MAPPING);
for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeployments) {
String envName = apiRevisionDeployment.getDeployment();
String vhost = apiRevisionDeployment.getVhost();
// set VHost as null, if it is the default vhost of the read only environment
statement.setString(1, apiRevisionDeployment.getDeployment());
statement.setString(2, VHostUtils.resolveIfDefaultVhostToNull(envName, vhost));
statement.setString(3, apiRevisionId);
statement.setBoolean(4, apiRevisionDeployment.isDisplayOnDevportal());
statement.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
statement.addBatch();
}
statement.executeBatch();
connection.commit();
} catch (SQLException e) {
connection.rollback();
handleException("Failed to add API Revision Deployment Mapping entry for Revision UUID " + apiRevisionId, e);
}
} catch (SQLException e) {
handleException("Failed to add API Revision Deployment Mapping entry for Revision UUID " + apiRevisionId, e);
}
}
Aggregations