Add junit coverage to RequestInfoBuilder class
[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 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 com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import org.onap.appc.exceptions.APPCException;
32 import org.onap.appc.listener.EventHandler;
33 import org.onap.appc.listener.LCM.conv.Converter;
34 import org.onap.appc.listener.LCM.model.DmaapMessage;
35 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
36 import org.onap.appc.listener.LCM.operation.ProviderOperations;
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
61         if (checkParametersForNull(event, dmaap, providerOperations)) {
62             throw new IllegalStateException("Cannot run worker. One of its parameters is null");
63         }
64
65         String requestIdWithSubId = extractRequestIdWithSubId(event.getBody());
66         LOG.debug(String.format("Started working on %s", requestIdWithSubId));
67
68         // Run the dg in a try catch to handle all exceptions and update the message at the end
69         try {
70
71             JsonNode outputJsonNode = doDG(event.getRpcName(), event.getBody());
72             DmaapOutgoingMessage dmaapOutgoingMessage = Converter
73                 .convertJsonNodeToDmaapOutgoingMessage(event, outputJsonNode);
74             postMessageToDMaaP(dmaapOutgoingMessage, requestIdWithSubId);
75             Integer statusCode = extractStatusCode(dmaapOutgoingMessage.getBody());
76             if (ProviderOperations.isSucceeded(statusCode)) {
77                 LOG.debug(String.format("Event %s finished successfully", requestIdWithSubId));
78             } else {
79                 LOG.warn(String.format("Event %s failed", requestIdWithSubId));
80             }
81
82         } catch (Exception e) {
83             // Unknown exception from DG method. Fail and pass the exception along
84             String msg = "Exception: " + e.getMessage();
85             LOG.error(String.format("Event %s finished with failure. %s", requestIdWithSubId, msg));
86             DmaapOutgoingMessage dmaapOutgoingMessage = Converter
87                 .buildDmaapOutgoingMessageWithUnexpectedError(event, e);
88             postMessageToDMaaP(dmaapOutgoingMessage, requestIdWithSubId);
89         }
90
91         LOG.debug("Done working on " + requestIdWithSubId);
92     }
93
94     private boolean checkParametersForNull(DmaapMessage message, EventHandler dmaap,
95         ProviderOperations providerOperations) {
96
97         return message == null || dmaap == null || providerOperations == null;
98     }
99
100     private Integer extractStatusCode(JsonNode event) {
101         Integer statusCode = null;
102         try {
103             statusCode = Converter.extractStatusCode(event);
104         } catch (Exception e) {
105             LOG.error("failed to parse statusCode. Json not in expected format", e);
106         }
107         return statusCode;
108     }
109
110
111     private String extractRequestIdWithSubId(JsonNode event) {
112         String requestId = "";
113         try {
114             requestId = Converter.extractRequestIdWithSubId(event);
115         } catch (Exception e) {
116             LOG.error("failed to parse request-id and sub-request-id. Json not in expected format", e);
117         }
118         return requestId;
119     }
120
121
122     private void postMessageToDMaaP(DmaapOutgoingMessage dmaapOutgoingMessage, String requestIdWithSubId) {
123         String dmaapOutgoingMessageJsonString;
124         try {
125             dmaapOutgoingMessageJsonString = Converter.convertDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
126             dmaap.postStatus(dmaapOutgoingMessage.getCambriaPartition(), dmaapOutgoingMessageJsonString);
127         } catch (JsonProcessingException e) {
128             LOG.error(
129                 "failed to postMessageToDMaaP requestIdWithSubId: " + requestIdWithSubId + " dmaapOutgoingMessage: "
130                     + dmaapOutgoingMessage, e);
131         }
132     }
133
134     private JsonNode doDG(String rpcName, JsonNode msg) throws APPCException {
135         return providerOperations.topologyDG(rpcName, msg);
136     }
137 }