7674e680eaff72a8aa09db64b581937cdd022836
[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  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.dg.common.impl;
26
27
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;
43
44 import java.util.HashSet;
45 import java.util.Map;
46 import java.util.Properties;
47 import java.util.Set;
48
49 public class IntermediateMessageSenderImpl implements IntermediateMessageSender {
50
51
52     private Producer producer;
53
54     private Configuration configuration;
55
56     private final EELFLogger logger = EELFManager.getInstance().getLogger(IntermediateMessageSenderImpl.class);
57
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";
62
63     private static final String RESPONSE = "response";
64     private static final String MSO = "MSO";
65
66     public void init(){
67         configuration = ConfigurationFactory.getConfiguration();
68         Properties properties=configuration.getProperties();
69
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");
74
75         logger.debug("Configuration Read : writeTopic = " + writeTopic +", " +
76                                             "hostNames = " + hostNames);
77
78         Set<String> pool = new HashSet<>();
79         if (!StringUtils.isEmpty(hostNames)) {
80             for (String name : hostNames.split(",")) {
81                 pool.add(name);
82             }
83         }
84
85         BundleContext ctx = FrameworkUtil.getBundle(IntermediateMessageSenderImpl.class).getBundleContext();
86         if (ctx != null) {
87             ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
88             if (svcRef != null) {
89                 producer = ((MessageAdapterFactory) ctx.getService(svcRef)).createProducer(pool, writeTopic,apiKey, apiSecret);
90             }
91         }
92     }
93
94     @Override
95     public void sendMessage(Map<String, String> params, SvcLogicContext context) {
96         String prefix = params.get("prefix");
97         prefix = StringUtils.isEmpty(prefix)? "":prefix+".";
98         try{
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);
104         }
105         catch(Exception e){
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);
110         }
111     }
112
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");
118         }
119         String requestId = context.getAttribute("input.common-header.request-id");
120         if(StringUtils.isEmpty(requestId)){
121             throw new APPCException("requestId is empty");
122         }
123     }
124
125     private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
126         ObjectMapper objectMapper = new ObjectMapper();
127
128         ObjectNode commonHeader = getCommonHeader(context);
129         ObjectNode status = getStatus(params);
130
131         ObjectNode output = objectMapper.createObjectNode();
132         output.put("common-header",commonHeader);
133         output.put("status",status);
134
135         ObjectNode body =  objectMapper.createObjectNode();
136         body.put("output",output);
137
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);
144
145         return root.toString();
146     }
147
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));
152     }
153
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"));
159         return status;
160     }
161
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"));
170         return commonHeader;
171     }
172 }