Merge "bump the SO dublin version to 1.4.1" into dublin
diff --git a/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java b/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java
index 51b44b0..9265e5a 100644
--- a/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java
+++ b/adapters/mso-catalog-db-adapter/src/test/java/org/onap/so/db/catalog/client/CatalogDbClientTest.java
@@ -673,4 +673,19 @@
         Assert.assertNull(workflow);
     }
 
+    @Test
+    public void getWorkflowBySource_validSource_expectedOutput() {
+        List<Workflow> workflows = client.findWorkflowBySource("sdc");
+        assertTrue(workflows != null);
+        assertTrue(workflows.size() != 0);
+
+        assertEquals("testingWorkflow", workflows.get(0).getArtifactName());
+    }
+
+    @Test
+    public void getWorkflowBySource_invalidSource_nullOutput() {
+        List<Workflow> workflow = client.findWorkflowBySource("abc");
+        Assert.assertNull(workflow);
+    }
+
 }
diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java b/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java
index 4e97b5f..ad3e2d8 100644
--- a/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java
+++ b/asdc-controller/src/main/java/org/onap/so/asdc/installer/heat/ToscaResourceInstaller.java
@@ -85,6 +85,7 @@
 import org.onap.so.db.catalog.beans.HeatFiles;
 import org.onap.so.db.catalog.beans.HeatTemplate;
 import org.onap.so.db.catalog.beans.HeatTemplateParam;
+import org.onap.so.db.catalog.beans.InstanceGroup;
 import org.onap.so.db.catalog.beans.InstanceGroupType;
 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
 import org.onap.so.db.catalog.beans.NetworkInstanceGroup;
@@ -1180,6 +1181,8 @@
                     vfModuleArtifact.getArtifactInfo().getArtifactUUID());
             heatTemplate.setParameters(heatParam);
             vfModuleArtifact.setHeatTemplate(heatTemplate);
+        } else {
+            vfModuleArtifact.setHeatTemplate(existingHeatTemplate);
         }
     }
 
@@ -1208,6 +1211,8 @@
                 heatEnvironment.setArtifactChecksum(MANUAL_RECORD);
             }
             vfModuleArtifact.setHeatEnvironment(heatEnvironment);
+        } else {
+            vfModuleArtifact.setHeatEnvironment(existingHeatEnvironment);
         }
     }
 
@@ -1232,7 +1237,8 @@
                 heatFile.setArtifactChecksum(MANUAL_RECORD);
             }
             vfModuleArtifact.setHeatFiles(heatFile);
-
+        } else {
+            vfModuleArtifact.setHeatFiles(existingHeatFiles);
         }
     }
 
@@ -1724,16 +1730,25 @@
             VnfResourceCustomization vnfResourceCustomization, ToscaResourceStructure toscaResourceStructure) {
 
         Metadata instanceMetadata = group.getMetadata();
-        // Populate InstanceGroup
+
+        InstanceGroup existingInstanceGroup =
+                instanceGroupRepo.findByModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
+
         VFCInstanceGroup vfcInstanceGroup = new VFCInstanceGroup();
 
-        vfcInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
-        vfcInstanceGroup.setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
-        vfcInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
-        vfcInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
-        vfcInstanceGroup.setToscaNodeType(group.getType());
-        vfcInstanceGroup.setRole("SUB-INTERFACE"); // Set Role
-        vfcInstanceGroup.setType(InstanceGroupType.VNFC); // Set type
+        if (existingInstanceGroup == null) {
+            // Populate InstanceGroup
+            vfcInstanceGroup.setModelName(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_NAME));
+            vfcInstanceGroup
+                    .setModelInvariantUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID));
+            vfcInstanceGroup.setModelUUID(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_UUID));
+            vfcInstanceGroup.setModelVersion(instanceMetadata.getValue(SdcPropertyNames.PROPERTY_NAME_VERSION));
+            vfcInstanceGroup.setToscaNodeType(group.getType());
+            vfcInstanceGroup.setRole("SUB-INTERFACE"); // Set Role
+            vfcInstanceGroup.setType(InstanceGroupType.VNFC); // Set type
+        } else {
+            vfcInstanceGroup = (VFCInstanceGroup) existingInstanceGroup;
+        }
 
         // Populate VNFCInstanceGroupCustomization
         VnfcInstanceGroupCustomization vfcInstanceGroupCustom = new VnfcInstanceGroupCustomization();
