b733b01ba1430bd7e65c8eb525d3018dd206f44c
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2020 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.server.restful;
22
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import java.util.List;
29 import java.util.UUID;
30 import java.util.stream.Collectors;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.Response.Status;
39 import org.onap.policy.aai.AaiManager;
40 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
41 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
42 import org.onap.policy.drools.apps.controlloop.feature.management.ControlLoopManagementFeature;
43 import org.onap.policy.drools.system.PolicyEngineConstants;
44 import org.onap.policy.rest.RestManager;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Telemetry Extensions for Control Loops in the PDP-D.
50  */
51
52 @Path("/policy/pdp")
53 @Produces(MediaType.APPLICATION_JSON)
54 @Consumes(MediaType.APPLICATION_JSON)
55 @Api
56 public class RestControlLoopManager {
57     private static final Logger logger = LoggerFactory.getLogger(RestControlLoopManager.class);
58
59     /**
60      * GET control loops.
61      *
62      * @param controllerName controller name.
63      * @param sessionName session name.
64      * @return list of controller names.
65      */
66     @GET
67     @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops")
68     @ApiOperation(value = "Control Loops", notes = "Compact list", responseContainer = "List")
69     @ApiResponses(value = {@ApiResponse(code = 404, message = "Control Loops cannot be found")})
70     public Response controlLoops(
71         @ApiParam(value = "Policy Controller Name", required = true) @PathParam("controller") String controllerName,
72         @ApiParam(value = "Drools Session Name", required = true) @PathParam("session") String sessionName) {
73
74         try {
75             List<String> controlLoopNames =
76                 ControlLoopManagementFeature.controlLoops(controllerName, sessionName)
77                     .map(ControlLoopParams::getClosedLoopControlName)
78                     .collect(Collectors.toList());
79
80             return Response.status(Response.Status.OK).entity(controlLoopNames).build();
81         } catch (IllegalArgumentException e) {
82             logger.error("'GET' controlloops threw an exception", e);
83             return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
84         }
85     }
86
87     /**
88      * GET control loop.
89      *
90      * @param controllerName controller name.
91      * @param sessionName session name.
92      * @param controlLoopName control loop name.
93      * @return control loop.
94      */
95     @GET
96     @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops/{controlLoopName}")
97     @ApiOperation(value = "Control Loop", notes = "Control Loop Parameters", responseContainer = "List")
98     @ApiResponses(value = {@ApiResponse(code = 404, message = "The Control Loop cannot be found")})
99     public Response controlLoop(
100         @ApiParam(value = "Policy Controller Name", required = true) @PathParam("controller") String controllerName,
101         @ApiParam(value = "Drools Session Name", required = true) @PathParam("session") String sessionName,
102         @ApiParam(value = "Control Loop Name", required = true) @PathParam("controlLoopName") String controlLoopName) {
103
104         try {
105             List<ControlLoopParams> controlLoopParams =
106                 ControlLoopManagementFeature.controlLoops(controllerName, sessionName)
107                     .filter(c -> c.getClosedLoopControlName().equals(controlLoopName))
108                     .collect(Collectors.toList());
109
110             return Response.status(Response.Status.OK).entity(controlLoopParams).build();
111         } catch (IllegalArgumentException e) {
112             logger.error("'GET' controlloop threw an exception", e);
113             return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
114         }
115     }
116
117     /**
118      * AAI Custom Query.
119      *
120      * @param vserverId vServer identifier.
121      * @return query results.
122      */
123     @GET
124     @Path("engine/tools/controlloops/aai/customQuery/{vserverId}")
125     @ApiOperation(value = "AAI Custom Query")
126     public Response aaiCustomQuery(@ApiParam(value = "vserver Identifier") String vserverId) {
127         return Response
128             .status(Status.OK)
129             .entity(new AaiManager(new RestManager())
130                 .getCustomQueryResponse(
131                     PolicyEngineConstants.getManager()
132                                     .getEnvironmentProperty(ControlLoopEventManager.AAI_URL),
133                     PolicyEngineConstants.getManager().getEnvironmentProperty(
134                                     ControlLoopEventManager.AAI_USERNAME_PROPERTY),
135                     PolicyEngineConstants.getManager().getEnvironmentProperty(
136                                     ControlLoopEventManager.AAI_PASS_PROPERTY),
137                     UUID.randomUUID(),
138                     vserverId))
139             .build();
140     }
141
142 }