Merge "Reorder modifiers"
[so.git] / adapters / mso-workflow-message-adapter / src / main / java / org / openecomp / mso / adapters / workflowmessage / WMAdapterRest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.openecomp.mso.adapters.workflowmessage;
22
23 import javax.servlet.http.HttpServletResponse;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.HEAD;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.HeaderParam;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35
36 import org.apache.http.entity.ContentType;
37
38 import org.openecomp.mso.HealthCheckUtils;
39 import org.openecomp.mso.logger.MessageEnum;
40 import org.openecomp.mso.logger.MsoAlarmLogger;
41 import org.openecomp.mso.logger.MsoLogger;
42 import org.openecomp.mso.utils.UUIDChecker;
43
44 /**
45  * Workflow Message Adapter interface added in 1707.  Supports delivery of
46  * callbacks from external systems to waiting BPMN workflows.
47  */
48 @Path("/")
49 public class WMAdapterRest {
50         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
51         private static final MsoAlarmLogger ALARMLOGGER = new MsoAlarmLogger();
52
53         @HEAD
54         @GET
55         @Path("/healthcheck")
56         @Produces(MediaType.TEXT_HTML)
57         public Response healthcheck(@QueryParam("requestId") String requestId) {
58                 long startTime = System.currentTimeMillis();
59                 MsoLogger.setServiceName("Healthcheck");
60                 UUIDChecker.verifyOldUUID(requestId, LOGGER);
61                 HealthCheckUtils healthCheck = new HealthCheckUtils();
62
63                 if (!healthCheck.siteStatusCheck(LOGGER)) {
64                         return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
65                 }
66
67                 if (!healthCheck.configFileCheck(LOGGER, startTime, WMAdapterConstants.MSO_PROPERTIES_ID)) {
68                         return HealthCheckUtils.NOT_STARTED_RESPONSE;
69                 }
70
71                 LOGGER.debug("healthcheck - Successful");
72                 return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
73         }
74
75         /**
76          * Receives a message from a remote system.
77          * @param content the message content
78          */
79         @POST
80         @Path("/message/{messageType}/{correlator}")
81         @Consumes("*/*")
82         @Produces({MediaType.TEXT_PLAIN})
83         public Response receiveWorkflowMessage(
84                         @HeaderParam("Content-Type") String contentTypeHeader,
85                         @PathParam("messageType") String messageType,
86                         @PathParam("correlator") String correlator,
87                         String content) {
88
89                 String path= "workflow/" + messageType + "/" + correlator;
90                 LOGGER.info(MessageEnum.RA_RECEIVE_WORKFLOW_MESSAGE, content, "WorkflowMessageAdapter", path);
91
92                 long startTime = System.currentTimeMillis();
93
94                 ContentType contentType = null;
95
96                 if (contentTypeHeader != null) {
97                         try {
98                                 contentType = ContentType.parse(contentTypeHeader);
99                         } catch (Exception e) {
100                                 // If we don't get a valid one, we handle it below.
101                                 LOGGER.debug("Exception :",e);
102                         }
103                 }
104
105                 if (contentType == null && content != null) {
106                         String error = "Missing or Invalid Content-Type";
107                         LOGGER.error(MessageEnum.RA_PARSING_REQUEST_ERROR, error, "WorkflowMessageAdapter", path,
108                                 MsoLogger.ErrorCode.DataError, "Bad Request");
109                         ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, error);
110                         return Response.status(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE).entity(error).build();
111                 }
112
113                 String bpUrl = WMAdapterProperties.getProperty(WMAdapterConstants.BPEL_URL_PROP, null);
114
115                 if (bpUrl == null) {
116                         String error = "Missing configuration for: " + WMAdapterConstants.BPEL_URL_PROP;
117                         LOGGER.error(MessageEnum.RA_CONFIG_EXC, error, "WorkflowMessageAdapter", path,
118                                 MsoLogger.ErrorCode.DataError, "Configuration Error");
119                         ALARMLOGGER.sendAlarm("MsoInternalError", MsoAlarmLogger.CRITICAL, error);
120                         return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(error).build();
121                 }
122
123                 long bpStartTime = System.currentTimeMillis();
124                 BPRestCallback callback = new BPRestCallback();
125                 boolean callbackSuccess = callback.send(bpUrl, messageType, correlator, contentType, content);
126
127                 if (callbackSuccess) {
128                         LOGGER.recordMetricEvent(bpStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
129                                 "Sent notification", "BPMN", bpUrl, null);
130                         LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful");
131                 } else {
132                         LOGGER.recordMetricEvent(bpStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError,
133                                 "Failed to send notification", "BPMN", bpUrl, null);
134                         LOGGER.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.CommunicationError,
135                                 "Failed to send notification");
136                 }
137
138                 return Response.status(204).build();
139         }
140 }