diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java index 238042dc24..f57c2fadc6 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java @@ -132,28 +132,77 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe APIApplicationManagerExtensionDataHolder.getInstance().getConsumerRESTAPIServices(); try { - consumerRESTAPIServices.getAllApplications(null, null, applicationName); + io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications = + consumerRESTAPIServices.getAllApplications(null, null, applicationName); - List uniqueApiList = new ArrayList(); + List uniqueApiList = new ArrayList<>(); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); - for (String tag: tags) { + for (String tag : tags) { Map queryParams = new HashMap<>(); queryParams.put("tag", tag); if ("carbon.super".equals(tenantDomain)) { consumerRESTAPIServices.getAllApis(null, null, queryParams); } else { //call All API getting call with carbon super header param + APIInfo[] apiInfos; + if (!"carbon.super".equals(tenantDomain)) { + headerParams.put("X-WSO2-Tenant", "carbon.super"); } + apiInfos = consumerRESTAPIServices.getAllApis(null, null, queryParams, headerParams); - uniqueApiList.add("Test"); - Set taggedAPISet = new HashSet<>(uniqueApiList); + uniqueApiList.addAll(List.of(apiInfos)); + Set taggedAPISet = new HashSet<>(uniqueApiList); uniqueApiList.clear(); uniqueApiList.addAll(taggedAPISet); } - consumerRESTAPIServices.getAllSubscriptions(null, null, "1"); + if (applications.length == 0) { + io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application application = + new io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application(); + + application.setName(applicationName); + application = consumerRESTAPIServices.createApplication(null, null, application); + List subscriptions = new ArrayList<>(); + for (APIInfo apiInfo : uniqueApiList) { + Subscription subscription = new Subscription(); + subscription.setApiId(apiInfo.getId()); + subscription.setApplicationId(application.getApplicationId()); + subscriptions.add(subscription); + } + consumerRESTAPIServices.createSubscriptions(null, null, subscriptions); + } else { + if (applications.length == 1) { + Optional application = + Arrays.stream(applications).findFirst(); + Subscription[] subscriptions = consumerRESTAPIServices.getAllSubscriptions(null, null, + application.get().getApplicationId()); + for (Subscription subscription : subscriptions) { + if (uniqueApiList.contains(subscription.getApiInfo())) { + uniqueApiList.remove(subscription.getApiInfo()); + } else { + uniqueApiList.add(subscription.getApiInfo()); + } + } + + + //duplicate code block + List subscriptionList = new ArrayList<>(); + for (APIInfo apiInfo : uniqueApiList) { + Subscription subscription = new Subscription(); + subscription.setApiId(apiInfo.getId()); + subscription.setApplicationId(application.get().getApplicationId()); + subscriptionList.add(subscription); + } + consumerRESTAPIServices.createSubscriptions(null, null, subscriptionList); + } else { + String msg = "Found more than one application for application name: " + applicationName; + log.error(msg); + throw new APIManagerException(msg); + } + + } } catch (APIServicesException e) { diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Subscription.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Subscription.java index 2ff525f5dc..11fc881c1b 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Subscription.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/bean/APIMConsumer/Subscription.java @@ -18,8 +18,6 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer; -import org.json.JSONObject; - public class Subscription { private String subscriptionId;