2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.tools.commands;
23 import java.io.BufferedWriter;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.time.Duration;
27 import java.time.Instant;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.Objects;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.Future;
35 import java.util.stream.Stream;
36 import org.apache.commons.cli.CommandLine;
37 import org.apache.commons.cli.Option;
38 import org.apache.commons.collections.CollectionUtils;
39 import org.openecomp.core.tools.exceptions.HealingRuntimeException;
40 import org.openecomp.core.tools.loaders.VersionInfoCassandraLoader;
41 import org.openecomp.sdc.healing.api.HealingManager;
42 import org.openecomp.sdc.healing.factory.HealingManagerFactory;
43 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
44 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
45 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
46 import org.openecomp.sdc.versioning.dao.types.Version;
47 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
50 * Created by ayalaben on 11/6/2017
52 public class HealAll extends Command {
54 private static final int DEFAULT_THREAD_NUMBER = 100;
55 private static final String THREAD_NUM_OPTION = "t";
56 private VendorSoftwareProductManager vspManager;
57 private HealingManager healingManager;
61 Option.builder(THREAD_NUM_OPTION).hasArg().argName("number").desc("number of threads").build());
65 public boolean execute(String[] args) {
66 CommandLine cmd = parseArgs(args);
68 vspManager = VspManagerFactory.getInstance().createInterface();
69 healingManager = HealingManagerFactory.getInstance().createInterface();
71 String logFileName = "healing.log";
72 try (BufferedWriter log = new BufferedWriter(new FileWriter(logFileName, true))) {
74 writeToLog("----starting healing------", log);
75 Instant startTime = Instant.now();
78 cmd.hasOption(THREAD_NUM_OPTION) && Objects.nonNull(cmd.getOptionValue(THREAD_NUM_OPTION))
79 ? Integer.valueOf(cmd.getOptionValue(THREAD_NUM_OPTION))
80 : DEFAULT_THREAD_NUMBER;
81 ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
83 filterByEntityType(VersionInfoCassandraLoader.list(),
84 VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE)
85 .forEach(this::addTaskToTasks);
87 executeAllTasks(executor, log);
89 writeToLog("----finished healing------", log);
90 Instant endTime = Instant.now();
91 writeToLog("Total runtime was: " + Duration.between(startTime, endTime), log);
92 } catch (IOException e) {
93 throw new HealingRuntimeException("can't initial healing log file '" + logFileName + "'", e);
99 public CommandName getCommandName() {
100 return CommandName.HEAL_ALL;
103 private static void executeAllTasks(ExecutorService executor, BufferedWriter log) {
108 private static Version resolveVersion(VersionInfoEntity versionInfoEntity) {
109 if (Objects.nonNull(versionInfoEntity.getCandidate())) {
110 return versionInfoEntity.getCandidate().getVersion();
111 } else if (!CollectionUtils.isEmpty(versionInfoEntity.getViewableVersions())) {
113 return versionInfoEntity.getViewableVersions().stream().max(Version::compareTo).orElse(new Version());
115 return versionInfoEntity.getActiveVersion();
118 private static void writeToLog(String message, BufferedWriter log) {
122 } catch (IOException e) {
123 throw new HealingRuntimeException("unable to write to healing all log file.", e);
127 private static Stream<VersionInfoEntity> filterByEntityType(Collection<VersionInfoEntity> versionInfoEntities,
129 return versionInfoEntities.stream()
130 .filter(versionInfoEntity -> versionInfoEntity.getEntityType().equals(entityType));
133 private void addTaskToTasks(VersionInfoEntity versionInfoEntity) {