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