cbedefe7eb5241d36ca431a86ad14e2011e186f1
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / dmaapproperties / DmaapPropertiesClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.client.dmaapproperties;
24
25 import javax.inject.Provider;
26
27 import org.onap.so.client.avpn.dmaap.beans.AVPNDmaapBean;
28 import org.onap.so.client.avpn.dmaap.beans.AsyncRequestStatus;
29 import org.onap.so.client.avpn.dmaap.beans.InstanceReferences;
30 import org.onap.so.client.avpn.dmaap.beans.RequestStatus;
31 import org.onap.so.client.exception.MapperException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 @Component
41 public class DmaapPropertiesClient {
42
43         private static final Logger logger = LoggerFactory.getLogger(DmaapPropertiesClient.class);
44
45         @Autowired
46         private Provider<GlobalDmaapPublisher> dmaapPublisher;
47
48         protected AVPNDmaapBean buildRequestJson(String requestId, String clientSource, String correlator, String serviceInstanceId, String startTime, String finishTime,
49                                                                                          String requestScope, String requestType, String timestamp, String requestState, String statusMessage, String percentProgress, Boolean wasRolledBack) {
50
51                 RequestStatus requestStatus = buildRequestStatus(timestamp, requestState, statusMessage, percentProgress, wasRolledBack);
52
53                 InstanceReferences instanceReferences = buildInstanceReferences(serviceInstanceId);
54
55                 AsyncRequestStatus asyncRequestStatus = buildAsyncRequestStatus(requestId, clientSource, correlator, startTime, finishTime,
56                                 requestScope, requestType, requestStatus, instanceReferences);
57
58                 AVPNDmaapBean dmaapBean = new AVPNDmaapBean();
59                 dmaapBean.setAsyncRequestStatus(asyncRequestStatus);
60
61                 return dmaapBean;
62         }
63
64         private String jsonToString(AVPNDmaapBean dmaapBean) throws JsonProcessingException, MapperException {
65                 try {
66                         return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(dmaapBean);
67                 } catch (JsonProcessingException e) {
68                         logger.error("Exception occurred", e);
69                         throw new MapperException(e.getMessage());
70                 }
71         }
72
73         private AsyncRequestStatus buildAsyncRequestStatus(String requestId, String clientSource, String correlator, String startTime,
74                                                                                                            String finishTime, String requestScope, String requestType,
75                                                                                                            RequestStatus requestStatus, InstanceReferences instanceReferences) {
76
77                 AsyncRequestStatus asyncRequestStatus = new AsyncRequestStatus();
78                 asyncRequestStatus.setRequestId(requestId);
79                 asyncRequestStatus.setClientSource(clientSource);
80                 asyncRequestStatus.setCorrelator(correlator);
81                 asyncRequestStatus.setStartTime(startTime);
82                 asyncRequestStatus.setFinishTime(finishTime);
83                 asyncRequestStatus.setRequestScope(requestScope);
84                 asyncRequestStatus.setRequestType(requestType);
85                 asyncRequestStatus.setInstanceReferences(instanceReferences);
86                 asyncRequestStatus.setRequestStatus(requestStatus);
87
88                 return asyncRequestStatus;
89         }
90
91         private InstanceReferences buildInstanceReferences(String serviceInstanceId) {
92                 InstanceReferences instanceReferences = new InstanceReferences();
93                 instanceReferences.setServiceInstanceId(serviceInstanceId);
94                 return instanceReferences;
95         }
96
97         private RequestStatus buildRequestStatus(String timestamp, String requestState, String statusMessage,
98                                                                                          String percentProgress, Boolean wasRolledBack) {
99                 RequestStatus requestStatus = new RequestStatus();
100                 requestStatus.setTimestamp(timestamp);
101                 requestStatus.setRequestState(requestState);
102                 requestStatus.setStatusMessage(statusMessage);
103                 requestStatus.setPercentProgress(percentProgress);
104                 requestStatus.setWasRolledBack(wasRolledBack);
105                 return requestStatus;
106         }
107
108         public void dmaapPublishRequest(String requestId, String clientSource, String correlator, String serviceInstanceId, String startTime,
109                                                                         String finishTime, String requestScope, String requestType, String timestamp, String requestState,
110                                                                         String statusMessage, String percentProgress, Boolean wasRolledBack) throws MapperException, JsonProcessingException {
111
112                 AVPNDmaapBean bean = this.buildRequestJson(requestId, clientSource, correlator, serviceInstanceId, startTime, finishTime,
113                                 requestScope, requestType, timestamp, requestState, statusMessage, percentProgress, wasRolledBack);
114
115                 String request = jsonToString(bean);
116                 dmaapPublisher.get().send(request);
117         }
118 }