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.openecomp.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 org.apache.commons.lang.StringUtils;
33 import org.openecomp.appc.adapter.message.MessageAdapterFactory;
34 import org.openecomp.appc.adapter.message.Producer;
35 import org.openecomp.appc.configuration.Configuration;
36 import org.openecomp.appc.configuration.ConfigurationFactory;
37 import org.openecomp.appc.dg.common.IntermediateMessageSender;
38 import org.openecomp.appc.exceptions.APPCException;
39 import org.openecomp.sdnc.sli.SvcLogicContext;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.FrameworkUtil;
42 import org.osgi.framework.ServiceReference;
44 import java.util.HashSet;
46 import java.util.Properties;
49 public class IntermediateMessageSenderImpl implements IntermediateMessageSender {
52 private Producer producer;
54 private Configuration configuration;
56 private final EELFLogger logger = EELFManager.getInstance().getLogger(IntermediateMessageSenderImpl.class);
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 configuration = ConfigurationFactory.getConfiguration();
68 Properties properties=configuration.getProperties();
70 String writeTopic = properties.getProperty("appc.LCM.topic.write");
71 String apiKey = properties.getProperty("appc.LCM.client.key");
72 String apiSecret = properties.getProperty("appc.LCM.client.secret");
73 String hostNames = properties.getProperty("appc.LCM.poolMembers");
75 logger.debug("Configuration Read : writeTopic = " + writeTopic +", " +
76 "hostNames = " + hostNames);
78 Set<String> pool = new HashSet<>();
79 if (!StringUtils.isEmpty(hostNames)) {
80 for (String name : hostNames.split(",")) {
85 BundleContext ctx = FrameworkUtil.getBundle(IntermediateMessageSenderImpl.class).getBundleContext();
87 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
89 producer = ((MessageAdapterFactory) ctx.getService(svcRef)).createProducer(pool, writeTopic,apiKey, apiSecret);
95 public void sendMessage(Map<String, String> params, SvcLogicContext context) {
96 String prefix = params.get("prefix");
97 prefix = StringUtils.isEmpty(prefix)? "":prefix+".";
99 validateInputs(params,context);
100 String jsonMessage = getJsonMessage(params, context);
101 logger.debug("Constructed JSON Message : " + jsonMessage);
102 producer.post("",jsonMessage);
103 context.setAttribute(prefix + STATUS, SUCCESS);
106 String errorMessage = "Error sending intermediate message to initiator " + e.getMessage();
107 context.setAttribute(prefix + STATUS, FAILURE);
108 context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
109 logger.error(errorMessage,e);
113 private void validateInputs(Map<String, String> params, SvcLogicContext context) throws APPCException {
114 String code = params.get("code");
115 String message = params.get("message");
116 if(StringUtils.isEmpty(code) || StringUtils.isEmpty(message)){
117 throw new APPCException("code or message is empty");
119 String requestId = context.getAttribute("input.common-header.request-id");
120 if(StringUtils.isEmpty(requestId)){
121 throw new APPCException("requestId is empty");
125 private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
126 ObjectMapper objectMapper = new ObjectMapper();
128 ObjectNode commonHeader = getCommonHeader(context);
129 ObjectNode status = getStatus(params);
131 ObjectNode output = objectMapper.createObjectNode();
132 output.put("common-header",commonHeader);
133 output.put("status",status);
135 ObjectNode body = objectMapper.createObjectNode();
136 body.put("output",output);
138 ObjectNode root = objectMapper.createObjectNode();
139 root.put("type", RESPONSE);
140 root.put("rpc-name",context.getAttribute("input.action"));
141 root.put("cambria.partition", MSO);
142 root.put("correlation-id",getCorrelationId(context));
143 root.put("body",body);
145 return root.toString();
148 private String getCorrelationId(SvcLogicContext context) {
149 String requestId = context.getAttribute("input.common-header.request-id");
150 String subRequestId = context.getAttribute("input.common-header.sub-request-id");
151 return requestId + (StringUtils.isEmpty(subRequestId)?"":("-"+subRequestId));
154 private ObjectNode getStatus(Map<String,String> params) {
155 ObjectMapper objectMapper = new ObjectMapper();
156 ObjectNode status = objectMapper.createObjectNode();
157 status.put("code",params.get("code"));
158 status.put("message",params.get("message"));
162 private ObjectNode getCommonHeader(SvcLogicContext context) {
163 ObjectMapper objectMapper = new ObjectMapper();
164 ObjectNode commonHeader = objectMapper.createObjectNode();
165 commonHeader.put("api-ver",context.getAttribute("input.common-header.api-ver"));
166 commonHeader.put("timestamp",context.getAttribute("input.common-header.timestamp"));
167 commonHeader.put("originator-id",context.getAttribute("input.common-header.originator-id"));
168 commonHeader.put("request-id",context.getAttribute("input.common-header.request-id"));
169 commonHeader.put("sub-request-id",context.getAttribute("input.common-header.sub-request-id"));