4e8443bf45c2e4be522d5d30c5b53f398d722aef
[so.git] /
1 package org.onap.so.adapters.tasks.audit;
2
3 import java.io.IOException;
4 import java.util.List;
5 import java.util.Optional;
6 import org.onap.so.audit.beans.AuditInventory;
7 import org.onap.aaiclient.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;
16
17 @Component
18 public class AuditDataService {
19
20     @Autowired
21     private RequestsDbClient requestsDbClient;
22
23     /**
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.
26      *
27      * @throws JsonProcessingException
28      */
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);;
37
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);
44
45             requestsDbClient.saveRequestProcessingData(requestProcessingData);
46         }
47     }
48
49     /**
50      * Retrieves audit stack data from the request database.
51      *
52      * @throws IOException
53      * @throws JsonMappingException
54      * @throws JsonParseException
55      */
56     public Optional<AAIObjectAuditList> getStackDataFromRequestDb(AuditInventory auditInventory)
57             throws JsonParseException, JsonMappingException, IOException {
58
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();
65
66             GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
67             AAIObjectAuditList auditList =
68                     objectMapper.getMapper().readValue(auditListString, AAIObjectAuditList.class);
69
70             return Optional.of(auditList);
71         } else {
72             return Optional.empty();
73         }
74     }
75
76
77 }