add rainy day handling with SERVICE_ROLE and type

add rainy day handling with SERVICE_ROLE and type

Issue-ID: SO-2205
Signed-off-by: Benjamin, Max (mb388a) <mb388a@att.com>
Change-Id: Ib85ccd787d4c8b210abb983e1f114da3d36be2b6
diff --git a/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/R__MacroData.sql b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/R__MacroData.sql
index 486b53c..13d736e 100644
--- a/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/R__MacroData.sql
+++ b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/R__MacroData.sql
@@ -813,3 +813,5 @@
 ('ConfigDeployVnfBB', 'NO_VALIDATE', 'CUSTOM');
 
 UPDATE rainy_day_handler_macro SET reg_ex_error_message = '*' WHERE reg_ex_error_message IS null;
+
+UPDATE rainy_day_handler_macro SET SERVICE_ROLE = '*' WHERE SERVICE_ROLE IS null;
diff --git a/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V6.1.1__AddServiceRoleToRainyDayHandling.sql b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V6.1.1__AddServiceRoleToRainyDayHandling.sql
new file mode 100644
index 0000000..8fe3caf
--- /dev/null
+++ b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V6.1.1__AddServiceRoleToRainyDayHandling.sql
@@ -0,0 +1,2 @@
+use catalogdb;
+ALTER TABLE rainy_day_handler_macro ADD COLUMN IF NOT EXISTS SERVICE_ROLE varchar(300) DEFAULT NULL;
\ No newline at end of file
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 f65f521..a955a2c 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
@@ -87,7 +87,7 @@
     @Test
     public void testGetRainyDayHandler_Regex() {
         RainyDayHandlerStatus rainyDayHandlerStatus = client.getRainyDayHandlerStatus("AssignServiceInstanceBB", "*",
-                "*", "*", "*", "The Flavor ID (nd.c6r16d20) could not be found.");
+                "*", "*", "*", "The Flavor ID (nd.c6r16d20) could not be found.", "*");
         Assert.assertEquals("Rollback", rainyDayHandlerStatus.getPolicy());
     }
 
@@ -95,7 +95,8 @@
     public void testGetRainyDayHandler__Encoding_Regex() {
         RainyDayHandlerStatus rainyDayHandlerStatus = client.getRainyDayHandlerStatus("AssignServiceInstanceBB", "*",
                 "*", "*", "*",
-                "resources.lba_0_dmz_vmi_0: Unknown id: Error: oper 1 url /fqname-to-id body {\"fq_name\": [\"zrdm6bvota05-dmz_sec_group\"], \"type\": \"security-group\"} response Name ['zrdm6bvota05-dmz_sec_group'] not found");
+                "resources.lba_0_dmz_vmi_0: Unknown id: Error: oper 1 url /fqname-to-id body {\"fq_name\": [\"zrdm6bvota05-dmz_sec_group\"], \"type\": \"security-group\"} response Name ['zrdm6bvota05-dmz_sec_group'] not found",
+                "*");
         Assert.assertEquals("Rollback", rainyDayHandlerStatus.getPolicy());
     }
 
diff --git a/adapters/mso-catalog-db-adapter/src/test/resources/db/migration/afterMigrate.sql b/adapters/mso-catalog-db-adapter/src/test/resources/db/migration/afterMigrate.sql
index 58b2983..32c5129 100644
--- a/adapters/mso-catalog-db-adapter/src/test/resources/db/migration/afterMigrate.sql
+++ b/adapters/mso-catalog-db-adapter/src/test/resources/db/migration/afterMigrate.sql
@@ -184,8 +184,8 @@
               'testVnfcCustomizationDescription',
               '2018-07-17 14:05:08');
 
-INSERT INTO `rainy_day_handler_macro` (`FLOW_NAME`,`SERVICE_TYPE`,`VNF_TYPE`,`ERROR_CODE`,`WORK_STEP`,`POLICY`,`SECONDARY_POLICY`,`REG_EX_ERROR_MESSAGE`) 
-VALUES ('AssignServiceInstanceBB','*','*','*','*','Rollback','Rollback','The Flavor ID.*could not be found.');
+INSERT INTO `rainy_day_handler_macro` (`FLOW_NAME`,`SERVICE_TYPE`,`VNF_TYPE`,`ERROR_CODE`,`WORK_STEP`,`POLICY`,`SECONDARY_POLICY`,`REG_EX_ERROR_MESSAGE`, `SERVICE_ROLE`) 
+VALUES ('AssignServiceInstanceBB','*','*','*','*','Rollback','Rollback','The Flavor ID.*could not be found.','*');
 
 INSERT INTO `cvnfc_customization`
             (`id`,
diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildingBlockRainyDay.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildingBlockRainyDay.java
index fd2054c..09a5424 100644
--- a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildingBlockRainyDay.java
+++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildingBlockRainyDay.java
@@ -144,9 +144,20 @@
                     // keep default workStep value
                 }
 
