8dd735e6750ee56520a5085491db79aa70895a1e
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-state-management
4  * ================================================================================
5  * Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.statemanagement;
23
24 import io.swagger.annotations.Api;
25 import io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.core.Response;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
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. "
60                         + "This can indicate operational state disabled or administrative state locked")
61         })
62     @GET
63     @Path("test")
64     public Response test() {
65         logger.debug("integrity monitor /test accessed");
66         // The responses are stored within the audit objects, so we need to
67         // invoke the audits and get responses before we handle another
68         // request.
69         synchronized (IntegrityMonitorRestManager.class) {
70             // will include messages associated with subsystem failures
71             var body = new StringBuilder();
72
73             // 200=SUCCESS, 500=failure
74             var responseValue = 200;
75
76             if (im == null) {
77                 try {
78                     im = DroolsPdpIntegrityMonitor.getInstance();
79                 } catch (Exception e) {
80                     logger.error("IntegrityMonitorRestManager: test() interface caught an exception", e);
81                     body.append("\nException: " + e + "\n");
82                     responseValue = 500;
83                 }
84             }
85
86             if (im != null) {
87                 try {
88                     // call 'IntegrityMonitor.evaluateSanity()'
89                     im.evaluateSanity();
90                 } catch (Exception e) {
91                     // this exception isn't coming from one of the audits,
92                     // because those are caught in 'subsystemTest()'
93                     logger.error("DroolsPDPIntegrityMonitor.evaluateSanity()", e);
94
95                     // include exception in HTTP response
96                     body.append("\nException: " + e + "\n");
97                     responseValue = 500;
98                 }
99             }
100
101             // send response, including the contents of 'body'
102             // (which is empty if everything is successful)
103             if (responseValue == 200) {
104                 return Response.status(Response.Status.OK).build();
105             } else {
106                 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(body.toString()).build();
107             }
108         }
109     }
110 }