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