2427dd2734ef516f7dc97787b101dae14877dde5
[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  * 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.apihandlerinfra.tenantisolation;
22
23 import org.apache.http.HttpStatus;
24 import org.onap.so.apihandler.common.ErrorNumbers;
25 import org.onap.so.apihandlerinfra.exceptions.ApiException;
26 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
27 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
28 import org.onap.so.apihandlerinfra.tenantisolation.process.ActivateVnfOperationalEnvironment;
29 import org.onap.so.apihandlerinfra.tenantisolation.process.ActivateVnfStatusOperationalEnvironment;
30 import org.onap.so.apihandlerinfra.tenantisolation.process.CreateEcompOperationalEnvironment;
31 import org.onap.so.apihandlerinfra.tenantisolation.process.CreateVnfOperationalEnvironment;
32 import org.onap.so.apihandlerinfra.tenantisolation.process.DeactivateVnfOperationalEnvironment;
33 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
34 import org.onap.so.apihandlerinfra.tenantisolationbeans.OperationalEnvironment;
35 import org.onap.so.logger.MessageEnum;
36 import org.onap.so.logger.MsoLogger;
37 import org.onap.so.requestsdb.RequestsDBHelper;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.Scope;
40 import org.springframework.scheduling.annotation.Async;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 @Scope("prototype")
45 public class TenantIsolationRunnable {
46
47         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.APIH, TenantIsolationRunnable.class);
48         
49         @Autowired 
50         private RequestsDBHelper requestDb; 
51         @Autowired 
52         private CreateEcompOperationalEnvironment createEcompOpEnv;
53         @Autowired 
54         private CreateVnfOperationalEnvironment createVnfOpEnv;
55         @Autowired 
56         private ActivateVnfOperationalEnvironment activateVnfOpEnv;
57         @Autowired 
58         private DeactivateVnfOperationalEnvironment deactivateVnfOpEnv;
59         @Autowired 
60         private ActivateVnfStatusOperationalEnvironment activateVnfStatusOpEnv;
61         
62         @Async
63         public void run(Action action, String operationalEnvType, CloudOrchestrationRequest cor, String requestId) throws ApiException {
64                 msoLogger.debug ("Starting threadExecution in TenantIsolationRunnable for Action " + action.name() + " and OperationalEnvType: " + operationalEnvType);
65                 try {
66                         
67                         if(Action.create.equals(action)) {
68                                 if(OperationalEnvironment.ECOMP.name().equalsIgnoreCase(operationalEnvType)) {
69                                         createEcompOpEnv.execute(requestId, cor);
70                                 } else if(OperationalEnvironment.VNF.name().equalsIgnoreCase(operationalEnvType)) {
71                                         createVnfOpEnv.execute(requestId, cor);
72                                 } else {
73                     ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
74                     ValidateException validateException = new ValidateException.Builder("Invalid OperationalEnvironment Type specified for Create Action",
75                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
76
77                     throw validateException;
78                                 }
79                         } else if(Action.activate.equals(action)) {
80                                 activateVnfOpEnv.execute(requestId, cor);
81                         } else if(Action.deactivate.equals(action)) {
82                                 deactivateVnfOpEnv.execute(requestId, cor);
83                         } else if(Action.distributionStatus.equals(action)) {
84                                 activateVnfStatusOpEnv.execute(requestId, cor);
85                         } else {
86                 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
87                 ValidateException validateException = new ValidateException.Builder("Invalid Action specified: " + action,
88                         HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
89                 throw validateException;
90                         }
91                 }catch(ApiException e) {
92             requestDb.updateInfraFailureCompletion(e.getMessage(), requestId, cor.getOperationalEnvironmentId());
93             throw e;
94         }
95         }
96 }
97