diff --git a/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/infrastructure/MSOInfrastructureApplication.java b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/infrastructure/MSOInfrastructureApplication.java
index c61808e..8168d2a 100644
--- a/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/infrastructure/MSOInfrastructureApplication.java
+++ b/bpmn/mso-infrastructure-bpmn/src/main/java/org/onap/so/bpmn/infrastructure/MSOInfrastructureApplication.java
@@ -24,14 +24,14 @@
 
 import java.util.List;
 import java.util.concurrent.Executor;
-import org.camunda.bpm.application.PostDeploy;
+import javax.annotation.PostConstruct;
 import org.camunda.bpm.application.PreUndeploy;
 import org.camunda.bpm.application.ProcessApplicationInfo;
 import org.camunda.bpm.engine.ProcessEngine;
 import org.camunda.bpm.engine.repository.DeploymentBuilder;
 import org.onap.so.bpmn.common.DefaultToShortClassNameBeanNameGenerator;
 import org.onap.so.db.catalog.beans.Workflow;
-import org.onap.so.db.catalog.data.repository.WorkflowRepository;
+import org.onap.so.db.catalog.client.CatalogDbClient;
 import org.onap.so.logging.jaxrs.filter.MDCTaskDecorator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,13 +39,11 @@
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.autoconfigure.domain.EntityScan;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.ComponentScan.Filter;
 import org.springframework.context.annotation.FilterType;
 import org.springframework.context.annotation.Primary;
-import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
@@ -56,17 +54,17 @@
 
 @SpringBootApplication
 @EnableAsync
-@EnableJpaRepositories("org.onap.so.db.catalog.data.repository")
-@EntityScan({"org.onap.so.db.catalog.beans"})
 @ComponentScan(basePackages = {"org.onap"}, nameGenerator = DefaultToShortClassNameBeanNameGenerator.class,
         excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = SpringBootApplication.class)})
 
 public class MSOInfrastructureApplication {
 
     private static final Logger logger = LoggerFactory.getLogger(MSOInfrastructureApplication.class);
+    @Autowired
+    private ProcessEngine processEngine;
 
     @Autowired
-    private WorkflowRepository workflowRepository;
+    private CatalogDbClient catalogDbClient;
 
     @Value("${mso.async.core-pool-size}")
     private int corePoolSize;
@@ -79,6 +77,7 @@
 
     private static final String LOGS_DIR = "logs_dir";
     private static final String BPMN_SUFFIX = ".bpmn";
+    private static final String SDC_SOURCE = "sdc";
 
 
     private static void setLogsDir() {
@@ -93,10 +92,14 @@
         setLogsDir();
     }
 
-    @PostDeploy
-    public void postDeploy(ProcessEngine processEngineInstance) {
-        DeploymentBuilder deploymentBuilder = processEngineInstance.getRepositoryService().createDeployment();
-        deployCustomWorkflows(deploymentBuilder);
+    @PostConstruct
+    public void postConstruct() {
+        try {
+            DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
+            deployCustomWorkflows(deploymentBuilder);
+        } catch (Exception e) {
+            logger.warn("Unable to invoke deploymentBuilder: " + e.getMessage());
+        }
     }
 
     @PreUndeploy
@@ -117,23 +120,26 @@
     }
 
     public void deployCustomWorkflows(DeploymentBuilder deploymentBuilder) {
-        if (workflowRepository == null) {
-            return;
-        }
-        List<Workflow> workflows = workflowRepository.findAll();
-        if (workflows != null && workflows.size() != 0) {
-            for (Workflow workflow : workflows) {
-                String workflowName = workflow.getName();
-                String workflowBody = workflow.getBody();
-                if (!workflowName.endsWith(BPMN_SUFFIX)) {
-                    workflowName += BPMN_SUFFIX;
+        logger.debug("Attempting to deploy custom workflows");
+        try {
+            List<Workflow> workflows = catalogDbClient.findWorkflowBySource(SDC_SOURCE);
+            if (workflows != null && workflows.size() != 0) {
+                for (Workflow workflow : workflows) {
+                    String workflowName = workflow.getName();
+                    String workflowBody = workflow.getBody();
+                    if (!workflowName.endsWith(BPMN_SUFFIX)) {
+                        workflowName += BPMN_SUFFIX;
+                    }
+                    if (workflowBody != null) {
+                        logger.info("{} {}", "Deploying custom workflow", workflowName);
+                        deploymentBuilder.addString(workflowName, workflowBody);
+                    }
                 }
-                if (workflowBody != null) {
-                    logger.info("{} {}", "Deploying custom workflow", workflowName);
-                    deploymentBuilder.addString(workflowName, workflowBody);
-                }
+                deploymentBuilder.enableDuplicateFiltering(true);
+                deploymentBuilder.deploy();
             }
-            deploymentBuilder.deploy();
+        } catch (Exception e) {
+            logger.warn("Unable to deploy custom workflows, " + e.getMessage());
         }
     }
 }
diff --git a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java
index 1920046..ab5fdb9 100644
--- a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java
+++ b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/client/CatalogDbClient.java
@@ -145,6 +145,7 @@
     private static final String CLOUD_VERSION = "cloudVersion";
     private static final String HOMING_INSTANCE = "/homingInstance";
     private static final String ARTIFACT_UUID = "artifactUUID";
+    private static final String SOURCE = "source";
 
     private static final String TARGET_ENTITY = "SO:CatalogDB";
     private static final String ASTERISK = "*";
@@ -191,6 +192,7 @@
     private String findPnfResourceCustomizationByModelUuid = "/findPnfResourceCustomizationByModelUuid";
     private String findWorkflowByArtifactUUID = "/findByArtifactUUID";
     private String findWorkflowByModelUUID = "/findWorkflowByModelUUID";
+    private String findWorkflowBySource = "/findBySource";
     private String findVnfResourceCustomizationByModelUuid = "/findVnfResourceCustomizationByModelUuid";
 
     private String serviceURI;
@@ -333,6 +335,7 @@
 
         findWorkflowByArtifactUUID = endpoint + WORKFLOW + SEARCH + findWorkflowByArtifactUUID;
         findWorkflowByModelUUID = endpoint + WORKFLOW + SEARCH + findWorkflowByModelUUID;
+        findWorkflowBySource = endpoint + WORKFLOW + SEARCH + findWorkflowBySource;
 
         findVnfResourceCustomizationByModelUuid =
                 endpoint + VNF_RESOURCE_CUSTOMIZATION + SEARCH + findVnfResourceCustomizationByModelUuid;
@@ -889,4 +892,9 @@
         return this.getMultipleResources(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByModelUUID)
                 .queryParam(VNF_RESOURCE_MODEL_UUID, vnfResourceModelUUID).build().toString()));
     }
