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