fee07501d4b0a40154edb1bc42885f7d87b2e681
[sdc.git] /
1 package org.openecomp.core.tools.commands;
2
3 import static org.openecomp.core.tools.commands.CommandName.DELETE_PUBLIC_VERSION;
4
5 import com.amdocs.zusammen.datatypes.Id;
6 import com.amdocs.zusammen.datatypes.SessionContext;
7 import com.amdocs.zusammen.datatypes.UserInfo;
8 import org.apache.commons.cli.CommandLine;
9 import org.apache.commons.cli.Option;
10 import org.openecomp.core.zusammen.db.ZusammenConnector;
11 import org.openecomp.core.zusammen.db.ZusammenConnectorFactory;
12 import org.openecomp.sdc.logging.api.Logger;
13 import org.openecomp.sdc.logging.api.LoggerFactory;
14
15 public class DeletePublicVersionCommand extends Command {
16
17     private static final Logger LOGGER = LoggerFactory.getLogger(DeletePublicVersionCommand.class);
18     private static final String ITEM_ID_OPTION = "i";
19     private static final String VERSION_ID_OPTION = "v";
20
21     DeletePublicVersionCommand() {
22         options.addOption(Option.builder(ITEM_ID_OPTION).hasArg().argName("item_id")
23                                 .desc("id of the item to delete from public, mandatory").build());
24         options.addOption(Option.builder(VERSION_ID_OPTION).hasArg().argName("version_id")
25                                 .desc("id of the version to delete from public, mandatory").build());
26     }
27
28     @Override
29     public boolean execute(String[] args) {
30         CommandLine cmd = parseArgs(args);
31         if (!cmd.hasOption(ITEM_ID_OPTION) || !cmd.hasOption(VERSION_ID_OPTION)) {
32             LOGGER.error("Arguments i and v are mandatory");
33             return false;
34         }
35         String itemId = cmd.getOptionValue(ITEM_ID_OPTION);
36         String versionId = cmd.getOptionValue(VERSION_ID_OPTION);
37
38         SessionContext context = createSessionContext();
39         ZusammenConnector zusammenConnector = ZusammenConnectorFactory.getInstance().createInterface();
40
41         try {
42             zusammenConnector.cleanVersion(context, new Id(itemId), new Id(versionId));
43         } catch (Exception e) {
44             LOGGER.error(String.format("Error occurred while deleting item %s version %s from public space", itemId,
45                     versionId), e);
46         }
47         return true;
48     }
49
50     @Override
51     public CommandName getCommandName() {
52         return DELETE_PUBLIC_VERSION;
53     }
54
55     private static SessionContext createSessionContext() {
56         SessionContext sessionContext = new SessionContext();
57         sessionContext.setUser(new UserInfo("public"));
58         sessionContext.setTenant("dox");
59         return sessionContext;
60     }
61 }