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