diff options
author | ayalaben <Ayala.Benzvi@Amdocs.Com> | 2017-11-16 09:21:41 +0200 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2017-11-19 13:51:24 +0000 |
commit | 914f3a5f952e0799b6fb05788d30562464384a13 (patch) | |
tree | 17dc7c2e27b9b127acbe4ba89c8e8b7b1f7c94fb /openecomp-be/tools/zusammen-tools/src/main/java | |
parent | 581e9aa490b015d9b46314663c165a8bf12523d8 (diff) |
heal all script
Issue - ID: SDC-665
Change-Id: Ib035549589ccd9c113235886442b1b2c9a198b86
Signed-off-by: ayalaben <ayala.benzvi@amdocs.com>
Diffstat (limited to 'openecomp-be/tools/zusammen-tools/src/main/java')
4 files changed, 220 insertions, 8 deletions
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/HealAll.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/HealAll.java new file mode 100644 index 0000000000..2bc48bd0e8 --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/HealAll.java @@ -0,0 +1,142 @@ +package org.openecomp.core.tools.Commands; + +import org.apache.commons.collections.CollectionUtils; +import org.openecomp.core.tools.concurrent.ItemHealingTask; +import org.openecomp.core.tools.loaders.VersionInfoCassandraLoader; +import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants; +import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager; +import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.VersioningManagerFactory; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.stream.Stream; + +/** + * Created by ayalaben on 11/6/2017 + */ +public class HealAll { + + private static final String HEALING_USER = "healing_user"; + private static final int defaulThreadNumber =100; + private static BufferedWriter log; + private static List<ItemHealingTask> tasks = new ArrayList<>(); + private static VendorSoftwareProductManager vspManager = VspManagerFactory + .getInstance().createInterface(); + private static VersioningManager versioningManager = VersioningManagerFactory.getInstance() + .createInterface(); + + static { + try { + log = + new BufferedWriter(new FileWriter("healing.log",true)); + } catch (IOException e) { + if (log != null) { + try { + log.close(); + } catch (IOException e1) { + throw new RuntimeException("can't initial healing log file: " + e1.getMessage()); + } + } + throw new RuntimeException("can't initial healing log file: " + e.getMessage()); + } + } + + public static void healAll(String threadNumber) { + + writeToLog("----starting healing------"); + Instant startTime = Instant.now(); + + int numberOfThreads = Objects.nonNull(threadNumber) ? Integer.valueOf(threadNumber) : + defaulThreadNumber; + ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads); + + filterByEntityType(VersionInfoCassandraLoader.list(), + VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE).forEach + (HealAll::addTaskToTasks); + + executeAllTasks(executor); + + writeToLog("----finished healing------" ); + Instant endTime = Instant.now(); + writeToLog("Total runtime was: " + Duration.between(startTime, endTime)); + + try { + if (log != null) { + log.close(); + } + } catch (IOException e) { + writeToLog("Error:" + e.getMessage()); + } + + System.exit(1); + } + + private static void executeAllTasks(ExecutorService executor) { + List<Future<String>> futureTasks; + try { + futureTasks = executor.invokeAll(tasks); + futureTasks.forEach(future -> { + try { + log.write(future.get()); + log.newLine(); + } catch (Exception e) { + writeToLog(e.getMessage()); + } + }); + } catch (InterruptedException e) { + writeToLog("migration tasks failed with message: " + e.getMessage()); + throw new RuntimeException(e); + } + + boolean isThreadOpen = true; + while (isThreadOpen) { + isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone()); + } + } + + private static Version resolveVersion(VersionInfoEntity versionInfoEntity) { + if (Objects.nonNull(versionInfoEntity.getCandidate())) { + return versionInfoEntity.getCandidate().getVersion(); + } else if (!CollectionUtils.isEmpty(versionInfoEntity.getViewableVersions())) { + return versionInfoEntity.getViewableVersions().stream().max(Version::compateTo).get(); + } + return versionInfoEntity.getActiveVersion(); + } + + private static void writeToLog(String message) { + try { + log.write(message); + log.newLine(); + } catch (IOException e) { + throw new RuntimeException("unable to write to healing all log file."); + } + } + + private static Stream<VersionInfoEntity> filterByEntityType(Collection<VersionInfoEntity> + versionInfoEntities, + String entityType){ + return versionInfoEntities.stream().filter(versionInfoEntity -> versionInfoEntity + .getEntityType().equals(entityType)); + } + private static void addTaskToTasks(VersionInfoEntity versionInfoEntity){ + tasks.add(new ItemHealingTask(versionInfoEntity.getEntityId(), resolveVersion + (versionInfoEntity).toString(), versionInfoEntity.getCandidate() == null ? + HEALING_USER : versionInfoEntity.getCandidate().getUser(), + vspManager, versioningManager)); + } + +} diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/concurrent/ItemHealingTask.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/concurrent/ItemHealingTask.java new file mode 100644 index 0000000000..39b62d4e99 --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/concurrent/ItemHealingTask.java @@ -0,0 +1,71 @@ +package org.openecomp.core.tools.concurrent; + +import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants; +import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.VersioningUtil; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.types.VersionInfo; +import org.openecomp.sdc.versioning.types.VersionableEntityAction; + +import java.util.concurrent.Callable; + +/** + * Created by ayalaben on 11/7/2017 + */ +public class ItemHealingTask implements Callable<String> { + + private String itemId; + private String versionId; + private String user; + private VendorSoftwareProductManager vspManager; + private VersioningManager versioningManager; + + public String getItemId() { + return itemId; + } + + public String getVersionId() { + return versionId; + } + + + public ItemHealingTask(String itemId, String versionId, String user, + VendorSoftwareProductManager vspManager, + VersioningManager versioningManager) { + this.itemId = itemId; + this.versionId = versionId; + this.user = user; + this.versioningManager = versioningManager; + this.vspManager = vspManager; + } + + @Override + public String call() throws Exception { + + VersionInfo versionInfo = getVersionInfo(itemId, VersionableEntityAction.Read, user); + Version resolvedVersion = VersioningUtil.resolveVersion(Version.valueOf(versionId), + versionInfo, user); + VspDetails vspDetails = vspManager.getVsp(itemId, resolvedVersion, user); + + try { + vspManager.callAutoHeal(itemId, versionInfo, vspDetails, user); + + } catch (Exception e) { + return (String.format("healing failed on vsp: %s with id: %s, versionId: %s, resolved " + + "Version: %s, with message: %s",vspDetails.getName(),itemId, + versionId,resolvedVersion,e.getMessage())); + } + + return String.format("healed vsp: %s, with id: %s, versionId:%s, resolved version: %s", + vspDetails.getName(), itemId, versionId,resolvedVersion ); + } + + private VersionInfo getVersionInfo(String vendorSoftwareProductId, VersionableEntityAction action, + String user) { + return versioningManager.getEntityVersionInfo( + VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, + vendorSoftwareProductId, user, action); + } +} diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/loaders/VersionInfoCassandraLoader.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/loaders/VersionInfoCassandraLoader.java index 8840975a56..aba8754f4b 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/loaders/VersionInfoCassandraLoader.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/loaders/VersionInfoCassandraLoader.java @@ -20,7 +20,6 @@ package org.openecomp.core.tools.loaders; -import com.datastax.driver.mapping.Mapper; import com.datastax.driver.mapping.Result; import com.datastax.driver.mapping.annotations.Accessor; import com.datastax.driver.mapping.annotations.Query; @@ -30,18 +29,14 @@ import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity; import java.util.Collection; -public class VersionInfoCassandraLoader { +public class VersionInfoCassandraLoader { private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); - private static Mapper<VersionInfoEntity> mapper = - noSqlDb.getMappingManager().mapper(VersionInfoEntity.class); private static VersionInfoAccessor accessor = noSqlDb.getMappingManager().createAccessor(VersionInfoAccessor.class); - - - public Collection<VersionInfoEntity> list() { + public static Collection<VersionInfoEntity> list() { return accessor.getAll().all(); } diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java index 6f092d2f56..b55d51709d 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/main/ZusammenMainTool.java @@ -2,6 +2,7 @@ package org.openecomp.core.tools.main; import com.amdocs.zusammen.datatypes.SessionContext; import com.amdocs.zusammen.datatypes.UserInfo; +import org.openecomp.core.tools.Commands.HealAll; import org.openecomp.core.tools.Commands.ImportCommand; import org.openecomp.core.tools.Commands.ExportDataCommand; import org.openecomp.core.tools.Commands.ResetOldVersion; @@ -46,6 +47,8 @@ public class ZusammenMainTool { case IMPORT: ImportCommand.importData(context, ToolsUtil.getParam("f",args),ToolsUtil.getParam("i",args)); break; + case HEAL_ALL: + HealAll.healAll(ToolsUtil.getParam("t",args)); } @@ -68,7 +71,8 @@ public class ZusammenMainTool { RESET_OLD_VERSION("reset-old-version"), EXPORT("export"), - IMPORT("import"); + IMPORT("import"), + HEAL_ALL("heal-all"); COMMANDS(String command) { this.command = command; |