ed522fcee1c3b1182647c599d12a6891b2f93e23
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-state-management
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.policy.drools.statemanagement;
22
23 import javax.ws.rs.GET;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.core.Response;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import io.swagger.annotations.Api;
31 import io.swagger.annotations.ApiOperation;
32 import io.swagger.annotations.ApiResponse;
33 import io.swagger.annotations.ApiResponses;
34
35 @Api(value = "test")
36         @Path("/")
37 public class IntegrityMonitorRestManager {
38                 private static Logger logger = LoggerFactory.getLogger(IntegrityMonitorRestManager.class);      
39                 private DroolsPDPIntegrityMonitor im;
40                 
41                 /**
42                  * Test interface for Integrity Monitor
43                  * 
44                  * @return Exception message if exception, otherwise empty
45                  */
46                 @ApiOperation(
47                         value = "Test endpoint for integrity monitor",
48                         notes = "The TEST command is used to request data from a subcomponent "
49                                         + "instance that can be used to determine its operational state. "
50                                         + "A 200/success response status code should be returned if the "
51                                         + "subcomponent instance is functioning properly and able to respond to requests.",
52                         response = String.class)
53                 @ApiResponses(value = {
54                         @ApiResponse(
55                                 code = 200,
56                                 message = "Integrity monitor sanity check passed"),
57                         @ApiResponse(
58                                 code = 500,
59                                 message = "Integrity monitor sanity check encountered an exception. This can indicate operational state disabled or administrative state locked")
60                 })
61                 @GET
62                 @Path("test")
63                 public Response test() {
64                         logger.error("integrity monitor /test accessed");
65                         // The responses are stored within the audit objects, so we need to
66                         // invoke the audits and get responses before we handle another
67                         // request.
68                         synchronized (IntegrityMonitorRestManager.class) {
69                                 // will include messages associated with subsystem failures
70                                 StringBuilder body = new StringBuilder();
71
72                                 // 200=SUCCESS, 500=failure
73                                 int responseValue = 200;
74
75                                 if (im == null) {
76                                         try {
77                                                 im = DroolsPDPIntegrityMonitor.getInstance();
78                                         } catch (Exception e) {
79                                                 logger.error("IntegrityMonitorRestManager: test() interface caught an exception", e);
80                                                 body.append("\nException: " + e + "\n");
81                                                 responseValue = 500;
82                                         }
83                                 }
84
85                                 if (im != null) {
86                                         try {
87                                                 // call 'IntegrityMonitor.evaluateSanity()'
88                                                 im.evaluateSanity();
89                                         } catch (Exception e) {
90                                                 // this exception isn't coming from one of the audits,
91                                                 // because those are caught in 'subsystemTest()'
92                                                 logger.error("DroolsPDPIntegrityMonitor.evaluateSanity()", e);
93
94                                                 // include exception in HTTP response
95                                                 body.append("\nException: " + e + "\n");
96                                                 responseValue = 500;
97                                         }
98                                 }
99
100                                 // send response, including the contents of 'body'
101                                 // (which is empty if everything is successful)
102                                 if (responseValue == 200)
103                                         return Response.status(Response.Status.OK).build();
104                                 else
105                                         return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(body.toString()).build();
106                         }
107                 }
108 }