f76fde9ef19c8521a7dc783c7370e8a756561b44
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.common.workflow.service;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.HeaderParam;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.onap.so.logger.LoggingAnchor;
36 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackError;
37 import org.onap.so.bpmn.common.workflow.service.CallbackHandlerService.CallbackResult;
38 import org.onap.so.logger.ErrorCode;
39 import org.onap.so.logger.MessageEnum;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44 import io.swagger.annotations.Api;
45 import io.swagger.annotations.ApiOperation;
46
47 /**
48  * Generalized REST interface that injects a message event into a waiting BPMN process. Examples:
49  * 
50  * <pre>
51  *     /WorkflowMessage/SDNCAResponse/6d10d075-100c-42d0-9d84-a52432681cae-1478486185286
52  *     /WorkflowMessage/SDNCAEvent/USOSTCDALTX0101UJZZ01
53  * </pre>
54  */
55 @Path("/")
56 @Api(description = "Provides a generic service to inject messages into a waiting BPMN Proccess")
57 @Component
58 public class WorkflowMessageResource {
59     private static final Logger logger = LoggerFactory.getLogger(WorkflowMessageResource.class);
60     private static final String LOGMARKER = "[WORKFLOW-MESSAGE]";
61
62     @Autowired
63     CallbackHandlerService callback;
64
65     @POST
66     @Path("/WorkflowMessage/{messageType}/{correlator}")
67     @ApiOperation(value = "Workflow message correlator", notes = "")
68     @Consumes("*/*")
69     @Produces(MediaType.TEXT_PLAIN)
70     public Response deliver(@HeaderParam("Content-Type") String contentType,
71             @PathParam("messageType") String messageType, @PathParam("correlator") String correlator, String message) {
72
73         String method = "receiveWorkflowMessage";
74
75         logger.debug(LOGMARKER + " Received workflow message" + " type='" + messageType + "'" + " correlator='"
76                 + correlator + "'" + (contentType == null ? "" : " contentType='" + contentType + "'") + " message="
77                 + System.lineSeparator() + message);
78
79         if (messageType == null || messageType.isEmpty()) {
80             String msg = "Missing message type";
81             logger.debug(LOGMARKER + " " + msg);
82             logger.error(LoggingAnchor.FOUR, MessageEnum.BPMN_GENERAL_EXCEPTION.toString(), "BPMN",
83                     ErrorCode.DataError.getValue(), LOGMARKER + ":" + msg);
84             return Response.status(400).entity(msg).build();
85         }
86
87         if (correlator == null || correlator.isEmpty()) {
88             String msg = "Missing correlator";
89             logger.debug(LOGMARKER + " " + msg);
90             logger.error(LoggingAnchor.FOUR, MessageEnum.BPMN_GENERAL_EXCEPTION.toString(), "BPMN",
91                     ErrorCode.DataError.getValue(), LOGMARKER + ":" + msg);
92             return Response.status(400).entity(msg).build();
93         }
94
95         String messageEventName = "WorkflowMessage";
96         String messageVariable = messageType + "_MESSAGE";
97         String correlationVariable = messageType + "_CORRELATOR";
98         String correlationValue = correlator;
99         String contentTypeVariable = messageType + "_CONTENT_TYPE";
100
101         Map<String, Object> variables = new HashMap<>();
102
103         if (contentType != null) {
104             variables.put(contentTypeVariable, contentType);
105         }
106
107         CallbackResult result = callback.handleCallback(method, message, messageEventName, messageVariable,
108                 correlationVariable, correlationValue, LOGMARKER, variables);
109
110         if (result instanceof CallbackError) {
111             return Response.status(500).entity(((CallbackError) result).getErrorMessage()).build();
112         } else {
113             return Response.status(204).build();
114         }
115     }
116 }