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>
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}
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.
54 @Produces({ MediaType.APPLICATION_JSON })
55 @Consumes({ MediaType.APPLICATION_JSON })
57 public class ApexDeploymentRestResource {
58 // Get a reference to the logger
59 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexDeploymentRestResource.class);
62 * Constructor, a new resource director is created for each request.
64 public ApexDeploymentRestResource() {}
67 * Query the engine service for data
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
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);
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())
87 final JsonObject responseObject = new JsonObject();
89 // Engine Service data
90 responseObject.addProperty("engine_id", engineServiceFacade.getKey().getID());
91 responseObject.addProperty("model_id",
92 engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getID()
94 responseObject.addProperty("server", hostName);
95 responseObject.addProperty("port", Integer.toString(port));
97 return Response.ok(responseObject.toString(), MediaType.APPLICATION_JSON).build();
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
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);
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())
131 engineServiceFacade.deployModel(fileDetail.getFileName(), uploadedInputStream, ignoreConflicts,
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())
142 return Response.ok("Model " + fileDetail.getFileName() + " deployed on engine service "
143 + engineServiceFacade.getKey().getID()).build();