Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / openecomp / dcae / apod / analytics / cdap / tca / utils / AppPreferencesToPublisherConfigMapper.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 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.openecomp.dcae.apod.analytics.cdap.tca.utils;
22
23 import com.google.common.base.Function;
24 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;
25 import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig;
26
27 import javax.annotation.Nonnull;
28
29 import static org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils.isPresent;
30
31
32 /**
33  * Function which translates {@link TCAAppPreferences} to {@link DMaaPMRPublisherConfig}
34  * <p>
35  * @author Rajiv Singla . Creation Date: 11/17/2016.
36  */
37 public class AppPreferencesToPublisherConfigMapper implements Function<TCAAppPreferences, DMaaPMRPublisherConfig> {
38
39     /**
40      * Factory method to convert {@link TCAAppPreferences} to {@link DMaaPMRPublisherConfig} object
41      *
42      * @param tcaAppPreferences tca App Preferences
43      *
44      * @return publisher config object
45      */
46     public static DMaaPMRPublisherConfig map(final TCAAppPreferences tcaAppPreferences) {
47         return new AppPreferencesToPublisherConfigMapper().apply(tcaAppPreferences);
48     }
49
50     /**
51      * Implementation to convert {@link TCAAppPreferences} to {@link DMaaPMRPublisherConfig} object
52      *
53      * @param tcaAppPreferences tca App Preferences
54      *
55      * @return publisher config object
56      */
57     @Nonnull
58     @Override
59     public DMaaPMRPublisherConfig apply(@Nonnull TCAAppPreferences tcaAppPreferences) {
60
61         // Create a new publisher settings builder
62         final DMaaPMRPublisherConfig.Builder publisherConfigBuilder = new DMaaPMRPublisherConfig.Builder(
63                 tcaAppPreferences.getPublisherHostName(), tcaAppPreferences.getPublisherTopicName());
64
65         // Setup up any optional publisher parameters if they are present
66         final Integer publisherHostPort = tcaAppPreferences.getPublisherHostPort();
67         if (publisherHostPort != null) {
68             publisherConfigBuilder.setPortNumber(publisherHostPort);
69         }
70         final String publisherProtocol = tcaAppPreferences.getPublisherProtocol();
71         if (isPresent(publisherProtocol)) {
72             publisherConfigBuilder.setProtocol(publisherProtocol);
73         }
74         final String publisherUserName = tcaAppPreferences.getPublisherUserName();
75         if (isPresent(publisherUserName)) {
76             publisherConfigBuilder.setUserName(publisherUserName);
77         }
78         final String publisherUserPassword = tcaAppPreferences.getPublisherUserPassword();
79         if (isPresent(publisherUserPassword)) {
80             publisherConfigBuilder.setUserPassword(publisherUserPassword);
81         }
82         final String publisherContentType = tcaAppPreferences.getPublisherContentType();
83         if (isPresent(publisherContentType)) {
84             publisherConfigBuilder.setContentType(publisherContentType);
85         }
86         final Integer publisherMaxBatchSize = tcaAppPreferences.getPublisherMaxBatchSize();
87         if (publisherMaxBatchSize != null) {
88             publisherConfigBuilder.setMaxBatchSize(publisherMaxBatchSize);
89         }
90         final Integer publisherMaxRecoveryQueueSize = tcaAppPreferences.getPublisherMaxRecoveryQueueSize();
91         if (publisherMaxRecoveryQueueSize != null) {
92             publisherConfigBuilder.setMaxRecoveryQueueSize(publisherMaxRecoveryQueueSize);
93         }
94
95         return publisherConfigBuilder.build();
96     }
97 }