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.dg.common.impl;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
32 import java.util.Arrays;
33 import java.util.HashSet;
35 import java.util.Properties;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.appc.adapter.message.MessageAdapterFactory;
39 import org.onap.appc.adapter.message.Producer;
40 import org.onap.appc.configuration.Configuration;
41 import org.onap.appc.configuration.ConfigurationFactory;
42 import org.onap.appc.dg.common.IntermediateMessageSender;
43 import org.onap.appc.exceptions.APPCException;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.FrameworkUtil;
47 import org.osgi.framework.ServiceReference;
49 public class IntermediateMessageSenderImpl implements IntermediateMessageSender {
51 private final EELFLogger logger = EELFManager.getInstance().getLogger(IntermediateMessageSenderImpl.class);
53 private static final String PARAM_MESSAGE = "message";
54 private static final String ATTR_REQUEST_ID = "input.common-header.request-id";
56 private Producer producer;
58 private static final String STATUS = "STATUS";
59 private static final String FAILURE = "FAILURE";
60 private static final String SUCCESS = "SUCCESS";
61 private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
63 private static final String RESPONSE = "response";
64 private static final String MSO = "MSO";
67 Properties properties = ConfigurationFactory.getConfiguration().getProperties();
69 String writeTopic = properties.getProperty("appc.LCM.topic.write");
70 String apiKey = properties.getProperty("appc.LCM.client.key");
71 String apiSecret = properties.getProperty("appc.LCM.client.secret");
72 String hostNames = properties.getProperty("appc.LCM.poolMembers");
74 logger.debug("Configuration Read : writeTopic = " + writeTopic + ", " +
75 "hostNames = " + hostNames);
77 Set<String> pool = new HashSet<>();
78 if (!StringUtils.isEmpty(hostNames)) {
79 pool.addAll(Arrays.asList(hostNames.split(",")));
82 BundleContext ctx = FrameworkUtil.getBundle(IntermediateMessageSenderImpl.class).getBundleContext();
84 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
86 producer = ((MessageAdapterFactory) ctx.getService(svcRef))
87 .createProducer(pool, writeTopic, apiKey, apiSecret);
93 public void sendMessage(Map<String, String> params, SvcLogicContext context) {
94 String prefix = params.get("prefix");
95 prefix = StringUtils.isEmpty(prefix) ? "" : prefix + ".";
97 validateInputs(params, context);
98 String jsonMessage = getJsonMessage(params, context);
99 logger.debug("Constructed JSON Message : " + jsonMessage);
100 producer.post("", jsonMessage);
101 context.setAttribute(prefix + STATUS, SUCCESS);
102 } catch (Exception e) {
103 String errorMessage = "Error sending intermediate message to initiator " + e.getMessage();
104 context.setAttribute(prefix + STATUS, FAILURE);
105 context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
106 logger.error(errorMessage, e);
110 private void validateInputs(Map<String, String> params, SvcLogicContext context) throws APPCException {
111 String code = params.get("code");
112 String message = params.get(PARAM_MESSAGE);
113 if (StringUtils.isEmpty(code) || StringUtils.isEmpty(message)) {
114 throw new APPCException("code or message is empty");
116 String requestId = context.getAttribute(ATTR_REQUEST_ID);
117 if (StringUtils.isEmpty(requestId)) {
118 throw new APPCException("requestId is empty");
122 private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
123 ObjectMapper objectMapper = new ObjectMapper();
125 ObjectNode commonHeader = getCommonHeader(context);
126 ObjectNode status = getStatus(params);
128 ObjectNode output = objectMapper.createObjectNode();
129 output.put("common-header", commonHeader);
130 output.put("status", status);
132 ObjectNode body = objectMapper.createObjectNode();
133 body.put("output", output);
135 ObjectNode root = objectMapper.createObjectNode();
136 root.put("type", RESPONSE);
137 root.put("rpc-name", context.getAttribute("input.action"));
138 root.put("cambria.partition", MSO);
139 root.put("correlation-id", getCorrelationId(context));
140 root.put("body", body);
142 return root.toString();
145 private String getCorrelationId(SvcLogicContext context) {
146 String requestId = context.getAttribute(ATTR_REQUEST_ID);
147 String subRequestId = context.getAttribute("input.common-header.sub-request-id");
148 return requestId + (StringUtils.isEmpty(subRequestId) ? "" : ("-" + subRequestId));
151 private ObjectNode getStatus(Map<String, String> params) {
152 ObjectMapper objectMapper = new ObjectMapper();
153 ObjectNode status = objectMapper.createObjectNode();
154 status.put("code", params.get("code"));
155 status.put(PARAM_MESSAGE, params.get(PARAM_MESSAGE));
159 private ObjectNode getCommonHeader(SvcLogicContext context) {
160 ObjectMapper objectMapper = new ObjectMapper();
161 ObjectNode commonHeader = objectMapper.createObjectNode();
162 commonHeader.put("api-ver", context.getAttribute("input.common-header.api-ver"));
163 commonHeader.put("timestamp", context.getAttribute("input.common-header.timestamp"));
164 commonHeader.put("originator-id", context.getAttribute("input.common-header.originator-id"));
165 commonHeader.put("request-id", context.getAttribute(ATTR_REQUEST_ID));
166 commonHeader.put("sub-request-id", context.getAttribute("input.common-header.sub-request-id"));