1 package org.onap.so.adapters.tasks.audit;
 
   3 import java.io.IOException;
 
   5 import java.util.Optional;
 
   6 import org.onap.so.audit.beans.AuditInventory;
 
   7 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
 
   8 import org.onap.so.db.request.beans.RequestProcessingData;
 
   9 import org.onap.so.db.request.client.RequestsDbClient;
 
  10 import org.onap.so.objects.audit.AAIObjectAuditList;
 
  11 import org.springframework.beans.factory.annotation.Autowired;
 
  12 import org.springframework.stereotype.Component;
 
  13 import com.fasterxml.jackson.core.JsonParseException;
 
  14 import com.fasterxml.jackson.core.JsonProcessingException;
 
  15 import com.fasterxml.jackson.databind.JsonMappingException;
 
  18 public class AuditDataService {
 
  21     private RequestsDbClient requestsDbClient;
 
  24      * Checks to see if an entry already exist for the given heat stack and writes audit stack data to the request
 
  25      * database if it doesn't.
 
  27      * @throws JsonProcessingException
 
  29     public void writeStackDataToRequestDb(AuditInventory auditInventory, AAIObjectAuditList auditList)
 
  30             throws JsonProcessingException {
 
  31         List<RequestProcessingData> requestProcessingDataList =
 
  32                 requestsDbClient.getRequestProcessingDataByGroupingIdAndNameAndTag(auditInventory.getVfModuleId(),
 
  33                         auditInventory.getHeatStackName(), "AuditStackData");
 
  34         if (requestProcessingDataList.isEmpty()) {
 
  35             GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
 
  36             String auditListString = objectMapper.getMapper().writeValueAsString(auditList);;
 
  38             RequestProcessingData requestProcessingData = new RequestProcessingData();
 
  39             requestProcessingData.setSoRequestId(auditInventory.getMsoRequestId());
 
  40             requestProcessingData.setGroupingId(auditInventory.getVfModuleId());
 
  41             requestProcessingData.setName(auditInventory.getHeatStackName());
 
  42             requestProcessingData.setTag("AuditStackData");
 
  43             requestProcessingData.setValue(auditListString);
 
  45             requestsDbClient.saveRequestProcessingData(requestProcessingData);
 
  50      * Retrieves audit stack data from the request database.
 
  53      * @throws JsonMappingException
 
  54      * @throws JsonParseException
 
  56     public Optional<AAIObjectAuditList> getStackDataFromRequestDb(AuditInventory auditInventory)
 
  57             throws JsonParseException, JsonMappingException, IOException {
 
  59         List<RequestProcessingData> requestProcessingDataList =
 
  60                 requestsDbClient.getRequestProcessingDataByGroupingIdAndNameAndTag(auditInventory.getVfModuleId(),
 
  61                         auditInventory.getHeatStackName(), "AuditStackData");
 
  62         if (!requestProcessingDataList.isEmpty()) {
 
  63             RequestProcessingData requestProcessingData = requestProcessingDataList.get(0);
 
  64             String auditListString = requestProcessingData.getValue();
 
  66             GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
 
  67             AAIObjectAuditList auditList =
 
  68                     objectMapper.getMapper().readValue(auditListString, AAIObjectAuditList.class);
 
  70             return Optional.of(auditList);
 
  72             return Optional.empty();