2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.LCM.impl;
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;
38 public class WorkerImpl implements Runnable {
40 private final EELFLogger LOG = EELFManager.getInstance().getLogger(WorkerImpl.class);
42 // Should have all of the data we need for processing
43 private DmaapMessage event;
45 // So we can post messages from inside the worker.
46 private EventHandler dmaap;
48 //so we know were to post the messages
49 private final ProviderOperations providerOperations;
52 public WorkerImpl(DmaapMessage message, EventHandler dmaap, ProviderOperations providerOperations) {
55 this.providerOperations = providerOperations;
61 if (checkParametersForNull(event, dmaap, providerOperations)) {
62 throw new IllegalStateException("Cannot run worker. One of its parameters is null");
65 String requestIdWithSubId = extractRequestIdWithSubId(event.getBody());
66 LOG.debug(String.format("Started working on %s", requestIdWithSubId));
68 // Run the dg in a try catch to handle all exceptions and update the message at the end
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));
79 LOG.warn(String.format("Event %s failed", requestIdWithSubId));
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);
91 LOG.debug("Done working on " + requestIdWithSubId);
94 private boolean checkParametersForNull(DmaapMessage message, EventHandler dmaap,
95 ProviderOperations providerOperations) {
97 return message == null || dmaap == null || providerOperations == null;
100 private Integer extractStatusCode(JsonNode event) {
101 Integer statusCode = null;
103 statusCode = Converter.extractStatusCode(event);
104 } catch (Exception e) {
105 LOG.error("failed to parse statusCode. Json not in expected format", e);
111 private String extractRequestIdWithSubId(JsonNode event) {
112 String requestId = "";
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);
122 private void postMessageToDMaaP(DmaapOutgoingMessage dmaapOutgoingMessage, String requestIdWithSubId) {
123 String dmaapOutgoingMessageJsonString;
125 dmaapOutgoingMessageJsonString = Converter.convertDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
126 dmaap.postStatus(dmaapOutgoingMessage.getCambriaPartition(), dmaapOutgoingMessageJsonString);
127 } catch (JsonProcessingException e) {
129 "failed to postMessageToDMaaP requestIdWithSubId: " + requestIdWithSubId + " dmaapOutgoingMessage: "
130 + dmaapOutgoingMessage, e);
134 private JsonNode doDG(String rpcName, JsonNode msg) throws APPCException {
135 return providerOperations.topologyDG(rpcName, msg);