Change code in appc dispatcher for new LCMs in R6
[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  * ============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.Arrays;
33 import java.util.HashSet;
34 import java.util.Map;
35 import java.util.Properties;
36 import java.util.Set;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.appc.configuration.ConfigurationFactory;
39 import org.onap.appc.dg.common.IntermediateMessageSender;
40 import org.onap.appc.exceptions.APPCException;
41 import org.onap.appc.srvcomm.messaging.MessagingConnector;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.osgi.framework.BundleContext;
44 import org.osgi.framework.FrameworkUtil;
45 import org.osgi.framework.ServiceReference;
46
47 public class IntermediateMessageSenderImpl implements IntermediateMessageSender {
48
49     private final EELFLogger logger = EELFManager.getInstance().getLogger(IntermediateMessageSenderImpl.class);
50
51     private static final String PARAM_MESSAGE = "message";
52     private static final String ATTR_REQUEST_ID = "input.common-header.request-id";
53     private static final String PROPERTIES_PREFIX = "appc.LCM";
54
55     private MessagingConnector messageService;
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         messageService = new MessagingConnector();
67     }
68     public void init(MessagingConnector messagingConnector) {
69         messageService = messagingConnector;
70     }
71
72     @Override
73     public void sendMessage(Map<String, String> params, SvcLogicContext context) {
74         String prefix = params.get("prefix");
75         prefix = StringUtils.isEmpty(prefix) ? "" : prefix + ".";
76         try {
77             validateInputs(params, context);
78             String jsonMessage = getJsonMessage(params, context);
79             logger.debug("Constructed JSON Message: " + jsonMessage);
80             messageService.publishMessage(PROPERTIES_PREFIX, "", jsonMessage);
81             context.setAttribute(prefix + STATUS, SUCCESS);
82         } catch (Exception e) {
83             String errorMessage = "Error sending intermediate message to initiator " + e.getMessage();
84             context.setAttribute(prefix + STATUS, FAILURE);
85             context.setAttribute(prefix + ERROR_MESSAGE, errorMessage);
86             logger.error(errorMessage, e);
87         }
88     }
89
90     private void validateInputs(Map<String, String> params, SvcLogicContext context) throws APPCException {
91         String code = params.get("code");
92         String message = params.get(PARAM_MESSAGE);
93         if (StringUtils.isEmpty(code) || StringUtils.isEmpty(message)) {
94             throw new APPCException("code or message is empty");
95         }
96         String requestId = context.getAttribute(ATTR_REQUEST_ID);
97         if (StringUtils.isEmpty(requestId)) {
98             throw new APPCException("requestId is empty");
99         }
100     }
101
102     private String getJsonMessage(Map<String, String> params, SvcLogicContext context) {
103         ObjectMapper objectMapper = new ObjectMapper();
104
105         ObjectNode commonHeader = getCommonHeader(context);
106         ObjectNode status = getStatus(params);
107
108         ObjectNode output = objectMapper.createObjectNode();
109         output.put("common-header", commonHeader);
110         output.put("status", status);
111
112         ObjectNode body = objectMapper.createObjectNode();
113         body.put("output", output);
114
115         ObjectNode root = objectMapper.createObjectNode();
116         root.put("type", RESPONSE);
117         root.put("rpc-name", context.getAttribute("input.action"));
118         root.put("cambria.partition", MSO);
119         root.put("correlation-id", getCorrelationId(context));
120         root.put("body", body);
121
122         return root.toString();
123     }
124
125     private String getCorrelationId(SvcLogicContext context) {
126         String requestId = context.getAttribute(ATTR_REQUEST_ID);
127         String subRequestId = context.getAttribute("input.common-header.sub-request-id");
128         return requestId + (StringUtils.isEmpty(subRequestId) ? "" : ("-" + subRequestId));
129     }
130
131     private ObjectNode getStatus(Map<String, String> params) {
132         ObjectMapper objectMapper = new ObjectMapper();
133         ObjectNode status = objectMapper.createObjectNode();
134         status.put("code", params.get("code"));
135         status.put(PARAM_MESSAGE, params.get(PARAM_MESSAGE));
136         return status;
137     }
138
139     private ObjectNode getCommonHeader(SvcLogicContext context) {
140         ObjectMapper objectMapper = new ObjectMapper();
141         ObjectNode commonHeader = objectMapper.createObjectNode();
142         commonHeader.put("api-ver", context.getAttribute("input.common-header.api-ver"));
143         commonHeader.put("timestamp", context.getAttribute("input.common-header.timestamp"));
144         commonHeader.put("originator-id", context.getAttribute("input.common-header.originator-id"));
145         commonHeader.put("request-id", context.getAttribute(ATTR_REQUEST_ID));
146         commonHeader.put("sub-request-id", context.getAttribute("input.common-header.sub-request-id"));
147         return commonHeader;
148     }
149 }