aboutsummaryrefslogtreecommitdiffstats
path: root/client/client-deployment/src/main/java/org/onap/policy/apex/client/deployment/rest/ApexDeploymentRestResource.java
blob: 55cabe900e0e8c743cf77c6d3cf9764751b89ea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.client.deployment.rest;

import com.google.gson.JsonObject;
import java.io.InputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.onap.policy.apex.core.deployment.ApexDeploymentException;
import org.onap.policy.apex.core.deployment.EngineServiceFacade;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * The class represents the root resource exposed at the base URL<br>
 *
 * <p>The url to access this resource would be in the form {@code <baseURL>/rest/....} <br>
 * For example: a GET request to the following URL
 * {@code http://localhost:18989/apexservices/rest/?hostName=localhost&port=12345}
 *
 * <p><b>Note:</b> An allocated {@code hostName} and {@code port} query parameter must be included in all requests.
 * Datasets for different {@code hostName} are completely isolated from one another.
 *
 */
@Path("deployment/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })

public class ApexDeploymentRestResource {
    // Get a reference to the logger
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexDeploymentRestResource.class);

    /**
     * Query the engine service for data.
     *
     * @param hostName the host name of the engine service to connect to.
     * @param port the port number of the engine service to connect to.
     * @return a Response object containing the engines service, status and context data in JSON
     */
    @GET
    public Response createSession(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port) {
        final String host = hostName + ":" + port;
        final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port);

        try {
            engineServiceFacade.init();
        } catch (final ApexDeploymentException e) {
            final String errorMessage = "Error connecting to Apex Engine Service at " + host;
            LOGGER.warn(errorMessage + "<br>", e);
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
                    .build();
        }

        final JsonObject responseObject = new JsonObject();

        // Engine Service data
        responseObject.addProperty("engine_id", engineServiceFacade.getKey().getId());
        responseObject.addProperty("model_id",
                engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getId()
                        : "Not Set");
        responseObject.addProperty("server", hostName);
        responseObject.addProperty("port", Integer.toString(port));

        return Response.ok(responseObject.toString(), MediaType.APPLICATION_JSON).build();
    }

    /**
     * Upload a model.
     *
     * @param hostName the host name of the engine service to connect to.
     * @param port the port number of the engine service to connect to.
     * @param uploadedInputStream input stream
     * @param fileDetail details on the file
     * @param ignoreConflicts conflict policy
     * @param forceUpdate update policy
     * @return a response object in plain text confirming the upload was successful
     */
    @POST
    @Path("modelupload/")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response modelUpload(@FormDataParam("hostName") final String hostName, @FormDataParam("port") final int port,
            @FormDataParam("file") final InputStream uploadedInputStream,
            @FormDataParam("fileName") final String fileName,
            @FormDataParam("ignoreConflicts") final boolean ignoreConflicts,
            @FormDataParam("forceUpdate") final boolean forceUpdate) {
        final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port);

        try {
            engineServiceFacade.init();
        } catch (final ApexDeploymentException e) {
            final String errorMessage = "Error connecting to Apex Engine Service at " + hostName + ":" + port;
            LOGGER.warn(errorMessage + "<br>", e);
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
                    .build();
        }

        try {
            engineServiceFacade.deployModel(fileName, uploadedInputStream, ignoreConflicts, forceUpdate);
        } catch (final Exception e) {
            LOGGER.warn("Error updating model on engine service " + engineServiceFacade.getKey().getId(), e);
            final String errorMessage =
                    "Error updating model on engine service " + engineServiceFacade.getKey().getId();
            LOGGER.warn(errorMessage + "<br>", e);
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage())
                    .build();
        }

        return Response.ok("Model " + fileName + " deployed on engine service "
                + engineServiceFacade.getKey().getId()).build();
    }

    /**
     * Get an engine service facade for sending REST requests. This method is package because it is used by unit test.
     *
     * @param hostName the host name of the Apex engine
     * @param port the port of the Apex engine
     * @return the engine service facade
     */
    protected EngineServiceFacade getEngineServiceFacade(final String hostName, final int port) {
        return new EngineServiceFacade(hostName, port);
    }

}