2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.common.endpoints.event.comm.bus.internal;
25 import com.att.nsa.apiClient.http.HttpClient.ConnectionType;
26 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
27 import com.att.nsa.cambria.client.CambriaClientBuilders;
28 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
29 import java.net.MalformedURLException;
30 import java.security.GeneralSecurityException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import java.util.Properties;
35 import java.util.concurrent.TimeUnit;
36 import org.apache.commons.lang3.StringUtils;
37 import org.onap.dmaap.mr.client.impl.MRSimplerBatchPublisher;
38 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
39 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
40 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
41 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public interface BusPublisher {
50 * @param partitionId id
51 * @param message the message
52 * @return true if success, false otherwise
53 * @throws IllegalArgumentException if no message provided
55 public boolean send(String partitionId, String message);
58 * closes the publisher.
63 * Cambria based library publisher.
65 public static class CambriaPublisherWrapper implements BusPublisher {
67 private static Logger logger = LoggerFactory.getLogger(CambriaPublisherWrapper.class);
70 * The actual Cambria publisher.
73 protected CambriaBatchingPublisher publisher;
78 * @param busTopicParams topic parameters
80 public CambriaPublisherWrapper(BusTopicParams busTopicParams) {
82 PublisherBuilder builder = new CambriaClientBuilders.PublisherBuilder();
84 builder.usingHosts(busTopicParams.getServers()).onTopic(busTopicParams.getTopic());
86 // Set read timeout to 30 seconds (TBD: this should be configurable)
87 builder.withSocketTimeout(30000);
89 if (busTopicParams.isUseHttps()) {
90 if (busTopicParams.isAllowSelfSignedCerts()) {
91 builder.withConnectionType(ConnectionType.HTTPS_NO_VALIDATION);
93 builder.withConnectionType(ConnectionType.HTTPS);
98 if (busTopicParams.isApiKeyValid() && busTopicParams.isApiSecretValid()) {
99 builder.authenticatedBy(busTopicParams.getApiKey(), busTopicParams.getApiSecret());
102 if (busTopicParams.isUserNameValid() && busTopicParams.isPasswordValid()) {
103 builder.authenticatedByHttp(busTopicParams.getUserName(), busTopicParams.getPassword());
107 this.publisher = builder.build();
108 } catch (MalformedURLException | GeneralSecurityException e) {
109 throw new IllegalArgumentException(e);
114 public boolean send(String partitionId, String message) {
115 if (message == null) {
116 throw new IllegalArgumentException("No message provided");
120 this.publisher.send(partitionId, message);
121 } catch (Exception e) {
122 logger.warn("{}: SEND of {} cannot be performed because of {}", this, message, e.getMessage(), e);
129 public void close() {
130 logger.info("{}: CLOSE", this);
133 this.publisher.close();
134 } catch (Exception e) {
135 logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e);
141 public String toString() {
142 return "CambriaPublisherWrapper []";
148 * DmaapClient library wrapper.
150 public abstract class DmaapPublisherWrapper implements BusPublisher {
152 private static Logger logger = LoggerFactory.getLogger(DmaapPublisherWrapper.class);
155 * MR based Publisher.
157 protected MRSimplerBatchPublisher publisher;
158 protected Properties props;
161 * MR Publisher Wrapper.
163 * @param servers messaging bus hosts
165 * @param username AAF or DME2 Login
166 * @param password AAF or DME2 Password
168 protected DmaapPublisherWrapper(ProtocolTypeConstants protocol, List<String> servers, String topic,
169 String username, String password, boolean useHttps) {
172 if (StringUtils.isBlank(topic)) {
173 throw new IllegalArgumentException("No topic for DMaaP");
177 configureProtocol(topic, protocol, servers, useHttps);
179 this.publisher.logTo(LoggerFactory.getLogger(MRSimplerBatchPublisher.class.getName()));
181 this.publisher.setUsername(username);
182 this.publisher.setPassword(password);
184 props = new Properties();
186 props.setProperty("Protocol", (useHttps ? "https" : "http"));
187 props.setProperty("contenttype", "application/json");
188 props.setProperty("username", username);
189 props.setProperty("password", password);
191 props.setProperty("topic", topic);
193 this.publisher.setProps(props);
195 if (protocol == ProtocolTypeConstants.AAF_AUTH) {
196 this.publisher.setHost(servers.get(0));
199 logger.info("{}: CREATION: using protocol {}", this, protocol.getValue());
202 private void configureProtocol(String topic, ProtocolTypeConstants protocol, List<String> servers,
205 if (protocol == ProtocolTypeConstants.AAF_AUTH) {
206 if (servers == null || servers.isEmpty()) {
207 throw new IllegalArgumentException("No DMaaP servers or DME2 partner provided");
210 ArrayList<String> dmaapServers = new ArrayList<>();
211 String port = useHttps ? ":3905" : ":3904";
212 for (String server : servers) {
213 dmaapServers.add(server + port);
217 this.publisher = new MRSimplerBatchPublisher.Builder().againstUrls(dmaapServers).onTopic(topic).build();
219 this.publisher.setProtocolFlag(ProtocolTypeConstants.AAF_AUTH.getValue());
221 } else if (protocol == ProtocolTypeConstants.DME2) {
222 ArrayList<String> dmaapServers = new ArrayList<>();
223 dmaapServers.add("0.0.0.0:3904");
225 this.publisher = new MRSimplerBatchPublisher.Builder().againstUrls(dmaapServers).onTopic(topic).build();
227 this.publisher.setProtocolFlag(ProtocolTypeConstants.DME2.getValue());
230 throw new IllegalArgumentException("Invalid DMaaP protocol " + protocol);
235 public void close() {
236 logger.info("{}: CLOSE", this);
239 this.publisher.close(1, TimeUnit.SECONDS);
240 } catch (Exception e) {
241 logger.warn("{}: CLOSE FAILED because of {}", this, e.getMessage(), e);
246 public boolean send(String partitionId, String message) {
247 if (message == null) {
248 throw new IllegalArgumentException("No message provided");
251 this.publisher.setPubResponse(new MRPublisherResponse());
252 this.publisher.send(partitionId, message);
253 MRPublisherResponse response = this.publisher.sendBatchWithResponse();
254 if (response != null) {
255 logger.debug("DMaaP publisher received {} : {}", response.getResponseCode(),
256 response.getResponseMessage());
263 public String toString() {
264 return "DmaapPublisherWrapper [" + "publisher.getAuthDate()=" + publisher.getAuthDate()
265 + ", publisher.getAuthKey()=" + publisher.getAuthKey() + ", publisher.getHost()="
266 + publisher.getHost() + ", publisher.getProtocolFlag()=" + publisher.getProtocolFlag()
267 + ", publisher.getUsername()=" + publisher.getUsername() + "]";
272 * DmaapClient library wrapper.
274 public static class DmaapAafPublisherWrapper extends DmaapPublisherWrapper {
276 * MR based Publisher.
278 public DmaapAafPublisherWrapper(List<String> servers, String topic, String aafLogin, String aafPassword,
281 super(ProtocolTypeConstants.AAF_AUTH, servers, topic, aafLogin, aafPassword, useHttps);
285 public static class DmaapDmePublisherWrapper extends DmaapPublisherWrapper {
290 * @param busTopicParams topic parameters
292 public DmaapDmePublisherWrapper(BusTopicParams busTopicParams) {
294 super(ProtocolTypeConstants.DME2, busTopicParams.getServers(), busTopicParams.getTopic(),
295 busTopicParams.getUserName(), busTopicParams.getPassword(), busTopicParams.isUseHttps());
297 String dme2RouteOffer = busTopicParams.isAdditionalPropsValid()
298 ? busTopicParams.getAdditionalProps().get(
299 PolicyEndPointProperties.DME2_ROUTE_OFFER_PROPERTY)
302 validateParams(busTopicParams, dme2RouteOffer);
304 String serviceName = busTopicParams.getServers().get(0);
306 /* These are required, no defaults */
307 props.setProperty("Environment", busTopicParams.getEnvironment());
308 props.setProperty("AFT_ENVIRONMENT", busTopicParams.getAftEnvironment());
310 props.setProperty(PolicyEndPointProperties.DME2_SERVICE_NAME_PROPERTY, serviceName);
312 if (busTopicParams.getPartner() != null) {
313 props.setProperty("Partner", busTopicParams.getPartner());
315 if (dme2RouteOffer != null) {
316 props.setProperty(PolicyEndPointProperties.DME2_ROUTE_OFFER_PROPERTY, dme2RouteOffer);
319 props.setProperty("Latitude", busTopicParams.getLatitude());
320 props.setProperty("Longitude", busTopicParams.getLongitude());
322 // ServiceName also a default, found in additionalProps
324 /* These are optional, will default to these values if not set in optionalProps */
325 props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", "50000");
326 props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", "240000");
327 props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", "15000");
328 props.setProperty("Version", "1.0");
329 props.setProperty("SubContextPath", "/");
330 props.setProperty("sessionstickinessrequired", "no");
332 /* These should not change */
333 props.setProperty("TransportType", "DME2");
334 props.setProperty("MethodType", "POST");
336 if (busTopicParams.isAdditionalPropsValid()) {
337 addAdditionalProps(busTopicParams);
340 this.publisher.setProps(props);
343 private void validateParams(BusTopicParams busTopicParams, String dme2RouteOffer) {
344 if (busTopicParams.isEnvironmentInvalid()) {
345 throw parmException(busTopicParams.getTopic(),
346 PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ENVIRONMENT_SUFFIX);
348 if (busTopicParams.isAftEnvironmentInvalid()) {
349 throw parmException(busTopicParams.getTopic(),
350 PolicyEndPointProperties.PROPERTY_DMAAP_DME2_AFT_ENVIRONMENT_SUFFIX);
352 if (busTopicParams.isLatitudeInvalid()) {
353 throw parmException(busTopicParams.getTopic(),
354 PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LATITUDE_SUFFIX);
356 if (busTopicParams.isLongitudeInvalid()) {
357 throw parmException(busTopicParams.getTopic(),
358 PolicyEndPointProperties.PROPERTY_DMAAP_DME2_LONGITUDE_SUFFIX);
361 if ((busTopicParams.isPartnerInvalid())
362 && StringUtils.isBlank(dme2RouteOffer)) {
363 throw new IllegalArgumentException(
364 "Must provide at least " + PolicyEndPointProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."
365 + busTopicParams.getTopic()
366 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_PARTNER_SUFFIX + " or "
367 + PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "." + busTopicParams.getTopic()
368 + PolicyEndPointProperties.PROPERTY_DMAAP_DME2_ROUTE_OFFER_SUFFIX + " for DME2");
372 private void addAdditionalProps(BusTopicParams busTopicParams) {
373 for (Map.Entry<String, String> entry : busTopicParams.getAdditionalProps().entrySet()) {
374 String key = entry.getKey();
375 String value = entry.getValue();
378 props.setProperty(key, value);
383 private IllegalArgumentException parmException(String topic, String propnm) {
384 return new IllegalArgumentException("Missing " + PolicyEndPointProperties.PROPERTY_DMAAP_SINK_TOPICS + "."
385 + topic + propnm + " property for DME2 in DMaaP");