2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.listener.LCM.impl;
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;
37 public class WorkerImpl implements Runnable {
39 private final EELFLogger LOG = EELFManager.getInstance().getLogger(WorkerImpl.class);
41 // Should have all of the data we need for processing
42 private DmaapMessage event;
44 // So we can post messages from inside the worker.
45 private EventHandler dmaap;
47 //so we know were to post the messages
48 private final ProviderOperations providerOperations;
51 public WorkerImpl(DmaapMessage message, EventHandler dmaap, ProviderOperations providerOperations) {
54 this.providerOperations = providerOperations;
60 if (checkParametersForNull(event, dmaap, providerOperations)) {
61 throw new IllegalStateException("Cannot run worker. One of its parameters is null");
64 String requestIdWithSubId = extractRequestIdWithSubId(event.getBody());
65 LOG.debug(String.format("Started working on %s", requestIdWithSubId));
67 // Run the dg in a try catch to handle all exceptions and update the message at the end
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));
78 LOG.warn(String.format("Event %s failed", requestIdWithSubId));
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);
90 LOG.debug("Done working on " + requestIdWithSubId);
93 private boolean checkParametersForNull(DmaapMessage message, EventHandler dmaap,
94 ProviderOperations providerOperations) {
96 return message == null || dmaap == null || providerOperations == null;
99 private Integer extractStatusCode(JsonNode event) {
100 Integer statusCode = null;
102 statusCode = Converter.extractStatusCode(event);
103 } catch (Exception e) {
104 LOG.error("failed to parse statusCode. Json not in expected format", e);
110 private String extractRequestIdWithSubId(JsonNode event) {
111 String requestId = "";
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);
121 private void postMessageToDMaaP(DmaapOutgoingMessage dmaapOutgoingMessage, String requestIdWithSubId) {
122 String dmaapOutgoingMessageJsonString;
124 dmaapOutgoingMessageJsonString = Converter.convertDmaapOutgoingMessageToJsonString(dmaapOutgoingMessage);
125 dmaap.postStatus(dmaapOutgoingMessage.getCambriaPartition(), dmaapOutgoingMessageJsonString);
126 } catch (JsonProcessingException e) {
128 "failed to postMessageToDMaaP requestIdWithSubId: " + requestIdWithSubId + " dmaapOutgoingMessage: "
129 + dmaapOutgoingMessage, e);
133 private JsonNode doDG(String rpcName, JsonNode msg) throws APPCException {
134 return providerOperations.topologyDG(rpcName, msg);