18aae5001ab20295c78e4703d7a332369adde9fb
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.deployment.rest;
22
23 import com.google.gson.JsonObject;
24
25 import java.io.InputStream;
26
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35
36 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
37 import org.glassfish.jersey.media.multipart.FormDataParam;
38 import org.onap.policy.apex.core.deployment.ApexDeploymentException;
39 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
40 import org.slf4j.ext.XLogger;
41 import org.slf4j.ext.XLoggerFactory;
42
43 /**
44  * The class represents the root resource exposed at the base URL<br>
45  *
46  * <p>The url to access this resource would be in the form {@code <baseURL>/rest/....} <br>
47  * For example: a GET request to the following URL
48  * {@code http://localhost:18989/apexservices/rest/?hostName=localhost&port=12345}
49  *
50  * <p><b>Note:</b> An allocated {@code hostName} and {@code port} query parameter must be included in all requests.
51  * Datasets for different {@code hostName} are completely isolated from one another.
52  *
53  */
54 @Path("deployment/")
55 @Produces({ MediaType.APPLICATION_JSON })
56 @Consumes({ MediaType.APPLICATION_JSON })
57
58 public class ApexDeploymentRestResource {
59     // Get a reference to the logger
60     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexDeploymentRestResource.class);
61
62     /**
63      * Constructor, a new resource director is created for each request.
64      */
65     public ApexDeploymentRestResource() {}
66
67     /**
68      * Query the engine service for data.
69      *
70      * @param hostName the host name of the engine service to connect to.
71      * @param port the port number of the engine service to connect to.
72      * @return a Response object containing the engines service, status and context data in JSON
73      */
74     @GET
75     public Response createSession(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port) {
76         final String host = hostName + ":" + port;
77         final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port);
78
79         try {
80             engineServiceFacade.init();
81         } catch (final ApexDeploymentException e) {
82             final String errorMessage = "Error connecting to Apex Engine Service at " + host;
83             LOGGER.warn(errorMessage + "<br>", e);
84             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
85                     .build();
86         }
87
88         final JsonObject responseObject = new JsonObject();
89
90         // Engine Service data
91         responseObject.addProperty("engine_id", engineServiceFacade.getKey().getId());
92         responseObject.addProperty("model_id",
93                 engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getId()
94                         : "Not Set");
95         responseObject.addProperty("server", hostName);
96         responseObject.addProperty("port", Integer.toString(port));
97
98         return Response.ok(responseObject.toString(), MediaType.APPLICATION_JSON).build();
99     }
100
101     /**
102      * Upload a model.
103      *
104      * @param hostName the host name of the engine service to connect to.
105      * @param port the port number of the engine service to connect to.
106      * @param uploadedInputStream input stream
107      * @param fileDetail details on the file
108      * @param ignoreConflicts conflict policy
109      * @param forceUpdate update policy
110      * @return a response object in plain text confirming the upload was successful
111      */
112     @POST
113     @Path("modelupload/")
114     @Consumes(MediaType.MULTIPART_FORM_DATA)
115     public Response modelUpload(@FormDataParam("hostName") final String hostName, @FormDataParam("port") final int port,
116             @FormDataParam("file") final InputStream uploadedInputStream,
117             @FormDataParam("file") final FormDataContentDisposition fileDetail,
118             @FormDataParam("ignoreConflicts") final boolean ignoreConflicts,
119             @FormDataParam("forceUpdate") final boolean forceUpdate) {
120         final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port);
121
122         try {
123             engineServiceFacade.init();
124         } catch (final ApexDeploymentException e) {
125             final String errorMessage = "Error connecting to Apex Engine Service at " + hostName + ":" + port;
126             LOGGER.warn(errorMessage + "<br>", e);
127             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
128                     .build();
129         }
130
131         try {
132             engineServiceFacade.deployModel(fileDetail.getFileName(), uploadedInputStream, ignoreConflicts,
133                     forceUpdate);
134         } catch (final Exception e) {
135             LOGGER.warn("Error updating model on engine service " + engineServiceFacade.getKey().getId(), e);
136             final String errorMessage =
137                     "Error updating model on engine service " + engineServiceFacade.getKey().getId();
138             LOGGER.warn(errorMessage + "<br>", e);
139             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
140                     .build();
141         }
142
143         return Response.ok("Model " + fileDetail.getFileName() + " deployed on engine service "
144                 + engineServiceFacade.getKey().getId()).build();
145     }
146
147 }