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