0e3ab155752f41c3c6baeb361dbe79d98cd1b754
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.listener.LCM.impl;
24
25 import com.fasterxml.jackson.databind.JsonNode;
26 import org.apache.commons.lang3.StringUtils;
27 import org.openecomp.appc.listener.AbstractListener;
28 import org.openecomp.appc.listener.ListenerProperties;
29 import org.openecomp.appc.listener.LCM.conv.Converter;
30 import org.openecomp.appc.listener.LCM.model.DmaapIncomingMessage;
31 import org.openecomp.appc.listener.LCM.operation.ProviderOperations;
32
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import com.att.eelf.i18n.EELFResourceManager;
36
37 import java.text.DateFormat;
38 import java.text.SimpleDateFormat;
39 import java.util.Date;
40 import java.util.List;
41 import java.util.TimeZone;
42 import java.util.concurrent.RejectedExecutionException;
43
44 public class ListenerImpl extends AbstractListener {
45
46     private final EELFLogger LOG = EELFManager.getInstance().getLogger(ListenerImpl.class);
47
48     private long startTime = 0;
49
50     private final ProviderOperations providerOperations;
51
52     public ListenerImpl(ListenerProperties props) {
53         super(props);
54
55         String url = props.getProperty("provider.url");
56         LOG.info("DMaaP Provider Endpoint: " + url);
57         providerOperations = new ProviderOperations();
58         providerOperations.setUrl(url);
59
60         // Set Basic Auth
61         String user = props.getProperty("provider.user");
62         String pass = props.getProperty("provider.pass");
63         providerOperations.setAuthentication(user, pass);
64     }
65
66     @Override
67     public void run() {
68         // Some vars for benchmarking
69         startTime = System.currentTimeMillis();
70
71         LOG.info("Running DMaaP Listener");
72
73         while (run.get()) {
74             // Only update if the queue is low. otherwise we read in more
75             // messages than we need
76             try {
77                 if (executor.getQueue().size() <= QUEUED_MIN) {
78                     LOG.debug("DMaaP queue running low. Querying for more jobs");
79
80
81                     List<DmaapIncomingMessage> messages = dmaap.getIncomingEvents(DmaapIncomingMessage.class, QUEUED_MAX);
82                     LOG.debug(String.format("Read %d messages from dmaap", messages.size()));
83                     for (DmaapIncomingMessage incoming : messages) {
84                         // Acknowledge that we read the event
85                         if (isValid(incoming)) {
86                             String requestIdWithSubId = getRequestIdWithSubId(incoming.getBody());
87                             LOG.info("Acknowledging Message: " + requestIdWithSubId);
88 //                            dmaap.postStatus(incoming.toOutgoing(OperationStatus.PENDING));
89                         }
90                     }
91                     for (DmaapIncomingMessage incoming : messages) {
92                         String requestIdWithSubId = getRequestIdWithSubId(incoming.getBody());
93                         // Add to pool if still running
94                         if (run.get()) {
95                             if (isValid(incoming)) {
96                                 LOG.info(String.format("Adding DMaaP message to pool queue [%s]", requestIdWithSubId));
97                                 try {
98                                     executor.execute(new WorkerImpl(incoming, dmaap, providerOperations));
99                                 } catch (RejectedExecutionException rejectEx) {
100                                     LOG.error("Task Rejected: ", rejectEx);
101                                 }
102                             } else {
103                                 // Badly formed message
104                                 LOG.error("Message was not valid. Rejecting message: "+incoming);
105                             }
106                         } else {
107                             if (isValid(incoming)) {
108                                 LOG.info("Run stopped. Orphaning Message: " + requestIdWithSubId);
109                             }
110                             else {
111                                 // Badly formed message
112                                 LOG.error("Message was not valid. Rejecting message: "+incoming);
113                             }
114                         }
115                     }
116                 }
117             } catch (Exception e) {
118                 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
119                 LOG.error(EELFResourceManager.format(e));
120                 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
121             }
122         }
123
124         LOG.info("Stopping DMaaP Listener thread");
125
126         // We've told the listener to stop
127         // TODO - Should we:
128         // 1) Put a message back on the queue indicating that APP-C never got to
129         // the message
130         // or
131         // 2) Let downstream figure it out after timeout between PENDING and
132         // ACTIVE messages
133     }
134
135     private boolean isValid(DmaapIncomingMessage incoming) {
136         return ((incoming != null) &&
137                 incoming.getBody() != null
138                 && !StringUtils.isEmpty(incoming.getRpcName()));
139     }
140
141     @Override
142     public String getBenchmark() {
143         long time = System.currentTimeMillis();
144         DateFormat df = new SimpleDateFormat("HH:mm:ss");
145         df.setTimeZone(TimeZone.getTimeZone("UTC"));
146         String runningTime = df.format(new Date(time - startTime));
147
148         String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
149                 executor.getCompletedTaskCount(), executor.getPoolSize());
150         LOG.info("***BENCHMARK*** " + out);
151         return out;
152     }
153
154     private String getRequestIdWithSubId(JsonNode event){
155         String requestId = "";
156         try {
157             requestId = Converter.extractRequestIdWithSubId(event);
158         } catch (Exception e) {
159             LOG.error("failed to parse request-id and sub-request-id. Json not in expected format", e);
160         }
161         return requestId;
162     }
163 }