db90652b8f4faa0808c25555297fc4da5fc49178
[sdc.git] /
1 /*
2 * Copyright © 2016-2018 European Support Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.openecomp.core.tools.commands;
18
19 import static org.openecomp.core.tools.commands.CommandName.SET_HEAL_BY_ITEM_VERSION;
20
21 import com.datastax.driver.core.ResultSet;
22
23 import java.util.List;
24 import java.util.stream.Collectors;
25
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.Option;
28 import org.openecomp.core.tools.store.HealingHandler;
29 import org.openecomp.core.tools.store.VersionCassandraLoader;
30 import org.openecomp.core.tools.store.zusammen.datatypes.HealingEntity;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33
34 public class SetHealingFlagByItemVersionCommand extends Command {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(SetHealingFlagByItemVersionCommand.class);
37     private static final String ITEM_ID_OPTION = "i";
38     private static final String VERSION_ID_OPTION = "v";
39     private static final String PROJECT_OPTION = "o";
40
41     SetHealingFlagByItemVersionCommand() {
42         options.addOption(Option.builder(ITEM_ID_OPTION).hasArg().argName("item_id")
43                                 .desc("id of the item to reset healing flag, mandatory").build());
44         options.addOption(Option.builder(VERSION_ID_OPTION).hasArg().argName("version_id")
45                                 .desc("id of the version to delete from public, mandatory").build());
46         options.addOption(Option.builder(PROJECT_OPTION).hasArg().argName("old_project_version")
47                                 .desc("old project version, mandatory").build());
48     }
49
50     @Override
51     public boolean execute(String[] args) {
52         CommandLine cmd = parseArgs(args);
53         if (!(cmd.hasOption(ITEM_ID_OPTION) && cmd.hasOption(VERSION_ID_OPTION) && cmd.hasOption(PROJECT_OPTION))) {
54             LOGGER.error("Arguments i, v and o are mandatory");
55             return false;
56         }
57         String itemId = cmd.getOptionValue(ITEM_ID_OPTION);
58         String versionId = cmd.getOptionValue(VERSION_ID_OPTION);
59         String projectVersion = cmd.getOptionValue(PROJECT_OPTION);
60
61         VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();
62         ResultSet listItemVersion = versionCassandraLoader.listItemVersion();
63
64         List<HealingEntity> healingEntities = listItemVersion.all().stream().filter(
65                 entry -> (entry.getString("item_id").equals(itemId)
66                 && entry.getString("version_id").equals(versionId))).map(entry ->
67                 new HealingEntity(entry.getString("space"), entry.getString("item_id"),
68                 entry.getString("version_id"), true, projectVersion)).collect(Collectors.toList());
69
70         HealingHandler healingHandler = new HealingHandler();
71         healingHandler.populateHealingTable(healingEntities);
72
73         return true;
74     }
75
76     @Override
77     public CommandName getCommandName() {
78         return SET_HEAL_BY_ITEM_VERSION;
79     }
80 }