9547017e64556436c00a856617d7bee898fb27bb
[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.logging.filter.base.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.v3.oas.annotations.OpenAPIDefinition;
45 import io.swagger.v3.oas.annotations.Operation;
46 import io.swagger.v3.oas.annotations.info.Info;
47
48 /**
49  * Generalized REST interface that injects a message event into a waiting BPMN process. Examples:
50  * 
51  * <pre>
52  *     /WorkflowMessage/SDNCAResponse/6d10d075-100c-42d0-9d84-a52432681cae-1478486185286
53  *     /WorkflowMessage/SDNCAEvent/USOSTCDALTX0101UJZZ01
54  * </pre>
55  */
56 @Path("/")
57 @OpenAPIDefinition(info = @Info(title = "/",
58         description = "Provides a generic service to inject messages into a waiting BPMN Proccess"))
59 @Component
60 public class WorkflowMessageResource {
61     private static final Logger logger = LoggerFactory.getLogger(WorkflowMessageResource.class);
62     private static final String LOGMARKER = "[WORKFLOW-MESSAGE]";
63
64     @Autowired
65     CallbackHandlerService callback;
66
67     @POST
68     @Path("/WorkflowMessage/{messageType}/{correlator}")
69     @Operation(description = "Workflow message correlator")
70     @Consumes("*/*")
71     @Produces(MediaType.TEXT_PLAIN)
72     public Response deliver(@HeaderParam("Content-Type") String contentType,
73             @PathParam("messageType") String messageType, @PathParam("correlator") String correlator, String message) {
74
75         String method = "receiveWorkflowMessage";
76
77         logger.debug(LOGMARKER + " Received workflow message" + " type='" + messageType + "'" + " correlator='"
78                 + correlator + "'" + (contentType == null ? "" : " contentType='" + contentType + "'") + " message="
79                 + System.lineSeparator() + message);
80
81         if (messageType == null || messageType.isEmpty()) {
82             String msg = "Missing message type";
83             logger.debug(LOGMARKER + " " + msg);
84             logger.error(LoggingAnchor.FOUR, MessageEnum.BPMN_GENERAL_EXCEPTION.toString(), "BPMN",
85                     ErrorCode.DataError.getValue(), LOGMARKER + ":" + msg);
86             return Response.status(400).entity(msg).build();
87         }
88
89         if (correlator == null || correlator.isEmpty()) {
90             String msg = "Missing correlator";
91             logger.debug(LOGMARKER + " " + msg);
92             logger.error(LoggingAnchor.FOUR, MessageEnum.BPMN_GENERAL_EXCEPTION.toString(), "BPMN",
93                     ErrorCode.DataError.getValue(), LOGMARKER + ":" + msg);
94             return Response.status(400).entity(msg).build();
95         }
96
97         String messageEventName = "WorkflowMessage";
98         String messageVariable = messageType + "_MESSAGE";
99         String correlationVariable = messageType + "_CORRELATOR";
100         String correlationValue = correlator;
101         String contentTypeVariable = messageType + "_CONTENT_TYPE";
102
103         Map<String, Object> variables = new HashMap<>();
104
105         if (contentType != null) {
106             variables.put(contentTypeVariable, contentType);
107         }
108
109         CallbackResult result = callback.handleCallback(method, message, messageEventName, messageVariable,
110                 correlationVariable, correlationValue, LOGMARKER, variables);
111
112         if (result instanceof CallbackError) {
113             return Response.status(500).entity(((CallbackError) result).getErrorMessage()).build();
114         } else {
115             return Response.status(204).build();
116         }
117     }
118 }