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