summaryrefslogtreecommitdiffstats
path: root/services/appc-dmaap-service/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/WorkerImpl.java
blob: 870871da321382043f38997527deaad3a908c14c (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * 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.onap.appc.listener.LCM.impl;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.onap.appc.exceptions.APPCException;
import org.onap.appc.listener.EventHandler;
import org.onap.appc.listener.LCM.conv.Converter;
import org.onap.appc.listener.LCM.model.DmaapMessage;
import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
import org.onap.appc.listener.LCM.operation.ProviderOperations;

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 DmaapMessage event;

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

    //so we know were to post the messages
    private final ProviderOperations providerOperations;


    public WorkerImpl(DmaapMessage message, EventHandler dmaap, ProviderOperations providerOperations) {
        this.event = message;
        this.dmaap = dmaap;
        this.providerOperations = providerOperations;
    }

    @Override
    public void run() {

        if (checkParametersForNull(event, dmaap, providerOperations)) {
            throw new IllegalStateException("Cannot run worker. One of its parameters is null");
        }

        String requestIdWithSubId = extractRequestIdWithSubId(event.getBody());
        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(event.getRpcName(), event.getBody());
            DmaapOutgoingMessage dmaapOutgoingMessage = Converter
                .convertJsonNodeToDmaapOutgoingMessage(event, outputJsonNode);
            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, e);
            postMessageToDMaaP(dmaapOutgoingMessage, requestIdWithSubId);
        }

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

    private boolean checkParametersForNull(DmaapMessage message, EventHandler dmaap,
        ProviderOperations providerOperations) {

        return message == null || dmaap == null || providerOperations == null;
    }

    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.convertDmaapOutgoingMessageToJsonString(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);
    }
}