+
+    public List<Workflow> findWorkflowBySource(String source) {
+        return this.getMultipleResources(workflowClient,
+                getUri(UriBuilder.fromUri(findWorkflowBySource).queryParam(SOURCE, source).build().toString()));
+    }
 }
diff --git a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/WorkflowRepository.java b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/WorkflowRepository.java
index 89df521..8bcc60c 100644
--- a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/WorkflowRepository.java
+++ b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/WorkflowRepository.java
@@ -31,6 +31,8 @@
 
     Workflow findByArtifactUUID(String artifactUUID);
 
+    List<Workflow> findBySource(String source);
+
     /**
      * Used to fetch the @{link Workflow} by the Model UUID.
      *
diff --git a/mso-catalog-db/src/test/java/org/onap/so/db/catalog/data/repository/WorkflowRepositoryTest.java b/mso-catalog-db/src/test/java/org/onap/so/db/catalog/data/repository/WorkflowRepositoryTest.java
index 547b8e5..171d4b2 100644
--- a/mso-catalog-db/src/test/java/org/onap/so/db/catalog/data/repository/WorkflowRepositoryTest.java
+++ b/mso-catalog-db/src/test/java/org/onap/so/db/catalog/data/repository/WorkflowRepositoryTest.java
@@ -44,4 +44,14 @@
         Assert.assertTrue("testingWorkflow".equals(workflows.get(0).getArtifactName()));
     }
 
+    @Test
+    public void findBySourceTest() throws Exception {
+        List<Workflow> workflows = workflowRepository.findBySource("sdc");
+
+        Assert.assertTrue(workflows != null);
+        Assert.assertTrue(workflows.size() != 0);
+
+        Assert.assertTrue("testingWorkflow".equals(workflows.get(0).getArtifactName()));
+    }
+
 }