1 package org.openecomp.core.tools.Commands;
3 import org.apache.commons.collections.CollectionUtils;
4 import org.openecomp.core.tools.concurrent.ItemHealingTask;
5 import org.openecomp.core.tools.loaders.VersionInfoCassandraLoader;
6 import org.openecomp.sdc.healing.api.HealingManager;
7 import org.openecomp.sdc.healing.factory.HealingManagerFactory;
8 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
9 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
10 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
11 import org.openecomp.sdc.versioning.VersioningManager;
12 import org.openecomp.sdc.versioning.VersioningManagerFactory;
13 import org.openecomp.sdc.versioning.dao.types.Version;
14 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
16 import java.io.BufferedWriter;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.time.Duration;
20 import java.time.Instant;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Objects;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import java.util.stream.Stream;
31 * Created by ayalaben on 11/6/2017
33 public class HealAll {
35 private static final String HEALING_USER = "healing_user";
36 private static final int defaulThreadNumber = 100;
37 private static BufferedWriter log;
38 private static List<ItemHealingTask> tasks = new ArrayList<>();
39 private static VendorSoftwareProductManager vspManager = VspManagerFactory
40 .getInstance().createInterface();
41 private static HealingManager healingManager = HealingManagerFactory.getInstance()
43 private static VersioningManager versioningManager = VersioningManagerFactory.getInstance()
49 new BufferedWriter(new FileWriter("healing.log", true));
50 } catch (IOException e) {
54 } catch (IOException e1) {
55 throw new RuntimeException("can't initial healing log file: " + e1.getMessage());
58 throw new RuntimeException("can't initial healing log file: " + e.getMessage());
62 public static void healAll(String threadNumber) {
64 writeToLog("----starting healing------");
65 Instant startTime = Instant.now();
67 int numberOfThreads = Objects.nonNull(threadNumber) ? Integer.valueOf(threadNumber) :
69 ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
71 filterByEntityType(VersionInfoCassandraLoader.list(),
72 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE).forEach
73 (HealAll::addTaskToTasks);
75 executeAllTasks(executor);
77 writeToLog("----finished healing------");
78 Instant endTime = Instant.now();
79 writeToLog("Total runtime was: " + Duration.between(startTime, endTime));
85 } catch (IOException e) {
86 writeToLog("Error:" + e.getMessage());
92 private static void executeAllTasks(ExecutorService executor) {
93 List<Future<String>> futureTasks;
95 futureTasks = executor.invokeAll(tasks);
96 futureTasks.forEach(future -> {
98 log.write(future.get());
100 } catch (Exception e) {
101 writeToLog(e.getMessage());
104 } catch (InterruptedException e) {
105 writeToLog("migration tasks failed with message: " + e.getMessage());
106 throw new RuntimeException(e);
109 boolean isThreadOpen = true;
110 while (isThreadOpen) {
111 isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
115 private static Version resolveVersion(VersionInfoEntity versionInfoEntity) {
116 if (Objects.nonNull(versionInfoEntity.getCandidate())) {
117 return versionInfoEntity.getCandidate().getVersion();
118 } else if (!CollectionUtils.isEmpty(versionInfoEntity.getViewableVersions())) {
119 return versionInfoEntity.getViewableVersions().stream().max(Version::compateTo).get();
121 return versionInfoEntity.getActiveVersion();
124 private static void writeToLog(String message) {
128 } catch (IOException e) {
129 throw new RuntimeException("unable to write to healing all log file.");
133 private static Stream<VersionInfoEntity> filterByEntityType(
134 Collection<VersionInfoEntity> versionInfoEntities, String entityType) {
135 return versionInfoEntities.stream().filter(versionInfoEntity -> versionInfoEntity
136 .getEntityType().equals(entityType));
139 private static void addTaskToTasks(VersionInfoEntity versionInfoEntity) {
140 tasks.add(new ItemHealingTask(versionInfoEntity.getEntityId(), resolveVersion
141 (versionInfoEntity).toString(),
142 vspManager, healingManager));