Change the header to SO
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.adapters.workflowmessage;
21
22 import javax.servlet.http.HttpServletResponse;
23 import javax.ws.rs.Consumes;
24 import javax.ws.rs.GET;
25 import javax.ws.rs.HEAD;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.HeaderParam;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36
37 import org.apache.http.entity.ContentType;
38
39 import org.openecomp.mso.HealthCheckUtils;
40 import org.openecomp.mso.logger.MessageEnum;
41 import org.openecomp.mso.logger.MsoAlarmLogger;
42 import org.openecomp.mso.logger.MsoLogger;
43 import org.openecomp.mso.utils.UUIDChecker;
44
45 /**
46  * Workflow Message Adapter interface added in 1707.  Supports delivery of
47  * callbacks from external systems to waiting BPMN workflows.
48  */
49 @Path("/")
50 public class WMAdapterRest {
51         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
52         private static final MsoAlarmLogger ALARMLOGGER = new MsoAlarmLogger();
53
54         @HEAD
55         @GET
56         @Path("/healthcheck")
57         @Produces(MediaType.TEXT_HTML)
58         public Response healthcheck(@QueryParam("requestId") String requestId) {
59                 long startTime = System.currentTimeMillis();
60                 MsoLogger.setServiceName("Healthcheck");
61                 UUIDChecker.verifyOldUUID(requestId, LOGGER);
62                 HealthCheckUtils healthCheck = new HealthCheckUtils();
63
64                 if (!healthCheck.siteStatusCheck(LOGGER, startTime)) {
65                         return HealthCheckUtils.HEALTH_CHECK_NOK_RESPONSE;
66                 }
67
68                 if (!healthCheck.configFileCheck(LOGGER, startTime, WMAdapterConstants.MSO_PROPERTIES_ID)) {
69                         return HealthCheckUtils.NOT_STARTED_RESPONSE;
70                 }
71
72                 LOGGER.debug("healthcheck - Successful");
73                 return HealthCheckUtils.HEALTH_CHECK_RESPONSE;
74         }
75
76         /**
77          * Receives a message from a remote system.
78          * @param content the message content
79          */
80         @POST
81         @Path("/message/{messageType}/{correlator}")
82         @Consumes("*/*")
83         @Produces({MediaType.TEXT_PLAIN})
84         public Response receiveWorkflowMessage(
85                         @HeaderParam("Content-Type") String contentTypeHeader,
86                         @PathParam("messageType") String messageType,
87                         @PathParam("correlator") String correlator,
88                         String content) {
89
90                 String path= "workflow/" + messageType + "/" + correlator;
91                 LOGGER.info(MessageEnum.RA_RECEIVE_WORKFLOW_MESSAGE, content, "WorkflowMessageAdapter", path);
92
93                 long startTime = System.currentTimeMillis();
94
95                 ContentType contentType = null;
96
97                 if (contentTypeHeader != null) {
98                         try {
99                                 contentType = ContentType.parse(contentTypeHeader);
100                         } catch (Exception e) {
101                                 // If we don't get a valid one, we handle it below.
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 }