vnr vrf fk issue

Updated flyway migration script to rename object as per review
comments. Used Tosca node type for determining VRF and VNF nodes
updated code based on review comments for schema.sql
Altered ConfigurationCustomizationResource for VRF-VNF to have
bidirectional relationship
ToscaResourceInstaller ConfigurationResourceCustomization was made
bidrectional

Change-Id: Ib45151ff7a40edca38d512ae76f8889b1cc4d0c4
Issue-ID: SO-1777
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
diff --git a/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.7__WorkFlowDesignerTables.sql b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.7.1__WorkFlowDesignerTables.sql
similarity index 100%
rename from adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.7__WorkFlowDesignerTables.sql
rename to adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.7.1__WorkFlowDesignerTables.sql
diff --git a/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.7__Use_ID_Configuration_Customization.sql b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.7__Use_ID_Configuration_Customization.sql
new file mode 100644
index 0000000..b393312
--- /dev/null
+++ b/adapters/mso-catalog-db-adapter/src/main/resources/db/migration/V5.7__Use_ID_Configuration_Customization.sql
@@ -0,0 +1,75 @@
+USE catalogdb;
+
+/* Drop existing foreign key */
+ALTER TABLE `catalogdb`.`configuration_customization` 
+DROP FOREIGN KEY IF EXISTS `fk_configuration_customization__configuration_customization1`;
+
+ALTER TABLE `catalogdb`.`configuration_customization` 
+DROP FOREIGN KEY IF EXISTS `fk_configuration_resource_customization__configuration_resour1`;
+/* Drop existing index */
+ALTER TABLE `catalogdb`.`configuration_customization` 
+DROP INDEX IF EXISTS `fk_configuration_customization__configuration_customization_idx` ;
+
+/* Create a new table */
+CREATE TABLE `tmp_configuration_customization` (
+    `ID` INT(11) NOT NULL AUTO_INCREMENT,
+    `MODEL_CUSTOMIZATION_UUID` VARCHAR(200) NOT NULL,
+    `MODEL_INSTANCE_NAME` VARCHAR(200) NOT NULL,
+    `CONFIGURATION_TYPE` VARCHAR(200) DEFAULT NULL,
+    `CONFIGURATION_ROLE` VARCHAR(200) DEFAULT NULL,
+    `CONFIGURATION_FUNCTION` VARCHAR(200) DEFAULT NULL,
+    `CREATION_TIMESTAMP` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+    `CONFIGURATION_MODEL_UUID` VARCHAR(200) NOT NULL,
+    `SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` VARCHAR(200) DEFAULT NULL,
+	`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_ID` int(11) DEFAULT NULL,
+    `SERVICE_MODEL_UUID` VARCHAR(200) NOT NULL,
+    PRIMARY KEY (`ID`) ,
+    KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
+    KEY `fk_configuration_customization__service_idx` (`SERVICE_MODEL_UUID`),
+	UNIQUE KEY `uk_configuration_customization`  (`MODEL_CUSTOMIZATION_UUID` ASC, `SERVICE_MODEL_UUID` ASC),
+	CONSTRAINT `fk_configuration_customization__configuration1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`)
+        REFERENCES `configuration` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE,
+    CONSTRAINT `fk_configuration_customization__service1` FOREIGN KEY (`SERVICE_MODEL_UUID`)
+        REFERENCES `service` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE
+	
+)  ENGINE=INNODB DEFAULT CHARSET=LATIN1;
+
+/* Migrate the existing data */
+INSERT INTO tmp_configuration_customization 
+(`model_customization_uuid` ,
+		  `model_instance_name`,
+		  `configuration_type` ,
+		  `configuration_role` ,
+		  `configuration_function` ,
+		  `creation_timestamp` ,
+		  `configuration_model_uuid` ,
+		  `service_proxy_customization_model_customization_uuid` ,
+		  `service_model_uuid`)
+SELECT `config`.`model_customization_uuid`,
+    `config`.`model_instance_name`,
+    `config`.`configuration_type`,
+    `config`.`configuration_role`,
+    `config`.`configuration_function`,
+    `config`.`creation_timestamp`,
+    `config`.`configuration_model_uuid`,
+    `config`.`service_proxy_customization_model_customization_uuid`,
+    `svc`.`model_uuid` service_model_uuid FROM
+    configuration_customization config,
+    service svc,
+    configuration_customization_to_service config_svc
+WHERE
+    config_svc.service_model_uuid = svc.model_uuid
+        AND config_svc.resource_model_customization_uuid = config.model_customization_uuid;
+      
+/* Drop the old tables */
+
+DROP TABLE `catalogdb`.`configuration_customization`;
+
+DROP TABLE `catalogdb`.`configuration_customization_to_service`;
+
+/* Rename the table */
+RENAME TABLE tmp_configuration_customization TO configuration_customization;       
+        
+	
\ No newline at end of file
diff --git a/adapters/mso-openstack-adapters/src/test/resources/schema.sql b/adapters/mso-openstack-adapters/src/test/resources/schema.sql
index 29a81e8..dbb3469 100644
--- a/adapters/mso-openstack-adapters/src/test/resources/schema.sql
+++ b/adapters/mso-openstack-adapters/src/test/resources/schema.sql
@@ -280,6 +280,7 @@
 /*!40101 SET @saved_cs_client     = @@character_set_client */;
 /*!40101 SET character_set_client = utf8 */;
 CREATE TABLE `configuration_customization` (
+  `ID` int(11) NOT NULL AUTO_INCREMENT,
   `MODEL_CUSTOMIZATION_UUID` varchar(200) NOT NULL,
   `MODEL_INSTANCE_NAME` varchar(200) NOT NULL,
   `CONFIGURATION_TYPE` varchar(200) DEFAULT NULL,
@@ -288,27 +289,18 @@
   `CREATION_TIMESTAMP` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `CONFIGURATION_MODEL_UUID` varchar(200) NOT NULL,
   `SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` varchar(200) DEFAULT NULL,
-  `CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` varchar(200) DEFAULT NULL,
-  PRIMARY KEY (`MODEL_CUSTOMIZATION_UUID`),
-  KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
-  KEY `fk_configuration_customization__service_proxy_customization_idx` (`SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`),
-  KEY `fk_configuration_customization__configuration_customization_idx` (`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`),
-  CONSTRAINT `fk_configuration_customization__configuration_customization1` FOREIGN KEY (`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`) REFERENCES `configuration_customization` (`MODEL_CUSTOMIZATION_UUID`) ON DELETE CASCADE ON UPDATE CASCADE,
-  CONSTRAINT `fk_configuration_resource_customization__configuration_resour1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`) REFERENCES `configuration` (`MODEL_UUID`) ON DELETE CASCADE ON UPDATE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-/*!40101 SET character_set_client = @saved_cs_client */;
-
---
--- Table structure for table `configuration_customization_to_service`
---
-
-DROP TABLE IF EXISTS `configuration_customization_to_service`;
-/*!40101 SET @saved_cs_client     = @@character_set_client */;
-/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `configuration_customization_to_service` (
-  `SERVICE_MODEL_UUID` varchar(200) NOT NULL,
-  `RESOURCE_MODEL_CUSTOMIZATION_UUID` varchar(200) NOT NULL,
-  PRIMARY KEY (`SERVICE_MODEL_UUID`,`RESOURCE_MODEL_CUSTOMIZATION_UUID`)
+  `CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_ID` int(11) DEFAULT NULL,
+  `SERVICE_MODEL_UUID` varchar(200),
+   PRIMARY KEY (`ID`),
+   KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
+   KEY `fk_configuration_customization__service_idx` (`SERVICE_MODEL_UUID`),
+   UNIQUE KEY `uk_configuration_customization`  (`MODEL_CUSTOMIZATION_UUID` ASC, `SERVICE_MODEL_UUID` ASC),
+   CONSTRAINT `fk_configuration_customization__configuration1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`)
+        REFERENCES `configuration` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE,
+   CONSTRAINT `fk_configuration_customization__service1` FOREIGN KEY (`SERVICE_MODEL_UUID`)
+        REFERENCES `service` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 /*!40101 SET character_set_client = @saved_cs_client */;
 
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 f3a4958..d3eab9a 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
@@ -35,6 +35,7 @@
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
+import org.hibernate.StaleObjectStateException;
 import org.hibernate.exception.ConstraintViolationException;
 import org.hibernate.exception.LockAcquisitionException;
 import org.onap.sdc.api.notification.IArtifactInfo;
