Moving all files to root directory
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / CL1607 / impl / ListenerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.listener.CL1607.impl;
23
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.TimeZone;
29 import java.util.concurrent.RejectedExecutionException;
30
31 import org.openecomp.appc.listener.AbstractListener;
32 import org.openecomp.appc.listener.ListenerProperties;
33 import org.openecomp.appc.listener.CL1607.model.IncomingMessage;
34 import org.openecomp.appc.listener.CL1607.model.Status;
35
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.att.eelf.i18n.EELFResourceManager;
39
40 public class ListenerImpl extends AbstractListener {
41
42     private final EELFLogger LOG = EELFManager.getInstance().getLogger(ListenerImpl.class);
43
44     private long startTime = 0;
45
46     public ListenerImpl(ListenerProperties props) {
47         super(props);
48         String url = props.getProperty("provider.url");        
49         LOG.info("DMaaP Provider Endpoint: " + url);
50         ProviderOperations.setUrl(url);
51
52         // Set Basic Auth
53         String user = props.getProperty("provider.user");
54         String pass = props.getProperty("provider.pass");
55         ProviderOperations.setAuthentication(user, pass);
56     }
57
58     @Override
59     public void run() {
60         // Some vars for benchmarking
61         startTime = System.currentTimeMillis();
62
63         LOG.info("Running DMaaP Listener");
64
65         while (run.get()) {
66             // Only update if the queue is low. otherwise we read in more
67             // messages than we need
68             try {
69                 if (executor.getQueue().size() <= QUEUED_MIN) {
70                     LOG.debug("DMaaP queue running low. Querying for more jobs");
71                     List<IncomingMessage> messages = dmaap.getIncomingEvents(IncomingMessage.class, QUEUED_MAX);
72                     LOG.debug(String.format("Read %d messages from dmaap", messages.size()));
73                     for (IncomingMessage incoming : messages) {
74                         // Acknowledge that we read the event
75                         LOG.info("Acknowledging Message: " + incoming.getHeader().getRequestID());
76                         
77                         //TODO: Should we post a pending status for 1607
78                         //dmaap.postStatus(incoming.toOutgoing(Status.PENDING, null).toString());
79                     }
80                     for (IncomingMessage incoming : messages) {
81                         // Add to pool if still running
82                         if (run.get()) {
83                             LOG.info(String.format("Adding DMaaP message to pool queue [%s]", incoming.getHeader().getRequestID()));
84                             if (incoming.isValid()) {
85                                 try {
86                                     executor.execute(new WorkerImpl(incoming, dmaap));
87                                 } catch (RejectedExecutionException rejectEx) {
88                                     LOG.error("Task Rejected: ", rejectEx);
89                                 }
90                             } else {
91                                 // Badly formed message
92                                 LOG.error("Message was not valid. Rejecting");
93                             }
94                         } else {
95                             LOG.info("Run stopped. Orphaning Message: " + incoming.getHeader().getRequestID());
96                         }
97                     }
98                 }
99             } catch (Exception e) {
100                 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
101                 LOG.error(EELFResourceManager.format(e));
102                 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
103             }
104         }
105
106         LOG.info("Stopping DMaaP Listener thread");
107
108         // We've told the listener to stop
109         // TODO - Should we:
110         // 1) Put a message back on the queue indicating that APP-C never got to
111         // the message
112         // or
113         // 2) Let downstream figure it out after timeout between PENDING and
114         // ACTIVE messages
115     }
116
117     @Override
118     public String getBenchmark() {
119         long time = System.currentTimeMillis();
120         DateFormat df = new SimpleDateFormat("HH:mm:ss");
121         df.setTimeZone(TimeZone.getTimeZone("UTC"));
122         String runningTime = df.format(new Date(time - startTime));
123
124         String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
125             executor.getCompletedTaskCount(), executor.getPoolSize());
126         LOG.info("***BENCHMARK*** " + out);
127         return out;
128     }
129
130 }