Merge "Enhanced List Level flow with backward support"
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / audit / AbstractAuditService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21
22 package org.onap.so.adapters.audit;
23
24 import java.util.Optional;
25 import org.camunda.bpm.client.task.ExternalTask;
26 import org.onap.logging.ref.slf4j.ONAPLogConstants;
27 import org.onap.so.objects.audit.AAIObjectAudit;
28 import org.onap.so.objects.audit.AAIObjectAuditList;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.slf4j.MDC;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.core.env.Environment;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public abstract class AbstractAuditService {
38
39     private static final Logger logger = LoggerFactory.getLogger(AbstractAuditService.class);
40
41
42
43     protected static final String UNABLE_TO_FIND_ALL_V_SERVERS_AND_L_INTERACES_IN_A_AI =
44             "Unable to find all VServers and L-Interaces in A&AI";
45
46     protected static final String UNABLE_TO_FIND_V_SERVERS_IN_OPENSTACK = "Unable to find VServers in Openstack";
47
48     @Autowired
49     public Environment env;
50
51     /**
52      * @param auditHeatStackFailed
53      * @param auditList
54      * @return
55      */
56     protected boolean didCreateAuditFail(Optional<AAIObjectAuditList> auditList) {
57         if (auditList.get().getAuditList() != null && !auditList.get().getAuditList().isEmpty()) {
58             if (logger.isInfoEnabled()) {
59                 logger.info("Audit Results: {}", auditList.get().toString());
60             }
61             return auditList.get().getAuditList().stream().filter(auditObject -> !auditObject.isDoesObjectExist())
62                     .findFirst().map(v -> true).orElse(false);
63         } else {
64             return false;
65         }
66     }
67
68     /**
69      * @param auditHeatStackFailed
70      * @param auditList
71      * @return
72      */
73     protected boolean didDeleteAuditFail(AAIObjectAuditList auditList) {
74         if (auditList.getAuditList() != null && !auditList.getAuditList().isEmpty()) {
75             if (logger.isInfoEnabled()) {
76                 logger.info("Audit Results: {}", auditList.toString());
77             }
78             return auditList.getAuditList().stream().filter(AAIObjectAudit::isDoesObjectExist).findFirst()
79                     .map(v -> true).orElse(false);
80         } else {
81             return false;
82         }
83     }
84
85     protected String[] getRetrySequence() {
86         return env.getProperty("mso.workflow.topics.retrySequence", String[].class);
87     }
88
89     protected void setupMDC(ExternalTask externalTask) {
90         logger.info(ONAPLogConstants.Markers.ENTRY, "Entering");
91         String msoRequestId = externalTask.getVariable("mso-request-id");
92         if (msoRequestId != null && !msoRequestId.isEmpty()) {
93             MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, msoRequestId);
94         }
95         MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, externalTask.getTopicName());
96     }
97
98     protected long calculateRetryDelay(int currentRetries) {
99         int retrySequence = getRetrySequence().length - currentRetries;
100         long retryMultiplier = Long.parseLong(env.getProperty("mso.workflow.topics.retryMultiplier", "6000"));
101         return Integer.parseInt(getRetrySequence()[retrySequence]) * retryMultiplier;
102     }
103 }