Containerization feature of SO
[so.git] / mso-api-handlers / mso-requests-db / src / main / java / org / onap / so / requestsdb / RequestsDBHelper.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.requestsdb;
22
23 import java.sql.Timestamp;
24
25 import org.onap.so.db.request.beans.InfraActiveRequests;
26 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
27 import org.onap.so.logger.MsoLogger;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Component;
30
31 @Component
32 public class RequestsDBHelper {
33                 
34         private static final String UNKNOWN = "UNKNOWN";
35         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, RequestsDBHelper.class);
36         private String className = this.getClass().getSimpleName() +" class\'s ";
37         private String methodName = "";
38         private String classMethodMessage = "";
39         @Autowired
40         private InfraActiveRequestsRepository infraActiveRequestsRepository;
41         /**
42          * This util method is to update the InfraRequest table to Complete
43          * @param msg - string, unique message for each caller
44          * @param requestId - string
45          * @param operationalEnvironmentId - string   
46          * @return void - nothing 
47          * @throws Exception 
48          */     
49         public void updateInfraSuccessCompletion(String msg, String requestId, String operationalEnvironmentId) {
50                 methodName = "updateInfraSuccessCompletion() method.";
51                 classMethodMessage = className + " " + methodName;
52                 msoLogger.debug("Begin of " + classMethodMessage);
53                         
54                 InfraActiveRequests request = infraActiveRequestsRepository.findOneByRequestId(requestId);
55         
56                 request.setRequestStatus("COMPLETE");
57                 request.setStatusMessage("SUCCESSFUL, operationalEnvironmentId - " + operationalEnvironmentId + "; Success Message: " + msg);
58                 request.setProgress(100L);
59                 request.setLastModifiedBy("APIH");
60                 request.setOperationalEnvId(operationalEnvironmentId);
61                 if(request.getAction() == null){
62                         request.setRequestAction(UNKNOWN);
63                 }
64                 if(request.getRequestScope() == null){
65                         request.setRequestScope(UNKNOWN);
66                 }
67                 Timestamp endTimeStamp = new Timestamp(System.currentTimeMillis());
68         request.setEndTime(endTimeStamp);
69                 infraActiveRequestsRepository.save(request);
70                 
71                 msoLogger.debug("End of " + classMethodMessage);
72                 
73         }
74         
75         /**
76          * This util method is to update the InfraRequest table to Failure
77          * @param msg - string, unique message for each caller
78          * @param requestId - string
79          * @param operationalEnvironmentId - string   
80          * @return void - nothing 
81          * @throws Exception 
82          */     
83         public void updateInfraFailureCompletion(String msg, String requestId, String operationalEnvironmentId) {
84                 methodName = "updateInfraFailureCompletion() method.";
85                 classMethodMessage = className + " " + methodName;
86                 msoLogger.debug("Begin of " + classMethodMessage);
87                 
88                 InfraActiveRequests request = infraActiveRequestsRepository.findOneByRequestId(requestId);
89                 request.setRequestStatus("FAILED");
90                 request.setStatusMessage("FAILURE, operationalEnvironmentId - " + operationalEnvironmentId + "; Error message: " + msg);
91                 request.setProgress(100L);
92                 request.setLastModifiedBy("APIH");
93                 request.setOperationalEnvId(operationalEnvironmentId);
94                 if(request.getAction() == null){
95                         request.setRequestAction(UNKNOWN);
96                 }
97                 if(request.getRequestScope() == null){
98                         request.setRequestScope(UNKNOWN);
99                 }
100                 Timestamp endTimeStamp = new Timestamp(System.currentTimeMillis());
101         request.setEndTime(endTimeStamp);
102                 infraActiveRequestsRepository.save(request);
103                 
104                 msoLogger.debug("End of " + classMethodMessage);
105                 
106         }       
107 }