fix - when retrieve topology we are using threadPool and the MDC values are not updated
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / AaiServiceImpl.java
index 961f110..b3ac168 100644 (file)
 
 package org.onap.vid.services;
 
+import static org.onap.vid.aai.AaiClient.QUERY_FORMAT_RESOURCE;
+import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
+
 import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import io.joshworks.restclient.http.HttpResponse;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ExecutorService;
+import java.util.stream.Collectors;
+import javax.validation.constraints.NotNull;
+import javax.ws.rs.core.Response;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpStatus;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
-import org.onap.vid.aai.*;
-import org.onap.vid.aai.model.*;
+import org.onap.vid.aai.AaiClientInterface;
+import org.onap.vid.aai.AaiGetVnfResponse;
+import org.onap.vid.aai.AaiResponse;
+import org.onap.vid.aai.AaiResponseTranslator;
+import org.onap.vid.aai.ExceptionWithRequestInfo;
+import org.onap.vid.aai.ServiceInstance;
+import org.onap.vid.aai.ServiceInstancesSearchResults;
+import org.onap.vid.aai.ServiceSubscription;
+import org.onap.vid.aai.ServiceSubscriptions;
+import org.onap.vid.aai.Services;
+import org.onap.vid.aai.SubscriberFilteredResults;
+import org.onap.vid.aai.model.AaiGetInstanceGroupsByCloudRegion;
 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.AaiGetRelatedInstanceGroupsByVnfId;
 import org.onap.vid.aai.model.AaiGetOperationalEnvironments.OperationalEnvironmentList;
 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
 import org.onap.vid.aai.model.AaiGetServicesRequestModel.GetServicesAAIRespone;
-import org.onap.vid.aai.model.Properties;
 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
+import org.onap.vid.aai.model.AaiRelationResponse;
+import org.onap.vid.aai.model.GetServiceModelsByDistributionStatusResponse;
+import org.onap.vid.aai.model.InstanceGroupInfo;
+import org.onap.vid.aai.model.LogicalLinkResponse;
+import org.onap.vid.aai.model.Model;
+import org.onap.vid.aai.model.ModelVer;
+import org.onap.vid.aai.model.OwningEntity;
+import org.onap.vid.aai.model.OwningEntityResponse;
+import org.onap.vid.aai.model.PortDetailsTranslator;
+import org.onap.vid.aai.model.Project;
+import org.onap.vid.aai.model.ProjectResponse;
+import org.onap.vid.aai.model.Properties;
+import org.onap.vid.aai.model.RelatedToProperty;
+import org.onap.vid.aai.model.Relationship;
+import org.onap.vid.aai.model.RelationshipData;
+import org.onap.vid.aai.model.RelationshipList;
+import org.onap.vid.aai.model.Result;
+import org.onap.vid.aai.model.ServiceRelationships;
 import org.onap.vid.asdc.beans.Service;
 import org.onap.vid.exceptions.GenericUncheckedException;
 import org.onap.vid.model.ServiceInstanceSearchResult;
 import org.onap.vid.model.SubscriberList;
 import org.onap.vid.model.aaiTree.AAITreeNode;
+import org.onap.vid.model.aaiTree.Network;
 import org.onap.vid.model.aaiTree.NodeType;
 import org.onap.vid.model.aaiTree.RelatedVnf;
+import org.onap.vid.model.aaiTree.VpnBinding;
+import org.onap.vid.model.aaiTree.VpnBindingKt;
 import org.onap.vid.roles.RoleValidator;
 import org.onap.vid.utils.Intersection;
+import org.onap.vid.utils.Logging;
 import org.onap.vid.utils.Tree;
+import org.slf4j.MDC;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpMethod;
 
