Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / commands / DeletePublicVersionCommand.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 static org.openecomp.core.tools.commands.CommandName.DELETE_PUBLIC_VERSION;
24
25 import com.amdocs.zusammen.datatypes.Id;
26 import com.amdocs.zusammen.datatypes.SessionContext;
27 import com.amdocs.zusammen.datatypes.UserInfo;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.Option;
30 import org.openecomp.core.zusammen.db.ZusammenConnector;
31 import org.openecomp.core.zusammen.db.ZusammenConnectorFactory;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34
35 public class DeletePublicVersionCommand extends Command {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(DeletePublicVersionCommand.class);
38     private static final String ITEM_ID_OPTION = "i";
39     private static final String VERSION_ID_OPTION = "v";
40
41     DeletePublicVersionCommand() {
42         options.addOption(Option.builder(ITEM_ID_OPTION).hasArg().argName("item_id")
43                                 .desc("id of the item to delete from public, 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     }
47
48     @Override
49     public boolean execute(String[] args) {
50         CommandLine cmd = parseArgs(args);
51         if (!cmd.hasOption(ITEM_ID_OPTION) || !cmd.hasOption(VERSION_ID_OPTION)) {
52             LOGGER.error("Arguments i and v are mandatory");
53             return false;
54         }
55         String itemId = cmd.getOptionValue(ITEM_ID_OPTION);
56         String versionId = cmd.getOptionValue(VERSION_ID_OPTION);
57
58         SessionContext context = createSessionContext();
59         ZusammenConnector zusammenConnector = ZusammenConnectorFactory.getInstance().createInterface();
60
61         try {
62             zusammenConnector.cleanVersion(context, new Id(itemId), new Id(versionId));
63         } catch (Exception e) {
64             LOGGER.error(String.format("Error occurred while deleting item %s version %s from public space", itemId,
65                     versionId), e);
66         }
67         return true;
68     }
69
70     @Override
71     public CommandName getCommandName() {
72         return DELETE_PUBLIC_VERSION;
73     }
74
75     private static SessionContext createSessionContext() {
76         SessionContext sessionContext = new SessionContext();
77         sessionContext.setUser(new UserInfo("public"));
78         sessionContext.setTenant("dox");
79         return sessionContext;
80     }
81 }