57d31bf3085c8b5fd0cf33384b49f5fe05faf4a1
[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     @Autowired
44     public Environment env;
45
46     @Autowired
47     private AuditCreateStackService auditCreateStack;
48
49     @Autowired
50     private AuditDeleteStackService auditDeleteStack;
51
52     @PostConstruct
53     public void auditAddAAIInventory() throws Exception {
54         for (int i = 0; i < getMaxClients(); i++) {
55             ExternalTaskClient client = createExternalTaskClient();
56             client.subscribe("InventoryAddAudit").lockDuration(60000).handler(auditCreateStack::executeExternalTask)
57                     .open();
58         }
59     }
60
61     @PostConstruct
62     public void auditDeleteAAIInventory() throws Exception {
63         for (int i = 0; i < getMaxClients(); i++) {
64             ExternalTaskClient client = createExternalTaskClient();
65             client.subscribe("InventoryDeleteAudit").lockDuration(60000).handler(auditDeleteStack::executeExternalTask)
66                     .open();
67         }
68     }
69
70     protected ExternalTaskClient createExternalTaskClient() throws Exception {
71         ClientRequestInterceptor interceptor = createClientRequestInterceptor();
72         ExternalTaskClient client = ExternalTaskClient.create()
73                 .baseUrl(env.getRequiredProperty("mso.workflow.endpoint")).maxTasks(1).addInterceptor(interceptor)
74                 .asyncResponseTimeout(120000).backoffStrategy(new ExponentialBackoffStrategy(10000, 2, 120000)).build();
75         return client;
76     }
77
78     protected ClientRequestInterceptor createClientRequestInterceptor() {
79         String auth = "";
80         try {
81             auth = CryptoUtils.decrypt(env.getRequiredProperty("mso.auth"), env.getRequiredProperty("mso.msoKey"));
82         } catch (IllegalStateException | GeneralSecurityException e) {
83             logger.error("Error Decrypting Password", e);
84         }
85         ClientRequestInterceptor interceptor =
86                 new BasicAuthProvider(env.getRequiredProperty("mso.config.cadi.aafId"), auth);
87         return interceptor;
88     }
89
90     protected int getMaxClients() {
91         return Integer.parseInt(env.getProperty("workflow.topics.maxClients", "10"));
92     }
93
94
95 }