30385a956f5fbc1a32f539a2773cfd3b5649edd9
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / openecomp / appc / dg / common / impl / IntermediateMessageSenderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
21  */
22
23 package org.openecomp.appc.dg.common.impl;
24
25
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;
41
42 import java.util.HashSet;
43 import java.util.Map;
44 import java.util.Properties;
45 import java.util.Set;
46
47 public class IntermediateMessageSenderImpl implements IntermediateMessageSender {
48
49
50     private Producer producer;
51
52     private Configuration configuration;
53
54     private final EELFLogger logger = EELFManager.getInstance().getLogger(IntermediateMessageSenderImpl.class);
55
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";
60
61     private static final String RESPONSE = "response";
62     private static final String MSO = "MSO";
63
64     public void init(){
65         configuration = ConfigurationFactory.getConfiguration();
66         Properties properties=configuration.getProperties();
67
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");
72
73         logger.debug("Configuration Read : writeTopic = " + writeTopic +", " +
74                                             "hostNames = " + hostNames);
75
76         Set<String> pool = new HashSet<>();
77         if (!StringUtils.isEmpty(hostNames)) {
78             for (String name : hostNames.split(",")) {
79                 pool.add(name);
80             }
81         }
82
83         BundleContext ctx = FrameworkUtil.getBundle(IntermediateMessageSenderImpl.class).getBundleContext();
84         if (ctx != null) {
85             ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
86             if (svcRef != null) {
87                 producer = ((MessageAdapterFactory) ctx.getService(svcRef)).createProducer(pool, writeTopic,apiKey, apiSecret);
88             }
89         }
90     }
91
92     @Override
93     public void sendMessage(Map<String, String> params, SvcLogicContext context) {
94         String prefix = params.get("prefix");
95         prefix = StringUtils.isEmpty(prefix)? "":prefix+".";
96         try{
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         }
103         catch(Exception e){
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);
108         }
109     }
110
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");
116         }
117         String requestId = context.getAttribute("input.common-header.request-id");
118         if(StringUtils.isEmpty(requestId)){
119             throw new APPCException("requestId is empty");
120         }
121     }
122
123     private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
124         ObjectMapper objectMapper = new ObjectMapper();
125
126         ObjectNode commonHeader = getCommonHeader(context);
127         ObjectNode status = getStatus(params);
128
129         ObjectNode output = objectMapper.createObjectNode();
130         output.put("common-header",commonHeader);
131         output.put("status",status);
132
133         ObjectNode body =  objectMapper.createObjectNode();
134         body.put("output",output);
135
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);
142
143         return root.toString();
144     }
145
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));
150     }
151
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"));
157         return status;
158     }
159
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"));
168         return commonHeader;
169     }
170 }