da7e6e50cb6658c558cf055f10a631ff3b60be09
[aai/gizmo.git] / src / main / java / org / onap / crud / service / CrudAsyncResponseConsumer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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  * ============LICENSE_END=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.service;
25
26 import java.util.TimerTask;
27
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.crud.event.GraphEvent;
31 import org.onap.crud.logging.CrudServiceMsgs;
32
33 import com.att.ecomp.event.api.EventConsumer;
34
35 public class CrudAsyncResponseConsumer extends TimerTask {
36
37   private static Logger logger = LoggerFactory.getInstance().getLogger(CrudAsyncResponseConsumer
38                                                                        .class.getName());
39
40   private static Logger auditLogger = LoggerFactory.getInstance()
41     .getAuditLogger(CrudAsyncResponseConsumer.class.getName());
42
43   private EventConsumer asyncResponseConsumer;
44
45  
46   public CrudAsyncResponseConsumer(EventConsumer asyncResponseConsumer) {
47     this.asyncResponseConsumer = asyncResponseConsumer;
48     logger.info(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_INFO,
49                 "CrudAsyncResponseConsumer initialized SUCCESSFULLY! with event consumer "
50                 + asyncResponseConsumer.getClass().getName());
51   }
52
53
54   @Override
55   public void run() {
56
57     logger.info(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_INFO, "Listening for graph events");
58
59     if (asyncResponseConsumer == null) {
60       logger.error(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_ERROR,
61                    "Unable to initialize CrudAsyncRequestProcessor");
62     }
63
64     Iterable<String> events = null;
65     try {
66       events = asyncResponseConsumer.consume();
67     } catch (Exception e) {
68       logger.error(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_ERROR, e.getMessage());
69       return;
70     }
71
72     if (events == null || !events.iterator().hasNext()) {
73       logger.info(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_INFO, "No events recieved");
74
75     }
76
77     for (String event : events) {
78       try {
79
80         GraphEvent graphEvent = GraphEvent.fromJson(event);
81         auditLogger.info(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_INFO,
82                            "Event received of type: " + graphEvent.getObjectType() + " with key: "
83                            + graphEvent.getObjectKey() + " , transaction-id: "
84                            + graphEvent.getTransactionId() + " , operation: "
85                            + graphEvent.getOperation().toString());
86           logger.info(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_INFO,
87                       "Event received of type: " + graphEvent.getObjectType() + " with key: "
88                       + graphEvent.getObjectKey() + " , transaction-id: "
89                       + graphEvent.getTransactionId() + " , operation: "
90                       + graphEvent.getOperation().toString());
91           logger.debug(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_INFO,
92                        "Event received with payload:" + event);
93
94         if (CrudAsyncGraphEventCache.get(graphEvent.getTransactionId()) != null) {
95           CrudAsyncGraphEventCache.get(graphEvent.getTransactionId())
96             .populateGraphEvent(graphEvent);
97         } else {
98           logger.error(CrudServiceMsgs.ASYNC_DATA_SERVICE_ERROR,
99                        "Request timed out. Not sending response for transaction-id: "
100                        + graphEvent.getTransactionId());
101         }
102
103       } catch (Exception e) {
104         logger.error(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_ERROR, e.getMessage());
105       }
106     }
107
108     try {
109       asyncResponseConsumer.commitOffsets();
110     } catch (Exception e) {
111       logger.error(CrudServiceMsgs.ASYNC_RESPONSE_CONSUMER_ERROR, e.getMessage());
112     }
113
114   }
115
116 }