Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / commands / HealAll.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.tools.commands;
22
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;
48
49 /**
50  * Created by ayalaben on 11/6/2017
51  */
52 public class HealAll extends Command {
53
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;
58
59     HealAll() {
60         options.addOption(
61                 Option.builder(THREAD_NUM_OPTION).hasArg().argName("number").desc("number of threads").build());
62     }
63
64     @Override
65     public boolean execute(String[] args) {
66         CommandLine cmd = parseArgs(args);
67
68         vspManager = VspManagerFactory.getInstance().createInterface();
69         healingManager = HealingManagerFactory.getInstance().createInterface();
70
71         String logFileName = "healing.log";
72         try (BufferedWriter log = new BufferedWriter(new FileWriter(logFileName, true))) {
73
74             writeToLog("----starting healing------", log);
75             Instant startTime = Instant.now();
76
77             int numberOfThreads =
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);
82
83             filterByEntityType(VersionInfoCassandraLoader.list(),
84                     VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE)
85                     .forEach(this::addTaskToTasks);
86
87             executeAllTasks(executor, log);
88
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);
94         }
95         return true;
96     }
97
98     @Override
99     public CommandName getCommandName() {
100         return CommandName.HEAL_ALL;
101     }
102
103     private static void executeAllTasks(ExecutorService executor, BufferedWriter log) {
104
105     }
106
107
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())) {
112
113             return versionInfoEntity.getViewableVersions().stream().max(Version::compareTo).orElse(new Version());
114         }
115         return versionInfoEntity.getActiveVersion();
116     }
117
118     private static void writeToLog(String message, BufferedWriter log) {
119         try {
120             log.write(message);
121             log.newLine();
122         } catch (IOException e) {
123             throw new HealingRuntimeException("unable to write to healing all log file.", e);
124         }
125     }
126
127     private static Stream<VersionInfoEntity> filterByEntityType(Collection<VersionInfoEntity> versionInfoEntities,
128             String entityType) {
129         return versionInfoEntities.stream()
130                                   .filter(versionInfoEntity -> versionInfoEntity.getEntityType().equals(entityType));
131     }
132
133     private void addTaskToTasks(VersionInfoEntity versionInfoEntity) {
134         
135     }
136
137 }