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