d9caf8f19da603898dcf27306a091a466e11f0e0
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / onap / 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.onap.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 java.util.HashSet;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.Set;
36 import org.apache.commons.lang3.StringUtils;
37 import org.onap.appc.adapter.message.MessageAdapterFactory;
38 import org.onap.appc.adapter.message.Producer;
39 import org.onap.appc.configuration.Configuration;
40 import org.onap.appc.configuration.ConfigurationFactory;
41 import org.onap.appc.dg.common.IntermediateMessageSender;
42 import org.onap.appc.exceptions.APPCException;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.FrameworkUtil;
46 import org.osgi.framework.ServiceReference;
47
48 public class IntermediateMessageSenderImpl implements IntermediateMessageSender {
49
50
51     private Producer producer;
52
53     private Configuration configuration;
54
55     private final EELFLogger logger = EELFManager.getInstance().getLogger(IntermediateMessageSenderImpl.class);
56
57     private static final String STATUS = "STATUS";
58     private static final String FAILURE = "FAILURE";
59     private static final String SUCCESS = "SUCCESS";
60     private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
61
62     private static final String RESPONSE = "response";
63     private static final String MSO = "MSO";
64
65     public void init() {
66         configuration = ConfigurationFactory.getConfiguration();
67         Properties properties = configuration.getProperties();
68
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");
73
74         logger.debug("Configuration Read : writeTopic = " + writeTopic + ", " +
75             "hostNames = " + hostNames);
76
77         Set<String> pool = new HashSet<>();
78         if (!StringUtils.isEmpty(hostNames)) {
79             for (String name : hostNames.split(",")) {
80                 pool.add(name);
81             }
82         }
83
84         BundleContext ctx = FrameworkUtil.getBundle(IntermediateMessageSenderImpl.class).getBundleContext();
85         if (ctx != null) {
86             ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
87             if (svcRef != null) {
88                 producer = ((MessageAdapterFactory) ctx.getService(svcRef))
89                     .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         } catch (Exception e) {
105             String errorMessage = "Error sending intermediate message to initiator " + e.getMessage();
106             context.setAttribute(prefix + STATUS, FAILURE);
107             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
108             logger.error(errorMessage, e);
109         }
110     }
111
112     private void validateInputs(Map<String, String> params, SvcLogicContext context) throws APPCException {
113         String code = params.get("code");
114         String message = params.get("message");
115         if (StringUtils.isEmpty(code) || StringUtils.isEmpty(message)) {
116             throw new APPCException("code or message is empty");
117         }
118         String requestId = context.getAttribute("input.common-header.request-id");
119         if (StringUtils.isEmpty(requestId)) {
120             throw new APPCException("requestId is empty");
121         }
122     }
123
124     private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
125         ObjectMapper objectMapper = new ObjectMapper();
126
127         ObjectNode commonHeader = getCommonHeader(context);
128         ObjectNode status = getStatus(params);
129
130         ObjectNode output = objectMapper.createObjectNode();
131         output.put("common-header", commonHeader);
132         output.put("status", status);
133
134         ObjectNode body = objectMapper.createObjectNode();
135         body.put("output", output);
136
137         ObjectNode root = objectMapper.createObjectNode();
138         root.put("type", RESPONSE);
139         root.put("rpc-name", context.getAttribute("input.action"));
140         root.put("cambria.partition", MSO);
141         root.put("correlation-id", getCorrelationId(context));
142         root.put("body", body);
143
144         return root.toString();
145     }
146
147     private String getCorrelationId(SvcLogicContext context) {
148         String requestId = context.getAttribute("input.common-header.request-id");
149         String subRequestId = context.getAttribute("input.common-header.sub-request-id");
150         return requestId + (StringUtils.isEmpty(subRequestId) ? "" : ("-" + subRequestId));
151     }
152
153     private ObjectNode getStatus(Map<String, String> params) {
154         ObjectMapper objectMapper = new ObjectMapper();
155         ObjectNode status = objectMapper.createObjectNode();
156         status.put("code", params.get("code"));
157         status.put("message", params.get("message"));
158         return status;
159     }
160
161     private ObjectNode getCommonHeader(SvcLogicContext context) {
162         ObjectMapper objectMapper = new ObjectMapper();
163         ObjectNode commonHeader = objectMapper.createObjectNode();
164         commonHeader.put("api-ver", context.getAttribute("input.common-header.api-ver"));
165         commonHeader.put("timestamp", context.getAttribute("input.common-header.timestamp"));
166         commonHeader.put("originator-id", context.getAttribute("input.common-header.originator-id"));
167         commonHeader.put("request-id", context.getAttribute("input.common-header.request-id"));
168         commonHeader.put("sub-request-id", context.getAttribute("input.common-header.sub-request-id"));
169         return commonHeader;
170     }
171 }