1 package org.openecomp.core.tools.Commands;
3 import com.amdocs.zusammen.datatypes.Id;
4 import com.amdocs.zusammen.datatypes.SessionContext;
5 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.impl.VersionCassandraDao;
6 import com.google.common.collect.Sets;
7 import org.openecomp.core.tools.store.ElementHandler;
8 import org.openecomp.core.tools.store.VersionInfoCassandraLoader;
9 import org.openecomp.core.tools.store.VspGeneralLoader;
10 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
11 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
12 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
13 import org.openecomp.sdc.versioning.dao.types.Version;
14 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.List;
23 import static org.openecomp.core.tools.store.VspGeneralLoader.buildKey;
25 public class ResetOldVersion {
28 public static final String OLD_VERSION = "oldVersion";
30 private static Map<String, List<String>> itemVersionMap = new HashMap<>();
31 private static Map<String, List<String>> itemChangeRefMap = new HashMap<>();
33 private static int count = 0;
35 public static void reset(SessionContext context, String oldVersion, String emptyOldVersion) {
38 CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
40 loadItemVersionInfo(context);
42 Map<String, ElementEntity> generalElementMap =
43 VspGeneralLoader.load(context,
44 itemVersionMap, itemChangeRefMap);
46 generalElementMap.values().forEach(elementEntity -> updateOldVersionFlag(elementEntity,
47 oldVersion, Boolean.TRUE.toString().equals(emptyOldVersion)));
50 itemVersionMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry));
52 itemChangeRefMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry));
54 System.out.println("number of element updated:" + count);
58 private static void updateElements(SessionContext context, Map<String,
59 ElementEntity> generalElementMap, Map.Entry<String, List<String>> entry) {
61 entry.getValue().stream().filter
62 (changeRef -> generalElementMap
63 .containsKey(buildKey(context, entry, changeRef))).
64 forEach(changeref -> ElementHandler.update(context, entry.getKey(), changeref, changeref,
65 generalElementMap.get(buildKey(context, entry, changeref))));
69 private static void updateOldVersionFlag(ElementEntity elementEntity, String oldVersion,
70 boolean emptyOldVersion) {
72 if (!emptyOldVersion) {
73 elementEntity.getInfo().addProperty(OLD_VERSION, oldVersion);
75 } else if (elementEntity.getInfo().getProperty(OLD_VERSION) == null
76 || "".equals(elementEntity.getInfo().getProperty(OLD_VERSION))) {
77 elementEntity.getInfo().addProperty(OLD_VERSION, oldVersion);
82 private static void loadItemVersionInfo(SessionContext context) {
84 List<String> items = new ArrayList<>();
85 System.setProperty("cassandra.dox.keystore", "dox");
86 VersionInfoCassandraLoader versionInfoCassandraLoader = new VersionInfoCassandraLoader();
87 Collection<VersionInfoEntity> versions = versionInfoCassandraLoader.list();
89 versions.stream().filter(versionInfoEntity -> versionInfoEntity.getEntityType()
90 .equals(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE)).forEach
91 (versionInfoEntity -> {
92 handleVersionInfoEntity(items, versionInfoEntity);
95 System.setProperty("cassandra.dox.keystore", "zusammen_dox");
96 VersionCassandraDao versionCassandraDao = new VersionCassandraDao();
98 items.forEach(itemId -> versionCassandraDao.list(context, context.getUser().getUserName(),
99 new Id(itemId)).forEach(itemVersion -> addItemVersion(itemId, itemVersion.getId())));
103 private static void handleVersionInfoEntity(List<String> items, VersionInfoEntity versionInfoEntity) {
104 items.add(versionInfoEntity.getEntityId());
105 Set<Version> viewableVersions;
106 if (versionInfoEntity.getViewableVersions() != null
107 && !versionInfoEntity.getViewableVersions().isEmpty()) {
108 viewableVersions = versionInfoEntity.getViewableVersions();
110 viewableVersions = Sets.newHashSet(versionInfoEntity.getActiveVersion());
112 addItemChangeRef(versionInfoEntity.getEntityId(), maxChangeRef(viewableVersions));
115 private static Id maxChangeRef(Set<Version> viewableVersions) {
117 return new Id(viewableVersions.stream().max((o1, o2) -> o1.getMajor() > o2.getMajor()
119 () == o2.getMajor() ? (o1.getMinor() > o2.getMinor() ? 1: o1.getMinor() == o2.getMinor()
120 ? 0 : -1) : -1).get().toString());
124 private static void addItemChangeRef(String itemId, Id changeRef) {
125 addItemVersion(itemChangeRefMap, itemId, changeRef);
128 private static void addItemVersion(String itemId, Id versionId) {
129 addItemVersion(itemVersionMap, itemId, versionId);
132 private static void addItemVersion(Map<String, List<String>> itemVersions, String itemId, Id id) {
134 if (!itemVersions.containsKey(itemId)) {
135 itemVersions.put(itemId, new ArrayList<>());
138 itemVersions.get(itemId).add(id.getValue());