Introduce Schema Set upgrade method
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / sync / ModuleSyncService.java
index 841368c..a6b85a5 100644 (file)
@@ -73,34 +73,36 @@ public class ModuleSyncService {
      */
     public void syncAndCreateOrUpgradeSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) {
 
-        final String moduleSetTag;
         final String cmHandleId = yangModelCmHandle.getId();
         final CompositeState compositeState = yangModelCmHandle.getCompositeState();
         final boolean inUpgrade = ModuleOperationsUtils.isInUpgradeOrUpgradeFailed(compositeState);
-
-        if (inUpgrade) {
-            moduleSetTag = ModuleOperationsUtils.getLockedCompositeStateDetails(compositeState.getLockReason())
-                    .get(ModuleOperationsUtils.MODULE_SET_TAG_KEY);
-        } else {
-            moduleSetTag = yangModelCmHandle.getModuleSetTag();
-        }
+        final String moduleSetTag = getModuleSetTag(yangModelCmHandle, compositeState, inUpgrade);
 
         final Collection<ModuleReference> moduleReferencesFromCache = moduleSetTagCache.get(moduleSetTag);
 
         if (moduleReferencesFromCache == null) {
-            final Optional<DataNode> optionalExistingCmHandleWithSameModuleSetTag
+            final Optional<DataNode> existingCmHandleWithSameModuleSetTag
                     = getFirstReadyDataNodeWithModuleSetTag(moduleSetTag);
 
-            if (optionalExistingCmHandleWithSameModuleSetTag.isPresent()) {
-                final String existingCmHandleAnchorName
-                        = optionalExistingCmHandleWithSameModuleSetTag.get().getAnchorName();
-                createOrUpgradeSchemaSetUsingModuleSetTag(cmHandleId, moduleSetTag, existingCmHandleAnchorName);
+            if (existingCmHandleWithSameModuleSetTag.isPresent()) {
+                final String existingAnchorName = existingCmHandleWithSameModuleSetTag.get().getAnchorName();
+                final Collection<ModuleReference> moduleReferencesFromExistingCmHandle =
+                        upgradeOrCreateSchemaSetUsingModuleSetTag(yangModelCmHandle.getId(), moduleSetTag,
+                                existingAnchorName, inUpgrade);
+                updateModuleSetTagCache(moduleSetTag, moduleReferencesFromExistingCmHandle);
             } else {
-                syncAndCreateSchemaSet(yangModelCmHandle, moduleSetTag);
+                final Collection<ModuleReference> allModuleReferencesFromCmHandle
+                        = syncAndCreateSchemaSet(yangModelCmHandle);
+                updateModuleSetTagCache(moduleSetTag, allModuleReferencesFromCmHandle);
             }
         } else {
-            cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
-                    cmHandleId, NO_NEW_MODULES, moduleReferencesFromCache);
+            if (inUpgrade) {
+                cpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
+                        NO_NEW_MODULES, moduleReferencesFromCache);
+            } else {
+                cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
+                        cmHandleId, NO_NEW_MODULES, moduleReferencesFromCache);
+            }
         }
         if (!inUpgrade) {
             cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, cmHandleId);
@@ -108,25 +110,6 @@ public class ModuleSyncService {
         setCmHandleModuleSetTag(yangModelCmHandle, moduleSetTag);
     }
 
-    private void syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle, final String moduleSetTag) {
-        final Collection<ModuleReference> allModuleReferencesFromCmHandle =
-                dmiModelOperations.getModuleReferences(yangModelCmHandle);
-        final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService
-                .identifyNewModuleReferences(allModuleReferencesFromCmHandle);
-        final Map<String, String> newModuleNameToContentMap;
-        if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) {
-            newModuleNameToContentMap = NO_NEW_MODULES;
-        } else {
-            newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
-                    identifiedNewModuleReferencesFromCmHandle);
-        }
-        cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
-            yangModelCmHandle.getId(), newModuleNameToContentMap, allModuleReferencesFromCmHandle);
-        if (StringUtils.isNotBlank(moduleSetTag)) {
-            moduleSetTagCache.put(moduleSetTag, allModuleReferencesFromCmHandle);
-        }
-    }
-
     /**
      * Deletes the SchemaSet for schema set id if the SchemaSet Exists.
      *
@@ -162,16 +145,56 @@ public class ModuleSyncService {
                 jsonObjectMapper.asJsonString(dmiRegistryProperties), OffsetDateTime.now());
     }
 
-    private void createOrUpgradeSchemaSetUsingModuleSetTag(final String schemaSetName,
-                                                           final String moduleSetTag,
-                                                           final String existingCmHandleAnchorName) {
+    private Collection<ModuleReference> syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle) {
+        final Collection<ModuleReference> allModuleReferencesFromCmHandle =
+                dmiModelOperations.getModuleReferences(yangModelCmHandle);
+        final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService
+                .identifyNewModuleReferences(allModuleReferencesFromCmHandle);
+        final Map<String, String> newModuleNameToContentMap;
+        if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) {
+            newModuleNameToContentMap = NO_NEW_MODULES;
+        } else {
+            newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
+                    identifiedNewModuleReferencesFromCmHandle);
+        }
+        cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
+                yangModelCmHandle.getId(), newModuleNameToContentMap, allModuleReferencesFromCmHandle);
+        return allModuleReferencesFromCmHandle;
+    }
+
+    private Collection<ModuleReference> upgradeOrCreateSchemaSetUsingModuleSetTag(final String schemaSetName,
+                                                                                  final String moduleSetTag,
+                                                                                  final String existingAnchorName,
+                                                                                  final boolean inUpgrade) {
         log.info("Found cm handle having module set tag: {}", moduleSetTag);
         final Collection<ModuleReference> moduleReferencesFromExistingCmHandle =
                 cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
-                        existingCmHandleAnchorName);
-        cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
-            schemaSetName, NO_NEW_MODULES, moduleReferencesFromExistingCmHandle);
-        moduleSetTagCache.put(moduleSetTag, moduleReferencesFromExistingCmHandle);
+                        existingAnchorName);
+        if (inUpgrade) {
+            cpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
+                    NO_NEW_MODULES, moduleReferencesFromExistingCmHandle);
+        } else {
+            cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
+                    schemaSetName, NO_NEW_MODULES, moduleReferencesFromExistingCmHandle);
+        }
+        return moduleReferencesFromExistingCmHandle;
+    }
+
+    private String getModuleSetTag(final YangModelCmHandle yangModelCmHandle,
+                                   final CompositeState compositeState,
+                                   final boolean inUpgrade) {
+        if (inUpgrade) {
+            return ModuleOperationsUtils.getLockedCompositeStateDetails(compositeState.getLockReason())
+                    .get(ModuleOperationsUtils.MODULE_SET_TAG_KEY);
+        }
+        return yangModelCmHandle.getModuleSetTag();
+    }
+
+    private void updateModuleSetTagCache(final String moduleSetTag,
+                                         final Collection<ModuleReference> allModuleReferencesFromCmHandle) {
+        if (StringUtils.isNotBlank(moduleSetTag)) {
+            moduleSetTagCache.putIfAbsent(moduleSetTag, allModuleReferencesFromCmHandle);
+        }
     }
 
 }