+                String serviceRole = ASTERISK;
+                try {
+                    serviceRole = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
+                            .getModelInfoServiceInstance().getServiceRole();
+                    if (serviceRole == null || serviceRole.isEmpty()) {
+                        serviceRole = ASTERISK;
+                    }
+                } catch (Exception ex) {
+                    // keep default serviceRole value
+                }
+
                 RainyDayHandlerStatus rainyDayHandlerStatus;
                 rainyDayHandlerStatus = catalogDbClient.getRainyDayHandlerStatus(bbName, serviceType, vnfType,
-                        errorCode, workStep, errorMessage);
+                        errorCode, workStep, errorMessage, serviceRole);
 
                 if (rainyDayHandlerStatus == null) {
                     handlingCode = "Abort";
diff --git a/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildlingBlockRainyDayTest.java b/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildlingBlockRainyDayTest.java
index 80373eb..18e0891 100644
--- a/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildlingBlockRainyDayTest.java
+++ b/bpmn/MSOCommonBPMN/src/test/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildlingBlockRainyDayTest.java
@@ -105,7 +105,7 @@
         rainyDayHandlerStatus.setWorkStep(ASTERISK);
 
         doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatus("AssignServiceInstanceBB",
-                "st1", "vnft1", "7000", "*", "errorMessage");
+                "st1", "vnft1", "7000", "*", "errorMessage", "*");
 
         executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
         assertEquals("Rollback", delegateExecution.getVariable("handlingCode"));
@@ -128,7 +128,7 @@
         rainyDayHandlerStatus.setWorkStep(ASTERISK);
 
         doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatus("AssignServiceInstanceBB",
-                "st1", "vnft1", ASTERISK, ASTERISK, "errorMessage");
+                "st1", "vnft1", ASTERISK, ASTERISK, "errorMessage", "*");
         executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
         assertEquals("Rollback", delegateExecution.getVariable("handlingCode"));
         assertEquals(5, delegateExecution.getVariable("maxRetries"));
@@ -141,7 +141,7 @@
         vnf.setVnfType("vnft1");
         delegateExecution.setVariable("aLaCarte", true);
         doReturn(null).when(MOCK_catalogDbClient).getRainyDayHandlerStatus(isA(String.class), isA(String.class),
-                isA(String.class), isA(String.class), isA(String.class), isA(String.class));
+                isA(String.class), isA(String.class), isA(String.class), isA(String.class), isA(String.class));
         delegateExecution.setVariable("suppressRollback", false);
 
         executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
@@ -152,7 +152,8 @@
     @Test
     public void queryRainyDayTableExceptionTest() {
         doThrow(RuntimeException.class).when(MOCK_catalogDbClient).getRainyDayHandlerStatus(isA(String.class),
-                isA(String.class), isA(String.class), isA(String.class), isA(String.class), isA(String.class));
+                isA(String.class), isA(String.class), isA(String.class), isA(String.class), isA(String.class),
+                isA(String.class));
         delegateExecution.setVariable("aLaCarte", true);
         executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
         delegateExecution.setVariable("suppressRollback", false);
@@ -178,7 +179,7 @@
         rainyDayHandlerStatus.setSecondaryPolicy("Abort");
 
         doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatus("AssignServiceInstanceBB",
-                "st1", "vnft1", "7000", "*", "errorMessage");
+                "st1", "vnft1", "7000", "*", "errorMessage", "*");
 
         executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, false);
 
@@ -203,7 +204,7 @@
         rainyDayHandlerStatus.setSecondaryPolicy("Abort");
 
         doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatus("AssignServiceInstanceBB",
-                "st1", "vnft1", "7000", "*", "errorMessage");
+                "st1", "vnft1", "7000", "*", "errorMessage", "*");
 
         executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
 
@@ -229,7 +230,7 @@
         rainyDayHandlerStatus.setSecondaryPolicy("Abort");
 
         doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatus("AssignServiceInstanceBB",
-                "st1", "vnft1", "7000", "*", "errorMessage");
+                "st1", "vnft1", "7000", "*", "errorMessage", "*");
 
         executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
 
@@ -255,7 +256,7 @@
         rainyDayHandlerStatus.setSecondaryPolicy("Abort");
 
         doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatus("AssignServiceInstanceBB",
-                "st1", "vnft1", "7000", "*", "errorMessage");
+                "st1", "vnft1", "7000", "*", "errorMessage", "*");
 
         executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
 
