aboutsummaryrefslogtreecommitdiffstats
path: root/appc-event-listener/appc-event-listener-bundle/src/main/java/org/openecomp/appc/listener/LCM/impl/WorkerImpl.java
blob: af8334ee4e183c228260ca4be557956e27c891e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.listener.LCM.impl;

import org.openecomp.appc.exceptions.APPCException;
import org.openecomp.appc.listener.EventHandler;
import org.openecomp.appc.listener.LCM.conv.Converter;
import org.openecomp.appc.listener.LCM.model.DmaapOutgoingMessage;
import org.openecomp.appc.listener.LCM.operation.ProviderOperations;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;

public class WorkerImpl implements Runnable {

    private final EELFLogger LOG = EELFManager.getInstance().getLogger(WorkerImpl.class);

    // Should have all of the data we need for processing
    private JsonNode event;

    // So we can post messages from inside the worker.
    private EventHandler dmaap;
    private String rpcName;

    public WorkerImpl(String rpcName,JsonNode message, EventHandler dmaap) {
        this.rpcName = rpcName;
        this.event = message;
        this.dmaap = dmaap;
    }

    @Override
    public void run() {
        String requestIdWithSubId = extractRequestIdWithSubId(event);
        LOG.debug(String.format("Started working on %s", requestIdWithSubId));

        // Run the dg in a try catch to handle all exceptions and update the
        // message at the end
        try {
            JsonNode outputJsonNode = doDG(rpcName, event);
            DmaapOutgoingMessage dmaapOutgoingMessage= Converter.convJsonNodeToDmaapOutgoingMessage(outputJsonNode,rpcName);
            postMessageToDMaaP(dmaapOutgoingMessage,requestIdWithSubId);
            Integer statusCode = extractStatusCode(dmaapOutgoingMessage.getBody());
            if (ProviderOperations.isSucceeded(statusCode)) {
                LOG.debug(String.format("Event %s finished successfully", requestIdWithSubId));
            } else {
                LOG.warn(String.format("Event %s failed", requestIdWithSubId));
            }

        } catch (Exception e) {
            // Unknown exception from DG method. Fail and pass the exception
            // along
            String msg = "Exception: " + e.getMessage();
            LOG.error(String.format("Event %s finished with failure. %s", requestIdWithSubId, msg));
            DmaapOutgoingMessage dmaapOutgoingMessage= Converter.buildDmaapOutgoingMessageWithUnexpectedError(event,rpcName,e);
            postMessageToDMaaP(dmaapOutgoingMessage,requestIdWithSubId);
        }

        LOG.debug("Done working on " + requestIdWithSubId);
    }


    private Integer extractStatusCode(JsonNode event) {
        Integer statusCode = null;
        try {
            statusCode = Converter.extractStatusCode(event);
        } catch (Exception e) {
            LOG.error("failed to parse statusCode. Json not in expected format", e);
        }
        return statusCode;
    }


    private String extractRequestIdWithSubId(JsonNode event){
        String requestId = "";
        try {
            requestId = Converter.extractRequestIdWithSubId(event);
        } catch (Exception e) {
            LOG.error("failed to parse request-id and sub-request-id. Json not in expected format", e);
        }
        return requestId;
    }



    private void postMessageToDMaaP(DmaapOutgoingMessage dmaapOutgoingMessage,String requestIdWithSubId) {
        String dmaapOutgoingMessageJsonString;
        try {
            dmaapOutgoingMessageJsonString = Converter.convDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
            dmaap.postStatus(dmaapOutgoingMessage.getCambriaPartition(),dmaapOutgoingMessageJsonString);
        } catch (JsonProcessingException e) {
            LOG.error("failed to postMessageToDMaaP requestIdWithSubId: "+requestIdWithSubId+" dmaapOutgoingMessage: "+dmaapOutgoingMessage, e);
        }
    }

    private JsonNode doDG(String rpcName, JsonNode msg) throws APPCException {
        return ProviderOperations.topologyDG(rpcName,msg);
    }
}