033951612da206f0dcd41eea85f2f7ac8de75faa
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / dmaap / DmaapConsumer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.client.dmaap;
22
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.util.concurrent.TimeUnit;
26
27 import org.openecomp.mso.client.dmaap.exceptions.DMaaPConsumerFailure;
28 import org.openecomp.mso.client.dmaap.exceptions.ExceededMaximumPollingTime;
29 import org.openecomp.mso.client.dmaap.rest.RestConsumer;
30
31 import com.google.common.base.Stopwatch;
32
33 public abstract class DmaapConsumer extends DmaapClient {
34
35         public DmaapConsumer() throws FileNotFoundException, IOException {
36                 super("dmaap/default-consumer.properties");
37         }
38         
39         public Consumer getConsumer() throws FileNotFoundException, IOException {
40                 return new RestConsumer(this.properties);
41         }
42         public boolean consume() throws Exception {
43                 
44                 Consumer mrConsumer = this.getConsumer();
45                 int iterations = 0;
46                 boolean accepted = false;
47                 Stopwatch stopwatch = Stopwatch.createUnstarted();
48                 try {
49                         while (this.continuePolling()) {
50                                 if (stopwatch.elapsed(TimeUnit.MILLISECONDS) >= this.getMaximumElapsedTime()) {
51                                         final String message = "exceeded maximum retries on " + this.getRequestId() + " on " + this.getTopic();
52                                         auditLogger.error(message);
53                                         throw new ExceededMaximumPollingTime(message);
54                                 }
55                                 stopwatch.start();
56                                 Iterable<String> itr = mrConsumer.fetch();
57                                 stopwatch.stop();
58                                 for (String message : itr) {
59                                         if (!accepted && this.isAccepted(message)) {
60                                                 auditLogger.info("accepted message found for " + this.getRequestId() + " on " + this.getTopic());
61                                                 accepted = true;
62                                         } 
63                                         if (accepted) {
64                                                 if (this.isFailure(message)) {
65                                                         this.stopProcessingMessages();
66                                                         auditLogger.info("received dmaap message: " + message);
67                                                         final String errorMsg = "failure received from dmaap topic " + this.getTopic();
68                                                         auditLogger.error(errorMsg);
69                                                         throw new DMaaPConsumerFailure(errorMsg);
70                                                 } else {
71                                                         auditLogger.info("received dmaap message: " + message);
72                                                         this.processMessage(message);
73                                                 }
74                                         }
75                                 }
76                                 iterations++;
77                         }
78                         return true;
79                 } catch (Exception e ) {
80                         throw e;
81                 } finally {
82                         if (stopwatch.isRunning()) {
83                                 stopwatch.stop();
84                         }
85                 }
86         }
87         
88         /**
89          * Should this consumer continue to consume messages from the topic?
90          * @return
91          */
92         public abstract boolean continuePolling();
93         /**
94          * Process a message from a DMaaP topic
95          * 
96          * @param message
97          * @throws Exception
98          */
99         public abstract void processMessage(String message) throws Exception;
100         /**
101          * Has the request been accepted by the receiving system?
102          * Should the consumer move to processing messages?
103          * 
104          * @param message
105          * @return
106          */
107         public abstract boolean isAccepted(String message);
108         /**
109          * has the request failed?
110          * 
111          * @param message
112          * @return
113          */
114         public abstract boolean isFailure(String message);
115         /**
116          * The request id to filter messages on
117          * @return
118          */
119         public abstract String getRequestId();
120         /**
121          * Logic that defines when the consumer should stop processing messages
122          */
123         public abstract void stopProcessingMessages();
124         
125         /**
126          * time in milliseconds
127          */
128         public int getMaximumElapsedTime() {
129                 return 180000;
130         }
131 }