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