2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.LCM.impl;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.appc.listener.AbstractListener;
30 import org.onap.appc.listener.ListenerProperties;
31 import org.onap.appc.listener.LCM.conv.Converter;
32 import org.onap.appc.listener.LCM.model.DmaapIncomingMessage;
33 import org.onap.appc.listener.LCM.operation.ProviderOperations;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.i18n.EELFResourceManager;
39 import java.text.DateFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.Date;
42 import java.util.List;
43 import java.util.TimeZone;
44 import java.util.concurrent.RejectedExecutionException;
46 public class ListenerImpl extends AbstractListener {
48 private final EELFLogger LOG = EELFManager.getInstance().getLogger(ListenerImpl.class);
50 private long startTime = 0;
52 private final ProviderOperations providerOperations;
54 public ListenerImpl(ListenerProperties props) {
57 String url = props.getProperty("provider.url");
58 LOG.info("DMaaP Provider Endpoint: " + url);
59 providerOperations = new ProviderOperations();
60 providerOperations.setUrl(url);
63 String user = props.getProperty("provider.user");
64 String pass = props.getProperty("provider.pass");
65 providerOperations.setAuthentication(user, pass);
70 // Some vars for benchmarking
71 startTime = System.currentTimeMillis();
73 LOG.info("Running DMaaP Listener");
76 // Only update if the queue is low. otherwise we read in more
77 // messages than we need
79 if (executor.getQueue().size() <= QUEUED_MIN) {
80 LOG.debug("DMaaP queue running low. Querying for more jobs");
83 List<DmaapIncomingMessage> messages = dmaap.getIncomingEvents(DmaapIncomingMessage.class, QUEUED_MAX);
84 LOG.debug(String.format("Read %d messages from dmaap", messages.size()));
85 for (DmaapIncomingMessage incoming : messages) {
86 // Acknowledge that we read the event
87 if (isValid(incoming)) {
88 String requestIdWithSubId = getRequestIdWithSubId(incoming.getBody());
89 LOG.info("Acknowledging Message: " + requestIdWithSubId);
92 for (DmaapIncomingMessage incoming : messages) {
93 String requestIdWithSubId = getRequestIdWithSubId(incoming.getBody());
94 // Add to pool if still running
96 if (isValid(incoming)) {
97 LOG.info(String.format("Adding DMaaP message to pool queue [%s]", requestIdWithSubId));
99 executor.execute(new WorkerImpl(incoming, dmaap, providerOperations));
100 } catch (RejectedExecutionException rejectEx) {
101 LOG.error("Task Rejected: ", rejectEx);
104 // Badly formed message
105 LOG.error("Message was not valid. Rejecting message: "+incoming);
108 if (isValid(incoming)) {
109 LOG.info("Run stopped. Orphaning Message: " + requestIdWithSubId);
112 // Badly formed message
113 LOG.error("Message was not valid. Rejecting message: "+incoming);
118 } catch (Exception e) {
119 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
120 LOG.error(EELFResourceManager.format(e));
121 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
125 LOG.info("Stopping DMaaP Listener thread");
127 // We've told the listener to stop
129 // 1) Put a message back on the queue indicating that APP-C never got to
132 // 2) Let downstream figure it out after timeout between PENDING and
136 private boolean isValid(DmaapIncomingMessage incoming) {
137 return ((incoming != null) &&
138 incoming.getBody() != null
139 && !StringUtils.isEmpty(incoming.getRpcName()));
143 public String getBenchmark() {
144 long time = System.currentTimeMillis();
145 DateFormat df = new SimpleDateFormat("HH:mm:ss");
146 df.setTimeZone(TimeZone.getTimeZone("UTC"));
147 String runningTime = df.format(new Date(time - startTime));
149 String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
150 executor.getCompletedTaskCount(), executor.getPoolSize());
151 LOG.info("***BENCHMARK*** " + out);
155 private String getRequestIdWithSubId(JsonNode event){
156 String requestId = "";
158 requestId = Converter.extractRequestIdWithSubId(event);
159 } catch (Exception e) {
160 LOG.error("failed to parse request-id and sub-request-id. Json not in expected format", e);