@@ -55,7 +56,6 @@
 import org.onap.sdc.toscaparser.api.RequirementAssignment;
 import org.onap.sdc.toscaparser.api.RequirementAssignments;
 import org.onap.sdc.toscaparser.api.elements.Metadata;
-import org.onap.sdc.toscaparser.api.elements.StatefulEntityType;
 import org.onap.sdc.toscaparser.api.functions.GetInput;
 import org.onap.sdc.toscaparser.api.parameters.Input;
 import org.onap.sdc.utils.DistributionStatusEnum;
@@ -122,7 +122,6 @@
 import org.onap.so.db.catalog.data.repository.TempNetworkHeatTemplateRepository;
 import org.onap.so.db.catalog.data.repository.VFModuleCustomizationRepository;
 import org.onap.so.db.catalog.data.repository.VFModuleRepository;
-import org.onap.so.db.catalog.data.repository.VnfCustomizationRepository;
 import org.onap.so.db.catalog.data.repository.VnfResourceRepository;
 import org.onap.so.db.catalog.data.repository.VnfcCustomizationRepository;
 import org.onap.so.db.catalog.data.repository.VnfcInstanceGroupCustomizationRepository;
@@ -308,6 +307,7 @@
         }
     }
 
+
     @Transactional(rollbackFor = {ArtifactInstallerException.class})
     public void installTheResource(ToscaResourceStructure toscaResourceStruct, ResourceStructure resourceStruct)
             throws ArtifactInstallerException {
@@ -408,8 +408,10 @@
             processNetworkCollections(toscaResourceStruct, service);
             // Process Service Proxy & Configuration
             processServiceProxyAndConfiguration(toscaResourceStruct, service);
+
             logger.info("Saving Service: {} ", service.getModelName());
-            serviceRepo.save(service);
+            service = serviceRepo.save(service);
+            correlateConfigCustomResources(service);
 
             WatchdogComponentDistributionStatus status = new WatchdogComponentDistributionStatus(
                     vfResourceStruct.getNotification().getDistributionID(), MSO);
@@ -637,7 +639,8 @@
     }
 
     protected ConfigurationResourceCustomization getConfigurationResourceCustomization(NodeTemplate nodeTemplate,
-            ToscaResourceStructure toscaResourceStructure, ServiceProxyResourceCustomization spResourceCustomization) {
+            ToscaResourceStructure toscaResourceStructure, ServiceProxyResourceCustomization spResourceCustomization,
+            Service service) {
         Metadata metadata = nodeTemplate.getMetaData();
 
         ConfigurationResource configResource = getConfigurationResource(nodeTemplate);
@@ -660,31 +663,15 @@
                 .setServiceProxyResourceCustomizationUUID(spResourceCustomization.getModelCustomizationUUID());
 
         configCustomizationResource.setConfigurationResource(configResource);
+        configCustomizationResource.setService(service);
         configResourceCustomizationSet.add(configCustomizationResource);
 
         configResource.setConfigurationResourceCustomization(configResourceCustomizationSet);
+
         return configCustomizationResource;
     }
 
 
