Merge of new rebased code
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / LCM / impl / WorkerImpl.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.LCM.impl;
23
24 import org.openecomp.appc.exceptions.APPCException;
25 import org.openecomp.appc.listener.EventHandler;
26 import org.openecomp.appc.listener.LCM.conv.Converter;
27 import org.openecomp.appc.listener.LCM.model.DmaapMessage;
28 import org.openecomp.appc.listener.LCM.model.DmaapOutgoingMessage;
29 import org.openecomp.appc.listener.LCM.operation.ProviderOperations;
30
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.databind.JsonNode;
36
37 public class WorkerImpl implements Runnable {
38
39     private final EELFLogger LOG = EELFManager.getInstance().getLogger(WorkerImpl.class);
40
41     // Should have all of the data we need for processing
42     private DmaapMessage event;
43
44     // So we can post messages from inside the worker.
45     private EventHandler dmaap;
46
47     //so we know were to post the messages
48     private final ProviderOperations providerOperations;
49
50
51     public WorkerImpl(DmaapMessage message, EventHandler dmaap, ProviderOperations providerOperations) {
52         this.event = message;
53         this.dmaap = dmaap;
54         this.providerOperations = providerOperations;
55     }
56
57     @Override
58     public void run() {
59         String requestIdWithSubId = extractRequestIdWithSubId(event.getBody());
60         LOG.debug(String.format("Started working on %s", requestIdWithSubId));
61
62         // Run the dg in a try catch to handle all exceptions and update the
63         // message at the end
64         try {
65             JsonNode outputJsonNode = doDG(event.getRpcName(), event.getBody());
66             DmaapOutgoingMessage dmaapOutgoingMessage= Converter.convJsonNodeToDmaapOutgoingMessage(event, outputJsonNode);
67             postMessageToDMaaP(dmaapOutgoingMessage,requestIdWithSubId);
68             Integer statusCode = extractStatusCode(dmaapOutgoingMessage.getBody());
69             if (ProviderOperations.isSucceeded(statusCode)) {
70                 LOG.debug(String.format("Event %s finished successfully", requestIdWithSubId));
71             } else {
72                 LOG.warn(String.format("Event %s failed", requestIdWithSubId));
73             }
74
75         } catch (Exception e) {
76             // Unknown exception from DG method. Fail and pass the exception
77             // along
78             String msg = "Exception: " + e.getMessage();
79             LOG.error(String.format("Event %s finished with failure. %s", requestIdWithSubId, msg));
80             DmaapOutgoingMessage dmaapOutgoingMessage= Converter.buildDmaapOutgoingMessageWithUnexpectedError(event, e);
81             postMessageToDMaaP(dmaapOutgoingMessage,requestIdWithSubId);
82         }
83
84         LOG.debug("Done working on " + requestIdWithSubId);
85     }
86
87
88     private Integer extractStatusCode(JsonNode event) {
89         Integer statusCode = null;
90         try {
91             statusCode = Converter.extractStatusCode(event);
92         } catch (Exception e) {
93             LOG.error("failed to parse statusCode. Json not in expected format", e);
94         }
95         return statusCode;
96     }
97
98
99     private String extractRequestIdWithSubId(JsonNode event){
100         String requestId = "";
101         try {
102             requestId = Converter.extractRequestIdWithSubId(event);
103         } catch (Exception e) {
104             LOG.error("failed to parse request-id and sub-request-id. Json not in expected format", e);
105         }
106         return requestId;
107     }
108
109
110
111     private void postMessageToDMaaP(DmaapOutgoingMessage dmaapOutgoingMessage,String requestIdWithSubId) {
112         String dmaapOutgoingMessageJsonString;
113         try {
114             dmaapOutgoingMessageJsonString = Converter.convDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
115             dmaap.postStatus(dmaapOutgoingMessage.getCambriaPartition(),dmaapOutgoingMessageJsonString);
116         } catch (JsonProcessingException e) {
117             LOG.error("failed to postMessageToDMaaP requestIdWithSubId: "+requestIdWithSubId+" dmaapOutgoingMessage: "+dmaapOutgoingMessage, e);
118         }
119     }
120
121     private JsonNode doDG(String rpcName, JsonNode msg) throws APPCException {
122         return providerOperations.topologyDG(rpcName,msg);
123     }
124 }