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;
24 import java.io.InputStream;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.QueryParam;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import org.glassfish.jersey.media.multipart.FormDataParam;
34 import org.onap.policy.apex.core.deployment.ApexDeploymentException;
35 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
40 * The class represents the root resource exposed at the base URL<br>
42 * <p>The url to access this resource would be in the form {@code <baseURL>/rest/....} <br>
43 * For example: a GET request to the following URL
44 * {@code http://localhost:18989/apexservices/rest/?hostName=localhost&port=12345}
46 * <p><b>Note:</b> An allocated {@code hostName} and {@code port} query parameter must be included in all requests.
47 * Datasets for different {@code hostName} are completely isolated from one another.
51 @Produces({ MediaType.APPLICATION_JSON })
52 @Consumes({ MediaType.APPLICATION_JSON })
54 public class ApexDeploymentRestResource {
55 // Get a reference to the logger
56 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexDeploymentRestResource.class);
59 * Query the engine service for data.
61 * @param hostName the host name of the engine service to connect to.
62 * @param port the port number of the engine service to connect to.
63 * @return a Response object containing the engines service, status and context data in JSON
66 public Response createSession(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port) {
67 final String host = hostName + ":" + port;
68 final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port);
71 engineServiceFacade.init();
72 } catch (final ApexDeploymentException e) {
73 final String errorMessage = "Error connecting to Apex Engine Service at " + host;
74 LOGGER.warn(errorMessage + "<br>", e);
75 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
79 final JsonObject responseObject = new JsonObject();
81 // Engine Service data
82 responseObject.addProperty("engine_id", engineServiceFacade.getKey().getId());
83 responseObject.addProperty("model_id",
84 engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getId()
86 responseObject.addProperty("server", hostName);
87 responseObject.addProperty("port", Integer.toString(port));
89 return Response.ok(responseObject.toString(), MediaType.APPLICATION_JSON).build();
95 * @param hostName the host name of the engine service to connect to.
96 * @param port the port number of the engine service to connect to.
97 * @param uploadedInputStream input stream
98 * @param ignoreConflicts conflict policy
99 * @param forceUpdate update policy
100 * @return a response object in plain text confirming the upload was successful
103 @Path("modelupload/")
104 @Consumes(MediaType.MULTIPART_FORM_DATA)
105 public Response modelUpload(@FormDataParam("hostName") final String hostName, @FormDataParam("port") final int port,
106 @FormDataParam("file") final InputStream uploadedInputStream,
107 @FormDataParam("fileName") final String fileName,
108 @FormDataParam("ignoreConflicts") final boolean ignoreConflicts,
109 @FormDataParam("forceUpdate") final boolean forceUpdate) {
110 final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port);
113 engineServiceFacade.init();
114 } catch (final ApexDeploymentException e) {
115 final String errorMessage = "Error connecting to Apex Engine Service at " + hostName + ":" + port;
116 LOGGER.warn(errorMessage + "<br>", e);
117 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
122 engineServiceFacade.deployModel(fileName, uploadedInputStream, ignoreConflicts, forceUpdate);
123 } catch (final Exception e) {
124 LOGGER.warn("Error updating model on engine service " + engineServiceFacade.getKey().getId(), e);
125 final String errorMessage =
126 "Error updating model on engine service " + engineServiceFacade.getKey().getId();
127 LOGGER.warn(errorMessage + "<br>", e);
128 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
132 return Response.ok("Model " + fileName + " deployed on engine service "
133 + engineServiceFacade.getKey().getId()).build();
137 * Get an engine service facade for sending REST requests. This method is package because it is used by unit test.
139 * @param hostName the host name of the Apex engine
140 * @param port the port of the Apex engine
141 * @return the engine service facade
143 protected EngineServiceFacade getEngineServiceFacade(final String hostName, final int port) {
144 return new EngineServiceFacade(hostName, port);