0b4005a17a3723c563f78743e5a155c3e779650c
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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
49 /**
50  * Telemetry Extensions for Control Loops in the PDP-D.
51  */
52
53 @Path("/policy/pdp")
54 @Produces(MediaType.APPLICATION_JSON)
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Api
57 public class RestControlLoopManager {
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             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             return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
112         }
113     }
114
115     /**
116      * GET operational policy.
117      *
118      * @param controllerName controller name.
119      * @param sessionName session name.
120      * @param controlLoopName control loop name.
121      * @return operational policy.
122      */
123     @GET
124     @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops/{controlLoopName}/policy")
125     @Produces(MediaType.TEXT_PLAIN)
126     @ApiOperation( value = "Operational Policy", notes = "The policy is in yaml format")
127     @ApiResponses(value = {@ApiResponse(code = 404, message = "The Control Loop cannot be found"),
128         @ApiResponse(code = 500, message = "The Control Loop has invalid data")})
129     public Response policy(
130         @ApiParam(value = "Policy Controller Name", required = true) @PathParam("controller") String controllerName,
131         @ApiParam(value = "Drools Session Name", required = true) @PathParam("session") String sessionName,
132         @ApiParam(value = "Control Loop Name", required = true) @PathParam("controlLoopName") String controlLoopName) {
133
134         try {
135             ControlLoopParams controlLoopParams =
136                 ControlLoopManagementFeature.controlLoops(controllerName, sessionName)
137                     .filter(c -> c.getClosedLoopControlName().equals(controlLoopName))
138                     .findFirst()
139                     .orElse(null);
140
141             if (controlLoopParams == null || controlLoopParams.getControlLoopYaml() == null) {
142                 return Response.status(Response.Status.NOT_FOUND).entity("Policy not found").build();
143             }
144
145             return Response.status(Status.OK)
146                 .entity(URLDecoder.decode(controlLoopParams.getControlLoopYaml(), "UTF-8")).build();
147         } catch (IllegalArgumentException e) {
148             return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
149         } catch (UnsupportedEncodingException e) {
150             return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unreadable Policy").build();
151         }
152     }
153
154     /**
155      * PUT an Operational Policy.
156      *
157      * @param controllerName controller name.
158      * @param sessionName session name.
159      * @param controlLoopName control loop name.
160      * @param policy operational policy.
161      *
162      * @return operational policy.
163      */
164
165     @PUT
166     @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops/{controlLoopName}/policy")
167     @Consumes(MediaType.TEXT_PLAIN)
168     @ApiOperation( value = "Add Operational Policy", notes = "The Operational Policy should be syntactically correct")
169     @ApiResponses(value = {@ApiResponse(code = 404, message = "The Control Loop cannot be found"),
170         @ApiResponse(code = 409, message = "The Control Loop exists"),
171         @ApiResponse(code = 412, message = "The Control Loop Name must be matched in the URL"),
172         @ApiResponse(code = 406, message = "The Operational Policy is invalid")})
173     public Response opOffer(
174         @ApiParam(value = "Policy Controller Name", required = true) @PathParam("controller") String controllerName,
175         @ApiParam(value = "Drools Session Name", required = true) @PathParam("session") String sessionName,
176         @ApiParam(value = "Control Loop Name", required = true) @PathParam("controlLoopName") String controlLoopName,
177         @ApiParam(value = "Operational Policy", required = true) String policy) {
178
179         try {
180             ControlLoopParams controlLoop =
181                 ControlLoopManagementFeature.controlLoop(controllerName, sessionName, controlLoopName);
182
183             if (controlLoop != null) {
184                 return Response.status(Status.CONFLICT).entity(controlLoop).build();
185             }
186
187             /* validation */
188
189             ControlLoopProcessor controlLoopProcessor = new ControlLoopProcessor(policy);
190
191             if (!controlLoopName.equals(controlLoopProcessor.getControlLoop().getControlLoopName())) {
192                 return Response.status(Status.PRECONDITION_FAILED)
193                     .entity("Control Loop Name in URL does not match the Operational Policy")
194                     .build();
195             }
196
197             DroolsController controller = PolicyController.factory.get(controllerName).getDrools();
198
199             controlLoop = new ControlLoopParams();
200             controlLoop.setPolicyScope(controller.getGroupId());
201             controlLoop.setPolicyName(controller.getArtifactId());
202             controlLoop.setPolicyVersion(controller.getVersion());
203             controlLoop.setClosedLoopControlName(controlLoopName);
204             controlLoop.setControlLoopYaml(URLEncoder.encode(policy, "UTF-8"));
205
206             controller.getContainer().insertAll(controlLoop);
207             return Response.status(Status.OK).entity(controlLoop).build();
208
209         } catch (IllegalArgumentException i) {
210             return Response.status(Response.Status.NOT_FOUND).entity(i).build();
211         } catch (ControlLoopException | UnsupportedEncodingException e) {
212             return Response.status(Status.NOT_ACCEPTABLE).entity(e).build();
213         }
214     }
215 }