2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.core.tools.commands;
23 import static org.openecomp.core.tools.commands.CommandName.CLEAN_USER_DATA;
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;
37 public class CleanUserDataCommand extends Command {
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";
43 CleanUserDataCommand() {
45 Option.builder(ITEM_ID_OPTION).hasArg().argName("item_id").desc("id of the item to clean, mandatory")
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());
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");
58 String itemId = cmd.getOptionValue(ITEM_ID_OPTION);
59 String user = cmd.getOptionValue(USER_OPTION);
61 SessionContext context = createSessionContext(user);
62 ZusammenConnector zusammenConnector = ZusammenConnectorFactory.getInstance().createInterface();
64 Id itemIdObj = new Id(itemId);
65 Collection<ItemVersion> versions = zusammenConnector.listPublicVersions(context, itemIdObj);
66 for (ItemVersion version : versions) {
68 zusammenConnector.cleanVersion(context, itemIdObj, version.getId());
69 } catch (Exception e) {
71 String.format("Error occurred while cleaning item %s version %s from user %s space", itemId,
72 version.getId(), user), e);
79 public CommandName getCommandName() {
80 return CLEAN_USER_DATA;
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;