Change code to use dmaap microservice
[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-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2018 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dg.common.impl;
27
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.node.ObjectNode;
33 import java.util.Arrays;
34 import java.util.HashSet;
35 import java.util.Map;
36 import java.util.Properties;
37 import java.util.Set;
38 import org.apache.commons.lang3.StringUtils;
39 import org.onap.appc.configuration.ConfigurationFactory;
40 import org.onap.appc.dg.common.IntermediateMessageSender;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.appc.srvcomm.messaging.MessagingConnector;
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     private static final String PROPERTIES_PREFIX = "appc.LCM";
55
56     private MessagingConnector messageService;
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         messageService = new MessagingConnector();
68     }
69     public void init(MessagingConnector messagingConnector) {
70         messageService = messagingConnector;
71     }
72
73     @Override
74     public void sendMessage(Map<String, String> params, SvcLogicContext context) {
75         String prefix = params.get("prefix");
76         prefix = StringUtils.isEmpty(prefix) ? "" : prefix + ".";
77         try {
78             validateInputs(params, context);
79             String jsonMessage = getJsonMessage(params, context);
80             logger.debug("Constructed JSON Message : " + jsonMessage);
81             messageService.publishMessage(PROPERTIES_PREFIX, "", jsonMessage);
82             context.setAttribute(prefix + STATUS, SUCCESS);
83         } catch (Exception e) {
84             String errorMessage = "Error sending intermediate message to initiator " + e.getMessage();
85             context.setAttribute(prefix + STATUS, FAILURE);
86             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
87             logger.error(errorMessage, e);
88         }
89     }
90
91     private void validateInputs(Map<String, String> params, SvcLogicContext context) throws APPCException {
92         String code = params.get("code");
93         String message = params.get(PARAM_MESSAGE);
94         if (StringUtils.isEmpty(code) || StringUtils.isEmpty(message)) {
95             throw new APPCException("code or message is empty");
96         }
97         String requestId = context.getAttribute(ATTR_REQUEST_ID);
98         if (StringUtils.isEmpty(requestId)) {
99             throw new APPCException("requestId is empty");
100         }
101     }
102
103     private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
104         ObjectMapper objectMapper = new ObjectMapper();
105
106         ObjectNode commonHeader = getCommonHeader(context);
107         ObjectNode status = getStatus(params);
108
109         ObjectNode output = objectMapper.createObjectNode();
110         output.put("common-header", commonHeader);
111         output.put("status", status);
112
113         ObjectNode body = objectMapper.createObjectNode();
114         body.put("output", output);
115
116         ObjectNode root = objectMapper.createObjectNode();
117         root.put("type", RESPONSE);
118         root.put("rpc-name", context.getAttribute("input.action"));
119         root.put("cambria.partition", MSO);
120         root.put("correlation-id", getCorrelationId(context));
121         root.put("body", body);
122
123         return root.toString();
124     }
125
126     private String getCorrelationId(SvcLogicContext context) {
127         String requestId = context.getAttribute(ATTR_REQUEST_ID);
128         String subRequestId = context.getAttribute("input.common-header.sub-request-id");
129         return requestId + (StringUtils.isEmpty(subRequestId) ? "" : ("-" + subRequestId));
130     }
131
132     private ObjectNode getStatus(Map<String, String> params) {
133         ObjectMapper objectMapper = new ObjectMapper();
134         ObjectNode status = objectMapper.createObjectNode();
135         status.put("code", params.get("code"));
136         status.put(PARAM_MESSAGE, params.get(PARAM_MESSAGE));
137         return status;
138     }
139
140     private ObjectNode getCommonHeader(SvcLogicContext context) {
141         ObjectMapper objectMapper = new ObjectMapper();
142         ObjectNode commonHeader = objectMapper.createObjectNode();
143         commonHeader.put("api-ver", context.getAttribute("input.common-header.api-ver"));
144         commonHeader.put("timestamp", context.getAttribute("input.common-header.timestamp"));
145         commonHeader.put("originator-id", context.getAttribute("input.common-header.originator-id"));
146         commonHeader.put("request-id", context.getAttribute(ATTR_REQUEST_ID));
147         commonHeader.put("sub-request-id", context.getAttribute("input.common-header.sub-request-id"));
148         return commonHeader;
149     }
150 }