Remove commented methods/fields in APPC
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / demo / 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.demo.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.demo.model.IncomingMessage;
34
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.i18n.EELFResourceManager;
38
39 public class ListenerImpl extends AbstractListener {
40
41     private final EELFLogger LOG = EELFManager.getInstance().getLogger(ListenerImpl.class);
42
43     private long startTime = 0;
44
45     public ListenerImpl(ListenerProperties props) {
46         super(props);
47         String url = props.getProperty("provider.url");        
48         LOG.info("DMaaP Provider Endpoint: " + url);
49         ProviderOperations.setUrl(url);
50
51         // Set Basic Auth
52         String user = props.getProperty("provider.user");
53         String pass = props.getProperty("provider.pass");
54         ProviderOperations.setAuthentication(user, pass);
55     }
56
57     @Override
58     public void run() {
59         // Some vars for benchmarking
60         startTime = System.currentTimeMillis();
61
62         LOG.info("Running DMaaP Listener");
63
64         while (run.get()) {
65             // Only update if the queue is low. otherwise we read in more
66             // messages than we need
67             try {
68                 if (executor.getQueue().size() <= QUEUED_MIN) {
69                     LOG.debug("DMaaP queue running low. Querying for more jobs");
70                     List<IncomingMessage> messages = dmaap.getIncomingEvents(IncomingMessage.class, QUEUED_MAX);
71                     LOG.debug(String.format("Read %d messages from dmaap", messages.size()));
72                     for (IncomingMessage incoming : messages) {
73                         // Acknowledge that we read the event
74                         LOG.info("Acknowledging Message: " + incoming.getHeader().getRequestID());
75                         
76                         //TODO: Should we post a pending status for 1607
77                         //dmaap.postStatus(incoming.toOutgoing(Status.PENDING, null).toString());
78                     }
79                     for (IncomingMessage incoming : messages) {
80                         // Add to pool if still running
81                         if (run.get()) {
82                             LOG.info(String.format("Adding DMaaP message to pool queue [%s]", incoming.getHeader().getRequestID()));
83                             if (incoming.isValid()) {
84                                 try {
85                                     executor.execute(new WorkerImpl(incoming, dmaap));
86                                 } catch (RejectedExecutionException rejectEx) {
87                                     LOG.error("Task Rejected: ", rejectEx);
88                                 }
89                             } else {
90                                 // Badly formed message
91                                 LOG.error("Message was not valid. Rejecting");
92                             }
93                         } else {
94                             LOG.info("Run stopped. Orphaning Message: " + incoming.getHeader().getRequestID());
95                         }
96                     }
97                 }
98             } catch (Exception e) {
99                 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
100                 LOG.error(EELFResourceManager.format(e));
101                 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
102             }
103         }
104
105         LOG.info("Stopping DMaaP Listener thread");
106
107         // We've told the listener to stop
108         // TODO - Should we:
109         // 1) Put a message back on the queue indicating that APP-C never got to
110         // the message
111         // or
112         // 2) Let downstream figure it out after timeout between PENDING and
113         // ACTIVE messages
114     }
115
116     @Override
117     public String getBenchmark() {
118         long time = System.currentTimeMillis();
119         DateFormat df = new SimpleDateFormat("HH:mm:ss");
120         df.setTimeZone(TimeZone.getTimeZone("UTC"));
121         String runningTime = df.format(new Date(time - startTime));
122
123         String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
124             executor.getCompletedTaskCount(), executor.getPoolSize());
125         LOG.info("***BENCHMARK*** " + out);
126         return out;
127     }
128
129 }