200f6375ea4acf9b76afef973aace16597d73a7c
[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     private static final String AUDIT_STACK_DATA = "AuditStackData";
21
22     @Autowired
23     private RequestsDbClient requestsDbClient;
24
25     /**
26      * Checks to see if an entry already exist for the given heat stack and writes audit stack data to the request
27      * database if it doesn't.
28      *
29      * @throws JsonProcessingException
30      */
31     public void writeStackDataToRequestDb(AuditInventory auditInventory, AAIObjectAuditList auditList)
32             throws JsonProcessingException {
33         List<RequestProcessingData> requestProcessingDataList =
34                 requestsDbClient.getRequestProcessingDataByGroupingIdAndNameAndTag(auditInventory.getVfModuleId(),
35                         auditInventory.getHeatStackName(), AUDIT_STACK_DATA);
36         if (requestProcessingDataList.isEmpty()) {
37             GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
38             String auditListString = objectMapper.getMapper().writeValueAsString(auditList);;
39
40             RequestProcessingData requestProcessingData = new RequestProcessingData();
41             requestProcessingData.setSoRequestId(auditInventory.getMsoRequestId());
42             requestProcessingData.setGroupingId(auditInventory.getVfModuleId());
43             requestProcessingData.setName(auditInventory.getHeatStackName());
44             requestProcessingData.setTag(AUDIT_STACK_DATA);
45             requestProcessingData.setValue(auditListString);
46
47             requestsDbClient.saveRequestProcessingData(requestProcessingData);
48         }
49     }
50
51     /**
52      * Retrieves audit stack data from the request database.
53      *
54      * @throws IOException
55      * @throws JsonMappingException
56      * @throws JsonParseException
57      */
58     public Optional<AAIObjectAuditList> getStackDataFromRequestDb(AuditInventory auditInventory) throws IOException {
59
60         List<RequestProcessingData> requestProcessingDataList =
61                 requestsDbClient.getRequestProcessingDataByGroupingIdAndNameAndTag(auditInventory.getVfModuleId(),
62                         auditInventory.getHeatStackName(), AUDIT_STACK_DATA);
63         if (!requestProcessingDataList.isEmpty()) {
64             RequestProcessingData requestProcessingData = requestProcessingDataList.get(0);
65             String auditListString = requestProcessingData.getValue();
66
67             GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
68             AAIObjectAuditList auditList =
69                     objectMapper.getMapper().readValue(auditListString, AAIObjectAuditList.class);
70
71             return Optional.of(auditList);
72         } else {
73             return Optional.empty();
74         }
75     }
76
77
78 }