Search in sources :

Example 61 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateAPIWSDLArchive.

@Override
public void updateAPIWSDLArchive(String apiId, InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
    WSDLArchiveInfo archiveInfo = null;
    InputStream fileInputStream = null;
    try {
        archiveInfo = extractAndValidateWSDLArchive(inputStream);
        if (log.isDebugEnabled()) {
            log.debug("Successfully extracted and validated WSDL file. Location: " + archiveInfo.getAbsoluteFilePath());
        }
        fileInputStream = new FileInputStream(archiveInfo.getAbsoluteFilePath());
        getApiDAO().addOrUpdateWSDLArchive(apiId, fileInputStream, getUsername());
        if (log.isDebugEnabled()) {
            log.debug("Successfully updated the WSDL archive in DB. API uuid: " + apiId);
        }
    } catch (IOException e) {
        throw new APIMgtWSDLException("Unable to process WSDL archive at " + archiveInfo.getAbsoluteFilePath(), e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (archiveInfo != null) {
                APIFileUtils.deleteDirectory(archiveInfo.getLocation());
            }
        } catch (APIMgtDAOException | IOException e) {
            // This is not a blocker. Give a warning and continue
            log.warn("Error occured while deleting processed WSDL artifacts folder : " + archiveInfo.getLocation());
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 62 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method exportAPIs.

/**
 * Export a given set of APIs to the file system as a zip archive.
 * The export root location is given by {@link FileBasedApiImportExportManager#path}/exported-apis.
 *
 * @param apiDetailSet        Set of {@link APIDetails} objects to be exported
 * @param exportDirectoryName Name of the directory to do the export
 * @return Path to the directory  with exported artifacts
 * @throws APIMgtEntityImportExportException if an error occurred while exporting APIs to file system or
 *                                           no APIs are exported successfully
 */
public String exportAPIs(Set<APIDetails> apiDetailSet, String exportDirectoryName) throws APIMgtEntityImportExportException {
    // this is the base directory for the archive. after export happens, this directory will
    // be archived to be sent as a application/zip response to the client
    String apiArtifactsBaseDirectoryPath = path + File.separator + exportDirectoryName;
    try {
        APIFileUtils.createDirectory(apiArtifactsBaseDirectoryPath);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to create directory for export API at :" + apiArtifactsBaseDirectoryPath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
    for (APIDetails apiDetails : apiDetailSet) {
        // derive the folder structure
        String apiExportDirectory = APIFileUtils.getAPIBaseDirectory(apiArtifactsBaseDirectoryPath, new FileApi(apiDetails.getApi()));
        API exportAPI = apiDetails.getApi();
        try {
            // create per-api export directory
            APIFileUtils.createDirectory(apiExportDirectory);
            // export API definition
            APIFileUtils.exportApiDefinitionToFileSystem(new FileApi(exportAPI), apiExportDirectory);
            // export swagger definition
            APIFileUtils.exportSwaggerDefinitionToFileSystem(apiDetails.getSwaggerDefinition(), exportAPI, apiExportDirectory);
            // export gateway configs
            APIFileUtils.exportGatewayConfigToFileSystem(apiDetails.getGatewayConfiguration(), exportAPI, apiExportDirectory);
            if (apiDetails.getEndpoints() != null && !apiDetails.getEndpoints().isEmpty()) {
                exportEndpointsToFileSystem(apiDetails.getEndpoints(), exportAPI, apiExportDirectory);
            }
        } catch (APIMgtDAOException e) {
            // no need to throw, log
            log.error("Error in exporting API: " + exportAPI.getName() + ", version: " + apiDetails.getApi().getVersion(), e);
            // cleanup the API directory
            try {
                APIFileUtils.deleteDirectory(path);
            } catch (APIMgtDAOException e1) {
                log.warn("Unable to remove directory " + path);
            }
            // skip this API
            continue;
        }
        // as exported correctly.
        if (apiDetails.getThumbnailStream() != null) {
            try {
                APIFileUtils.exportThumbnailToFileSystem(apiDetails.getThumbnailStream(), apiExportDirectory);
            } catch (APIMgtDAOException warn) {
                // log the warning without throwing
                log.warn("Error in exporting thumbnail to file system for api: " + exportAPI.getName() + ", version: " + exportAPI.getVersion());
            }
        }
        exportDocumentationToFileSystem(apiDetails.getAllDocumentInformation(), apiDetails, apiExportDirectory);
        log.info("Successfully exported API: " + exportAPI.getName() + ", version: " + exportAPI.getVersion());
    }
    // if the directory is empty, no APIs have been exported!
    try {
        if (APIFileUtils.getDirectoryList(apiArtifactsBaseDirectoryPath).isEmpty()) {
            // cleanup the archive root directory
            APIFileUtils.deleteDirectory(path);
            String errorMsg = "No APIs exported successfully";
            throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_EXPORT_ERROR);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBaseDirectoryPath;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    return apiArtifactsBaseDirectoryPath;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) API(org.wso2.carbon.apimgt.core.models.API) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 63 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project kubernetes by ballerinax.

the class KubernetesPlugin method process.

@Override
public void process(PackageNode packageNode) {
    BLangPackage bPackage = (BLangPackage) packageNode;
    KubernetesContext.getInstance().addDataHolder(bPackage.packageID, sourceDirectory.getPath());
    // Get dependency jar paths
    JarResolver jarResolver = KubernetesContext.getInstance().getCompilerContext().get(JAR_RESOLVER_KEY);
    if (jarResolver != null) {
        Set<Path> dependencyJarPaths = new HashSet<>(jarResolver.allDependencies(bPackage));
        KubernetesContext.getInstance().getDataHolder(bPackage.packageID).getDockerModel().addDependencyJarPaths(dependencyJarPaths);
    }
    // Get the imports with alias _
    List<BLangImportPackage> kubernetesImports = bPackage.getImports().stream().filter(i -> i.symbol.toString().startsWith("ballerina/kubernetes") && i.getAlias().toString().equals("_")).collect(Collectors.toList());
    if (kubernetesImports.size() > 0) {
        for (BLangImportPackage kubernetesImport : kubernetesImports) {
            // Get the units of the file which has kubernetes import as _
            List<TopLevelNode> topLevelNodes = bPackage.getCompilationUnits().stream().filter(cu -> cu.getName().equals(kubernetesImport.compUnit.getValue())).flatMap(cu -> cu.getTopLevelNodes().stream()).collect(Collectors.toList());
            // Filter out the services
            List<ServiceNode> serviceNodes = topLevelNodes.stream().filter(tln -> tln instanceof ServiceNode).map(tln -> (ServiceNode) tln).collect(Collectors.toList());
            // Generate artifacts for services for all services
            serviceNodes.forEach(sn -> process(sn, Collections.singletonList(createAnnotation("Deployment"))));
            // Create Service annotation with NodePort service type
            AnnotationAttachmentNode serviceAnnotation = createAnnotation("Service");
            BLangRecordLiteral svcRecordLiteral = (BLangRecordLiteral) TreeBuilder.createRecordLiteralNode();
            serviceAnnotation.setExpression(svcRecordLiteral);
            BLangLiteral serviceTypeKey = (BLangLiteral) TreeBuilder.createLiteralExpression();
            serviceTypeKey.value = ServiceAnnotationProcessor.ServiceConfiguration.serviceType.name();
            serviceTypeKey.type = new BType(TypeTags.STRING, null);
            BLangLiteral serviceTypeValue = new BLangLiteral();
            serviceTypeValue.value = KubernetesConstants.ServiceType.NodePort.name();
            serviceTypeValue.type = new BType(TypeTags.STRING, null);
            BLangRecordLiteral.BLangRecordKeyValueField serviceTypeRecordField = new BLangRecordLiteral.BLangRecordKeyValueField();
            serviceTypeRecordField.key = new BLangRecordLiteral.BLangRecordKey(serviceTypeKey);
            serviceTypeRecordField.valueExpr = serviceTypeValue;
            svcRecordLiteral.fields.add(serviceTypeRecordField);
            // Filter services with 'new Listener()' and generate services
            for (ServiceNode serviceNode : serviceNodes) {
                Optional<? extends ExpressionNode> initListener = serviceNode.getAttachedExprs().stream().filter(aex -> aex instanceof BLangTypeInit).findAny();
                if (initListener.isPresent()) {
                    serviceNodes.forEach(sn -> process(sn, Collections.singletonList(serviceAnnotation)));
                }
            }
            // Get the variable names of the listeners attached to services
            List<String> listenerNamesToExpose = serviceNodes.stream().map(ServiceNode::getAttachedExprs).flatMap(Collection::stream).filter(aex -> aex instanceof BLangSimpleVarRef).map(aex -> (BLangSimpleVarRef) aex).map(BLangSimpleVarRef::toString).collect(Collectors.toList());
            // Generate artifacts for listeners attached to services
            topLevelNodes.stream().filter(tln -> tln instanceof SimpleVariableNode).map(tln -> (SimpleVariableNode) tln).filter(listener -> listenerNamesToExpose.contains(listener.getName().getValue())).forEach(listener -> process(listener, Collections.singletonList(serviceAnnotation)));
            // Generate artifacts for main functions
            topLevelNodes.stream().filter(tln -> tln instanceof FunctionNode).map(tln -> (FunctionNode) tln).filter(fn -> "main".equals(fn.getName().getValue())).forEach(fn -> process(fn, Collections.singletonList(createAnnotation("Job"))));
        }
    }
}
Also used : JAR_RESOLVER_KEY(org.ballerinalang.compiler.JarResolver.JAR_RESOLVER_KEY) AnnotationProcessorFactory(org.ballerinax.kubernetes.processors.AnnotationProcessorFactory) LoggerFactory(org.slf4j.LoggerFactory) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Flag(org.ballerinalang.model.elements.Flag) Map(java.util.Map) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) Path(java.nio.file.Path) KUBERNETES(org.ballerinax.kubernetes.KubernetesConstants.KUBERNETES) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) JarResolver(org.ballerinalang.compiler.JarResolver) DOCKER(org.ballerinax.kubernetes.KubernetesConstants.DOCKER) KubernetesUtils(org.ballerinax.kubernetes.utils.KubernetesUtils) Collection(java.util.Collection) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) Collectors(java.util.stream.Collectors) KubernetesUtils.printError(org.ballerinax.kubernetes.utils.KubernetesUtils.printError) List(java.util.List) KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder) Optional(java.util.Optional) DependencyValidator(org.ballerinax.kubernetes.utils.DependencyValidator) PackageNode(org.ballerinalang.model.tree.PackageNode) KubernetesContext(org.ballerinax.kubernetes.models.KubernetesContext) SimpleVariableNode(org.ballerinalang.model.tree.SimpleVariableNode) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) DockerGenUtils.extractJarName(org.ballerinax.docker.generator.utils.DockerGenUtils.extractJarName) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) ArrayList(java.util.ArrayList) SourceDirectory(org.wso2.ballerinalang.compiler.SourceDirectory) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) ServiceAnnotationProcessor(org.ballerinax.kubernetes.processors.ServiceAnnotationProcessor) TreeBuilder(org.ballerinalang.model.TreeBuilder) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) DiagnosticSeverity(io.ballerina.tools.diagnostics.DiagnosticSeverity) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Logger(org.slf4j.Logger) Files(java.nio.file.Files) KubernetesUtils.createAnnotation(org.ballerinax.kubernetes.utils.KubernetesUtils.createAnnotation) FunctionNode(org.ballerinalang.model.tree.FunctionNode) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) ServiceNode(org.ballerinalang.model.tree.ServiceNode) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) Collections(java.util.Collections) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ServiceNode(org.ballerinalang.model.tree.ServiceNode) JarResolver(org.ballerinalang.compiler.JarResolver) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) HashSet(java.util.HashSet) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) Path(java.nio.file.Path) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SimpleVariableNode(org.ballerinalang.model.tree.SimpleVariableNode) Collection(java.util.Collection) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 64 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-business-process by wso2.

