create custom spring aop annotation for logging
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / tenantisolation / TenantIsolationRunnable.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.apihandlerinfra.tenantisolation;
24
25 import org.apache.http.HttpStatus;
26 import org.onap.so.apihandler.common.ErrorNumbers;
27 import org.onap.so.apihandlerinfra.exceptions.ApiException;
28 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
29 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
30 import org.onap.so.apihandlerinfra.tenantisolation.process.ActivateVnfOperationalEnvironment;
31 import org.onap.so.apihandlerinfra.tenantisolation.process.ActivateVnfStatusOperationalEnvironment;
32 import org.onap.so.apihandlerinfra.tenantisolation.process.CreateEcompOperationalEnvironment;
33 import org.onap.so.apihandlerinfra.tenantisolation.process.CreateVnfOperationalEnvironment;
34 import org.onap.so.apihandlerinfra.tenantisolation.process.DeactivateVnfOperationalEnvironment;
35 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
36 import org.onap.so.apihandlerinfra.tenantisolationbeans.OperationalEnvironment;
37 import org.onap.logging.filter.base.ErrorCode;
38 import org.onap.so.logger.MessageEnum;
39 import org.onap.so.requestsdb.RequestsDBHelper;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.Scope;
44 import org.springframework.scheduling.annotation.Async;
45 import org.springframework.stereotype.Component;
46
47 @Component
48 @Scope("prototype")
49 public class TenantIsolationRunnable {
50
51     private static final Logger logger = LoggerFactory.getLogger(TenantIsolationRunnable.class);
52
53     @Autowired
54     private RequestsDBHelper requestDb;
55     @Autowired
56     private CreateEcompOperationalEnvironment createEcompOpEnv;
57     @Autowired
58     private CreateVnfOperationalEnvironment createVnfOpEnv;
59     @Autowired
60     private ActivateVnfOperationalEnvironment activateVnfOpEnv;
61     @Autowired
62     private DeactivateVnfOperationalEnvironment deactivateVnfOpEnv;
63     @Autowired
64     private ActivateVnfStatusOperationalEnvironment activateVnfStatusOpEnv;
65
66     @Async
67     public void run(Action action, String operationalEnvType, CloudOrchestrationRequest cor, String requestId)
68             throws ApiException {
69
70         logger.debug("Starting threadExecution in TenantIsolationRunnable for Action {} and OperationalEnvType: {}",
71                 action.name(), operationalEnvType);
72         try {
73
74             if (Action.create.equals(action)) {
75                 if (OperationalEnvironment.ECOMP.name().equalsIgnoreCase(operationalEnvType)) {
76                     createEcompOpEnv.execute(requestId, cor);
77                 } else if (OperationalEnvironment.VNF.name().equalsIgnoreCase(operationalEnvType)) {
78                     createVnfOpEnv.execute(requestId, cor);
79                 } else {
80                     ErrorLoggerInfo errorLoggerInfo =
81                             new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError)
82                                     .build();
83                     ValidateException validateException = new ValidateException.Builder(
84                             "Invalid OperationalEnvironment Type specified for Create Action",
85                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
86                                     .build();
87
88                     throw validateException;
89                 }
90             } else if (Action.activate.equals(action)) {
91                 activateVnfOpEnv.execute(requestId, cor);
92             } else if (Action.deactivate.equals(action)) {
93                 deactivateVnfOpEnv.execute(requestId, cor);
94             } else if (Action.distributionStatus.equals(action)) {
95                 activateVnfStatusOpEnv.execute(requestId, cor);
96             } else {
97                 ErrorLoggerInfo errorLoggerInfo =
98                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
99                 ValidateException validateException =
100                         new ValidateException.Builder("Invalid Action specified: " + action, HttpStatus.SC_BAD_REQUEST,
101                                 ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
102                 throw validateException;
103             }
104         } catch (ApiException e) {
105             requestDb.updateInfraFailureCompletion(e.getMessage(), requestId, cor.getOperationalEnvironmentId());
106             throw e;
107         }
108     }
109 }
110