d5bf8a9c34b3d2fb8644363aeedbc7292f510360
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.dg.common.impl;
25
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ObjectNode;
31 import java.util.Arrays;
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     private final EELFLogger logger = EELFManager.getInstance().getLogger(IntermediateMessageSenderImpl.class);
51
52     private static final String PARAM_MESSAGE = "message";
53     private static final String ATTR_REQUEST_ID = "input.common-header.request-id";
54
55     private Producer producer;
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         Properties properties =  ConfigurationFactory.getConfiguration().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             pool.addAll(Arrays.asList(hostNames.split(",")));
79         }
80
81         BundleContext ctx = FrameworkUtil.getBundle(IntermediateMessageSenderImpl.class).getBundleContext();
82         if (ctx != null) {
83             ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
84             if (svcRef != null) {
85                 producer = ((MessageAdapterFactory) ctx.getService(svcRef))
86                     .createProducer(pool, writeTopic, apiKey, apiSecret);
87             }
88         }
89     }
90
91     @Override
92     public void sendMessage(Map<String, String> params, SvcLogicContext context) {
93         String prefix = params.get("prefix");
94         prefix = StringUtils.isEmpty(prefix) ? "" : prefix + ".";
95         try {
96             validateInputs(params, context);
97             String jsonMessage = getJsonMessage(params, context);
98             logger.debug("Constructed JSON Message : " + jsonMessage);
99             producer.post("", jsonMessage);
100             context.setAttribute(prefix + STATUS, SUCCESS);
101         } catch (Exception e) {
102             String errorMessage = "Error sending intermediate message to initiator " + e.getMessage();
103             context.setAttribute(prefix + STATUS, FAILURE);
104             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
105             logger.error(errorMessage, e);
106         }
107     }
108
109     private void validateInputs(Map<String, String> params, SvcLogicContext context) throws APPCException {
110         String code = params.get("code");
111         String message = params.get(PARAM_MESSAGE);
112         if (StringUtils.isEmpty(code) || StringUtils.isEmpty(message)) {
113             throw new APPCException("code or message is empty");
114         }
115         String requestId = context.getAttribute(ATTR_REQUEST_ID);
116         if (StringUtils.isEmpty(requestId)) {
117             throw new APPCException("requestId is empty");
118         }
119     }
120
121     private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
122         ObjectMapper objectMapper = new ObjectMapper();
123
124         ObjectNode commonHeader = getCommonHeader(context);
125         ObjectNode status = getStatus(params);
126
127         ObjectNode output = objectMapper.createObjectNode();
128         output.put("common-header", commonHeader);
129         output.put("status", status);
130
131         ObjectNode body = objectMapper.createObjectNode();
132         body.put("output", output);
133
134         ObjectNode root = objectMapper.createObjectNode();
135         root.put("type", RESPONSE);
136         root.put("rpc-name", context.getAttribute("input.action"));
137         root.put("cambria.partition", MSO);
138         root.put("correlation-id", getCorrelationId(context));
139         root.put("body", body);
140
141         return root.toString();
142     }
143
144     private String getCorrelationId(SvcLogicContext context) {
145         String requestId = context.getAttribute(ATTR_REQUEST_ID);
146         String subRequestId = context.getAttribute("input.common-header.sub-request-id");
147         return requestId + (StringUtils.isEmpty(subRequestId) ? "" : ("-" + subRequestId));
148     }
149
150     private ObjectNode getStatus(Map<String, String> params) {
151         ObjectMapper objectMapper = new ObjectMapper();
152         ObjectNode status = objectMapper.createObjectNode();
153         status.put("code", params.get("code"));
154         status.put(PARAM_MESSAGE, params.get(PARAM_MESSAGE));
155         return status;
156     }
157
158     private ObjectNode getCommonHeader(SvcLogicContext context) {
159         ObjectMapper objectMapper = new ObjectMapper();
160         ObjectNode commonHeader = objectMapper.createObjectNode();
161         commonHeader.put("api-ver", context.getAttribute("input.common-header.api-ver"));
162         commonHeader.put("timestamp", context.getAttribute("input.common-header.timestamp"));
163         commonHeader.put("originator-id", context.getAttribute("input.common-header.originator-id"));
164         commonHeader.put("request-id", context.getAttribute(ATTR_REQUEST_ID));
165         commonHeader.put("sub-request-id", context.getAttribute("input.common-header.sub-request-id"));
166         return commonHeader;
167     }
168 }