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