ce2f9acf62b0746418437de70301278b6f5a884b
[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     private static final String ITEM_ID = "item_id";
41     private static final String VERSION_ID = "version_id";
42
43     SetHealingFlagByItemVersionCommand() {
44         options.addOption(Option.builder(ITEM_ID_OPTION).hasArg().argName(ITEM_ID)
45                                 .desc("id of the item to reset healing flag, mandatory").build());
46         options.addOption(Option.builder(VERSION_ID_OPTION).hasArg().argName(VERSION_ID)
47                                 .desc("id of the version to delete from public, mandatory").build());
48         options.addOption(Option.builder(PROJECT_OPTION).hasArg().argName("old_project_version")
49                                 .desc("old project version, mandatory").build());
50     }
51
52     @Override
53     public boolean execute(String[] args) {
54         CommandLine cmd = parseArgs(args);
55         if (!(cmd.hasOption(ITEM_ID_OPTION) && cmd.hasOption(VERSION_ID_OPTION) && cmd.hasOption(PROJECT_OPTION))) {
56             LOGGER.error("Arguments i, v and o are mandatory");
57             return false;
58         }
59         String itemId = cmd.getOptionValue(ITEM_ID_OPTION);
60         String versionId = cmd.getOptionValue(VERSION_ID_OPTION);
61         String projectVersion = cmd.getOptionValue(PROJECT_OPTION);
62
63         VersionCassandraLoader versionCassandraLoader = new VersionCassandraLoader();
64         ResultSet listItemVersion = versionCassandraLoader.listItemVersion();
65
66         List<HealingEntity> healingEntities = listItemVersion.all().stream().filter(
67                 entry -> (entry.getString(ITEM_ID).equals(itemId)
68                 && entry.getString(VERSION_ID).equals(versionId))).map(entry ->
69                 new HealingEntity(entry.getString("space"), entry.getString(ITEM_ID),
70                 entry.getString(VERSION_ID), true, projectVersion)).collect(Collectors.toList());
71
72         HealingHandler healingHandler = new HealingHandler();
73         healingHandler.populateHealingTable(healingEntities);
74
75         return true;
76     }
77
78     @Override
79     public CommandName getCommandName() {
80         return SET_HEAL_BY_ITEM_VERSION;
81     }
82 }