Resolved all possible sonar issues 39/26039/1
authorsiddharth0905 <siddharth.singh4@amdocs.com>
Mon, 11 Dec 2017 12:23:55 +0000 (17:53 +0530)
committersiddharth0905 <siddharth.singh4@amdocs.com>
Mon, 11 Dec 2017 12:23:55 +0000 (17:53 +0530)
Resolved all Sonar qube issues and changes discussed with Vitaliy

Change-Id: I8e49fb3d05a6735f0f6b109525de9184b3fd5d18
Issue-ID: SDC-343
Signed-off-by: siddharth0905 <siddharth.singh4@amdocs.com>
openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/ResetOldVersion.java

index 1725df7..eb9a9ee 100644 (file)
@@ -1,14 +1,33 @@
+/*
+ * Copyright © 2016-2017 European Support Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package org.openecomp.core.tools.Commands;
 
 import com.amdocs.zusammen.datatypes.Id;
 import com.amdocs.zusammen.datatypes.SessionContext;
 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.impl.VersionCassandraDao;
 import com.google.common.collect.Sets;
+import org.apache.commons.lang3.StringUtils;
 import org.openecomp.core.tools.store.ElementHandler;
 import org.openecomp.core.tools.store.VersionInfoCassandraLoader;
 import org.openecomp.core.tools.store.VspGeneralLoader;
 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
 import org.openecomp.sdc.versioning.dao.types.Version;
 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
@@ -18,6 +37,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 
 import static org.openecomp.core.tools.store.VspGeneralLoader.buildKey;
@@ -25,107 +45,118 @@ import static org.openecomp.core.tools.store.VspGeneralLoader.buildKey;
 public class ResetOldVersion {
 
 
-  public static final String OLD_VERSION = "oldVersion";
-
-  private static Map<String, List<String>> itemVersionMap = new HashMap<>();
-  private static Map<String, List<String>> itemChangeRefMap = new HashMap<>();
+  private static final String OLD_VERSION = "oldVersion";
 
+  private static final Logger LOGGER = LoggerFactory.getLogger(ResetOldVersion.class);
+  private static final String CASSANDRA_DOX_KEYSTORE = "cassandra.dox.keystore";
   private static int count = 0;
 
-  public static void reset(SessionContext context, String oldVersion, String emptyOldVersion) {
+  private ResetOldVersion() {
+  }
 
+  public static void reset(SessionContext context, String oldVersion, String emptyOldVersion) {
+    Map<String, List<String>> itemVersionMap = new HashMap<>();
+    Map<String, List<String>> itemChangeRefMap = new HashMap<>();
 
     CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
 
-    loadItemVersionInfo(context);
+    loadItemVersionInfo(context, itemChangeRefMap, itemVersionMap);
 
     Map<String, ElementEntity> generalElementMap =
-            VspGeneralLoader.load(context,
-                    itemVersionMap, itemChangeRefMap);
+        VspGeneralLoader.load(context, itemVersionMap, itemChangeRefMap);
 
     generalElementMap.values().forEach(elementEntity -> updateOldVersionFlag(elementEntity,
-            oldVersion, Boolean.TRUE.toString().equals(emptyOldVersion)));
+        oldVersion, Boolean.TRUE.toString().equals(emptyOldVersion)));
 
 
     itemVersionMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry));
 
     itemChangeRefMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry));
-
-    System.out.println("number of element updated:" + count);
-
+    LOGGER.info("number of element updated:" + count);
   }
 
   private static void updateElements(SessionContext context, Map<String,
-          ElementEntity> generalElementMap, Map.Entry<String, List<String>> entry) {
+      ElementEntity> generalElementMap, Map.Entry<String, List<String>> entry) {
 
-    entry.getValue().stream().filter
-            (changeRef -> generalElementMap
-                    .containsKey(buildKey(context, entry, changeRef))).
-            forEach(changeref -> ElementHandler.update(context, entry.getKey(), changeref, changeref,
-                    generalElementMap.get(buildKey(context, entry, changeref))));
+    entry.getValue().stream()
+        .filter(changeRef -> generalElementMap.containsKey(buildKey(context, entry, changeRef)))
+        .forEach(changeref -> ElementHandler.update(context, entry.getKey(), changeref, changeref,
+            generalElementMap.get(buildKey(context, entry, changeref))));
 
   }
 
   private static void updateOldVersionFlag(ElementEntity elementEntity, String oldVersion,
                                            boolean emptyOldVersion) {
 
-    if (!emptyOldVersion) {
-      elementEntity.getInfo().addProperty(OLD_VERSION, oldVersion);
-      count++;
-    } else if (elementEntity.getInfo().getProperty(OLD_VERSION) == null
-            || "".equals(elementEntity.getInfo().getProperty(OLD_VERSION))) {
+    if (!emptyOldVersion
+        || StringUtils.isBlank(elementEntity.getInfo().getProperty(OLD_VERSION))) {
       elementEntity.getInfo().addProperty(OLD_VERSION, oldVersion);
       count++;
     }
   }
 
-  private static void loadItemVersionInfo(SessionContext context) {
+  private static void loadItemVersionInfo(SessionContext context,
+                                          Map<String, List<String>> itemChangeRefMap,
+                                          Map<String, List<String>> itemVersionMap) {
 
     List<String> items = new ArrayList<>();
-    System.setProperty("cassandra.dox.keystore", "dox");
+    System.setProperty(CASSANDRA_DOX_KEYSTORE, "dox");
     VersionInfoCassandraLoader versionInfoCassandraLoader = new VersionInfoCassandraLoader();
     Collection<VersionInfoEntity> versions = versionInfoCassandraLoader.list();
 
     versions.stream().filter(versionInfoEntity -> versionInfoEntity.getEntityType()
-            .equals(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE)).forEach
-            (versionInfoEntity -> {
-              handleVersionInfoEntity(items, versionInfoEntity);
-            });
+        .equals(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE))
+        .forEach(versionInfoEntity -> handleVersionInfoEntity(items, versionInfoEntity,
+            itemChangeRefMap));
 
-    System.setProperty("cassandra.dox.keystore", "zusammen_dox");
+    System.setProperty(CASSANDRA_DOX_KEYSTORE, "zusammen_dox");
     VersionCassandraDao versionCassandraDao = new VersionCassandraDao();
 
     items.forEach(itemId -> versionCassandraDao.list(context, context.getUser().getUserName(),
-            new Id(itemId)).forEach(itemVersion -> addItemVersion(itemId, itemVersion.getId())));
+        new Id(itemId)).forEach(itemVersion -> addItemVersion(itemId, itemVersion.getId(),
+        itemVersionMap)));
 
   }
 
-  private static void handleVersionInfoEntity(List<String> items, VersionInfoEntity versionInfoEntity) {
+  private static void handleVersionInfoEntity(List<String> items,
+                                              VersionInfoEntity versionInfoEntity,
+                                              Map<String, List<String>> itemChangeRefMap) {
     items.add(versionInfoEntity.getEntityId());
     Set<Version> viewableVersions;
     if (versionInfoEntity.getViewableVersions() != null
-            && !versionInfoEntity.getViewableVersions().isEmpty()) {
+        && !versionInfoEntity.getViewableVersions().isEmpty()) {
       viewableVersions = versionInfoEntity.getViewableVersions();
     } else {
       viewableVersions = Sets.newHashSet(versionInfoEntity.getActiveVersion());
     }
-    addItemChangeRef(versionInfoEntity.getEntityId(), maxChangeRef(viewableVersions));
+    addItemChangeRef(versionInfoEntity.getEntityId(), maxChangeRef(viewableVersions),
+        itemChangeRefMap);
   }
 
   private static Id maxChangeRef(Set<Version> viewableVersions) {
+    Optional<Version> maxVersion = viewableVersions.stream()
+        .max(ResetOldVersion::evaluateMaxVersion);
 
-    return new Id(viewableVersions.stream().max((o1, o2) -> o1.getMajor() > o2.getMajor()
-            ?1:o1.getMajor
-            () == o2.getMajor() ? (o1.getMinor() > o2.getMinor() ? 1: o1.getMinor() == o2.getMinor()
-            ? 0 : -1) : -1).get().toString());
+    return maxVersion.map(version -> new Id(version.toString())).orElse(null);
+  }
 
+  private static int evaluateMaxVersion(Version version1, Version version2) {
+    if (version1.getMajor() > version2.getMajor()) {
+      return 1;
+    } else if (version1.getMajor() == version2.getMajor()) {
+      return Integer.compare(version1.getMinor(), version2.getMinor());
+    } else {
+      return -1;
+    }
   }
 
-  private static void addItemChangeRef(String itemId, Id changeRef) {
+  private static void addItemChangeRef(String itemId, Id changeRef,
+                                       Map<String, List<String>> itemChangeRefMap) {
     addItemVersion(itemChangeRefMap, itemId, changeRef);
   }
 
-  private static void addItemVersion(String itemId, Id versionId) {
+  private static void addItemVersion(String itemId, Id versionId,
+                                     Map<String, List<String>> itemVersionMap) {
     addItemVersion(itemVersionMap, itemId, versionId);
   }
 
@@ -137,5 +168,4 @@ public class ResetOldVersion {
 
     itemVersions.get(itemId).add(id.getValue());
   }
-}
-
+}
\ No newline at end of file