-import javax.ws.rs.core.Response;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.util.*;
-import java.util.concurrent.ExecutorService;
-import java.util.stream.Collectors;
-
-/**
- * Created by Oren on 7/4/17.
- */
 public class AaiServiceImpl implements AaiService {
     private static final String SERVICE_INSTANCE_ID = "service-instance.service-instance-id";
     private static final String SERVICE_TYPE = "service-subscription.service-type";
@@ -68,11 +103,10 @@ public class AaiServiceImpl implements AaiService {
     private int indexOfSubscriberName = 6;
 
     private AaiClientInterface aaiClient;
-    private AaiOverTLSClientInterface aaiOverTLSClient;
     private AaiResponseTranslator aaiResponseTranslator;
-    private AAITreeNodeBuilder aaiTreeNode;
     private AAIServiceTree aaiServiceTree;
     private ExecutorService executorService;
+    private final Logging logging;
 
 
     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(AaiServiceImpl.class);
@@ -80,18 +114,15 @@ public class AaiServiceImpl implements AaiService {
     @Autowired
     public AaiServiceImpl(
         AaiClientInterface aaiClient,
-        AaiOverTLSClientInterface aaiOverTLSClient,
         AaiResponseTranslator aaiResponseTranslator,
-        AAITreeNodeBuilder aaiTreeNode,
         AAIServiceTree aaiServiceTree,
-        ExecutorService executorService)
+        ExecutorService executorService, Logging logging)
     {
         this.aaiClient = aaiClient;
-        this.aaiOverTLSClient = aaiOverTLSClient;
         this.aaiResponseTranslator = aaiResponseTranslator;
-        this.aaiTreeNode = aaiTreeNode;
         this.aaiServiceTree = aaiServiceTree;
         this.executorService = executorService;
+        this.logging = logging;
     }
 
     private List<Service> convertModelToService(Model model) {
@@ -109,7 +140,9 @@ public class AaiServiceImpl implements AaiService {
                         .setToscaModelURL(null)
                         .setLifecycleState(null)
                         .setArtifacts(null)
-                        .setResources(null).build();
+                        .setResources(null)
+                        .setOrchestrationType(modelVer.getOrchestrationType())
+                        .build();
 
 
 
@@ -122,6 +155,10 @@ public class AaiServiceImpl implements AaiService {
         return services;
     }
 
+    private boolean hasData(AaiResponse<?> aaiResponse) {
+        return aaiResponse != null && aaiResponse.getT() != null;
+    }
+
     private boolean validateModel(Model model){
         if (model == null) {
             return false;
@@ -208,13 +245,11 @@ public class AaiServiceImpl implements AaiService {
 
     @Override
     public SubscriberFilteredResults getFullSubscriberList(RoleValidator roleValidator) {
-        HttpResponse<SubscriberList> allSubscribers = aaiOverTLSClient.getAllSubscribers();
-        return new SubscriberFilteredResults(
-            roleValidator,
-            allSubscribers.getBody(),
-            allSubscribers.getStatusText(),
-            allSubscribers.getStatus()
-        );
+        AaiResponse<SubscriberList> subscriberResponse = aaiClient.getAllSubscribers();
+
+        return new SubscriberFilteredResults(roleValidator, subscriberResponse.getT(),
+                subscriberResponse.getErrorMessage(),
+                subscriberResponse.getHttpCode());
     }
 
     @Override
@@ -223,8 +258,8 @@ public class AaiServiceImpl implements AaiService {
     }
 
     @Override
-    public HttpResponse<SubscriberList> getFullSubscriberList() {
-        return aaiOverTLSClient.getAllSubscribers();
+    public AaiResponse<SubscriberList> getFullSubscriberList() {
+        return aaiClient.getAllSubscribers();
     }
 
     @Override
@@ -362,24 +397,7 @@ public class AaiServiceImpl implements AaiService {
     @Override
     public AaiResponse<AaiGetVnfResponse> getVNFData(String globalSubscriberId, String serviceType) {
         AaiResponse<AaiGetVnfResponse> response = aaiClient.getVNFData(globalSubscriberId, serviceType);
-        return filterChangeManagementVNFCandidatesResponse(response);
-    }
-
-    private AaiResponse<AaiGetVnfResponse> filterChangeManagementVNFCandidatesResponse(AaiResponse<AaiGetVnfResponse> response) {
-
-
-        if (response != null && response.getT() != null) {
-            response.getT().results =
-                    response.getT().results.stream()
-                            .filter(result -> (
-                                    result.nodeType.equalsIgnoreCase("generic-vnf") ||
-                                            result.nodeType.equalsIgnoreCase("service-instance")))
-                            .collect(Collectors.toList());
-
-            return response;
-        }
-
-        return new AaiResponse<>();
+        return hasData(response) ? response : new AaiResponse<>();
     }
 
     @Override
@@ -509,10 +527,12 @@ public class AaiServiceImpl implements AaiService {
                 .map(RelatedVnf::from)
                 .collect(Collectors.toList());
 
+        final Map<String, String> copyOfParentMDC = MDC.getCopyOfContextMap();
+
         try {
             return executorService.submit(() ->
                     convertedVnfs.parallelStream()
-                            .map(this::enrichRelatedVnfWithCloudRegionAndTenant)
+                            .map(logging.withMDC(copyOfParentMDC, this::enrichRelatedVnfWithCloudRegionAndTenant))
                             .collect(Collectors.toList())
             ).get();
         } catch (Exception e) {
@@ -521,7 +541,6 @@ public class AaiServiceImpl implements AaiService {
         }
     }
 
-
     private List<AAITreeNode> filterByInstanceGroupRoleAndType(List<AAITreeNode> aaiTree, String groupRole, String groupType) {
 
         return aaiTree.stream()
@@ -554,6 +573,7 @@ public class AaiServiceImpl implements AaiService {
 
         return vnf;
     }
+
     private List<InstanceGroupInfo> convertGetInstanceGroupsResponseToSimpleResponse(AaiGetRelatedInstanceGroupsByVnfId response) {
         List<InstanceGroupInfo> instanceGroupInfoList = new ArrayList<>();
         for(org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship relationship: response.getRelationshipList().getRelationship()){
@@ -628,14 +648,57 @@ public class AaiServiceImpl implements AaiService {
     }
 
     public String getAAIServiceTree(String globalCustomerId, String serviceType, String serviceInstanceId) {
-        ObjectMapper om = new ObjectMapper();
         String result;
         try {
             org.onap.vid.model.aaiTree.ServiceInstance tree = aaiServiceTree.getServiceInstanceTopology(globalCustomerId, serviceType, serviceInstanceId);
-            result = om.writeValueAsString(tree);
+            result = JACKSON_OBJECT_MAPPER.writeValueAsString(tree);
         } catch (Exception e) {
             throw new GenericUncheckedException(e);
         }
         return result;
     }
+
+    @Override
+    public List<VpnBinding> getVpnListByVpnType(String vpnType) {
+        String path = "network/vpn-bindings?vpn-type=" + vpnType;
+
+        try {
+            List<AAITreeNode> aaiTree = aaiServiceTree.buildAAITreeForUniqueResource(path, NodeType.VPN_BINDING);
+            return aaiTree.stream().map(VpnBindingKt::from).collect(Collectors.toList());
+        } catch (ExceptionWithRequestInfo exception) {
+            if (Objects.equals(404, exception.getHttpCode())) {
+                return Collections.emptyList();
+            }
+            throw exception;
+        }
+
+    }
+
+    @Override
+    public List<Network> getL3NetworksByCloudRegion(String cloudRegionId, String tenantId, String networkRole) {
+        String payload = buildPayloadForL3NetworksByCloudRegion(cloudRegionId, tenantId, networkRole);
+
+        try {
+            List<AAITreeNode> aaiTree = aaiServiceTree.buildAAITreeForUniqueResourceFromCustomQuery(QUERY_FORMAT_RESOURCE, payload, HttpMethod.PUT, NodeType.NETWORK);
+            return aaiTree.stream().map(Network::from).collect(Collectors.toList());
+        } catch (ExceptionWithRequestInfo exception) {
+            if (Objects.equals(404, exception.getHttpCode())) {
+                return Collections.emptyList();
+            }
+            throw exception;
+        }
+    }
+
+    @NotNull
+    protected String buildPayloadForL3NetworksByCloudRegion(String cloudRegionId, String tenantId, String networkRole) {
+        String networkRolePart = StringUtils.isEmpty(networkRole) ? "" : "&networkRole=" + networkRole;
+        String cloudOwner = aaiClient.getCloudOwnerByCloudRegionId(cloudRegionId);
+        return "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/" + cloudOwner + "/" + cloudRegionId + "\"," +
+                "\"query\":\"query/l3-networks-by-cloud-region?tenantId=" + tenantId + networkRolePart + "\"}";
+    }
+
+    @Override
+    public ModelVer getNewestModelVersionByInvariantId(String modelInvariantId){
+        return aaiClient.getLatestVersionByInvariantId(modelInvariantId);
+    }
 }