airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chath...@apache.org
Subject [22/51] [abbrv] [partial] airavata git commit: registry refactoring
Date Thu, 04 Jun 2015 20:34:10 GMT
http://git-wip-us.apache.org/repos/asf/airavata/blob/ec8c6202/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/impl/ApplicationDeploymentImpl.java
----------------------------------------------------------------------
diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/impl/ApplicationDeploymentImpl.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/impl/ApplicationDeploymentImpl.java
new file mode 100644
index 0000000..cd9ccae
--- /dev/null
+++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/impl/ApplicationDeploymentImpl.java
@@ -0,0 +1,413 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.airavata.registry.core.app.catalog.impl;
+
+import org.apache.airavata.model.appcatalog.appdeployment.ApplicationDeploymentDescription;
+import org.apache.airavata.model.appcatalog.appdeployment.ApplicationParallelismType;
+import org.apache.airavata.model.appcatalog.appdeployment.SetEnvPaths;
+import org.apache.airavata.registry.core.app.catalog.resources.*;
+import org.apache.airavata.registry.core.app.catalog.util.AppCatalogThriftConversion;
+import org.apache.airavata.registry.cpi.AppCatalogException;
+import org.apache.airavata.registry.cpi.ApplicationDeployment;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ApplicationDeploymentImpl implements ApplicationDeployment {
+    private final static Logger logger = LoggerFactory.getLogger(ApplicationDeploymentImpl.class);
+
+    @Override
+    public String addApplicationDeployment(ApplicationDeploymentDescription deploymentDescription, String gatewayId) throws AppCatalogException {
+        try {
+            AppDeploymentAppCatalogResourceAppCat deploymentResource = new AppDeploymentAppCatalogResourceAppCat();
+            ComputeResourceAppCatalogResourceAppCat computeHostResource = new ComputeResourceAppCatalogResourceAppCat();
+            AppModuleAppCatalogResourceAppCat moduleResource = new AppModuleAppCatalogResourceAppCat();
+            if (!computeHostResource.isExists(deploymentDescription.getComputeHostId())){
+                logger.error("Compute host does not exist in the system. Please create a Compute host first...");
+                throw new AppCatalogException("Compute host does not exist in the system. Please create a Compute host first...");
+            }
+            if (!moduleResource.isExists(deploymentDescription.getAppModuleId())){
+                logger.error("Application module does not exist in the system. Please create an application module first...");
+                throw new AppCatalogException("Application module does not exist in the system. Please create an application module first...");
+            }
+            AppModuleAppCatalogResourceAppCat module = (AppModuleAppCatalogResourceAppCat)moduleResource.get(deploymentDescription.getAppModuleId());
+            ComputeResourceAppCatalogResourceAppCat hostResource = (ComputeResourceAppCatalogResourceAppCat) computeHostResource.get(deploymentDescription.getComputeHostId());
+            deploymentResource.setDeploymentId(hostResource.getHostName() + "_" + deploymentDescription.getAppModuleId());
+            deploymentResource.setAppModuleId(deploymentDescription.getAppModuleId());
+            deploymentResource.setModuleResource(module);
+            deploymentResource.setHostId(deploymentDescription.getComputeHostId());
+            deploymentResource.setHostResource(hostResource);
+            deploymentResource.setAppDes(deploymentDescription.getAppDeploymentDescription());
+            deploymentResource.setExecutablePath(deploymentDescription.getExecutablePath());
+            deploymentResource.setGatewayId(gatewayId);
+            ApplicationParallelismType parallelism = deploymentDescription.getParallelism();
+            if (parallelism != null){
+                deploymentResource.setParallelism(parallelism.toString());
+            }
+            deploymentResource.save();
+            deploymentDescription.setAppDeploymentId(deploymentResource.getDeploymentId());
+
+            List<String> moduleLoadCmds = deploymentDescription.getModuleLoadCmds();
+            if (moduleLoadCmds != null && !moduleLoadCmds.isEmpty()){
+                for (String cmd : moduleLoadCmds){
+                    ModuleLoadCmdAppCatalogResourceAppCat cmdResource = new ModuleLoadCmdAppCatalogResourceAppCat();
+                    cmdResource.setAppDeploymentId(deploymentDescription.getAppDeploymentId());
+                    cmdResource.setCmd(cmd);
+                    cmdResource.save();
+                }
+            }
+
+            List<String> preJobCommands = deploymentDescription.getPreJobCommands();
+            if (preJobCommands != null && !preJobCommands.isEmpty()){
+                for (String cmd : preJobCommands){
+                    PreJobCommandAppCatalogResourceAppCat cmdResource = new PreJobCommandAppCatalogResourceAppCat();
+                    cmdResource.setAppDeploymentId(deploymentDescription.getAppDeploymentId());
+                    cmdResource.setCommand(cmd);
+                    cmdResource.save();
+                }
+            }
+
+            List<String> postJobCommands = deploymentDescription.getPostJobCommands();
+            if (postJobCommands != null && !postJobCommands.isEmpty()){
+                for (String cmd : postJobCommands){
+                    PostJobCommandAppCatalogResourceAppCat cmdResource = new PostJobCommandAppCatalogResourceAppCat();
+                    cmdResource.setAppDeploymentId(deploymentDescription.getAppDeploymentId());
+                    cmdResource.setCommand(cmd);
+                    cmdResource.save();
+                }
+            }
+
+            List<SetEnvPaths> libPrependPaths = deploymentDescription.getLibPrependPaths();
+            if (libPrependPaths != null && !libPrependPaths.isEmpty()){
+                for (SetEnvPaths path : libPrependPaths){
+                    LibraryPrepandPathAppCatalogResourceAppCat prepandPathResource = new LibraryPrepandPathAppCatalogResourceAppCat();
+                    prepandPathResource.setAppDeploymentResource(deploymentResource);
+                    prepandPathResource.setName(path.getName());
+                    prepandPathResource.setValue(path.getValue());
+                    prepandPathResource.setDeploymentId(deploymentResource.getDeploymentId());
+                    prepandPathResource.save();
+                }
+            }
+
+            List<SetEnvPaths> libApendPaths = deploymentDescription.getLibAppendPaths();
+            if (libApendPaths != null && !libApendPaths.isEmpty()){
+                for (SetEnvPaths path : libApendPaths){
+                    LibraryApendPathAppCatalogResourceAppCat apendPathResource = new LibraryApendPathAppCatalogResourceAppCat();
+                    apendPathResource.setAppDeploymentResource(deploymentResource);
+                    apendPathResource.setName(path.getName());
+                    apendPathResource.setValue(path.getValue());
+                    apendPathResource.setDeploymentId(deploymentResource.getDeploymentId());
+                    apendPathResource.save();
+                }
+            }
+            List<SetEnvPaths> setEnvironment = deploymentDescription.getSetEnvironment();
+            if (setEnvironment != null && !setEnvironment.isEmpty()){
+                for (SetEnvPaths path : setEnvironment){
+                    AppEnvironmentAppCatalogResourceAppCat environmentResource = new AppEnvironmentAppCatalogResourceAppCat();
+                    environmentResource.setAppDeploymentResource(deploymentResource);
+                    environmentResource.setName(path.getName());
+                    environmentResource.setValue(path.getValue());
+                    environmentResource.setDeploymentId(deploymentResource.getDeploymentId());
+                    environmentResource.save();
+                }
+            }
+            return deploymentResource.getDeploymentId();
+        }catch (Exception e) {
+            logger.error("Error while saving application deployment...", e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public void updateApplicationDeployment(String deploymentId, ApplicationDeploymentDescription updatedDeployment) throws AppCatalogException {
+        try {
+            AppDeploymentAppCatalogResourceAppCat deploymentResource = new AppDeploymentAppCatalogResourceAppCat();
+            AppDeploymentAppCatalogResourceAppCat existingDep = (AppDeploymentAppCatalogResourceAppCat)deploymentResource.get(deploymentId);
+            ComputeResourceAppCatalogResourceAppCat computeHostResource = new ComputeResourceAppCatalogResourceAppCat();
+            AppModuleAppCatalogResourceAppCat moduleResource = new AppModuleAppCatalogResourceAppCat();
+            if (!computeHostResource.isExists(updatedDeployment.getComputeHostId())){
+                logger.error("Compute host does not exist in the system. Please create a Compute host first...");
+                throw new AppCatalogException("Compute host does not exist in the system. Please create a Compute host first...");
+            }
+            if (!moduleResource.isExists(updatedDeployment.getAppModuleId())){
+                logger.error("Application module does not exist in the system. Please create an application module first...");
+                throw new AppCatalogException("Application module does not exist in the system. Please create an application module first...");
+            }
+            AppModuleAppCatalogResourceAppCat module = (AppModuleAppCatalogResourceAppCat)moduleResource.get(updatedDeployment.getAppModuleId());
+            existingDep.setAppModuleId(updatedDeployment.getAppModuleId());
+            existingDep.setModuleResource(module);
+            existingDep.setHostId(updatedDeployment.getComputeHostId());
+            existingDep.setHostResource((ComputeResourceAppCatalogResourceAppCat)computeHostResource.get(updatedDeployment.getComputeHostId()));
+            existingDep.setAppDes(updatedDeployment.getAppDeploymentDescription());
+            existingDep.setExecutablePath(updatedDeployment.getExecutablePath());
+            if (updatedDeployment.getParallelism() != null){
+                deploymentResource.setParallelism(updatedDeployment.getParallelism().toString());
+            }
+
+            existingDep.save();
+
+            // remove existing module load commands
+            ModuleLoadCmdAppCatalogResourceAppCat cmdResource = new ModuleLoadCmdAppCatalogResourceAppCat();
+            Map<String, String> ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.ModuleLoadCmdConstants.APP_DEPLOYMENT_ID, deploymentId);
+            cmdResource.remove(ids);
+            List<String> moduleLoadCmds = updatedDeployment.getModuleLoadCmds();
+            if (moduleLoadCmds != null && !moduleLoadCmds.isEmpty()){
+                for (String cmd : moduleLoadCmds){
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.ModuleLoadCmdConstants.APP_DEPLOYMENT_ID, deploymentId);
+                    ids.put(AppCatAbstractResource.ModuleLoadCmdConstants.CMD, cmd);
+                    if (cmdResource.isExists(ids)){
+                        cmdResource = (ModuleLoadCmdAppCatalogResourceAppCat)cmdResource.get(ids);
+                    }
+                    cmdResource.setCmd(cmd);
+                    cmdResource.setAppDeploymentResource(existingDep);
+                    cmdResource.setAppDeploymentId(deploymentId);
+                    cmdResource.save();
+                }
+            }
+
+            PreJobCommandAppCatalogResourceAppCat preJobCommandResource = new PreJobCommandAppCatalogResourceAppCat();
+            ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.PreJobCommandConstants.DEPLOYMENT_ID, deploymentId);
+            preJobCommandResource.remove(ids);
+            List<String> preJobCommands = updatedDeployment.getPreJobCommands();
+            if (preJobCommands != null && !preJobCommands.isEmpty()){
+                for (String cmd : preJobCommands){
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.PreJobCommandConstants.DEPLOYMENT_ID, deploymentId);
+                    ids.put(AppCatAbstractResource.PreJobCommandConstants.COMMAND, cmd);
+                    if (preJobCommandResource.isExists(ids)){
+                        preJobCommandResource = (PreJobCommandAppCatalogResourceAppCat)preJobCommandResource.get(ids);
+                    }
+                    preJobCommandResource.setCommand(cmd);
+                    preJobCommandResource.setAppDeploymentResource(existingDep);
+                    preJobCommandResource.setAppDeploymentId(deploymentId);
+                    preJobCommandResource.save();
+                }
+            }
+
+            PostJobCommandAppCatalogResourceAppCat postJobCommandResource = new PostJobCommandAppCatalogResourceAppCat();
+            ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.PostJobCommandConstants.DEPLOYMENT_ID, deploymentId);
+            postJobCommandResource.remove(ids);
+            List<String> postJobCommands = updatedDeployment.getPostJobCommands();
+            if (postJobCommands != null && !postJobCommands.isEmpty()){
+                for (String cmd : postJobCommands){
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.PostJobCommandConstants.DEPLOYMENT_ID, deploymentId);
+                    ids.put(AppCatAbstractResource.PostJobCommandConstants.COMMAND, cmd);
+                    if (postJobCommandResource.isExists(ids)){
+                        postJobCommandResource = (PostJobCommandAppCatalogResourceAppCat)postJobCommandResource.get(ids);
+                    }
+                    postJobCommandResource.setCommand(cmd);
+                    postJobCommandResource.setAppDeploymentResource(existingDep);
+                    postJobCommandResource.setAppDeploymentId(deploymentId);
+                    postJobCommandResource.save();
+                }
+            }
+
+            // remove existing lib prepand paths
+            LibraryPrepandPathAppCatalogResourceAppCat prepandPathResource = new LibraryPrepandPathAppCatalogResourceAppCat();
+            ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.LibraryPrepandPathConstants.DEPLOYMENT_ID, deploymentId);
+            prepandPathResource.remove(ids);
+            List<SetEnvPaths> libPrependPaths = updatedDeployment.getLibPrependPaths();
+            if (libPrependPaths != null && !libPrependPaths.isEmpty()){
+                for (SetEnvPaths path : libPrependPaths){
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.LibraryPrepandPathConstants.DEPLOYMENT_ID, deploymentId);
+                    ids.put(AppCatAbstractResource.LibraryPrepandPathConstants.NAME, path.getName());
+                    if (prepandPathResource.isExists(ids)){
+                        prepandPathResource = (LibraryPrepandPathAppCatalogResourceAppCat)prepandPathResource.get(ids);
+                    }
+                    prepandPathResource.setAppDeploymentResource(existingDep);
+                    prepandPathResource.setName(path.getName());
+                    prepandPathResource.setValue(path.getValue());
+                    prepandPathResource.setDeploymentId(deploymentId);
+                    prepandPathResource.save();
+                }
+            }
+
+            List<SetEnvPaths> libApendPaths = updatedDeployment.getLibAppendPaths();
+            // remove lib append paths
+            LibraryApendPathAppCatalogResourceAppCat apendPathResource = new LibraryApendPathAppCatalogResourceAppCat();
+            ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.LibraryApendPathConstants.DEPLOYMENT_ID, deploymentId);
+            apendPathResource.remove(ids);
+            if (libApendPaths != null && !libApendPaths.isEmpty()){
+                for (SetEnvPaths path : libApendPaths){
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.LibraryApendPathConstants.DEPLOYMENT_ID, deploymentId);
+                    ids.put(AppCatAbstractResource.LibraryApendPathConstants.NAME, path.getName());
+                    if (apendPathResource.isExists(ids)){
+                        apendPathResource = (LibraryApendPathAppCatalogResourceAppCat)apendPathResource.get(ids);
+                    }
+                    apendPathResource.setAppDeploymentResource(existingDep);
+                    apendPathResource.setName(path.getName());
+                    apendPathResource.setValue(path.getValue());
+                    apendPathResource.setDeploymentId(deploymentId);
+                    apendPathResource.save();
+                }
+            }
+
+            List<SetEnvPaths> setEnvironment = updatedDeployment.getSetEnvironment();
+            // remove existing setEnvPaths
+            AppEnvironmentAppCatalogResourceAppCat environmentResource = new AppEnvironmentAppCatalogResourceAppCat();
+            ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.AppEnvironmentConstants.DEPLOYMENT_ID, deploymentId);
+            environmentResource.remove(ids);
+            if (setEnvironment != null && !setEnvironment.isEmpty()){
+                for (SetEnvPaths path : setEnvironment){
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.AppEnvironmentConstants.DEPLOYMENT_ID, deploymentId);
+                    ids.put(AppCatAbstractResource.AppEnvironmentConstants.NAME, path.getName());
+                    if (environmentResource.isExists(ids)){
+                        environmentResource = (AppEnvironmentAppCatalogResourceAppCat)environmentResource.get(ids);
+                    }
+                    environmentResource.setAppDeploymentResource(existingDep);
+                    environmentResource.setName(path.getName());
+                    environmentResource.setValue(path.getValue());
+                    environmentResource.setDeploymentId(deploymentId);
+                    environmentResource.save();
+                }
+            }
+        }catch (Exception e) {
+            logger.error("Error while updating application deployment...", e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public ApplicationDeploymentDescription getApplicationDeployement(String deploymentId) throws AppCatalogException {
+        try {
+            AppDeploymentAppCatalogResourceAppCat deploymentResource = new AppDeploymentAppCatalogResourceAppCat();
+            AppDeploymentAppCatalogResourceAppCat appDep = (AppDeploymentAppCatalogResourceAppCat)deploymentResource.get(deploymentId);
+            return AppCatalogThriftConversion.getApplicationDeploymentDescription(appDep);
+        }catch (Exception e) {
+            logger.error("Error while retrieving application deployment...", e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public List<ApplicationDeploymentDescription> getApplicationDeployements(Map<String, String> filters) throws AppCatalogException {
+        List<ApplicationDeploymentDescription> deploymentDescriptions = new ArrayList<ApplicationDeploymentDescription>();
+        try {
+            AppDeploymentAppCatalogResourceAppCat resource = new AppDeploymentAppCatalogResourceAppCat();
+            boolean firstTry=true;
+            for (String fieldName : filters.keySet() ){
+                List<ApplicationDeploymentDescription> tmpDescriptions = new ArrayList<ApplicationDeploymentDescription>();
+                if (fieldName.equals(AppCatAbstractResource.ApplicationDeploymentConstants.APP_MODULE_ID)){
+                    List<AppCatalogResource> resources = resource.get(AppCatAbstractResource.ApplicationDeploymentConstants.APP_MODULE_ID, filters.get(fieldName));
+                    if (resources != null && !resources.isEmpty()){
+                    	tmpDescriptions = AppCatalogThriftConversion.getAppDepDescList(resources);
+                    }
+                }else if (fieldName.equals(AppCatAbstractResource.ApplicationDeploymentConstants.COMPUTE_HOST_ID)){
+                    List<AppCatalogResource> resources = resource.get(AppCatAbstractResource.ApplicationDeploymentConstants.COMPUTE_HOST_ID, filters.get(fieldName));
+                    if (resources != null && !resources.isEmpty()){
+                    	tmpDescriptions = AppCatalogThriftConversion.getAppDepDescList(resources);
+                    }
+                } else {
+                    logger.error("Unsupported field name for app deployment.", new IllegalArgumentException());
+                    throw new IllegalArgumentException("Unsupported field name for app deployment.");
+                }
+                if (firstTry){
+                	deploymentDescriptions.addAll(tmpDescriptions);
+                    firstTry=false;
+                }else{
+                    List<String> ids=new ArrayList<String>();
+                	for (ApplicationDeploymentDescription applicationDeploymentDescription : deploymentDescriptions) {
+						ids.add(applicationDeploymentDescription.getAppDeploymentId());
+					}
+                    List<ApplicationDeploymentDescription> tmp2Descriptions = new ArrayList<ApplicationDeploymentDescription>();
+                	for (ApplicationDeploymentDescription applicationDeploymentDescription : tmpDescriptions) {
+						if (ids.contains(applicationDeploymentDescription.getAppDeploymentId())){
+							tmp2Descriptions.add(applicationDeploymentDescription);
+						}
+					}
+                	deploymentDescriptions.clear();
+                	deploymentDescriptions.addAll(tmp2Descriptions);
+                }
+            }
+        }catch (Exception e){
+            logger.error("Error while retrieving app deployment list...", e);
+            throw new AppCatalogException(e);
+        }
+        return deploymentDescriptions;
+    }
+
+    @Override
+    public List<ApplicationDeploymentDescription> getAllApplicationDeployements(String gatewayId) throws AppCatalogException {
+        List<ApplicationDeploymentDescription> deploymentDescriptions = new ArrayList<ApplicationDeploymentDescription>();
+        try {
+            AppDeploymentAppCatalogResourceAppCat resource = new AppDeploymentAppCatalogResourceAppCat();
+            resource.setGatewayId(gatewayId);
+            List<AppCatalogResource> resources = resource.getAll();
+            if (resources != null && !resources.isEmpty()){
+                deploymentDescriptions = AppCatalogThriftConversion.getAppDepDescList(resources);
+            }
+
+        }catch (Exception e){
+            logger.error("Error while retrieving app deployment list...", e);
+            throw new AppCatalogException(e);
+        }
+        return deploymentDescriptions;
+    }
+
+    @Override
+    public List<String> getAllApplicationDeployementIds() throws AppCatalogException {
+        try {
+            AppDeploymentAppCatalogResourceAppCat resource = new AppDeploymentAppCatalogResourceAppCat();
+            return resource.getAllIds();
+        }catch (Exception e){
+            logger.error("Error while retrieving app deployment list...", e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public boolean isAppDeploymentExists(String deploymentId) throws AppCatalogException {
+        try {
+           AppDeploymentAppCatalogResourceAppCat deploymentResource = new AppDeploymentAppCatalogResourceAppCat();
+            return deploymentResource.isExists(deploymentId);
+        }catch (Exception e){
+            logger.error("Error while retrieving app deployment...", e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public void removeAppDeployment(String deploymentId) throws AppCatalogException {
+        try {
+            AppDeploymentAppCatalogResourceAppCat deploymentResource = new AppDeploymentAppCatalogResourceAppCat();
+            deploymentResource.remove(deploymentId);
+        }catch (Exception e){
+            logger.error("Error while deleting app deployment...", e);
+            throw new AppCatalogException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/airavata/blob/ec8c6202/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/impl/ApplicationInterfaceImpl.java
----------------------------------------------------------------------
diff --git a/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/impl/ApplicationInterfaceImpl.java b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/impl/ApplicationInterfaceImpl.java
new file mode 100644
index 0000000..c830f0a
--- /dev/null
+++ b/modules/registry/registry-core/src/main/java/org/apache/airavata/registry/core/app/catalog/impl/ApplicationInterfaceImpl.java
@@ -0,0 +1,450 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.airavata.registry.core.app.catalog.impl;
+
+import org.apache.airavata.model.appcatalog.appdeployment.ApplicationModule;
+import org.apache.airavata.model.appcatalog.appinterface.ApplicationInterfaceDescription;
+import org.apache.airavata.model.appcatalog.appinterface.InputDataObjectType;
+import org.apache.airavata.model.appcatalog.appinterface.OutputDataObjectType;
+import org.apache.airavata.model.appcatalog.appinterface.applicationInterfaceModelConstants;
+import org.apache.airavata.registry.core.app.catalog.resources.*;
+import org.apache.airavata.registry.core.app.catalog.util.AppCatalogThriftConversion;
+import org.apache.airavata.registry.core.app.catalog.util.AppCatalogUtils;
+import org.apache.airavata.registry.cpi.AppCatalogException;
+import org.apache.airavata.registry.cpi.ApplicationInterface;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ApplicationInterfaceImpl implements ApplicationInterface {
+    private final static Logger logger = LoggerFactory.getLogger(ApplicationInterfaceImpl.class);
+
+    @Override
+    public String addApplicationModule(ApplicationModule applicationModule, String gatewayId) throws AppCatalogException {
+        try {
+            AppModuleAppCatalogResourceAppCat moduleResource = new AppModuleAppCatalogResourceAppCat();
+            moduleResource.setModuleName(applicationModule.getAppModuleName());
+            moduleResource.setGatewayId(gatewayId);
+            if (!applicationModule.getAppModuleId().equals("") && !applicationModule.getAppModuleId().equals(applicationInterfaceModelConstants.DEFAULT_ID)){
+                moduleResource.setModuleId(applicationModule.getAppModuleId());
+            }else {
+                moduleResource.setModuleId(AppCatalogUtils.getID(applicationModule.getAppModuleName()));
+            }
+            moduleResource.setModuleDesc(applicationModule.getAppModuleDescription());
+            moduleResource.setModuleVersion(applicationModule.getAppModuleVersion());
+            moduleResource.save();
+            applicationModule.setAppModuleId(moduleResource.getModuleId());
+            return moduleResource.getModuleId();
+        }catch (Exception e) {
+            logger.error("Error while adding application module "+applicationModule.getAppModuleName()+" ["+applicationModule.getAppModuleVersion()+"]", e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public String addApplicationInterface(ApplicationInterfaceDescription applicationInterfaceDescription, String gatewayId) throws AppCatalogException {
+        try {
+            AppInterfaceAppCatalogResourceAppCat resource = new AppInterfaceAppCatalogResourceAppCat();
+            resource.setAppName(applicationInterfaceDescription.getApplicationName());
+            if (!applicationInterfaceDescription.getApplicationInterfaceId().equals("") && !applicationInterfaceDescription.getApplicationInterfaceId().equals(applicationInterfaceModelConstants.DEFAULT_ID)){
+                resource.setInterfaceId(applicationInterfaceDescription.getApplicationInterfaceId());
+            }else {
+                resource.setInterfaceId(AppCatalogUtils.getID(applicationInterfaceDescription.getApplicationName()));
+            }
+            resource.setAppDescription(applicationInterfaceDescription.getApplicationDescription());
+            resource.setGatewayId(gatewayId);
+            resource.save();
+            applicationInterfaceDescription.setApplicationInterfaceId(resource.getInterfaceId());
+
+            List<String> applicationModules = applicationInterfaceDescription.getApplicationModules();
+            if (applicationModules != null && !applicationModules.isEmpty()){
+                for (String moduleId : applicationModules){
+                    AppModuleAppCatalogResourceAppCat appModuleResource = new AppModuleAppCatalogResourceAppCat();
+                    AppModuleMappingAppCatalogResourceAppCat moduleMappingResource = new AppModuleMappingAppCatalogResourceAppCat();
+                    moduleMappingResource.setInterfaceId(resource.getInterfaceId());
+                    moduleMappingResource.setModuleId(moduleId);
+                    moduleMappingResource.setModuleResource((AppModuleAppCatalogResourceAppCat)appModuleResource.get(moduleId));
+                    moduleMappingResource.setAppInterfaceResource(resource);
+                    moduleMappingResource.save();
+                }
+            }
+
+            List<InputDataObjectType> applicationInputs = applicationInterfaceDescription.getApplicationInputs();
+            if (applicationInputs != null && !applicationInputs.isEmpty()){
+                for (InputDataObjectType input : applicationInputs){
+                    ApplicationInputAppCatalogResourceAppCat inputResource = new ApplicationInputAppCatalogResourceAppCat();
+                    inputResource.setAppInterfaceResource(resource);
+                    inputResource.setInterfaceID(resource.getInterfaceId());
+                    inputResource.setUserFriendlyDesc(input.getUserFriendlyDescription());
+                    inputResource.setInputKey(input.getName());
+                    inputResource.setInputVal(input.getValue());
+                    inputResource.setDataType(input.getType().toString());
+                    inputResource.setMetadata(input.getMetaData());
+                    inputResource.setStandardInput(input.isStandardInput());
+                    inputResource.setAppArgument(input.getApplicationArgument());
+                    inputResource.setInputOrder(input.getInputOrder());
+                    inputResource.setRequired(input.isIsRequired());
+                    inputResource.setRequiredToCMD(input.isRequiredToAddedToCommandLine());
+                    inputResource.save();
+                }
+            }
+
+            List<OutputDataObjectType> applicationOutputs = applicationInterfaceDescription.getApplicationOutputs();
+            if (applicationOutputs != null && !applicationOutputs.isEmpty()) {
+                for (OutputDataObjectType output : applicationOutputs) {
+                    ApplicationOutputAppCatalogResourceAppCat outputResource = new ApplicationOutputAppCatalogResourceAppCat();
+                    outputResource.setInterfaceID(resource.getInterfaceId());
+                    outputResource.setAppInterfaceResource(resource);
+                    outputResource.setOutputKey(output.getName());
+                    outputResource.setOutputVal(output.getValue());
+                    outputResource.setDataType(output.getType().toString());
+                    outputResource.setRequired(output.isIsRequired());
+                    outputResource.setRequiredToCMD(output.isRequiredToAddedToCommandLine());
+                    outputResource.setDataMovement(output.isDataMovement());
+                    outputResource.setDataNameLocation(output.getLocation());
+                    outputResource.setAppArgument(output.getApplicationArgument());
+                    outputResource.setSearchQuery(output.getSearchQuery());
+                    outputResource.save();
+                }
+            }
+            return resource.getInterfaceId();
+        }catch (Exception e) {
+            logger.error("Error while adding application interface "+applicationInterfaceDescription.getApplicationName(), e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public void addApplicationModuleMapping(String moduleId, String interfaceId) throws AppCatalogException {
+        try {
+            AppModuleAppCatalogResourceAppCat appModuleResource = new AppModuleAppCatalogResourceAppCat();
+            AppInterfaceAppCatalogResourceAppCat interfaceResource = new AppInterfaceAppCatalogResourceAppCat();
+            AppModuleMappingAppCatalogResourceAppCat moduleMappingResource = new AppModuleMappingAppCatalogResourceAppCat();
+            moduleMappingResource.setInterfaceId(interfaceId);
+            moduleMappingResource.setModuleId(moduleId);
+            moduleMappingResource.setModuleResource((AppModuleAppCatalogResourceAppCat)appModuleResource.get(moduleId));
+            moduleMappingResource.setAppInterfaceResource((AppInterfaceAppCatalogResourceAppCat)interfaceResource.get(interfaceId));
+            moduleMappingResource.save();
+        }catch (Exception e) {
+            logger.error("Error while saving application module mapping "+moduleId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public void updateApplicationModule(String moduleId, ApplicationModule updatedModule) throws AppCatalogException {
+        try {
+            AppModuleAppCatalogResourceAppCat moduleResource = new AppModuleAppCatalogResourceAppCat();
+            AppModuleAppCatalogResourceAppCat existingModule = (AppModuleAppCatalogResourceAppCat)moduleResource.get(moduleId);
+            existingModule.setModuleName(updatedModule.getAppModuleName());
+            existingModule.setModuleDesc(updatedModule.getAppModuleDescription());
+            existingModule.setModuleVersion(updatedModule.getAppModuleVersion());
+            existingModule.save();
+        }catch (Exception e) {
+            logger.error("Error while updating application module "+moduleId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public void updateApplicationInterface(String interfaceId, ApplicationInterfaceDescription updatedInterface) throws AppCatalogException {
+        try {
+            AppInterfaceAppCatalogResourceAppCat resource = new AppInterfaceAppCatalogResourceAppCat();
+            AppInterfaceAppCatalogResourceAppCat existingInterface = (AppInterfaceAppCatalogResourceAppCat) resource.get(interfaceId);
+            existingInterface.setAppName(updatedInterface.getApplicationName());
+            existingInterface.setAppDescription(updatedInterface.getApplicationDescription());
+            existingInterface.save();
+
+            // remove existing modules before adding
+            Map<String, String> ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.AppModuleMappingConstants.INTERFACE_ID, interfaceId);
+            AppModuleMappingAppCatalogResourceAppCat moduleMappingResource = new AppModuleMappingAppCatalogResourceAppCat();
+            moduleMappingResource.remove(ids);
+            List<String> applicationModules = updatedInterface.getApplicationModules();
+            if (applicationModules != null && !applicationModules.isEmpty()) {
+                for (String moduleId : applicationModules) {
+                    AppModuleAppCatalogResourceAppCat appModuleResource = new AppModuleAppCatalogResourceAppCat();
+                    moduleMappingResource = new AppModuleMappingAppCatalogResourceAppCat();
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.AppModuleMappingConstants.MODULE_ID, moduleId);
+                    ids.put(AppCatAbstractResource.AppModuleMappingConstants.INTERFACE_ID, interfaceId);
+                    AppModuleMappingAppCatalogResourceAppCat existingMapping;
+                    if (!moduleMappingResource.isExists(ids)) {
+                        existingMapping = new AppModuleMappingAppCatalogResourceAppCat();
+                    } else {
+                        existingMapping = (AppModuleMappingAppCatalogResourceAppCat) moduleMappingResource.get(ids);
+                    }
+                    existingMapping.setInterfaceId(interfaceId);
+                    existingMapping.setModuleId(moduleId);
+                    existingMapping.setModuleResource((AppModuleAppCatalogResourceAppCat) appModuleResource.get(moduleId));
+                    existingMapping.setAppInterfaceResource(existingInterface);
+                    existingMapping.save();
+                }
+            }
+
+            // remove existing application inputs
+            ApplicationInputAppCatalogResourceAppCat inputResource = new ApplicationInputAppCatalogResourceAppCat();
+            ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.AppInputConstants.INTERFACE_ID, interfaceId);
+            inputResource.remove(ids);
+            List<InputDataObjectType> applicationInputs = updatedInterface.getApplicationInputs();
+            if (applicationInputs != null && !applicationInputs.isEmpty()) {
+                for (InputDataObjectType input : applicationInputs) {
+                    inputResource = new ApplicationInputAppCatalogResourceAppCat();
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.AppInputConstants.INTERFACE_ID, interfaceId);
+                    ids.put(AppCatAbstractResource.AppInputConstants.INPUT_KEY, input.getName());
+                    if (inputResource.isExists(ids)) {
+                        inputResource = (ApplicationInputAppCatalogResourceAppCat) inputResource.get(ids);
+                    }
+                    inputResource.setAppInterfaceResource(existingInterface);
+                    inputResource.setInterfaceID(interfaceId);
+                    inputResource.setUserFriendlyDesc(input.getUserFriendlyDescription());
+                    inputResource.setInputKey(input.getName());
+                    inputResource.setInputVal(input.getValue());
+                    inputResource.setDataType(input.getType().toString());
+                    inputResource.setMetadata(input.getMetaData());
+                    inputResource.setStandardInput(input.isStandardInput());
+                    inputResource.setAppArgument(input.getApplicationArgument());
+                    inputResource.setInputOrder(input.getInputOrder());
+                    inputResource.setRequired(input.isIsRequired());
+                    inputResource.setRequiredToCMD(input.isRequiredToAddedToCommandLine());
+                    inputResource.save();
+                }
+            }
+
+            // remove existing app outputs before adding
+            ApplicationOutputAppCatalogResourceAppCat outputResource = new ApplicationOutputAppCatalogResourceAppCat();
+            ids = new HashMap<String, String>();
+            ids.put(AppCatAbstractResource.AppOutputConstants.INTERFACE_ID, interfaceId);
+            outputResource.remove(ids);
+            List<OutputDataObjectType> applicationOutputs = updatedInterface.getApplicationOutputs();
+            if (applicationOutputs != null && !applicationOutputs.isEmpty()) {
+                for (OutputDataObjectType output : applicationOutputs) {
+                    outputResource = new ApplicationOutputAppCatalogResourceAppCat();
+                    ids = new HashMap<String, String>();
+                    ids.put(AppCatAbstractResource.AppOutputConstants.INTERFACE_ID, interfaceId);
+                    ids.put(AppCatAbstractResource.AppOutputConstants.OUTPUT_KEY, output.getName());
+                    if (outputResource.isExists(ids)) {
+                        outputResource = (ApplicationOutputAppCatalogResourceAppCat) outputResource.get(ids);
+                    }
+                    outputResource.setInterfaceID(interfaceId);
+                    outputResource.setAppInterfaceResource(existingInterface);
+                    outputResource.setOutputKey(output.getName());
+                    outputResource.setOutputVal(output.getValue());
+                    outputResource.setDataType(output.getType().toString());
+                    outputResource.setRequired(output.isIsRequired());
+                    outputResource.setRequiredToCMD(output.isRequiredToAddedToCommandLine());
+                    outputResource.setDataMovement(output.isDataMovement());
+                    outputResource.setDataNameLocation(output.getLocation());
+                    outputResource.setAppArgument(output.getApplicationArgument());
+                    outputResource.setSearchQuery(output.getSearchQuery());
+                    outputResource.save();
+                }
+            }
+        } catch (Exception e) {
+            logger.error("Error while updating application interface " + interfaceId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public ApplicationModule getApplicationModule(String moduleId) throws AppCatalogException {
+        try {
+            AppModuleAppCatalogResourceAppCat appModuleResource = new AppModuleAppCatalogResourceAppCat();
+            return AppCatalogThriftConversion.getApplicationModuleDesc((AppModuleAppCatalogResourceAppCat) appModuleResource.get(moduleId));
+        }catch (Exception e) {
+            logger.error("Error while retrieving application module "+moduleId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public ApplicationInterfaceDescription getApplicationInterface(String interfaceId) throws AppCatalogException {
+        try {
+            AppInterfaceAppCatalogResourceAppCat interfaceResource = new AppInterfaceAppCatalogResourceAppCat();
+            return AppCatalogThriftConversion.getApplicationInterfaceDescription((AppInterfaceAppCatalogResourceAppCat)interfaceResource.get(interfaceId));
+        }catch (Exception e) {
+            logger.error("Error while retrieving application interface '"+interfaceId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public List<ApplicationModule> getApplicationModules(Map<String, String> filters) throws AppCatalogException {
+        List<ApplicationModule> modules = new ArrayList<ApplicationModule>();
+        try {
+            AppModuleAppCatalogResourceAppCat resource = new AppModuleAppCatalogResourceAppCat();
+            for (String fieldName : filters.keySet() ){
+                if (fieldName.equals(AppCatAbstractResource.ApplicationModuleConstants.MODULE_NAME)){
+                    List<AppCatalogResource> resources = resource.get(AppCatAbstractResource.ApplicationModuleConstants.MODULE_NAME, filters.get(fieldName));
+                    if (resources != null && !resources.isEmpty()){
+                        modules = AppCatalogThriftConversion.getAppModules(resources);
+                    }
+                }else {
+                    logger.error("Unsupported field name for app module.", new IllegalArgumentException());
+                    throw new IllegalArgumentException("Unsupported field name for app module.");
+                }
+            }
+        }catch (Exception e){
+            logger.error("Error while retrieving app module list...", e);
+            throw new AppCatalogException(e);
+        }
+        return modules;
+    }
+
+    @Override
+    public List<ApplicationModule> getAllApplicationModules(String gatewayId) throws AppCatalogException {
+        List<ApplicationModule> applicationModules = new ArrayList<ApplicationModule>();
+        try {
+            AppModuleAppCatalogResourceAppCat resource = new AppModuleAppCatalogResourceAppCat();
+            resource.setGatewayId(gatewayId);
+            List<AppCatalogResource> resources = resource.getAll();
+            if (resources != null && !resources.isEmpty()){
+                applicationModules = AppCatalogThriftConversion.getAppModules(resources);
+            }
+        }catch (Exception e){
+            logger.error("Error while retrieving compute resource list...", e);
+            throw new AppCatalogException(e);
+        }
+        return applicationModules;
+    }
+
+    @Override
+    public List<ApplicationInterfaceDescription> getApplicationInterfaces(Map<String, String> filters) throws AppCatalogException {
+        List<ApplicationInterfaceDescription> appInterfaces = new ArrayList<ApplicationInterfaceDescription>();
+        try {
+            AppInterfaceAppCatalogResourceAppCat resource = new AppInterfaceAppCatalogResourceAppCat();
+            for (String fieldName : filters.keySet() ){
+                if (fieldName.equals(AppCatAbstractResource.ApplicationInterfaceConstants.APPLICATION_NAME)){
+                    List<AppCatalogResource> resources = resource.get(AppCatAbstractResource.ApplicationInterfaceConstants.APPLICATION_NAME, filters.get(fieldName));
+                    appInterfaces = AppCatalogThriftConversion.getAppInterfaceDescList(resources);
+                }else {
+                    logger.error("Unsupported field name for app interface.", new IllegalArgumentException());
+                    throw new IllegalArgumentException("Unsupported field name '"+fieldName+"' for app interface.");
+                }
+            }
+        }catch (Exception e){
+            logger.error("Error while retrieving app interface list...", e);
+            throw new AppCatalogException(e);
+        }
+        return appInterfaces;
+    }
+
+    @Override
+    public List<ApplicationInterfaceDescription> getAllApplicationInterfaces(String gatewayId) throws AppCatalogException {
+        try {
+            AppInterfaceAppCatalogResourceAppCat resource = new AppInterfaceAppCatalogResourceAppCat();
+            resource.setGatewayId(gatewayId);
+            List<AppCatalogResource> resources = resource.getAll();
+            return AppCatalogThriftConversion.getAppInterfaceDescList(resources);
+        }catch (Exception e){
+            logger.error("Error while retrieving app interface list...", e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public List<String> getAllApplicationInterfaceIds() throws AppCatalogException {
+        try {
+            AppInterfaceAppCatalogResourceAppCat resource = new AppInterfaceAppCatalogResourceAppCat();
+            return resource.getAllIds();
+        }catch (Exception e){
+            logger.error("Error while retrieving app interface list...", e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public boolean removeApplicationInterface(String interfaceId) throws AppCatalogException {
+        try {
+            AppInterfaceAppCatalogResourceAppCat resource = new AppInterfaceAppCatalogResourceAppCat();
+            resource.remove(interfaceId);
+            return true;
+        }catch (Exception e){
+            logger.error("Error while removing app interface "+interfaceId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public boolean removeApplicationModule(String moduleId) throws AppCatalogException {
+        try {
+            AppModuleAppCatalogResourceAppCat resource = new AppModuleAppCatalogResourceAppCat();
+            resource.remove(moduleId);
+            return true;
+        }catch (Exception e){
+            logger.error("Error while removing app module "+moduleId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public boolean isApplicationInterfaceExists(String interfaceId) throws AppCatalogException {
+        try {
+            AppInterfaceAppCatalogResourceAppCat resource = new AppInterfaceAppCatalogResourceAppCat();
+            return resource.isExists(interfaceId);
+        }catch (Exception e){
+            logger.error("Error while checking app interface existence "+interfaceId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public boolean isApplicationModuleExists(String moduleId) throws AppCatalogException {
+        try {
+            AppModuleAppCatalogResourceAppCat resource = new AppModuleAppCatalogResourceAppCat();
+            return resource.isExists(moduleId);
+        }catch (Exception e){
+            logger.error("Error while checking app module existence "+moduleId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public List<InputDataObjectType> getApplicationInputs(String interfaceId) throws AppCatalogException {
+        try {
+            ApplicationInputAppCatalogResourceAppCat resource = new ApplicationInputAppCatalogResourceAppCat();
+            List<AppCatalogResource> resources = resource.get(AppCatAbstractResource.AppInputConstants.INTERFACE_ID, interfaceId);
+            return AppCatalogThriftConversion.getAppInputs(resources);
+        }catch (Exception e){
+            logger.error("Error while retrieving app inputs for application "+interfaceId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+
+    @Override
+    public List<OutputDataObjectType> getApplicationOutputs(String interfaceId) throws AppCatalogException {
+        try {
+            ApplicationOutputAppCatalogResourceAppCat resource = new ApplicationOutputAppCatalogResourceAppCat();
+            List<AppCatalogResource> resources = resource.get(AppCatAbstractResource.AppOutputConstants.INTERFACE_ID, interfaceId);
+            return AppCatalogThriftConversion.getAppOutputs(resources);
+        }catch (Exception e){
+            logger.error("Error while retrieving app outputs for application "+interfaceId, e);
+            throw new AppCatalogException(e);
+        }
+    }
+}


Mime
View raw message