2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
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
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.gui.editors.apex.rest.handling.plugin.upload;
22 import java.nio.charset.StandardCharsets;
23 import java.util.Base64;
24 import javax.ws.rs.client.ClientBuilder;
25 import javax.ws.rs.client.Entity;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30 import org.onap.policy.apex.model.modelapi.ApexApiResult;
31 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
32 import org.onap.policy.gui.editors.apex.rest.ApexEditorMain;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
37 * Handles the Policy Model upload.
39 public class PolicyUploadHandler {
40 private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyUploadHandler.class);
42 // Recurring string constants
43 private static final String MODEL_UPLOAD_NOT_OK = "Model/Upload: NOT OK";
46 * Handles the policy model upload converting it to TOSCA with given template files.
48 * @param toscaServiceTemplate the TOSCA service template
49 * @param policyModelKey the key of the policy model
50 * @param policyModelUuid the UUID of the policy model
51 * @return the result of the upload process
53 public ApexApiResult doUpload(final String toscaServiceTemplate, final AxArtifactKey policyModelKey,
54 final String policyModelUuid) {
57 if (StringUtils.isBlank(ApexEditorMain.getParameters().getUploadUrl())) {
58 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
59 apexApiResult.addMessage("Model upload is disabled, parameter upload-url is not set on server");
60 LOGGER.exit(MODEL_UPLOAD_NOT_OK);
65 final UploadPolicyRequestDto uploadPolicyRequestDto = new UploadPolicyRequestDto();
66 uploadPolicyRequestDto.setUserId(ApexEditorMain.getParameters().getUploadUserid());
67 uploadPolicyRequestDto
68 .setFileData(Base64.getEncoder().encodeToString(toscaServiceTemplate.getBytes(StandardCharsets.UTF_8)));
69 uploadPolicyRequestDto.setFilename(
70 String.format("%s.%s.%s", policyModelUuid, policyModelKey.getName(), policyModelKey.getVersion()));
73 final Response response = ClientBuilder.newClient().target(ApexEditorMain.getParameters().getUploadUrl())
74 .request(MediaType.APPLICATION_JSON)
75 .post(Entity.entity(uploadPolicyRequestDto, MediaType.APPLICATION_JSON));
77 if (response.getStatus() == 201) {
78 final ApexApiResult apexApiResult = new ApexApiResult(Result.SUCCESS);
79 String.format("uploading Policy '%s' to URL '%s' with userId '%s' was successful",
80 policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
81 ApexEditorMain.getParameters().getUploadUserid());
82 LOGGER.exit("Model/Upload: OK");
85 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
86 apexApiResult.addMessage(
87 String.format("uploading Policy '%s' to URL '%s' with userId '%s' failed with status %s",
88 policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
89 ApexEditorMain.getParameters().getUploadUserid(), response.getStatus()));
90 LOGGER.exit(MODEL_UPLOAD_NOT_OK);
93 } catch (Exception e) {
94 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
96 .addMessage(String.format("uploading Policy '%s' to URL '%s' with userId '%s' failed with error %s",
97 policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
98 ApexEditorMain.getParameters().getUploadUserid(), e.getMessage()));
99 LOGGER.exit(MODEL_UPLOAD_NOT_OK);
100 return apexApiResult;