Switched from Dropwizard to Springboot
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / CorrelationUtil.java
index d0a4160..51b4cf0 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright 2017 ZTE Corporation.
+ * Copyright 2017-2021 ZTE Corporation.
  *
  * 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
  */
 package org.onap.holmes.common.aai;
 
-import java.util.List;
-import java.util.Optional;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
 import org.onap.holmes.common.aai.entity.VmEntity;
 import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.SpringContextUtil;
+
+import java.util.List;
+import java.util.Optional;
 
 @Slf4j
 public class CorrelationUtil {
 
-    private AaiQuery aaiQuery;
+    private static AaiQuery aaiQuery;
 
     private static class LazyHolder {
         private static final CorrelationUtil INSTANCE = new CorrelationUtil();
@@ -31,33 +33,35 @@ public class CorrelationUtil {
     private CorrelationUtil (){}
 
     public static final CorrelationUtil getInstance() {
+        if (aaiQuery == null) {
+            aaiQuery = SpringContextUtil.getBean(AaiQuery.class);
+        }
         return LazyHolder.INSTANCE;
     }
 
-    public boolean isTopologicallyRelated(String childId, String rootId) {
-
-        return Optional.ofNullable(getVmEntity(rootId)).map(vmEntity ->
-                getIsRelated(childId, vmEntity)).orElse(false);
+    public boolean isTopologicallyRelated(String sourceId, String rootSourceId, String rootSourceName) {
+        return Optional.ofNullable(getVmEntity(rootSourceId, rootSourceName)).map(vmEntity ->
+                getIsRelated(sourceId, vmEntity)).orElse(false);
     }
 
-    private boolean getIsRelated(String childId, VmEntity vmEntity) {
+    private boolean getIsRelated(String sourceId, VmEntity vmEntity) {
         List<Relationship> relationships = vmEntity.getRelationshipList().getRelationships();
         for (Relationship relationship : relationships) {
             boolean isRelated = relationship.getRelationshipDataList().stream().anyMatch(
-                    relationshipData -> relationshipData.getRelationshipValue().equals(childId));
+                    relationshipData -> relationshipData.getRelationshipValue().equals(sourceId));
             if (isRelated) {
                 return true;
             }
         }
-        return false;
+        return  false;
     }
 
-    private VmEntity getVmEntity(String rootId) {
+    private VmEntity getVmEntity(String rootSourceId, String rootSourceName) {
         VmEntity vmEntity = null;
         try {
-            vmEntity = aaiQuery.getAaiVmData("", rootId);
+            vmEntity = aaiQuery.getAaiVmData(rootSourceId, rootSourceName);
         } catch (CorrelationException e) {
-            log.error("Failed to get vm data", e.getMessage());
+            log.error("Failed to get the VM data.", e);
         }
         return vmEntity;
     }