@@ -272,4 +273,52 @@
         assertEquals("Abort", delegateExecution.getVariable("handlingCode"));
     }
 
+    @Test
+    public void queryRainyDayTableServiceRoleNotDefined() throws Exception {
+        customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
+        serviceInstance.getModelInfoServiceInstance().setServiceType("st1");
+        serviceInstance.getModelInfoServiceInstance().setServiceRole("sr1");
+        vnf.setVnfType("vnft1");
+        delegateExecution.setVariable("aLaCarte", true);
+        delegateExecution.setVariable("suppressRollback", false);
+        delegateExecution.setVariable("WorkflowExceptionCode", "7000");
+        RainyDayHandlerStatus rainyDayHandlerStatus = new RainyDayHandlerStatus();
+        rainyDayHandlerStatus.setErrorCode("7000");
+        rainyDayHandlerStatus.setFlowName("AssignServiceInstanceBB");
+        rainyDayHandlerStatus.setServiceType("st1");
+        rainyDayHandlerStatus.setVnfType("vnft1");
+        rainyDayHandlerStatus.setPolicy("Rollback");
+        rainyDayHandlerStatus.setWorkStep(ASTERISK);
+
+        doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatus("AssignServiceInstanceBB",
+                "st1", "vnft1", "7000", "*", "errorMessage", "sr1");
+
+        executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
+        assertEquals("Rollback", delegateExecution.getVariable("handlingCode"));
+    }
+
+    @Test
+    public void queryRainyDayTableServiceRoleNC() throws Exception {
+        customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
+        serviceInstance.getModelInfoServiceInstance().setServiceType("st1");
+        serviceInstance.getModelInfoServiceInstance().setServiceRole("NETWORK-COLLECTION");
+        vnf.setVnfType("vnft1");
+        delegateExecution.setVariable("aLaCarte", true);
+        delegateExecution.setVariable("suppressRollback", false);
+        delegateExecution.setVariable("WorkflowExceptionCode", "7000");
+        RainyDayHandlerStatus rainyDayHandlerStatus = new RainyDayHandlerStatus();
+        rainyDayHandlerStatus.setErrorCode("7000");
+        rainyDayHandlerStatus.setFlowName("ActivateServiceInstanceBB");
+        rainyDayHandlerStatus.setServiceType("st1");
+        rainyDayHandlerStatus.setVnfType("vnft1");
+        rainyDayHandlerStatus.setPolicy("Abort");
+        rainyDayHandlerStatus.setWorkStep(ASTERISK);
+
+        doReturn(rainyDayHandlerStatus).when(MOCK_catalogDbClient).getRainyDayHandlerStatus("AssignServiceInstanceBB",
+                "st1", "vnft1", "7000", "*", "errorMessage", "NETWORK-COLLECTION");
+
+        executeBuildingBlockRainyDay.queryRainyDayTable(delegateExecution, true);
+        assertEquals("Abort", delegateExecution.getVariable("handlingCode"));
+    }
+
 }
diff --git a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/macro/RainyDayHandlerStatus.java b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/macro/RainyDayHandlerStatus.java
index 1882ad5..39217c7 100644
--- a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/macro/RainyDayHandlerStatus.java
+++ b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/macro/RainyDayHandlerStatus.java
@@ -77,12 +77,16 @@
     @Column(name = "SECONDARY_POLICY")
     private String secondaryPolicy;
 
+    @BusinessKey
+    @Column(name = "SERVICE_ROLE")
+    private String serviceRole;
+
     @Override
     public String toString() {
         return new ToStringBuilder(this).append("id", id).append("flowName", flowName)
                 .append("serviceType", serviceType).append("vnfType", vnfType).append("errorCode", errorCode)
                 .append("errorMessage", errorMessage).append("workStep", workStep).append("policy", policy)
-                .append("secondaryPolicy", secondaryPolicy).toString();
+                .append("secondaryPolicy", secondaryPolicy).append("serviceRole", serviceRole).toString();
     }
 
     @Override
@@ -93,13 +97,14 @@
         RainyDayHandlerStatus castOther = (RainyDayHandlerStatus) other;
         return new EqualsBuilder().append(flowName, castOther.flowName).append(serviceType, castOther.serviceType)
                 .append(vnfType, castOther.vnfType).append(errorCode, castOther.errorCode)
