02cda8009f21a02c211d1be7d680f38a7117c84a
[so.git] /
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
21 package org.onap.so.bpmn.common.workflow.service;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.HeaderParam;
28 import javax.ws.rs.POST;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34
35 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackError;
36 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackResult;
37 import org.onap.so.logger.MessageEnum;
38 import org.onap.so.logger.MsoLogger;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 import io.swagger.annotations.Api;
43 import io.swagger.annotations.ApiOperation;
44
45 /**
46  * Generalized REST interface that injects a message event into a waiting BPMN process.
47  * Examples:
48  * <pre>
49  *     /WorkflowMessage/SDNCAResponse/6d10d075-100c-42d0-9d84-a52432681cae-1478486185286
50  *     /WorkflowMessage/SDNCAEvent/USOSTCDALTX0101UJZZ01
51  * </pre>
52  */
53 @Path("/")
54 @Api(description = "Provides a generic service to inject messages into a waiting BPMN Proccess")
55 @Component
56 public class WorkflowMessageResource{
57         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, WorkflowMessageResource.class);
58         private static final String LOGMARKER = "[WORKFLOW-MESSAGE]";
59         
60         @Autowired
61         CallbackHandlerService callback;
62         
63         @POST
64         @Path("/WorkflowMessage/{messageType}/{correlator}")
65         @ApiOperation(
66                 value = "Workflow message correlator",
67                 notes = ""
68             )
69         @Consumes("*/*")
70         @Produces(MediaType.TEXT_PLAIN)
71         public Response deliver(
72                         @HeaderParam("Content-Type") String contentType,
73                         @PathParam("messageType") String messageType,
74                         @PathParam("correlator") String correlator,
75                         String message) {
76
77                 String method = "receiveWorkflowMessage";
78                 MsoLogger.setServiceName("MSO." + method);
79                 MsoLogger.setLogContext(correlator, "N/A");
80
81                 LOGGER.debug(LOGMARKER + " Received workflow message"
82                         + " type='" + messageType + "'"
83                         + " correlator='" + correlator + "'"
84                         + (contentType == null ? "" : " contentType='" + contentType + "'")
85                         + " message=" + System.lineSeparator() + message);
86
87                 if (messageType == null || messageType.isEmpty()) {
88                         String msg = "Missing message type";
89                         LOGGER.debug(LOGMARKER + " " + msg);
90                         LOGGER.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(),
91                                 MsoLogger.ErrorCode.DataError, LOGMARKER + ":" + msg);
92                         return Response.status(400).entity(msg).build();
93                 }
94
95                 if (correlator == null || correlator.isEmpty()) {
96                         String msg = "Missing correlator";
97                         LOGGER.debug(LOGMARKER + " " + msg);
98                         LOGGER.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(),
99                                 MsoLogger.ErrorCode.DataError, LOGMARKER + ":" + msg);
100                         return Response.status(400).entity(msg).build();
101                 }
102
103                 String messageEventName = "WorkflowMessage";
104                 String messageVariable = messageType + "_MESSAGE";
105                 String correlationVariable = messageType + "_CORRELATOR";
106                 String correlationValue = correlator;
107                 String contentTypeVariable = messageType + "_CONTENT_TYPE";
108
109                 Map<String, Object> variables = new HashMap<>();
110
111                 if (contentType != null) {
112                         variables.put(contentTypeVariable, contentType);
113                 }
114
115                 CallbackResult result = callback.handleCallback(method, message, messageEventName,
116                         messageVariable, correlationVariable, correlationValue, LOGMARKER, variables);
117
118                 if (result instanceof CallbackError) {
119                         return Response.status(500).entity(((CallbackError)result).getErrorMessage()).build();
120                 } else {
121                         return Response.status(204).build();
122                 }
123         }
124 }