2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.common.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
30 import org.apache.commons.lang.StringUtils;
31 import org.openecomp.appc.adapter.message.MessageAdapterFactory;
32 import org.openecomp.appc.adapter.message.Producer;
33 import org.openecomp.appc.configuration.Configuration;
34 import org.openecomp.appc.configuration.ConfigurationFactory;
35 import org.openecomp.appc.dg.common.IntermediateMessageSender;
36 import org.openecomp.appc.exceptions.APPCException;
37 import org.openecomp.sdnc.sli.SvcLogicContext;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.osgi.framework.ServiceReference;
42 import java.util.HashSet;
44 import java.util.Properties;
47 public class IntermediateMessageSenderImpl implements IntermediateMessageSender {
50 private Producer producer;
52 private Configuration configuration;
54 private final EELFLogger logger = EELFManager.getInstance().getLogger(IntermediateMessageSenderImpl.class);
56 private static final String STATUS = "STATUS";
57 private static final String FAILURE = "FAILURE";
58 private static final String SUCCESS = "SUCCESS";
59 private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
61 private static final String RESPONSE = "response";
62 private static final String MSO = "MSO";
65 configuration = ConfigurationFactory.getConfiguration();
66 Properties properties=configuration.getProperties();
68 String writeTopic = properties.getProperty("appc.LCM.topic.write");
69 String apiKey = properties.getProperty("appc.LCM.client.key");
70 String apiSecret = properties.getProperty("appc.LCM.client.secret");
71 String hostNames = properties.getProperty("appc.LCM.poolMembers");
73 logger.debug("Configuration Read : writeTopic = " + writeTopic +", " +
74 "hostNames = " + hostNames);
76 Set<String> pool = new HashSet<>();
77 if (!StringUtils.isEmpty(hostNames)) {
78 for (String name : hostNames.split(",")) {
83 BundleContext ctx = FrameworkUtil.getBundle(IntermediateMessageSenderImpl.class).getBundleContext();
85 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
87 producer = ((MessageAdapterFactory) ctx.getService(svcRef)).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);
104 String errorMessage = "Error sending intermediate message to initiator " + e.getMessage();
105 context.setAttribute(prefix + STATUS, FAILURE);
106 context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
107 logger.error(errorMessage,e);
111 private void validateInputs(Map<String, String> params, SvcLogicContext context) throws APPCException {
112 String code = params.get("code");
113 String message = params.get("message");
114 if(StringUtils.isEmpty(code) || StringUtils.isEmpty(message)){
115 throw new APPCException("code or message is empty");
117 String requestId = context.getAttribute("input.common-header.request-id");
118 if(StringUtils.isEmpty(requestId)){
119 throw new APPCException("requestId is empty");
123 private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
124 ObjectMapper objectMapper = new ObjectMapper();
126 ObjectNode commonHeader = getCommonHeader(context);
127 ObjectNode status = getStatus(params);
129 ObjectNode output = objectMapper.createObjectNode();
130 output.put("common-header",commonHeader);
131 output.put("status",status);
133 ObjectNode body = objectMapper.createObjectNode();
134 body.put("output",output);
136 ObjectNode root = objectMapper.createObjectNode();
137 root.put("type", RESPONSE);
138 root.put("rpc-name",context.getAttribute("input.action"));
139 root.put("cambria.partition", MSO);
140 root.put("correlation-id",getCorrelationId(context));
141 root.put("body",body);
143 return root.toString();
146 private String getCorrelationId(SvcLogicContext context) {
147 String requestId = context.getAttribute("input.common-header.request-id");
148 String subRequestId = context.getAttribute("input.common-header.sub-request-id");
149 return requestId + (StringUtils.isEmpty(subRequestId)?"":("-"+subRequestId));
152 private ObjectNode getStatus(Map<String,String> params) {
153 ObjectMapper objectMapper = new ObjectMapper();
154 ObjectNode status = objectMapper.createObjectNode();
155 status.put("code",params.get("code"));
156 status.put("message",params.get("message"));
160 private ObjectNode getCommonHeader(SvcLogicContext context) {
161 ObjectMapper objectMapper = new ObjectMapper();
162 ObjectNode commonHeader = objectMapper.createObjectNode();
163 commonHeader.put("api-ver",context.getAttribute("input.common-header.api-ver"));
164 commonHeader.put("timestamp",context.getAttribute("input.common-header.timestamp"));
165 commonHeader.put("originator-id",context.getAttribute("input.common-header.originator-id"));
166 commonHeader.put("request-id",context.getAttribute("input.common-header.request-id"));
167 commonHeader.put("sub-request-id",context.getAttribute("input.common-header.sub-request-id"));