00ce137ee369ac17e25f9ea0bc76beb1df556e11
[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.so.logger.MessageEnum;
38 import org.onap.so.logger.MsoLogger;
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) throws ApiException {
68
69                 logger.debug("Starting threadExecution in TenantIsolationRunnable for Action {} and OperationalEnvType: {}",
70                         action.name(), operationalEnvType);
71                 try {
72                         
73                         if(Action.create.equals(action)) {
74                                 if(OperationalEnvironment.ECOMP.name().equalsIgnoreCase(operationalEnvType)) {
75                                         createEcompOpEnv.execute(requestId, cor);
76                                 } else if(OperationalEnvironment.VNF.name().equalsIgnoreCase(operationalEnvType)) {
77                                         createVnfOpEnv.execute(requestId, cor);
78                                 } else {
79                     ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
80                     ValidateException validateException = new ValidateException.Builder("Invalid OperationalEnvironment Type specified for Create Action",
81                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
82                                                                                         .build();
83
84                     throw validateException;
85                                 }
86                         } else if(Action.activate.equals(action)) {
87                                 activateVnfOpEnv.execute(requestId, cor);
88                         } else if(Action.deactivate.equals(action)) {
89                                 deactivateVnfOpEnv.execute(requestId, cor);
90                         } else if(Action.distributionStatus.equals(action)) {
91                                 activateVnfStatusOpEnv.execute(requestId, cor);
92                         } else {
93                 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
94                 ValidateException validateException = new ValidateException.Builder("Invalid Action specified: " + action,
95                         HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
96                 throw validateException;
97                         }
98                 }catch(ApiException e) {
99             requestDb.updateInfraFailureCompletion(e.getMessage(), requestId, cor.getOperationalEnvironmentId());
100             throw e;
101         }
102         }
103 }
104