2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.server.restful;
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.io.UnsupportedEncodingException;
29 import java.net.URLDecoder;
30 import java.net.URLEncoder;
31 import java.util.List;
32 import java.util.stream.Collectors;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.Response.Status;
42 import org.onap.policy.controlloop.ControlLoopException;
43 import org.onap.policy.controlloop.params.ControlLoopParams;
44 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
45 import org.onap.policy.drools.apps.controlloop.feature.management.ControlLoopManagementFeature;
46 import org.onap.policy.drools.controller.DroolsController;
47 import org.onap.policy.drools.system.PolicyController;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Telemetry Extensions for Control Loops in the PDP-D.
56 @Produces(MediaType.APPLICATION_JSON)
57 @Consumes(MediaType.APPLICATION_JSON)
59 public class RestControlLoopManager {
60 private static final Logger logger = LoggerFactory.getLogger(RestControlLoopManager.class);
65 * @param controllerName controller name.
66 * @param sessionName session name.
67 * @return list of controller names.
70 @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops")
71 @ApiOperation(value = "Control Loops", notes = "Compact list", responseContainer = "List")
72 @ApiResponses(value = {@ApiResponse(code = 404, message = "Control Loops cannot be found")})
73 public Response controlLoops(
74 @ApiParam(value = "Policy Controller Name", required = true) @PathParam("controller") String controllerName,
75 @ApiParam(value = "Drools Session Name", required = true) @PathParam("session") String sessionName) {
78 List<String> controlLoopNames =
79 ControlLoopManagementFeature.controlLoops(controllerName, sessionName)
80 .map(ControlLoopParams::getClosedLoopControlName)
81 .collect(Collectors.toList());
83 return Response.status(Response.Status.OK).entity(controlLoopNames).build();
84 } catch (IllegalArgumentException e) {
85 logger.error("{}", e);
86 return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
93 * @param controllerName controller name.
94 * @param sessionName session name.
95 * @param controlLoopName control loop name.
96 * @return control loop.
99 @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops/{controlLoopName}")
100 @ApiOperation( value = "Control Loop", notes = "Control Loop Parameters", responseContainer = "List")
101 @ApiResponses(value = {@ApiResponse(code = 404, message = "The Control Loop cannot be found")})
102 public Response controlLoop(
103 @ApiParam(value = "Policy Controller Name", required = true) @PathParam("controller") String controllerName,
104 @ApiParam(value = "Drools Session Name", required = true) @PathParam("session") String sessionName,
105 @ApiParam(value = "Control Loop Name", required = true) @PathParam("controlLoopName") String controlLoopName) {
108 List<ControlLoopParams> controlLoopParams =
109 ControlLoopManagementFeature.controlLoops(controllerName, sessionName)
110 .filter(c -> c.getClosedLoopControlName().equals(controlLoopName))
111 .collect(Collectors.toList());
113 return Response.status(Response.Status.OK).entity(controlLoopParams).build();
114 } catch (IllegalArgumentException e) {
115 logger.error("{}", e);
116 return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
121 * GET operational policy.
123 * @param controllerName controller name.
124 * @param sessionName session name.
125 * @param controlLoopName control loop name.
126 * @return operational policy.
129 @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops/{controlLoopName}/policy")
130 @Produces(MediaType.TEXT_PLAIN)
131 @ApiOperation( value = "Operational Policy", notes = "The policy is in yaml format")
132 @ApiResponses(value = {@ApiResponse(code = 404, message = "The Control Loop cannot be found"),
133 @ApiResponse(code = 500, message = "The Control Loop has invalid data")})
134 public Response policy(
135 @ApiParam(value = "Policy Controller Name", required = true) @PathParam("controller") String controllerName,
136 @ApiParam(value = "Drools Session Name", required = true) @PathParam("session") String sessionName,
137 @ApiParam(value = "Control Loop Name", required = true) @PathParam("controlLoopName") String controlLoopName) {
140 ControlLoopParams controlLoopParams =
141 ControlLoopManagementFeature.controlLoops(controllerName, sessionName)
142 .filter(c -> c.getClosedLoopControlName().equals(controlLoopName))
146 if (controlLoopParams == null || controlLoopParams.getControlLoopYaml() == null) {
147 return Response.status(Response.Status.NOT_FOUND).entity("Policy not found").build();
150 return Response.status(Status.OK)
151 .entity(URLDecoder.decode(controlLoopParams.getControlLoopYaml(), "UTF-8")).build();
152 } catch (IllegalArgumentException e) {
153 logger.error("{}", e);
154 return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
155 } catch (UnsupportedEncodingException e) {
156 logger.error("{}", e);
157 return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unreadable Policy").build();
162 * PUT an Operational Policy.
164 * @param controllerName controller name.
165 * @param sessionName session name.
166 * @param controlLoopName control loop name.
167 * @param policy operational policy.
169 * @return operational policy.
173 @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops/{controlLoopName}/policy")
174 @Consumes(MediaType.TEXT_PLAIN)
175 @ApiOperation( value = "Add Operational Policy", notes = "The Operational Policy should be syntactically correct")
176 @ApiResponses(value = {@ApiResponse(code = 404, message = "The Control Loop cannot be found"),
177 @ApiResponse(code = 409, message = "The Control Loop exists"),
178 @ApiResponse(code = 412, message = "The Control Loop Name must be matched in the URL"),
179 @ApiResponse(code = 406, message = "The Operational Policy is invalid")})
180 public Response opOffer(
181 @ApiParam(value = "Policy Controller Name", required = true) @PathParam("controller") String controllerName,
182 @ApiParam(value = "Drools Session Name", required = true) @PathParam("session") String sessionName,
183 @ApiParam(value = "Control Loop Name", required = true) @PathParam("controlLoopName") String controlLoopName,
184 @ApiParam(value = "Operational Policy", required = true) String policy) {
187 ControlLoopParams controlLoop =
188 ControlLoopManagementFeature.controlLoop(controllerName, sessionName, controlLoopName);
190 if (controlLoop != null) {
191 return Response.status(Status.CONFLICT).entity(controlLoop).build();
196 ControlLoopProcessor controlLoopProcessor = new ControlLoopProcessor(policy);
198 if (!controlLoopName.equals(controlLoopProcessor.getControlLoop().getControlLoopName())) {
199 return Response.status(Status.PRECONDITION_FAILED)
200 .entity("Control Loop Name in URL does not match the Operational Policy")
204 DroolsController controller = PolicyController.factory.get(controllerName).getDrools();
206 controlLoop = new ControlLoopParams();
207 controlLoop.setPolicyScope(controller.getGroupId());
208 controlLoop.setPolicyName(controller.getArtifactId());
209 controlLoop.setPolicyVersion(controller.getVersion());
210 controlLoop.setClosedLoopControlName(controlLoopName);
211 controlLoop.setControlLoopYaml(URLEncoder.encode(policy, "UTF-8"));
213 controller.getContainer().insertAll(controlLoop);
214 return Response.status(Status.OK).entity(controlLoop).build();
216 } catch (IllegalArgumentException i) {
217 logger.error("{}", i);
218 return Response.status(Response.Status.NOT_FOUND).entity(i).build();
219 } catch (ControlLoopException | UnsupportedEncodingException e) {
220 logger.error("{}", e);
221 return Response.status(Status.NOT_ACCEPTABLE).entity(e).build();