2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.components.distribution.engine;
22 import com.att.nsa.mr.client.MRBatchingPublisher;
23 import com.att.nsa.mr.client.MRClientFactory;
24 import com.att.nsa.mr.client.MRConsumer;
25 import fj.data.Either;
27 import java.io.IOException;
28 import java.security.GeneralSecurityException;
29 import java.util.Properties;
30 import org.onap.sdc.security.SecurityUtil;
31 import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
32 import org.openecomp.sdc.be.config.DmaapProducerConfiguration;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.springframework.stereotype.Component;
37 * Allows to create DMAAP client of type MRConsumer according received configuration parameters
39 @Component("dmaapClientFactory")
40 public class DmaapClientFactory {
42 private static final Logger logger = Logger.getLogger(DmaapClientFactory.class.getName());
45 * Creates DMAAP consumer according to received parameters
48 * @return an instance object of type MRConsumer
51 public MRConsumer create(DmaapConsumerConfiguration parameters) throws GeneralSecurityException, IOException {
52 MRConsumer consumer = MRClientFactory.createConsumer(buildProperties(parameters));
53 logger.info("MRConsumer created for topic {}", parameters.getTopic());
58 * Creates DMAAP consumer according to received parameters
61 * @return an instance object of type MRConsumer
64 public MRBatchingPublisher createProducer(DmaapProducerConfiguration parameters) throws Exception {
65 Properties prop = buildProducerProperties(parameters);
66 MRBatchingPublisher producer = MRClientFactory.createBatchingPublisher(prop);
67 logger.info("MRBatchingPublisher created for topic {}", parameters.getTopic());
71 private Properties buildProperties(DmaapConsumerConfiguration parameters) throws GeneralSecurityException, IOException {
72 Properties props = new Properties();
73 Either<String, String> passkey = SecurityUtil.decrypt(parameters.getCredential().getPassword());
74 if (passkey.isRight()) {
75 throw new GeneralSecurityException("invalid password, cannot build properties");
77 props.setProperty("Latitude", Double.toString(parameters.getLatitude()));
78 props.setProperty("Longitude", Double.toString(parameters.getLongitude()));
79 props.setProperty("Version", parameters.getVersion());
80 props.setProperty("ServiceName", parameters.getServiceName());
81 props.setProperty("Environment", parameters.getEnvironment());
82 props.setProperty("Partner", parameters.getPartner());
83 props.setProperty("routeOffer", parameters.getRouteOffer());
84 props.setProperty("Protocol", parameters.getProtocol());
85 props.setProperty("username", parameters.getCredential().getUsername());
86 props.setProperty("password", passkey.left().value());
87 props.setProperty("contenttype", parameters.getContenttype());
88 props.setProperty("host", parameters.getHosts());
89 props.setProperty("topic", parameters.getTopic());
90 props.setProperty("group", parameters.getConsumerGroup());
91 props.setProperty("id", parameters.getConsumerId());
92 props.setProperty("timeout", Integer.toString(parameters.getTimeoutMs()));
93 props.setProperty("limit", Integer.toString(parameters.getLimit()));
94 props.setProperty("AFT_DME2_REQ_TRACE_ON", Boolean.toString(parameters.isDme2TraceOn()));
95 props.setProperty("AFT_ENVIRONMENT", parameters.getAftEnvironment());
96 props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", Integer.toString(parameters.getAftDme2ConnectionTimeoutMs()));
97 props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", Integer.toString(parameters.getAftDme2RoundtripTimeoutMs()));
98 props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", Integer.toString(parameters.getAftDme2ReadTimeoutMs()));
99 props.setProperty("AFT_DME2_SSL_ENABLE", Boolean.toString(parameters.isAftDme2SslEnable()));
100 props.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", Boolean.toString(parameters.isAftDme2ClientIgnoreSslConfig()));
101 props.setProperty("AFT_DME2_CLIENT_KEYSTORE", parameters.getAftDme2ClientKeystore());
102 props.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", parameters.getAftDme2ClientKeystorePassword());
103 props.setProperty("AFT_DME2_CLIENT_SSL_CERT_ALIAS", parameters.getAftDme2ClientSslCertAlias());
104 String dme2PreferredRouterFilePath = parameters.getDme2preferredRouterFilePath();
105 ensureFileExists(dme2PreferredRouterFilePath);
106 props.setProperty("DME2preferredRouterFilePath", dme2PreferredRouterFilePath);
107 props.setProperty("TransportType", "DME2");
108 props.setProperty("SubContextPath", "/");
109 props.setProperty("MethodType", "GET");
110 props.setProperty("authKey", "");
111 props.setProperty("authDate", "");
112 props.setProperty("filter", "");
113 props.setProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS", "");
114 props.setProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "");
115 props.setProperty("sessionstickinessrequired", "no");
119 private Properties buildProducerProperties(DmaapProducerConfiguration parameters) throws GeneralSecurityException, IOException {
120 logger.info("The DmaapProducerConfiguration is {} ", parameters);
121 Properties props = new Properties();
122 Either<String, String> passkey = SecurityUtil.decrypt(parameters.getCredential().getPassword());
123 if (passkey.isRight()) {
124 throw new GeneralSecurityException("invalid password, cannot build properties");
126 props.setProperty("Latitude", Double.toString(parameters.getLatitude()));
127 props.setProperty("Longitude", Double.toString(parameters.getLongitude()));
128 props.setProperty("Version", parameters.getVersion());
129 props.setProperty("ServiceName", parameters.getServiceName());
130 props.setProperty("Environment", parameters.getEnvironment());
131 props.setProperty("Partner", parameters.getPartner());
132 props.setProperty("routeOffer", parameters.getRouteOffer());
133 props.setProperty("Protocol", parameters.getProtocol());
134 props.setProperty("username", parameters.getCredential().getUsername());
135 props.setProperty("password", passkey.left().value());
136 props.setProperty("contenttype", parameters.getContenttype());
137 props.setProperty("host", parameters.getHosts());
138 props.setProperty("topic", parameters.getTopic());
139 props.setProperty("group", parameters.getConsumerGroup());
140 props.setProperty("id", parameters.getConsumerId());
141 props.setProperty("AFT_DME2_REQ_TRACE_ON", Boolean.toString(parameters.isDme2TraceOn()));
142 props.setProperty("AFT_ENVIRONMENT", parameters.getAftEnvironment());
143 props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", Integer.toString(parameters.getAftDme2ConnectionTimeoutMs()));
144 props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", Integer.toString(parameters.getAftDme2RoundtripTimeoutMs()));
145 props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", Integer.toString(parameters.getAftDme2ReadTimeoutMs()));
146 props.setProperty("AFT_DME2_SSL_ENABLE", Boolean.toString(parameters.isAftDme2SslEnable()));
147 props.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", Boolean.toString(parameters.isAftDme2ClientIgnoreSslConfig()));
148 props.setProperty("AFT_DME2_CLIENT_KEYSTORE", parameters.getAftDme2ClientKeystore());
149 props.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", parameters.getAftDme2ClientKeystorePassword());
150 props.setProperty("AFT_DME2_CLIENT_SSL_CERT_ALIAS", parameters.getAftDme2ClientSslCertAlias());
151 String dme2PreferredRouterFilePath = parameters.getDme2preferredRouterFilePath();
152 ensureFileExists(dme2PreferredRouterFilePath);
153 props.setProperty("DME2preferredRouterFilePath", dme2PreferredRouterFilePath);
154 props.setProperty("TransportType", "HTTPAAF");
155 props.setProperty("SubContextPath", "/");
156 props.setProperty("MethodType", "POST");
157 props.setProperty("authKey", "");
158 props.setProperty("authDate", "");
159 props.setProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS", "");
160 props.setProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "");
161 props.setProperty("sessionstickinessrequired", "no");
162 props.setProperty("maxBatchSize", "1");
163 props.setProperty("maxAgeMs", "250");
164 props.setProperty("partition", "1");
165 props.setProperty("MessageSentThreadOccurance", "10");
166 props.setProperty("Authorization", "Basic bTEzMzMxQGNjZC5hdHQuY29tOkFhMTIzNDU2");
170 private void ensureFileExists(String filePath) throws IOException {
171 File file = new File(filePath);
172 if (file.createNewFile()) {
173 logger.info("The file {} has been created on the disk", file.getAbsolutePath());
175 logger.info("The file {} already exists", file.getAbsolutePath());