-    protected Optional<ConfigurationResourceCustomization> getVnrNodeTemplate(
-            List<NodeTemplate> configurationNodeTemplatesList, ToscaResourceStructure toscaResourceStructure,
-            ServiceProxyResourceCustomization spResourceCustomization) {
-        Optional<ConfigurationResourceCustomization> configurationResourceCust = Optional.empty();
-        for (NodeTemplate nodeTemplate : configurationNodeTemplatesList) {
-            StatefulEntityType entityType = nodeTemplate.getTypeDefinition();
-            String type = entityType.getType();
-
-            if (VLAN_NETWORK_RECEPTOR.equals(type)) {
-                configurationResourceCust = Optional.of(getConfigurationResourceCustomization(nodeTemplate,
-                        toscaResourceStructure, spResourceCustomization));
-                break;
-            }
-        }
-
-        return configurationResourceCust;
-    }
-
     protected void processServiceProxyAndConfiguration(ToscaResourceStructure toscaResourceStruct, Service service) {
 
         List<NodeTemplate> serviceProxyResourceList =
@@ -703,8 +690,6 @@
             for (NodeTemplate spNode : serviceProxyResourceList) {
                 serviceProxy = createServiceProxy(spNode, service, toscaResourceStruct);
                 serviceProxyList.add(serviceProxy);
-                Optional<ConfigurationResourceCustomization> vnrResourceCustomization =
-                        getVnrNodeTemplate(configurationNodeTemplatesList, toscaResourceStruct, serviceProxy);
 
                 for (NodeTemplate configNode : configurationNodeTemplatesList) {
 
@@ -712,19 +697,21 @@
                             toscaResourceStruct.getSdcCsarHelper().getRequirementsOf(configNode).getAll();
                     for (RequirementAssignment requirement : requirementsList) {
                         if (requirement.getNodeTemplateName().equals(spNode.getName())) {
-                            ConfigurationResourceCustomization configurationResource = createConfiguration(configNode,
-                                    toscaResourceStruct, serviceProxy, vnrResourceCustomization);
+                            ConfigurationResourceCustomization configurationResource =
+                                    createConfiguration(configNode, toscaResourceStruct, serviceProxy, service);
 
-                            Optional<ConfigurationResourceCustomization> matchingObject = configurationResourceList
-                                    .stream()
-                                    .filter(configurationResourceCustomization -> configNode.getMetaData()
-                                            .getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)
-                                            .equals(configurationResource.getModelCustomizationUUID()))
-                                    .findFirst();
+                            Optional<ConfigurationResourceCustomization> matchingObject =
+                                    configurationResourceList.stream()
+                                            .filter(configurationResourceCustomization -> configNode.getMetaData()
+                                                    .getValue(SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID)
+                                                    .equals(configurationResource.getModelCustomizationUUID()))
+                                            .filter(configurationResourceCustomization -> configurationResourceCustomization
+                                                    .getModelInstanceName()
+                                                    .equals(configurationResource.getModelInstanceName()))
+                                            .findFirst();
                             if (!matchingObject.isPresent()) {
                                 configurationResourceList.add(configurationResource);
                             }
-
                             break;
                         }
                     }
@@ -737,6 +724,37 @@
         service.setServiceProxyCustomizations(serviceProxyList);
     }
 
+    /*
+     * ConfigurationResourceCustomization objects have their IDs auto incremented in the database. Unless we know their
+     * IDs we cannot possibly associate their related records. So these ConfigResourceCustomizations are persisted first
+     * and subsequently correlated.
+     */
+
+    protected void correlateConfigCustomResources(Service service) {
+        /* Assuming that we have only one pair of VRF-VNR */
+        ConfigurationResourceCustomization vrfConfigCustomResource = null;
+        ConfigurationResourceCustomization vnrConfigCustomResource = null;
+        List<ConfigurationResourceCustomization> configCustomList = service.getConfigurationCustomizations();
+        for (ConfigurationResourceCustomization configResource : configCustomList) {
+            String nodeType = configResource.getConfigurationResource().getToscaNodeType();
+            if (NODES_VRF_ENTRY.equalsIgnoreCase(nodeType)) {
+                vrfConfigCustomResource = configResource;
+            } else if (VLAN_NETWORK_RECEPTOR.equalsIgnoreCase(nodeType)) {
+                vnrConfigCustomResource = configResource;
+            }
+        }
+
+        if (vrfConfigCustomResource != null) {
+            vrfConfigCustomResource.setConfigResourceCustomization(vnrConfigCustomResource);
+            configCustomizationRepo.save(vrfConfigCustomResource);
+
+        }
+        if (vnrConfigCustomResource != null) {
+            vnrConfigCustomResource.setConfigResourceCustomization(vrfConfigCustomResource);
+            configCustomizationRepo.save(vnrConfigCustomResource);
+        }
+    }
+
     protected void processNetworkCollections(ToscaResourceStructure toscaResourceStruct, Service service) {
 
         List<NodeTemplate> networkCollectionList =
@@ -1229,22 +1247,15 @@
 
     protected ConfigurationResourceCustomization createConfiguration(NodeTemplate nodeTemplate,
             ToscaResourceStructure toscaResourceStructure, ServiceProxyResourceCustomization spResourceCustomization,
-            Optional<ConfigurationResourceCustomization> vnrResourceCustomization) {
+            Service service) {
 
-        ConfigurationResourceCustomization configCustomizationResource =
-                getConfigurationResourceCustomization(nodeTemplate, toscaResourceStructure, spResourceCustomization);
+        ConfigurationResourceCustomization configCustomizationResource = getConfigurationResourceCustomization(
+                nodeTemplate, toscaResourceStructure, spResourceCustomization, service);
 
         ConfigurationResource configResource = getConfigurationResource(nodeTemplate);
 
         Set<ConfigurationResourceCustomization> configResourceCustomizationSet = new HashSet<>();
 
-        StatefulEntityType entityType = nodeTemplate.getTypeDefinition();
-        String type = entityType.getType();
-
-        if (NODES_VRF_ENTRY.equals(type)) {
-            configCustomizationResource.setConfigResourceCustomization(vnrResourceCustomization.orElse(null));
-        }
-
         configCustomizationResource.setConfigurationResource(configResource);
 
         configResourceCustomizationSet.add(configCustomizationResource);
@@ -1353,6 +1364,7 @@
 
             networkCustomizationRepo.saveAndFlush(networkResourceCustomization);
 
+
         } else if (networkResourceCustomization == null) {
             networkResourceCustomization =
                     createNetworkResourceCustomization(networkNodeTemplate, toscaResourceStructure);
diff --git a/asdc-controller/src/test/java/org/onap/so/asdc/installer/heat/ToscaResourceInstallerTest.java b/asdc-controller/src/test/java/org/onap/so/asdc/installer/heat/ToscaResourceInstallerTest.java
index d3c0bde..ce70a25 100644
--- a/asdc-controller/src/test/java/org/onap/so/asdc/installer/heat/ToscaResourceInstallerTest.java
+++ b/asdc-controller/src/test/java/org/onap/so/asdc/installer/heat/ToscaResourceInstallerTest.java
@@ -20,26 +20,24 @@
 
 package org.onap.so.asdc.installer.heat;
 
-import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
+import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Optional;
 import org.hibernate.exception.LockAcquisitionException;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.onap.sdc.api.notification.IResourceInstance;
 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
@@ -58,13 +56,17 @@
 import org.onap.so.asdc.installer.ToscaResourceStructure;
 import org.onap.so.db.catalog.beans.ConfigurationResource;
 import org.onap.so.db.catalog.beans.ConfigurationResourceCustomization;
+import org.onap.so.db.catalog.beans.Service;
 import org.onap.so.db.catalog.beans.ServiceProxyResourceCustomization;
 import org.onap.so.db.catalog.data.repository.AllottedResourceCustomizationRepository;
 import org.onap.so.db.catalog.data.repository.AllottedResourceRepository;
+import org.onap.so.db.catalog.data.repository.ConfigurationResourceCustomizationRepository;
 import org.onap.so.db.catalog.data.repository.ServiceRepository;
 import org.onap.so.db.request.beans.WatchdogComponentDistributionStatus;
 import org.onap.so.db.request.data.repository.WatchdogComponentDistributionStatusRepository;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.util.ReflectionTestUtils;
+
 
 public class ToscaResourceInstallerTest extends BaseTest {
     @Autowired
@@ -99,6 +101,8 @@
     private ISdcCsarHelper csarHelper;
     @Mock
     private StatefulEntityType entityType;
+    @Mock
+    private Service service;
 
     private NotificationDataImpl notificationData;
     private JsonStatusData statusData;
@@ -350,8 +354,9 @@
     public void getConfigurationResourceCustomizationTest() {
         prepareConfigurationResourceCustomization();
 
-        ConfigurationResourceCustomization configurationResourceCustomization = toscaInstaller
-                .getConfigurationResourceCustomization(nodeTemplate, toscaResourceStructure, spResourceCustomization);
+        ConfigurationResourceCustomization configurationResourceCustomization =
+                toscaInstaller.getConfigurationResourceCustomization(nodeTemplate, toscaResourceStructure,
+                        spResourceCustomization, service);
         assertNotNull(configurationResourceCustomization);
         assertNotNull(configurationResourceCustomization.getConfigurationResource());
         assertEquals(MockConstants.MODEL_CUSTOMIZATIONUUID,
@@ -359,16 +364,34 @@
     }
 
     @Test
-    public void getVnrNodeTemplateTest() {
-        prepareConfigurationResourceCustomization();
-        List<NodeTemplate> nodeTemplateList = new ArrayList<>();
-        doReturn(ToscaResourceInstaller.VLAN_NETWORK_RECEPTOR).when(entityType).getType();
-        doReturn(entityType).when(nodeTemplate).getTypeDefinition();
-        nodeTemplateList.add(nodeTemplate);
-        Optional<ConfigurationResourceCustomization> vnrResourceCustomization =
-                toscaInstaller.getVnrNodeTemplate(nodeTemplateList, toscaResourceStructure, spResourceCustomization);
-        assertTrue(vnrResourceCustomization.isPresent());
-        assertEquals(ToscaResourceInstaller.VLAN_NETWORK_RECEPTOR, entityType.getType());
+    public void correlateConfigCustomResourcesTest() {
+        ConfigurationResource vrfConfigResource = mock(ConfigurationResource.class);
+        ConfigurationResourceCustomization vrfConfigCustom = mock(ConfigurationResourceCustomization.class);
+        doReturn(ToscaResourceInstaller.NODES_VRF_ENTRY).when(vrfConfigResource).getToscaNodeType();
+        doReturn(vrfConfigResource).when(vrfConfigCustom).getConfigurationResource();
+
+        ConfigurationResource vnrConfigResource = mock(ConfigurationResource.class);
+        ConfigurationResourceCustomization vnrConfigCustom = mock(ConfigurationResourceCustomization.class);
+        doReturn(ToscaResourceInstaller.VLAN_NETWORK_RECEPTOR).when(vnrConfigResource).getToscaNodeType();
+        doReturn(vnrConfigResource).when(vnrConfigCustom).getConfigurationResource();
+
+        ConfigurationResourceCustomizationRepository configCustomizationRepo =
+                spy(ConfigurationResourceCustomizationRepository.class);
+        ReflectionTestUtils.setField(toscaInstaller, "configCustomizationRepo", configCustomizationRepo);
+        doReturn(vrfConfigCustom).when(configCustomizationRepo).save(vrfConfigCustom);
+        doReturn(vnrConfigCustom).when(configCustomizationRepo).save(vnrConfigCustom);
+
+        List<ConfigurationResourceCustomization> configList = new ArrayList<>();
+        configList.add(vrfConfigCustom);
+        configList.add(vnrConfigCustom);
+        doReturn(configList).when(service).getConfigurationCustomizations();
+
+        toscaInstaller.correlateConfigCustomResources(service);
+        verify(vrfConfigCustom, times(1)).getConfigurationResource();
+        verify(vrfConfigCustom, times(1)).setConfigResourceCustomization(vnrConfigCustom);
+        verify(service, times(1)).getConfigurationCustomizations();
+        verify(vnrConfigCustom, times(1)).getConfigurationResource();
+        verify(vnrConfigCustom, times(1)).setConfigResourceCustomization(vrfConfigCustom);
     }
 
     class MockConstants {
@@ -381,5 +404,6 @@
         public final static String TEMPLATE_TYPE = "org.openecomp.nodes.VLANNetworkReceptor";
         public final static String TEMPLATE_NAME = "VLAN Network Receptor Configuration 0";
 
+
     }
 }
diff --git a/asdc-controller/src/test/resources/schema.sql b/asdc-controller/src/test/resources/schema.sql
index 0b48b2e..99c81cf 100644
--- a/asdc-controller/src/test/resources/schema.sql
+++ b/asdc-controller/src/test/resources/schema.sql
@@ -283,6 +283,7 @@
 /*!40101 SET @saved_cs_client     = @@character_set_client */;
 /*!40101 SET character_set_client = utf8 */;
 CREATE TABLE `configuration_customization` (
+  `ID` int(11) NOT NULL AUTO_INCREMENT,
   `MODEL_CUSTOMIZATION_UUID` varchar(200) NOT NULL,
   `MODEL_INSTANCE_NAME` varchar(200) NOT NULL,
   `CONFIGURATION_TYPE` varchar(200) DEFAULT NULL,
@@ -291,27 +292,18 @@
   `CREATION_TIMESTAMP` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `CONFIGURATION_MODEL_UUID` varchar(200) NOT NULL,
   `SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` varchar(200) DEFAULT NULL,
-  `CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` varchar(200) DEFAULT NULL,
-  PRIMARY KEY (`MODEL_CUSTOMIZATION_UUID`),
-  KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
-  KEY `fk_configuration_customization__service_proxy_customization_idx` (`SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`),
-  KEY `fk_configuration_customization__configuration_customization_idx` (`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`),
-  CONSTRAINT `fk_configuration_customization__configuration_customization1` FOREIGN KEY (`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`) REFERENCES `configuration_customization` (`MODEL_CUSTOMIZATION_UUID`) ON DELETE CASCADE ON UPDATE CASCADE,
-  CONSTRAINT `fk_configuration_resource_customization__configuration_resour1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`) REFERENCES `configuration` (`MODEL_UUID`) ON DELETE CASCADE ON UPDATE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-/*!40101 SET character_set_client = @saved_cs_client */;
-
---
--- Table structure for table `configuration_customization_to_service`
---
-
-DROP TABLE IF EXISTS `configuration_customization_to_service`;
-/*!40101 SET @saved_cs_client     = @@character_set_client */;
-/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `configuration_customization_to_service` (
-  `SERVICE_MODEL_UUID` varchar(200) NOT NULL,
-  `RESOURCE_MODEL_CUSTOMIZATION_UUID` varchar(200) NOT NULL,
-  PRIMARY KEY (`SERVICE_MODEL_UUID`,`RESOURCE_MODEL_CUSTOMIZATION_UUID`)
+  `CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_ID` int(11) DEFAULT NULL,
+  `SERVICE_MODEL_UUID` varchar(200),
+   PRIMARY KEY (`ID`),
+   KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
+   KEY `fk_configuration_customization__service_idx` (`SERVICE_MODEL_UUID`),
+   UNIQUE KEY `uk_configuration_customization`  (`MODEL_CUSTOMIZATION_UUID` ASC, `SERVICE_MODEL_UUID` ASC),
+   CONSTRAINT `fk_configuration_customization__configuration1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`)
+        REFERENCES `configuration` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE,
+   CONSTRAINT `fk_configuration_customization__service1` FOREIGN KEY (`SERVICE_MODEL_UUID`)
+        REFERENCES `service` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 /*!40101 SET character_set_client = @saved_cs_client */;
 
diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/resources/schema.sql b/mso-api-handlers/mso-api-handler-infra/src/test/resources/schema.sql
index 2c03173..bc9003f 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/test/resources/schema.sql
+++ b/mso-api-handlers/mso-api-handler-infra/src/test/resources/schema.sql
@@ -283,6 +283,7 @@
 /*!40101 SET @saved_cs_client     = @@character_set_client */;
 /*!40101 SET character_set_client = utf8 */;
 CREATE TABLE `configuration_customization` (
+  `ID` int(11) NOT NULL AUTO_INCREMENT,
   `MODEL_CUSTOMIZATION_UUID` varchar(200) NOT NULL,
   `MODEL_INSTANCE_NAME` varchar(200) NOT NULL,
   `CONFIGURATION_TYPE` varchar(200) DEFAULT NULL,
@@ -291,29 +292,20 @@
   `CREATION_TIMESTAMP` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `CONFIGURATION_MODEL_UUID` varchar(200) NOT NULL,
   `SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` varchar(200) DEFAULT NULL,
-  `CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` varchar(200) DEFAULT NULL,
-  PRIMARY KEY (`MODEL_CUSTOMIZATION_UUID`),
-  KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
-  KEY `fk_configuration_customization__service_proxy_customization_idx` (`SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`),
-  KEY `fk_configuration_customization__configuration_customization_idx` (`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`),
-  CONSTRAINT `fk_configuration_customization__configuration_customization1` FOREIGN KEY (`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`) REFERENCES `configuration_customization` (`MODEL_CUSTOMIZATION_UUID`) ON DELETE CASCADE ON UPDATE CASCADE,
-  CONSTRAINT `fk_configuration_resource_customization__configuration_resour1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`) REFERENCES `configuration` (`MODEL_UUID`) ON DELETE CASCADE ON UPDATE CASCADE
+  `CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_ID` int(11) DEFAULT NULL,
+  `SERVICE_MODEL_UUID` varchar(200),
+   PRIMARY KEY (`ID`),
+   KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
+   KEY `fk_configuration_customization__service_idx` (`SERVICE_MODEL_UUID`),
+   UNIQUE KEY `uk_configuration_customization`  (`MODEL_CUSTOMIZATION_UUID` ASC, `SERVICE_MODEL_UUID` ASC),
+   CONSTRAINT `fk_configuration_customization__configuration1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`)
+        REFERENCES `configuration` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE,
+   CONSTRAINT `fk_configuration_customization__service1` FOREIGN KEY (`SERVICE_MODEL_UUID`)
+        REFERENCES `service` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-/*!40101 SET character_set_client = @saved_cs_client */;
-
---
--- Table structure for table `configuration_customization_to_service`
---
-
-DROP TABLE IF EXISTS `configuration_customization_to_service`;
-/*!40101 SET @saved_cs_client     = @@character_set_client */;
-/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `configuration_customization_to_service` (
-  `SERVICE_MODEL_UUID` varchar(200) NOT NULL,
-  `RESOURCE_MODEL_CUSTOMIZATION_UUID` varchar(200) NOT NULL,
-  PRIMARY KEY (`SERVICE_MODEL_UUID`,`RESOURCE_MODEL_CUSTOMIZATION_UUID`)
-) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-/*!40101 SET character_set_client = @saved_cs_client */;
+/*!40101 SET character_set_client = @saved_cs_client */;/*!40101 SET character_set_client = @saved_cs_client */;
 
 --
 -- Table structure for table `controller_selection_reference`
diff --git a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/ConfigurationResourceCustomization.java b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/ConfigurationResourceCustomization.java
index 059935f..1117648 100644
--- a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/ConfigurationResourceCustomization.java
+++ b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/ConfigurationResourceCustomization.java
@@ -27,6 +27,8 @@
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
@@ -52,8 +54,12 @@
      */
     private static final long serialVersionUID = 1230671937560638856L;
 
-    @BusinessKey
     @Id
+    @BusinessKey
+    @Column(name = "ID")
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Integer id;
+
     @Column(name = "MODEL_CUSTOMIZATION_UUID")
     private String modelCustomizationUUID;
 
@@ -77,18 +83,30 @@
     private String serviceProxyResourceCustomizationUUID;
 
     @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
-    @JoinColumn(name = "CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID")
+    @JoinColumn(name = "CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_ID")
     private ConfigurationResourceCustomization configResourceCustomization;
 
     @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
     @JoinColumn(name = "CONFIGURATION_MODEL_UUID")
     private ConfigurationResource configurationResource;
 
+    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
+    @JoinColumn(name = "SERVICE_MODEL_UUID")
+    private Service service;
+
     @PrePersist
     protected void onCreate() {
         this.created = new Date();
     }
 
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
     public String getModelCustomizationUUID() {
         return modelCustomizationUUID;
     }
@@ -141,6 +159,7 @@
         this.serviceProxyResourceCustomizationUUID = serviceProxyResourceCustomizationUUID;
     }
 
+
     @LinkedResource
     public ConfigurationResourceCustomization getConfigResourceCustomization() {
         return configResourceCustomization;
@@ -159,14 +178,22 @@
         this.configurationResource = configurationResource;
     }
 
+    public Service getService() {
+        return service;
+    }
+
+    public void setService(Service service) {
+        this.service = service;
+    }
+
     @Override
     public String toString() {
-        return new ToStringBuilder(this).append("modelCustomizationUUID", modelCustomizationUUID)
+        return new ToStringBuilder(this).append("id", id).append("modelCustomizationUUID", modelCustomizationUUID)
                 .append("modelInstanceName", modelInstanceName).append("nfFunction", nfFunction)
                 .append("nfType", nfType).append("nfRole", nfRole).append("created", created)
                 // .append("serviceProxyResourceCustomization", serviceProxyResourceCustomization)
                 .append("configResourceCustomization", configResourceCustomization)
-                .append("configurationResource", configurationResource).toString();
+                .append("configurationResource", configurationResource).append("service", service).toString();
     }
 
     @Override
@@ -175,12 +202,12 @@
             return false;
         }
         ConfigurationResourceCustomization castOther = (ConfigurationResourceCustomization) other;
-        return new EqualsBuilder().append(modelCustomizationUUID, castOther.modelCustomizationUUID).isEquals();
+        return new EqualsBuilder().append(id, castOther.id).isEquals();
     }
 
     @Override
     public int hashCode() {
-        return new HashCodeBuilder().append(modelCustomizationUUID).toHashCode();
+        return new HashCodeBuilder().append(id).toHashCode();
     }
 
 }
