Merge "Sonar fix in mso-adapter-utils"
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / audit / AuditStackService.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 package org.onap.so.adapters.audit;
22
23 import java.security.GeneralSecurityException;
24 import javax.annotation.PostConstruct;
25 import org.camunda.bpm.client.ExternalTaskClient;
26 import org.camunda.bpm.client.backoff.ExponentialBackoffStrategy;
27 import org.camunda.bpm.client.interceptor.ClientRequestInterceptor;
28 import org.camunda.bpm.client.interceptor.auth.BasicAuthProvider;
29 import org.onap.so.utils.CryptoUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.annotation.Profile;
34 import org.springframework.core.env.Environment;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 @Profile("!test")
39 public class AuditStackService {
40
41     private static final Logger logger = LoggerFactory.getLogger(AuditStackService.class);
42
43     private static final String DEFAULT_AUDIT_LOCK_TIME = "60000";
44
45     private static final String DEFAULT_MAX_CLIENTS_FOR_TOPIC = "10";
46
47     @Autowired
48     public Environment env;
49
50     @Autowired
51     private AuditCreateStackService auditCreateStack;
52
53     @Autowired
54     private AuditDeleteStackService auditDeleteStack;
55
56     @Autowired
57     private AuditQueryStackService auditQueryStack;
58
59     @PostConstruct
60     public void auditAddAAIInventory() {
61         for (int i = 0; i < getMaxClients(); i++) {
62             ExternalTaskClient client = createExternalTaskClient();
63             client.subscribe("InventoryAddAudit")
64                     .lockDuration(Long.parseLong(env.getProperty("mso.audit.lock-time", DEFAULT_AUDIT_LOCK_TIME)))
65                     .handler(auditCreateStack::executeExternalTask).open();
66         }
67     }
68
69     @PostConstruct
70     public void auditDeleteAAIInventory() {
71         for (int i = 0; i < getMaxClients(); i++) {
72             ExternalTaskClient client = createExternalTaskClient();
73             client.subscribe("InventoryDeleteAudit")
74                     .lockDuration(Long.parseLong(env.getProperty("mso.audit.lock-time", DEFAULT_AUDIT_LOCK_TIME)))
75                     .handler(auditDeleteStack::executeExternalTask).open();
76         }
77     }
78
79     @PostConstruct
80     public void auditQueryInventory() {
81         for (int i = 0; i < getMaxClients(); i++) {
82             ExternalTaskClient client = createExternalTaskClient();
83             client.subscribe("InventoryQueryAudit")
84                     .lockDuration(Long.parseLong(env.getProperty("mso.audit.lock-time", DEFAULT_AUDIT_LOCK_TIME)))
85                     .handler(auditQueryStack::executeExternalTask).open();
86         }
87     }
88
89     protected ExternalTaskClient createExternalTaskClient() {
90         ClientRequestInterceptor interceptor = createClientRequestInterceptor();
91         return ExternalTaskClient.create().baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1)
92                 .addInterceptor(interceptor).asyncResponseTimeout(120000)
93                 .backoffStrategy(new ExponentialBackoffStrategy(0, 0, 0)).build();
94     }
95
96     protected ClientRequestInterceptor createClientRequestInterceptor() {
97         String auth = "";
98         try {
99             auth = CryptoUtils.decrypt(env.getRequiredProperty("mso.auth"), env.getRequiredProperty("mso.msoKey"));
100         } catch (IllegalStateException | GeneralSecurityException e) {
101             logger.error("Error Decrypting Password", e);
102         }
103         return new BasicAuthProvider(env.getRequiredProperty("mso.config.cadi.aafId"), auth);
104     }
105
106     protected int getMaxClients() {
107         return Integer.parseInt(env.getProperty("workflow.topics.maxClients", DEFAULT_MAX_CLIENTS_FOR_TOPIC));
108     }
109
110
111 }