2  * Copyright © 2016-2017 European Support Limited
 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
 
   5  * you may not use this file except in compliance with the License.
 
   6  * You may obtain a copy of the License at
 
   8  *      http://www.apache.org/licenses/LICENSE-2.0
 
  10  * Unless required by applicable law or agreed to in writing, software
 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  * See the License for the specific language governing permissions and
 
  14  * limitations under the License.
 
  17 package org.openecomp.core.tools.Commands;
 
  19 import com.amdocs.zusammen.datatypes.Id;
 
  20 import com.amdocs.zusammen.datatypes.SessionContext;
 
  21 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.impl.VersionCassandraDao;
 
  22 import com.google.common.collect.Sets;
 
  23 import org.apache.commons.lang3.StringUtils;
 
  24 import org.openecomp.core.tools.store.ElementHandler;
 
  25 import org.openecomp.core.tools.store.VersionInfoCassandraLoader;
 
  26 import org.openecomp.core.tools.store.VspGeneralLoader;
 
  27 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
 
  28 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
 
  29 import org.openecomp.sdc.logging.api.Logger;
 
  30 import org.openecomp.sdc.logging.api.LoggerFactory;
 
  31 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
 
  32 import org.openecomp.sdc.versioning.dao.types.Version;
 
  33 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
 
  35 import java.util.ArrayList;
 
  36 import java.util.Collection;
 
  37 import java.util.HashMap;
 
  38 import java.util.List;
 
  40 import java.util.Optional;
 
  43 import static org.openecomp.core.tools.store.VspGeneralLoader.buildKey;
 
  45 public class ResetOldVersion {
 
  48   private static final String OLD_VERSION = "oldVersion";
 
  50   private static final Logger LOGGER = LoggerFactory.getLogger(ResetOldVersion.class);
 
  51   private static final String CASSANDRA_DOX_KEYSTORE = "cassandra.dox.keystore";
 
  52   private static int count = 0;
 
  54   private ResetOldVersion() {
 
  57   public static void reset(SessionContext context, String oldVersion, String emptyOldVersion) {
 
  58     Map<String, List<String>> itemVersionMap = new HashMap<>();
 
  59     Map<String, List<String>> itemChangeRefMap = new HashMap<>();
 
  61     CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
 
  63     loadItemVersionInfo(context, itemChangeRefMap, itemVersionMap);
 
  65     Map<String, ElementEntity> generalElementMap =
 
  66         VspGeneralLoader.load(context, itemVersionMap, itemChangeRefMap);
 
  68     generalElementMap.values().forEach(elementEntity -> updateOldVersionFlag(elementEntity,
 
  69         oldVersion, Boolean.TRUE.toString().equals(emptyOldVersion)));
 
  72     itemVersionMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry));
 
  74     itemChangeRefMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry));
 
  75     LOGGER.info("number of element updated:" + count);
 
  78   private static void updateElements(SessionContext context, Map<String,
 
  79       ElementEntity> generalElementMap, Map.Entry<String, List<String>> entry) {
 
  81     entry.getValue().stream()
 
  82         .filter(changeRef -> generalElementMap.containsKey(buildKey(context, entry, changeRef)))
 
  83         .forEach(changeref -> ElementHandler.update(context, entry.getKey(), changeref, changeref,
 
  84             generalElementMap.get(buildKey(context, entry, changeref))));
 
  88   private static void updateOldVersionFlag(ElementEntity elementEntity, String oldVersion,
 
  89                                            boolean emptyOldVersion) {
 
  92         || StringUtils.isBlank(elementEntity.getInfo().getProperty(OLD_VERSION))) {
 
  93       elementEntity.getInfo().addProperty(OLD_VERSION, oldVersion);
 
  98   private static void loadItemVersionInfo(SessionContext context,
 
  99                                           Map<String, List<String>> itemChangeRefMap,
 
 100                                           Map<String, List<String>> itemVersionMap) {
 
 102     List<String> items = new ArrayList<>();
 
 103     System.setProperty(CASSANDRA_DOX_KEYSTORE, "dox");
 
 104     VersionInfoCassandraLoader versionInfoCassandraLoader = new VersionInfoCassandraLoader();
 
 105     Collection<VersionInfoEntity> versions = versionInfoCassandraLoader.list();
 
 107     versions.stream().filter(versionInfoEntity -> versionInfoEntity.getEntityType()
 
 108         .equals(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE))
 
 109         .forEach(versionInfoEntity -> handleVersionInfoEntity(items, versionInfoEntity,
 
 112     System.setProperty(CASSANDRA_DOX_KEYSTORE, "zusammen_dox");
 
 113     VersionCassandraDao versionCassandraDao = new VersionCassandraDao();
 
 115     items.forEach(itemId -> versionCassandraDao.list(context, context.getUser().getUserName(),
 
 116         new Id(itemId)).forEach(itemVersion -> addItemVersion(itemId, itemVersion.getId(),
 
 121   private static void handleVersionInfoEntity(List<String> items,
 
 122                                               VersionInfoEntity versionInfoEntity,
 
 123                                               Map<String, List<String>> itemChangeRefMap) {
 
 124     items.add(versionInfoEntity.getEntityId());
 
 125     Set<Version> viewableVersions;
 
 126     if (versionInfoEntity.getViewableVersions() != null
 
 127         && !versionInfoEntity.getViewableVersions().isEmpty()) {
 
 128       viewableVersions = versionInfoEntity.getViewableVersions();
 
 130       viewableVersions = Sets.newHashSet(versionInfoEntity.getActiveVersion());
 
 132     addItemChangeRef(versionInfoEntity.getEntityId(), maxChangeRef(viewableVersions),
 
 136   private static Id maxChangeRef(Set<Version> viewableVersions) {
 
 137     Optional<Version> maxVersion = viewableVersions.stream()
 
 138         .max(ResetOldVersion::evaluateMaxVersion);
 
 140     return maxVersion.map(version -> new Id(version.toString())).orElse(null);
 
 143   private static int evaluateMaxVersion(Version version1, Version version2) {
 
 144     if (version1.getMajor() > version2.getMajor()) {
 
 146     } else if (version1.getMajor() == version2.getMajor()) {
 
 147       return Integer.compare(version1.getMinor(), version2.getMinor());
 
 153   private static void addItemChangeRef(String itemId, Id changeRef,
 
 154                                        Map<String, List<String>> itemChangeRefMap) {
 
 155     addItemVersion(itemChangeRefMap, itemId, changeRef);
 
 158   private static void addItemVersion(String itemId, Id versionId,
 
 159                                      Map<String, List<String>> itemVersionMap) {
 
 160     addItemVersion(itemVersionMap, itemId, versionId);
 
 163   private static void addItemVersion(Map<String, List<String>> itemVersions, String itemId, Id id) {
 
 165     if (!itemVersions.containsKey(itemId)) {
 
 166       itemVersions.put(itemId, new ArrayList<>());
 
 169     itemVersions.get(itemId).add(id.getValue());