1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.so.monitoring.rest.api;
import java.util.List;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.onap.so.montoring.db.service.DatabaseServiceProvider;
import org.onap.so.montoring.exception.InvalidRestRequestException;
import org.onap.so.montoring.exception.RestProcessingException;
import org.onap.so.montoring.model.ActivityInstanceDetail;
import org.onap.so.montoring.model.ProcessDefinitionDetail;
import org.onap.so.montoring.model.ProcessInstanceDetail;
import org.onap.so.montoring.model.ProcessInstanceIdDetail;
import org.onap.so.montoring.model.ProcessInstanceVariableDetail;
import org.onap.so.montoring.model.SoInfraRequest;
import org.onap.so.montoring.rest.service.CamundaProcessDataServiceProvider;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.common.base.Optional;
/**
* @author waqas.ikram@ericsson.com
*/
@Component
@Path("/")
public class SoMonitoringController {
private static final String INVALID_PROCESS_INSTANCE_ERROR_MESSAGE = "Invalid process instance id: ";
private static final XLogger LOGGER = XLoggerFactory.getXLogger(SoMonitoringController.class);
private final DatabaseServiceProvider databaseServiceProvider;
private final CamundaProcessDataServiceProvider camundaProcessDataServiceProvider;
@Autowired
public SoMonitoringController(final DatabaseServiceProvider databaseServiceProvider,
final CamundaProcessDataServiceProvider camundaProcessDataServiceProvider) {
this.databaseServiceProvider = databaseServiceProvider;
this.camundaProcessDataServiceProvider = camundaProcessDataServiceProvider;
}
@GET
@Path("/process-instance-id/{requestId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getProcessInstanceId(final @PathParam("requestId") String requestId) {
if (requestId == null || requestId.isEmpty()) {
return Response.status(Status.BAD_REQUEST).entity("Invalid Request id: " + requestId).build();
}
try {
final Optional<ProcessInstanceIdDetail> processInstanceId =
camundaProcessDataServiceProvider.getProcessInstanceIdDetail(requestId);
if (processInstanceId.isPresent()) {
return Response.status(Status.OK).entity(processInstanceId.get()).build();
}
LOGGER.error("Unable to find process instance id for : {}", requestId);
return Response.status(Status.NO_CONTENT).build();
} catch (final InvalidRestRequestException extensions) {
final String message = "Unable to find process instance id for : " + requestId;
LOGGER.error(message);
return Response.status(Status.BAD_REQUEST).entity(message).build();
} catch (final RestProcessingException restProcessingException) {
final String message = "Unable to process request for id: " + requestId;
LOGGER.error(message);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
}
}
@GET
@Path("/process-instance/{processInstanceId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getSingleProcessInstance(final @PathParam("processInstanceId") String processInstanceId) {
if (processInstanceId == null || processInstanceId.isEmpty()) {
return Response.status(Status.BAD_REQUEST)
.entity(INVALID_PROCESS_INSTANCE_ERROR_MESSAGE + processInstanceId).build();
}
try {
final Optional<ProcessInstanceDetail> processInstanceDetail =
camundaProcessDataServiceProvider.getSingleProcessInstanceDetail(processInstanceId);
if (processInstanceDetail.isPresent()) {
return Response.status(Status.OK).entity(processInstanceDetail.get()).build();
}
LOGGER.error("Unable to find process instance id for : {}", processInstanceId);
return Response.status(Status.NO_CONTENT).build();
} catch (final InvalidRestRequestException extensions) {
final String message = "Unable to find process instance id for : " + processInstanceId;
LOGGER.error(message);
return Response.status(Status.BAD_REQUEST).entity(message).build();
} catch (final RestProcessingException restProcessingException) {
final String message = "Unable to process request for id: " + processInstanceId;
LOGGER.error(message);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
}
}
@GET
@Path("/process-definition/{processDefinitionId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getProcessDefinitionXml(final @PathParam("processDefinitionId") String processDefinitionId) {
if (processDefinitionId == null || processDefinitionId.isEmpty()) {
return Response.status(Status.BAD_REQUEST).entity("Invalid process definition id: " + processDefinitionId)
.build();
}
try {
final Optional<ProcessDefinitionDetail> response =
camundaProcessDataServiceProvider.getProcessDefinition(processDefinitionId);
if (response.isPresent()) {
final ProcessDefinitionDetail definitionDetail = response.get();
return Response.status(Status.OK).entity(definitionDetail).build();
}
LOGGER.error("Unable to find process definition xml for processDefinitionId: {}", processDefinitionId);
return Response.status(Status.NO_CONTENT).build();
} catch (final InvalidRestRequestException extensions) {
final String message =
"Unable to find process definition xml for processDefinitionId: {}" + processDefinitionId;
return Response.status(Status.BAD_REQUEST).entity(message).build();
} catch (final RestProcessingException restProcessingException) {
final String message = "Unable to get process definition xml for id: " + processDefinitionId;
LOGGER.error(message);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
}
}
@GET
@Path("/activity-instance/{processInstanceId}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getActivityInstanceDetail(final @PathParam("processInstanceId") String processInstanceId) {
if (processInstanceId == null || processInstanceId.isEmpty()) {
return Response.status(Status.BAD_REQUEST)
.entity(INVALID_PROCESS_INSTANCE_ERROR_MESSAGE + processInstanceId).build();
}
try {
final List<ActivityInstanceDetail> activityInstanceDetails =
camundaProcessDataServiceProvider.getActivityInstance(processInstanceId);
return Response.status(Status.OK).entity(activityInstanceDetails).build();
} catch (final InvalidRestRequestException extensions) {
final String message = "Unable to find activity instance for processInstanceId: " + processInstanceId;
LOGGER.error(message);
return Response.status(Status.BAD_REQUEST).entity(message).build();
} catch (final RestProcessingException restProcessingException) {
final String message = "Unable to get activity instance detail for id: " + processInstanceId;
LOGGER.error(message);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
}
}
@GET
@Path("/variable-instance/{processInstanceId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getProcessInstanceVariables(final @PathParam("processInstanceId") String processInstanceId) {
if (processInstanceId == null || processInstanceId.isEmpty()) {
return Response.status(Status.BAD_REQUEST)
.entity(INVALID_PROCESS_INSTANCE_ERROR_MESSAGE + processInstanceId).build();
}
try {
final List<ProcessInstanceVariableDetail> processInstanceVariable =
camundaProcessDataServiceProvider.getProcessInstanceVariable(processInstanceId);
return Response.status(Status.OK).entity(processInstanceVariable).build();
} catch (final InvalidRestRequestException extensions) {
final String message =
"Unable to find process instance variables for processInstanceId: " + processInstanceId;
LOGGER.error(message);
return Response.status(Status.BAD_REQUEST).entity(message).build();
} catch (final RestProcessingException restProcessingException) {
final String message = "Unable to get process instance variables for id: " + processInstanceId;
LOGGER.error(message);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
}
}
@POST
@Path("/v1/search")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getInfraActiveRequests(final Map<String, String[]> filters,
@QueryParam("from") final long startTime, @QueryParam("to") final long endTime,
@QueryParam("maxResult") final Integer maxResult) {
if (filters == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid filters: " + filters).build();
}
try {
final List<SoInfraRequest> requests =
databaseServiceProvider.getSoInfraRequest(filters, startTime, endTime, maxResult);
LOGGER.info("result size: {}", requests.size());
return Response.status(Status.OK).entity(requests).build();
} catch (final InvalidRestRequestException extensions) {
final String message = "Unable to search request for filters: " + filters + ", from: " + startTime
+ ", to: " + endTime + ", maxResult: " + maxResult;
LOGGER.error(message);
return Response.status(Status.BAD_REQUEST).entity(message).build();
} catch (final RestProcessingException restProcessingException) {
final String message = "Unable to search request for filters: " + filters + ", from: " + startTime
+ ", to: " + endTime + ", maxResult: " + maxResult;
LOGGER.error(message);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).build();
}
}
}
|