2 * ===============================LICENSE_START======================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.dcae.apod.analytics.cdap.common.utils;
\r
23 import co.cask.cdap.api.metrics.Metrics;
\r
24 import com.google.common.base.Optional;
\r
25 import com.google.common.base.Stopwatch;
\r
26 import org.openecomp.dcae.apod.analytics.cdap.common.CDAPMetricsConstants;
\r
27 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
\r
28 import org.openecomp.dcae.apod.analytics.common.utils.HTTPUtils;
\r
29 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse;
\r
30 import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;
\r
31 import org.slf4j.Logger;
\r
32 import org.slf4j.LoggerFactory;
\r
34 import java.util.List;
\r
37 * Utility common methods for DMaaP MR functionality
\r
39 * @author Rajiv Singla . Creation Date: 2/6/2017.
\r
41 public abstract class DMaaPMRUtils {
\r
43 private static final Logger LOG = LoggerFactory.getLogger(DMaaPMRUtils.class);
\r
45 private DMaaPMRUtils() {
\r
46 // private constructor
\r
51 * Returns messages fetched from DMaaP MR Subscriber.
\r
53 * @param subscriber DMaaP MR Subscriber instance
\r
54 * @param metrics CDAP metrics
\r
56 * @return messages fetched from DMaaP MR topic
\r
58 public static Optional<List<String>> getSubscriberMessages(final DMaaPMRSubscriber subscriber,
\r
59 final Metrics metrics) {
\r
61 final Optional<DMaaPMRSubscriberResponse> subscriberResponseOptional =
\r
62 getSubscriberResponse(subscriber, metrics);
\r
64 // If response is not present, unable to proceed
\r
65 if (!subscriberResponseOptional.isPresent()) {
\r
66 return Optional.absent();
\r
69 final DMaaPMRSubscriberResponse subscriberResponse = subscriberResponseOptional.get();
\r
71 // If response code return by the subscriber call is not successful, unable to do proceed
\r
72 if (!HTTPUtils.isSuccessfulResponseCode(subscriberResponse.getResponseCode())) {
\r
73 LOG.error("Subscriber was unable to fetch messages properly.Subscriber Response Code: {} " +
\r
74 "Unable to proceed further....", subscriberResponse.getResponseCode());
\r
75 metrics.count(CDAPMetricsConstants.DMAAP_MR_SUBSCRIBER_UNSUCCESSFUL_RESPONSES_METRIC, 1);
\r
76 return Optional.absent();
\r
79 LOG.debug("Subscriber HTTP Response Status Code match successful: {}", subscriberResponse,
\r
80 HTTPUtils.HTTP_SUCCESS_STATUS_CODE);
\r
82 final List<String> actualMessages = subscriberResponse.getFetchedMessages();
\r
84 // If there are no message returned during from Subscriber, nothing to write to CDAP Stream
\r
85 if (actualMessages.isEmpty()) {
\r
86 LOG.debug("Subscriber Response has no messages. Nothing to write....");
\r
87 metrics.count(CDAPMetricsConstants.DMAAP_MR_SUBSCRIBER_RESPONSES_WITH_NO_MESSAGES_METRIC, 1);
\r
88 return Optional.absent();
\r
91 LOG.debug("DMaaP MR Subscriber found new messages in DMaaP Topic. Message count: {}", actualMessages.size());
\r
92 metrics.count(CDAPMetricsConstants.DMAAP_MR_SUBSCRIBER_TOTAL_MESSAGES_PROCESSED_METRIC, actualMessages.size());
\r
94 return Optional.of(actualMessages);
\r
100 * Get Subscriber response and records time taken to fetch messages. Returns Optional.None if Subscriber response
\r
101 * is null or response status code is not present
\r
103 * @param subscriber - DMaaP Subscriber
\r
104 * @param metrics - CDAP Metrics collector
\r
106 * @return - Optional of Subscriber Response
\r
108 public static Optional<DMaaPMRSubscriberResponse> getSubscriberResponse(final DMaaPMRSubscriber subscriber,
\r
109 final Metrics metrics) {
\r
111 // Record all response count from subscriber
\r
112 metrics.count(CDAPMetricsConstants.DMAAP_MR_SUBSCRIBER_ALL_RESPONSES_COUNT_METRIC, 1);
\r
114 // Check how long it took for subscriber to respond
\r
115 final Stopwatch stopwatch = new Stopwatch();
\r
118 // Fetch messages from DMaaP MR Topic
\r
119 DMaaPMRSubscriberResponse subscriberResponse = null;
\r
121 subscriberResponse = subscriber.fetchMessages();
\r
122 } catch (DCAEAnalyticsRuntimeException e) {
\r
123 LOG.error("Error while fetching messages for DMaaP MR Topic: {}", e);
\r
127 final long subscriberResponseTimeMS = stopwatch.elapsedMillis();
\r
129 // If response is null is null or response code is null, unable to proceed nothing to do
\r
130 if (subscriberResponse == null || subscriberResponse.getResponseCode() == null) {
\r
131 LOG.error("Subscriber Response is null or subscriber Response code is null. Unable to proceed further...");
\r
132 return Optional.absent();
\r
135 LOG.debug("Subscriber Response:{}, Subscriber HTTP Response Status Code {}, Subscriber Response Time(ms): {}",
\r
136 subscriberResponse, subscriberResponse.getResponseCode(), subscriberResponseTimeMS);
\r
138 // Record subscriber response time
\r
139 metrics.gauge(CDAPMetricsConstants.DMAAP_MR_SUBSCRIBER_RESPONSE_TIME_MS_METRIC, subscriberResponseTimeMS);
\r
141 return Optional.of(subscriberResponse);
\r