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