2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.listener.LCM.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.att.eelf.i18n.EELFResourceManager;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import java.text.DateFormat;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.List;
34 import java.util.TimeZone;
35 import java.util.concurrent.RejectedExecutionException;
36 import org.apache.commons.lang3.StringUtils;
37 import org.onap.appc.listener.AbstractListener;
38 import org.onap.appc.listener.LCM.conv.Converter;
39 import org.onap.appc.listener.LCM.model.DmaapIncomingMessage;
40 import org.onap.appc.listener.LCM.operation.ProviderOperations;
41 import org.onap.appc.listener.ListenerProperties;
43 public class ListenerImpl extends AbstractListener {
45 private final EELFLogger LOG = EELFManager.getInstance().getLogger(ListenerImpl.class);
47 private long startTime = 0;
49 private final ProviderOperations providerOperations;
51 public ListenerImpl(ListenerProperties props) {
54 String url = props.getProperty("provider.url");
55 String user = props.getProperty("provider.user");
56 String pass = props.getProperty("provider.pass");
57 providerOperations = new ProviderOperations(url, user, pass);
58 LOG.info("DMaaP Provider Endpoint: " + url);
63 // Some vars for benchmarking
64 startTime = System.currentTimeMillis();
66 LOG.info("Running DMaaP Listener");
69 // Only update if the queue is low. otherwise we read in more
70 // messages than we need
72 if (executor.getQueue().size() <= QUEUED_MIN) {
73 LOG.debug("DMaaP queue running low. Querying for more jobs");
75 List<DmaapIncomingMessage> messages = dmaap
76 .getIncomingEvents(DmaapIncomingMessage.class, QUEUED_MAX);
77 LOG.debug(String.format("Read %d messages from dmaap", messages.size()));
78 for (DmaapIncomingMessage incoming : messages) {
79 // Acknowledge that we read the event
80 if (isValid(incoming)) {
81 String requestIdWithSubId = getRequestIdWithSubId(incoming.getBody());
82 LOG.info("Acknowledging Message: " + requestIdWithSubId);
85 for (DmaapIncomingMessage incoming : messages) {
86 String requestIdWithSubId = getRequestIdWithSubId(incoming.getBody());
87 // Add to pool if still running
89 if (isValid(incoming)) {
90 LOG.info(String.format("Adding DMaaP message to pool queue [%s]", requestIdWithSubId));
92 executor.execute(new WorkerImpl(incoming, dmaap, providerOperations));
93 } catch (RejectedExecutionException rejectEx) {
94 LOG.error("Task Rejected: ", rejectEx);
97 // Badly formed message
98 LOG.error("Message was not valid. Rejecting message: " + incoming);
101 if (isValid(incoming)) {
102 LOG.info("Run stopped. Orphaning Message: " + requestIdWithSubId);
104 // Badly formed message
105 LOG.error("Message was not valid. Rejecting message: " + incoming);
110 } catch (Exception e) {
111 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
112 LOG.error(EELFResourceManager.format(e));
113 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
117 LOG.info("Stopping DMaaP Listener thread");
119 // We've told the listener to stop
121 // 1) Put a message back on the queue indicating that APP-C never got to
124 // 2) Let downstream figure it out after timeout between PENDING and
128 private boolean isValid(DmaapIncomingMessage incoming) {
129 return ((incoming != null) &&
130 incoming.getBody() != null
131 && !StringUtils.isEmpty(incoming.getRpcName()));
135 public String getBenchmark() {
136 long time = System.currentTimeMillis();
137 DateFormat df = new SimpleDateFormat("HH:mm:ss");
138 df.setTimeZone(TimeZone.getTimeZone("UTC"));
139 String runningTime = df.format(new Date(time - startTime));
141 String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
142 executor.getCompletedTaskCount(), executor.getPoolSize());
143 LOG.info("***BENCHMARK*** " + out);
147 private String getRequestIdWithSubId(JsonNode event) {
148 String requestId = "";
150 requestId = Converter.extractRequestIdWithSubId(event);
151 } catch (Exception e) {
152 LOG.error("failed to parse request-id and sub-request-id. Json not in expected format", e);