diff --git a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/Service.java b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/Service.java
index c333033..ffcc8e9 100644
--- a/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/Service.java
+++ b/mso-catalog-db/src/main/java/org/onap/so/db/catalog/beans/Service.java
@@ -119,9 +119,7 @@
             inverseJoinColumns = @JoinColumn(name = "RESOURCE_MODEL_CUSTOMIZATION_UUID"))
     private List<ServiceProxyResourceCustomization> serviceProxyCustomizations;
 
-    @OneToMany(cascade = CascadeType.ALL)
-    @JoinTable(name = "configuration_customization_to_service", joinColumns = @JoinColumn(name = "SERVICE_MODEL_UUID"),
-            inverseJoinColumns = @JoinColumn(name = "RESOURCE_MODEL_CUSTOMIZATION_UUID"))
+    @OneToMany(cascade = CascadeType.ALL, mappedBy = "service")
     private List<ConfigurationResourceCustomization> configurationCustomizations;
 
     @OneToMany(cascade = CascadeType.ALL)
diff --git a/mso-catalog-db/src/test/resources/schema.sql b/mso-catalog-db/src/test/resources/schema.sql
index 5e43f8f..f5e7d52 100644
--- a/mso-catalog-db/src/test/resources/schema.sql
+++ b/mso-catalog-db/src/test/resources/schema.sql
@@ -280,6 +280,7 @@
 /*!40101 SET @saved_cs_client     = @@character_set_client */;
 /*!40101 SET character_set_client = utf8 */;
 CREATE TABLE `configuration_customization` (
+  `ID` int(11) NOT NULL AUTO_INCREMENT,
   `MODEL_CUSTOMIZATION_UUID` varchar(200) NOT NULL,
   `MODEL_INSTANCE_NAME` varchar(200) NOT NULL,
   `CONFIGURATION_TYPE` varchar(200) DEFAULT NULL,
@@ -288,27 +289,18 @@
   `CREATION_TIMESTAMP` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `CONFIGURATION_MODEL_UUID` varchar(200) NOT NULL,
   `SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` varchar(200) DEFAULT NULL,
-  `CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID` varchar(200) DEFAULT NULL,
-  PRIMARY KEY (`MODEL_CUSTOMIZATION_UUID`),
-  KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
-  KEY `fk_configuration_customization__service_proxy_customization_idx` (`SERVICE_PROXY_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`),
-  KEY `fk_configuration_customization__configuration_customization_idx` (`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`),
-  CONSTRAINT `fk_configuration_customization__configuration_customization1` FOREIGN KEY (`CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_UUID`) REFERENCES `configuration_customization` (`MODEL_CUSTOMIZATION_UUID`) ON DELETE CASCADE ON UPDATE CASCADE,
-  CONSTRAINT `fk_configuration_resource_customization__configuration_resour1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`) REFERENCES `configuration` (`MODEL_UUID`) ON DELETE CASCADE ON UPDATE CASCADE
-) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-/*!40101 SET character_set_client = @saved_cs_client */;
-
---
--- Table structure for table `configuration_customization_to_service`
---
-
-DROP TABLE IF EXISTS `configuration_customization_to_service`;
-/*!40101 SET @saved_cs_client     = @@character_set_client */;
-/*!40101 SET character_set_client = utf8 */;
-CREATE TABLE `configuration_customization_to_service` (
-  `SERVICE_MODEL_UUID` varchar(200) NOT NULL,
-  `RESOURCE_MODEL_CUSTOMIZATION_UUID` varchar(200) NOT NULL,
-  PRIMARY KEY (`SERVICE_MODEL_UUID`,`RESOURCE_MODEL_CUSTOMIZATION_UUID`)
+  `CONFIGURATION_CUSTOMIZATION_MODEL_CUSTOMIZATION_ID` int(11) DEFAULT NULL,
+  `SERVICE_MODEL_UUID` varchar(200),
+   PRIMARY KEY (`ID`),
+   KEY `fk_configuration_customization__configuration_idx` (`CONFIGURATION_MODEL_UUID`),
+   KEY `fk_configuration_customization__service_idx` (`SERVICE_MODEL_UUID`),
+   UNIQUE KEY `uk_configuration_customization`  (`MODEL_CUSTOMIZATION_UUID` ASC, `SERVICE_MODEL_UUID` ASC),
+   CONSTRAINT `fk_configuration_customization__configuration1` FOREIGN KEY (`CONFIGURATION_MODEL_UUID`)
+        REFERENCES `configuration` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE,
+   CONSTRAINT `fk_configuration_customization__service1` FOREIGN KEY (`SERVICE_MODEL_UUID`)
+        REFERENCES `service` (`MODEL_UUID`)
+        ON DELETE CASCADE ON UPDATE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 /*!40101 SET character_set_client = @saved_cs_client */;