the class BPELAppDeployer method undeployArtifacts.

/**
 * Check the artifact type and if it is a BPEL, delete the file from the BPEL
 * deployment hot folder
 *
 * @param carbonApp  - CarbonApplication instance to check for BPEL artifacts
 * @param axisConfig - - axisConfig of the current tenant
 */
public void undeployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig) {
    List<Artifact.Dependency> artifacts = carbonApp.getAppConfig().getApplicationArtifact().getDependencies();
    for (Artifact.Dependency dep : artifacts) {
        Deployer deployer;
        Artifact artifact = dep.getArtifact();
        if (artifact == null) {
            continue;
        }
        if (BPEL_TYPE.equals(artifact.getType())) {
            deployer = AppDeployerUtils.getArtifactDeployer(axisConfig, BPEL_DIR, "zip");
        } else {
            continue;
        }
        // loop through all dependencies
        List<CappFile> files = artifact.getFiles();
        if (files.size() != 1) {
            log.error("A BPEL workflow must have a single file. But " + files.size() + " files found.");
            continue;
        }
        if (deployer != null && AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED.equals(artifact.getDeploymentStatus())) {
            String fileName = artifact.getFiles().get(0).getName();
            String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
            try {
                File artifactFile = new File(artifactPath);
                if (artifactFile.exists() && !artifactFile.delete()) {
                    log.warn("Couldn't delete App artifact file : " + artifactPath);
                }
                deployer.undeploy(artifactPath);
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_PENDING);
            } catch (DeploymentException e) {
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
                log.error("Error occured while trying to un deploy : " + artifact.getName());
            }
        }
    }
}
Also used : DeploymentException(org.apache.axis2.deployment.DeploymentException) File(java.io.File) CappFile(org.wso2.carbon.application.deployer.config.CappFile) Artifact(org.wso2.carbon.application.deployer.config.Artifact) Deployer(org.apache.axis2.deployment.Deployer) CappFile(org.wso2.carbon.application.deployer.config.CappFile)

