Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / main / java / org / openecomp / dcae / apod / analytics / cdap / tca / utils / AppPreferencesToPublisherConfigMapper.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.cdap.tca.utils;\r
22 \r
23 import com.google.common.base.Function;\r
24 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAAppPreferences;\r
25 import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig;\r
26 \r
27 import javax.annotation.Nonnull;\r
28 \r
29 import static org.openecomp.dcae.apod.analytics.cdap.common.utils.ValidationUtils.isPresent;\r
30 \r
31 \r
32 /**\r
33  * Function which translates {@link TCAAppPreferences} to {@link DMaaPMRPublisherConfig}\r
34  * <p>\r
35  * @author Rajiv Singla . Creation Date: 11/17/2016.\r
36  */\r
37 public class AppPreferencesToPublisherConfigMapper implements Function<TCAAppPreferences, DMaaPMRPublisherConfig> {\r
38 \r
39     /**\r
40      * Factory method to convert {@link TCAAppPreferences} to {@link DMaaPMRPublisherConfig} object\r
41      *\r
42      * @param tcaAppPreferences tca App Preferences\r
43      *\r
44      * @return publisher config object\r
45      */\r
46     public static DMaaPMRPublisherConfig map(final TCAAppPreferences tcaAppPreferences) {\r
47         return new AppPreferencesToPublisherConfigMapper().apply(tcaAppPreferences);\r
48     }\r
49 \r
50     /**\r
51      * Implementation to convert {@link TCAAppPreferences} to {@link DMaaPMRPublisherConfig} object\r
52      *\r
53      * @param tcaAppPreferences tca App Preferences\r
54      *\r
55      * @return publisher config object\r
56      */\r
57     @Nonnull\r
58     @Override\r
59     public DMaaPMRPublisherConfig apply(@Nonnull TCAAppPreferences tcaAppPreferences) {\r
60 \r
61         // Create a new publisher settings builder\r
62         final DMaaPMRPublisherConfig.Builder publisherConfigBuilder = new DMaaPMRPublisherConfig.Builder(\r
63                 tcaAppPreferences.getPublisherHostName(), tcaAppPreferences.getPublisherTopicName());\r
64 \r
65         // Setup up any optional publisher parameters if they are present\r
66         final Integer publisherHostPort = tcaAppPreferences.getPublisherHostPort();\r
67         if (publisherHostPort != null) {\r
68             publisherConfigBuilder.setPortNumber(publisherHostPort);\r
69         }\r
70         final String publisherProtocol = tcaAppPreferences.getPublisherProtocol();\r
71         if (isPresent(publisherProtocol)) {\r
72             publisherConfigBuilder.setProtocol(publisherProtocol);\r
73         }\r
74         final String publisherUserName = tcaAppPreferences.getPublisherUserName();\r
75         if (isPresent(publisherUserName)) {\r
76             publisherConfigBuilder.setUserName(publisherUserName);\r
77         }\r
78         final String publisherUserPassword = tcaAppPreferences.getPublisherUserPassword();\r
79         if (isPresent(publisherUserPassword)) {\r
80             publisherConfigBuilder.setUserPassword(publisherUserPassword);\r
81         }\r
82         final String publisherContentType = tcaAppPreferences.getPublisherContentType();\r
83         if (isPresent(publisherContentType)) {\r
84             publisherConfigBuilder.setContentType(publisherContentType);\r
85         }\r
86         final Integer publisherMaxBatchSize = tcaAppPreferences.getPublisherMaxBatchSize();\r
87         if (publisherMaxBatchSize != null) {\r
88             publisherConfigBuilder.setMaxBatchSize(publisherMaxBatchSize);\r
89         }\r
90         final Integer publisherMaxRecoveryQueueSize = tcaAppPreferences.getPublisherMaxRecoveryQueueSize();\r
91         if (publisherMaxRecoveryQueueSize != null) {\r
92             publisherConfigBuilder.setMaxRecoveryQueueSize(publisherMaxRecoveryQueueSize);\r
93         }\r
94 \r
95         return publisherConfigBuilder.build();\r
96     }\r
97 }\r