-                .append(workStep, castOther.workStep).append(policy, castOther.policy).isEquals();
+                .append(workStep, castOther.workStep).append(policy, castOther.policy)
+                .append(serviceRole, castOther.serviceRole).isEquals();
     }
 
     @Override
     public int hashCode() {
         return new HashCodeBuilder().append(flowName).append(serviceType).append(vnfType).append(errorCode)
-                .append(workStep).append(policy).toHashCode();
+                .append(workStep).append(policy).append(serviceRole).toHashCode();
     }
 
     public Integer getId() {
@@ -174,5 +179,12 @@
         this.errorMessage = errorMessage;
     }
 
+    public String getServiceRole() {
+        return serviceRole;
+    }
+
+    public void setServiceRole(String serviceRole) {
+        this.serviceRole = serviceRole;
+    }
 
 }
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 a18f870..a959f2f 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
@@ -137,6 +137,7 @@
     private static final String CLOUD_OWNER = "cloudOwner";
     private static final String FLOW_NAME = "flowName";
     private static final String ERROR_MESSAGE = "errorMessage";
+    private static final String SERVICE_ROLE = "serviceRole";
     private static final String SERVICE_TYPE = "serviceType";
     private static final String VNF_TYPE = "vnfType";
     private static final String ERROR_CODE = "errorCode";
@@ -638,7 +639,7 @@
     }
 
     public RainyDayHandlerStatus getRainyDayHandlerStatus(String flowName, String serviceType, String vnfType,
-            String errorCode, String workStep, String errorMessage) {
+            String errorCode, String workStep, String errorMessage, String serviceRole) {
         logger.debug(
                 "Get Rainy Day Status - Flow Name {}, Service Type: {} , vnfType {} , errorCode {}, workStep {}, errorMessage {}",
                 flowName, serviceType, vnfType, errorCode, workStep, errorMessage);
@@ -646,7 +647,8 @@
                 UriComponentsBuilder.fromUriString(endpoint + RAINY_DAY_HANDLER_MACRO + SEARCH + findRainyDayHandler)
                         .queryParam(FLOW_NAME, flowName).queryParam(SERVICE_TYPE, serviceType)
                         .queryParam(VNF_TYPE, vnfType).queryParam(ERROR_CODE, errorCode).queryParam(WORK_STEP, workStep)
-                        .queryParam(ERROR_MESSAGE, errorMessage).build().encode().toUri());
+                        .queryParam(ERROR_MESSAGE, errorMessage).queryParam(SERVICE_ROLE, serviceRole).build().encode()
+                        .toUri());
     }
 
     public ServiceRecipe getFirstByServiceModelUUIDAndAction(String modelUUID, String action) {
diff --git a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/RainyDayHandlerStatusRepository.java b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/RainyDayHandlerStatusRepository.java
index 6819657..efe078b 100644
--- a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/RainyDayHandlerStatusRepository.java
+++ b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/data/repository/RainyDayHandlerStatusRepository.java
@@ -32,10 +32,11 @@
     @Query(value = "SELECT * FROM rainy_day_handler_macro WHERE (FLOW_NAME = :flowName ) AND SERVICE_TYPE IN (:serviceType ,'*') "
             + " AND VNF_TYPE IN ( :vnfType , '*') AND ERROR_CODE IN (:errorCode  ,'*') AND WORK_STEP IN (:workStep , '*' ) "
             + " AND ( :errorMessage REGEXP rainy_day_handler_macro.REG_EX_ERROR_MESSAGE OR REG_EX_ERROR_MESSAGE = '*') "
-            + " ORDER BY CASE WHEN :errorMessage REGEXP REG_EX_ERROR_MESSAGE THEN 0 WHEN ERROR_CODE != '*' THEN 1 WHEN VNF_TYPE != '*' THEN 2 WHEN SERVICE_TYPE != '*' THEN 3 ELSE 4 END LIMIT 1;",
+            + " AND SERVICE_ROLE IN ( :serviceRole , '*') "
+            + " ORDER BY CASE WHEN :errorMessage REGEXP REG_EX_ERROR_MESSAGE THEN 0 WHEN ERROR_CODE != '*' THEN 1 WHEN VNF_TYPE != '*' THEN 2 WHEN SERVICE_TYPE != '*' THEN 3 WHEN SERVICE_ROLE != '*' THEN 4 ELSE 5 END LIMIT 1;",
             nativeQuery = true)
     RainyDayHandlerStatus findRainyDayHandler(@Param("flowName") String flowName,
             @Param("serviceType") String serviceType, @Param("vnfType") String vnfType,
             @Param("errorCode") String errorCode, @Param("workStep") String workStep,
-            @Param("errorMessage") String errorMessage);
+            @Param("errorMessage") String errorMessage, @Param("serviceRole") String serviceRole);
 }