Example 65 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-business-process by wso2.

the class BPELAppDeployer method deployArtifacts.

/**
 * Check the artifact type and if it is a BPEL, copy it to the BPEL deployment hot folder
 *
 * @param carbonApp  - CarbonApplication instance to check for BPEL artifacts
 * @param axisConfig - AxisConfiguration of the current tenant
 */
public void deployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig) throws DeploymentException {
    List<Artifact.Dependency> artifacts = carbonApp.getAppConfig().getApplicationArtifact().getDependencies();
    // loop through all artifacts
    for (Artifact.Dependency dep : artifacts) {
        Deployer deployer;
        Artifact artifact = dep.getArtifact();
        if (artifact == null) {
            continue;
        }
        if (!isAccepted(artifact.getType())) {
            log.warn("Can't deploy artifact : " + artifact.getName() + " of type : " + artifact.getType() + ". Required features are not installed in the system");
            continue;
        }
        if (BPEL_TYPE.equals(artifact.getType())) {
            deployer = AppDeployerUtils.getArtifactDeployer(axisConfig, BPEL_DIR, "zip");
        } else {
            continue;
        }
        List<CappFile> files = artifact.getFiles();
        if (files.size() != 1) {
            log.error("BPEL workflows must have a single file to " + "be deployed. But " + files.size() + " files found.");
            continue;
        }
        if (deployer != null) {
            String fileName = artifact.getFiles().get(0).getName();
            String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
            try {
                deployer.deploy(new DeploymentFileData(new File(artifactPath), deployer));
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED);
            } catch (DeploymentException e) {
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
                throw e;
            }
        }
    }
}
Also used : DeploymentFileData(org.apache.axis2.deployment.repository.util.DeploymentFileData) DeploymentException(org.apache.axis2.deployment.DeploymentException) File(java.io.File) CappFile(org.wso2.carbon.application.deployer.config.CappFile) Artifact(org.wso2.carbon.application.deployer.config.Artifact) Deployer(org.apache.axis2.deployment.Deployer) CappFile(org.wso2.carbon.application.deployer.config.CappFile)

Aggregations

File (java.io.File)23 IOException (java.io.IOException)18 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)18 ArrayList (java.util.ArrayList)17 CAppArtifacts (org.wso2.ei.dashboard.core.rest.model.CAppArtifacts)16 Operation (io.swagger.v3.oas.annotations.Operation)15 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)15 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)15 Response (javax.ws.rs.core.Response)15 Artifact (org.wso2.carbon.application.deployer.config.Artifact)15 Artifacts (org.wso2.ei.dashboard.core.rest.model.Artifacts)15 DeploymentException (org.apache.axis2.deployment.DeploymentException)11 CappFile (org.wso2.carbon.application.deployer.config.CappFile)11 Deployer (org.apache.axis2.deployment.Deployer)10 ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)10 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)10 InputStream (java.io.InputStream)9 FileInputStream (java.io.FileInputStream)7 APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)7 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7