810b59cc1352c2b0d5735d99ccbead772d65acfa
[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  * The url to access this resource would be in the form {@code <baseURL>/rest/....} <br>
46  * For example: a GET request to the following URL
47  * {@code http://localhost:18989/apexservices/rest/?hostName=localhost&port=12345}
48  *
49  * <b>Note:</b> An allocated {@code hostName} and {@code port} query parameter must be included in all requests.
50  * Datasets for different {@code hostName} are completely isolated from one another.
51  *
52  */
53 @Path("deployment/")
54 @Produces({ MediaType.APPLICATION_JSON })
55 @Consumes({ MediaType.APPLICATION_JSON })
56
57 public class ApexDeploymentRestResource {
58     // Get a reference to the logger
59     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexDeploymentRestResource.class);
60
61     /**
62      * Constructor, a new resource director is created for each request.
63      */
64     public ApexDeploymentRestResource() {}
65
66     /**
67      * Query the engine service for data
68      *
69      * @param hostName the host name of the engine service to connect to.
70      * @param port the port number of the engine service to connect to.
71      * @return a Response object containing the engines service, status and context data in JSON
72      */
73     @GET
74     public Response createSession(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port) {
75         final String host = hostName + ":" + port;
76         final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port);
77
78         try {
79             engineServiceFacade.init();
80         } catch (final ApexDeploymentException e) {
81             final String errorMessage = "Error connecting to Apex Engine Service at " + host;
82             LOGGER.warn(errorMessage + "<br>", e);
83             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
84                     .build();
85         }
86
87         final JsonObject responseObject = new JsonObject();
88
89         // Engine Service data
90         responseObject.addProperty("engine_id", engineServiceFacade.getKey().getID());
91         responseObject.addProperty("model_id",
92                 engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getID()
93                         : "Not Set");
94         responseObject.addProperty("server", hostName);
95         responseObject.addProperty("port", Integer.toString(port));
96
97         return Response.ok(responseObject.toString(), MediaType.APPLICATION_JSON).build();
98     }
99
100     /**
101      * Upload a model.
102      *
103      * @param hostName the host name of the engine service to connect to.
104      * @param port the port number of the engine service to connect to.
105      * @param uploadedInputStream input stream
106      * @param fileDetail details on the file
107      * @param ignoreConflicts conflict policy
108      * @param forceUpdate update policy
109      * @return a response object in plain text confirming the upload was successful
110      */
111     @POST
112     @Path("modelupload/")
113     @Consumes(MediaType.MULTIPART_FORM_DATA)
114     public Response modelUpload(@FormDataParam("hostName") final String hostName, @FormDataParam("port") final int port,
115             @FormDataParam("file") final InputStream uploadedInputStream,
116             @FormDataParam("file") final FormDataContentDisposition fileDetail,
117             @FormDataParam("ignoreConflicts") final boolean ignoreConflicts,
118             @FormDataParam("forceUpdate") final boolean forceUpdate) {
119         final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port);
120
121         try {
122             engineServiceFacade.init();
123         } catch (final ApexDeploymentException e) {
124             final String errorMessage = "Error connecting to Apex Engine Service at " + hostName + ":" + port;
125             LOGGER.warn(errorMessage + "<br>", e);
126             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
127                     .build();
128         }
129
130         try {
131             engineServiceFacade.deployModel(fileDetail.getFileName(), uploadedInputStream, ignoreConflicts,
132                     forceUpdate);
133         } catch (final Exception e) {
134             LOGGER.warn("Error updating model on engine service " + engineServiceFacade.getKey().getID(), e);
135             final String errorMessage =
136                     "Error updating model on engine service " + engineServiceFacade.getKey().getID();
137             LOGGER.warn(errorMessage + "<br>", e);
138             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
139                     .build();
140         }
141
142         return Response.ok("Model " + fileDetail.getFileName() + " deployed on engine service "
143                 + engineServiceFacade.getKey().getID()).build();
144     }
145
146 }