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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.deployment.rest;
23 import com.google.gson.JsonObject;
25 import java.io.InputStream;
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;
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;
44 * The class represents the root resource exposed at the base URL<br>
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}
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.
55 @Produces({ MediaType.APPLICATION_JSON })
56 @Consumes({ MediaType.APPLICATION_JSON })
58 public class ApexDeploymentRestResource {
59 // Get a reference to the logger
60 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexDeploymentRestResource.class);
63 * Query the engine service for data.
65 * @param hostName the host name of the engine service to connect to.
66 * @param port the port number of the engine service to connect to.
67 * @return a Response object containing the engines service, status and context data in JSON
70 public Response createSession(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port) {
71 final String host = hostName + ":" + port;
72 final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port);
75 engineServiceFacade.init();
76 } catch (final ApexDeploymentException e) {
77 final String errorMessage = "Error connecting to Apex Engine Service at " + host;
78 LOGGER.warn(errorMessage + "<br>", e);
79 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
83 final JsonObject responseObject = new JsonObject();
85 // Engine Service data
86 responseObject.addProperty("engine_id", engineServiceFacade.getKey().getId());
87 responseObject.addProperty("model_id",
88 engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getId()
90 responseObject.addProperty("server", hostName);
91 responseObject.addProperty("port", Integer.toString(port));
93 return Response.ok(responseObject.toString(), MediaType.APPLICATION_JSON).build();
99 * @param hostName the host name of the engine service to connect to.
100 * @param port the port number of the engine service to connect to.
101 * @param uploadedInputStream input stream
102 * @param fileDetail details on the file
103 * @param ignoreConflicts conflict policy
104 * @param forceUpdate update policy
105 * @return a response object in plain text confirming the upload was successful
108 @Path("modelupload/")
109 @Consumes(MediaType.MULTIPART_FORM_DATA)
110 public Response modelUpload(@FormDataParam("hostName") final String hostName, @FormDataParam("port") final int port,
111 @FormDataParam("file") final InputStream uploadedInputStream,
112 @FormDataParam("file") final FormDataContentDisposition fileDetail,
113 @FormDataParam("ignoreConflicts") final boolean ignoreConflicts,
114 @FormDataParam("forceUpdate") final boolean forceUpdate) {
115 final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port);
118 engineServiceFacade.init();
119 } catch (final ApexDeploymentException e) {
120 final String errorMessage = "Error connecting to Apex Engine Service at " + hostName + ":" + port;
121 LOGGER.warn(errorMessage + "<br>", e);
122 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
127 engineServiceFacade.deployModel(fileDetail.getFileName(), uploadedInputStream, ignoreConflicts,
129 } catch (final Exception e) {
130 LOGGER.warn("Error updating model on engine service " + engineServiceFacade.getKey().getId(), e);
131 final String errorMessage =
132 "Error updating model on engine service " + engineServiceFacade.getKey().getId();
133 LOGGER.warn(errorMessage + "<br>", e);
134 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
138 return Response.ok("Model " + fileDetail.getFileName() + " deployed on engine service "
139 + engineServiceFacade.getKey().getId()).build();