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