Changed to unmaintained
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / LCM / impl / WorkerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.listener.LCM.impl;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import org.onap.appc.exceptions.APPCException;
31 import org.onap.appc.listener.EventHandler;
32 import org.onap.appc.listener.LCM.conv.Converter;
33 import org.onap.appc.listener.LCM.model.DmaapMessage;
34 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
35 import org.onap.appc.listener.LCM.operation.ProviderOperations;
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
60         if (checkParametersForNull(event, dmaap, providerOperations)) {
61             throw new IllegalStateException("Cannot run worker. One of its parameters is null");
62         }
63
64         String requestIdWithSubId = extractRequestIdWithSubId(event.getBody());
65         LOG.debug(String.format("Started working on %s", requestIdWithSubId));
66
67         // Run the dg in a try catch to handle all exceptions and update the message at the end
68         try {
69
70             JsonNode outputJsonNode = doDG(event.getRpcName(), event.getBody());
71             DmaapOutgoingMessage dmaapOutgoingMessage = Converter
72                 .convertJsonNodeToDmaapOutgoingMessage(event, outputJsonNode);
73             postMessageToDMaaP(dmaapOutgoingMessage, requestIdWithSubId);
74             Integer statusCode = extractStatusCode(dmaapOutgoingMessage.getBody());
75             if (ProviderOperations.isSucceeded(statusCode)) {
76                 LOG.debug(String.format("Event %s finished successfully", requestIdWithSubId));
77             } else {
78                 LOG.warn(String.format("Event %s failed", requestIdWithSubId));
79             }
80
81         } catch (Exception e) {
82             // Unknown exception from DG method. Fail and pass the exception along
83             String msg = "Exception: " + e.getMessage();
84             LOG.error(String.format("Event %s finished with failure. %s", requestIdWithSubId, msg));
85             DmaapOutgoingMessage dmaapOutgoingMessage = Converter
86                 .buildDmaapOutgoingMessageWithUnexpectedError(event, e);
87             postMessageToDMaaP(dmaapOutgoingMessage, requestIdWithSubId);
88         }
89
90         LOG.debug("Done working on " + requestIdWithSubId);
91     }
92
93     private boolean checkParametersForNull(DmaapMessage message, EventHandler dmaap,
94         ProviderOperations providerOperations) {
95
96         return message == null || dmaap == null || providerOperations == null;
97     }
98
99     private Integer extractStatusCode(JsonNode event) {
100         Integer statusCode = null;
101         try {
102             statusCode = Converter.extractStatusCode(event);
103         } catch (Exception e) {
104             LOG.error("failed to parse statusCode. Json not in expected format", e);
105         }
106         return statusCode;
107     }
108
109
110     private String extractRequestIdWithSubId(JsonNode event) {
111         String requestId = "";
112         try {
113             requestId = Converter.extractRequestIdWithSubId(event);
114         } catch (Exception e) {
115             LOG.error("failed to parse request-id and sub-request-id. Json not in expected format", e);
116         }
117         return requestId;
118     }
119
120
121     private void postMessageToDMaaP(DmaapOutgoingMessage dmaapOutgoingMessage, String requestIdWithSubId) {
122         String dmaapOutgoingMessageJsonString;
123         try {
124             dmaapOutgoingMessageJsonString = Converter.convertDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
125             dmaap.postStatus(dmaapOutgoingMessage.getCambriaPartition(), dmaapOutgoingMessageJsonString);
126         } catch (JsonProcessingException e) {
127             LOG.error(
128                 "failed to postMessageToDMaaP requestIdWithSubId: " + requestIdWithSubId + " dmaapOutgoingMessage: "
129                     + dmaapOutgoingMessage, e);
130         }
131     }
132
133     private JsonNode doDG(String rpcName, JsonNode msg) throws APPCException {
134         return providerOperations.topologyDG(rpcName, msg);
135     }
136 }