Addressed refactoring comments related to method name
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsModulePersistenceServiceImpl.java
index 3e39a05..3719256 100755 (executable)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2020 Nordix Foundation
+ *  Copyright (C) 2020-2022 Nordix Foundation
  *  Modifications Copyright (C) 2020-2022 Bell Canada.
  *  Modifications Copyright (C) 2021 Pantheon.tech
  *  ================================================================================
@@ -40,6 +40,7 @@ import java.util.Set;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 import javax.transaction.Transactional;
+import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -53,9 +54,8 @@ import org.onap.cps.spi.exceptions.AlreadyDefinedException;
 import org.onap.cps.spi.exceptions.DuplicatedYangResourceException;
 import org.onap.cps.spi.exceptions.ModelValidationException;
 import org.onap.cps.spi.model.ModuleReference;
-import org.onap.cps.spi.repository.AnchorRepository;
 import org.onap.cps.spi.repository.DataspaceRepository;
-import org.onap.cps.spi.repository.FragmentRepository;
+import org.onap.cps.spi.repository.ModuleReferenceRepository;
 import org.onap.cps.spi.repository.SchemaSetRepository;
 import org.onap.cps.spi.repository.YangResourceRepository;
 import org.opendaylight.yangtools.yang.common.Revision;
@@ -63,15 +63,14 @@ import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException
 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.retry.annotation.Backoff;
 import org.springframework.retry.annotation.Retryable;
 import org.springframework.stereotype.Component;
 
-
-@Component
 @Slf4j
+@Component
+@AllArgsConstructor
 public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService {
 
     private static final String YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME = "yang_resource_checksum_key";
@@ -79,24 +78,16 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
     private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN = Pattern
             .compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE);
 
-    @Autowired
     private YangResourceRepository yangResourceRepository;
 
-    @Autowired
     private SchemaSetRepository schemaSetRepository;
 
-    @Autowired
     private DataspaceRepository dataspaceRepository;
 
-    @Autowired
-    private AnchorRepository anchorRepository;
-
-    @Autowired
-    private FragmentRepository fragmentRepository;
-
-    @Autowired
     private CpsAdminPersistenceService cpsAdminPersistenceService;
 
+    private ModuleReferenceRepository moduleReferenceRepository;
+
     @Override
     public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) {
         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
@@ -115,7 +106,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
     @Override
     public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) {
         final Set<YangResourceModuleReference> yangResourceModuleReferenceList =
-            yangResourceRepository.findAllModuleReferences(dataspaceName);
+            yangResourceRepository.findAllModuleReferencesByDataspace(dataspaceName);
         return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
             .collect(Collectors.toList());
     }
@@ -125,7 +116,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
         final String anchorName) {
         final Set<YangResourceModuleReference> yangResourceModuleReferenceList =
             yangResourceRepository
-                .findAllModuleReferences(dataspaceName, anchorName);
+                .findAllModuleReferencesByDataspaceAndAnchor(dataspaceName, anchorName);
         return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference)
             .collect(Collectors.toList());
     }
@@ -134,12 +125,12 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
     @Transactional
     // A retry is made to store the schema set if it fails because of duplicated yang resource exception that
     // can occur in case of specific concurrent requests.
-    @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 2, backoff = @Backoff(delay = 500))
+    @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 5, backoff =
+        @Backoff(random = true, delay = 200, maxDelay = 2000, multiplier = 2))
     public void storeSchemaSet(final String dataspaceName, final String schemaSetName,
-        final Map<String, String> yangResourcesNameToContentMap) {
-
+        final Map<String, String> moduleReferenceNameToContentMap) {
         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
-        final var yangResourceEntities = synchronizeYangResources(yangResourcesNameToContentMap);
+        final var yangResourceEntities = synchronizeYangResources(moduleReferenceNameToContentMap);
         final var schemaSetEntity = new SchemaSetEntity();
         schemaSetEntity.setName(schemaSetName);
         schemaSetEntity.setDataspace(dataspaceEntity);
@@ -153,10 +144,14 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
 
     @Override
     @Transactional
+    // A retry is made to store the schema set if it fails because of duplicated yang resource exception that
+    // can occur in case of specific concurrent requests.
+    @Retryable(value = DuplicatedYangResourceException.class, maxAttempts = 5, backoff =
+        @Backoff(random = true, delay = 200, maxDelay = 2000, multiplier = 2))
     public void storeSchemaSetFromModules(final String dataspaceName, final String schemaSetName,
-                                          final Map<String, String> newYangResourcesModuleNameToContentMap,
-                                          final List<ModuleReference> moduleReferences) {
-        storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesModuleNameToContentMap);
+                                          final Map<String, String> newModuleNameToContentMap,
+                                          final Collection<ModuleReference> moduleReferences) {
+        storeSchemaSet(dataspaceName, schemaSetName, newModuleNameToContentMap);
         final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
         final var schemaSetEntity =
                 schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
@@ -182,8 +177,15 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
         yangResourceRepository.deleteOrphans();
     }
 
-    private Set<YangResourceEntity> synchronizeYangResources(final Map<String, String> yangResourcesNameToContentMap) {
-        final Map<String, YangResourceEntity> checksumToEntityMap = yangResourcesNameToContentMap.entrySet().stream()
+    @Override
+    public Collection<ModuleReference> identifyNewModuleReferences(
+        final Collection<ModuleReference> moduleReferencesToCheck) {
+        return moduleReferenceRepository.identifyNewModuleReferences(moduleReferencesToCheck);
+    }
+
+    private Set<YangResourceEntity> synchronizeYangResources(
+        final Map<String, String> moduleReferenceNameToContentMap) {
+        final Map<String, YangResourceEntity> checksumToEntityMap = moduleReferenceNameToContentMap.entrySet().stream()
             .map(entry -> {
                 final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8));
                 final Map<String, String> moduleNameAndRevisionMap = createModuleNameAndRevisionMap(entry.getKey(),
@@ -219,7 +221,7 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ
                 convertedException.ifPresent(
                     e ->  log.warn(
                                 "Cannot persist duplicated yang resource. "
-                                        + "A total of 2 attempts to store the schema set are planned.", e));
+                                        + "System will attempt this method up to 5 times.", e));
                 throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException;
             }
         }