2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * 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.gui.editors.apex.rest.handling.plugin.upload;
23 import java.nio.charset.StandardCharsets;
24 import java.util.Base64;
25 import javax.ws.rs.client.ClientBuilder;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.modelapi.ApexApiResult;
32 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
33 import org.onap.policy.gui.editors.apex.rest.ApexEditorMain;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * Handles the Policy Model upload.
40 public class PolicyUploadHandler {
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyUploadHandler.class);
43 // Recurring string constants
44 private static final String MODEL_UPLOAD_NOT_OK = "Model/Upload: NOT OK";
47 * Handles the policy model upload converting it to TOSCA with given template files.
49 * @param toscaServiceTemplate the TOSCA service template
50 * @param policyModelKey the key of the policy model
51 * @param policyModelUuid the UUID of the policy model
52 * @return the result of the upload process
54 public ApexApiResult doUpload(final String toscaServiceTemplate, final AxArtifactKey policyModelKey,
55 final String policyModelUuid) {
58 if (StringUtils.isBlank(ApexEditorMain.getParameters().getUploadUrl())) {
59 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
60 apexApiResult.addMessage("Model upload is disabled, parameter upload-url is not set on server");
61 LOGGER.exit(MODEL_UPLOAD_NOT_OK);
66 final var uploadPolicyRequestDto = new UploadPolicyRequestDto();
67 uploadPolicyRequestDto.setUserId(ApexEditorMain.getParameters().getUploadUserid());
68 uploadPolicyRequestDto
69 .setFileData(Base64.getEncoder().encodeToString(toscaServiceTemplate.getBytes(StandardCharsets.UTF_8)));
70 uploadPolicyRequestDto.setFilename(
71 String.format("%s.%s.%s", policyModelUuid, policyModelKey.getName(), policyModelKey.getVersion()));
74 final var response = ClientBuilder.newClient().target(ApexEditorMain.getParameters().getUploadUrl())
75 .request(MediaType.APPLICATION_JSON)
76 .post(Entity.entity(uploadPolicyRequestDto, MediaType.APPLICATION_JSON));
78 if (response.getStatus() == 201) {
79 final var apexApiResult = new ApexApiResult(Result.SUCCESS);
80 String.format("uploading Policy '%s' to URL '%s' with userId '%s' was successful",
81 policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
82 ApexEditorMain.getParameters().getUploadUserid());
83 LOGGER.exit("Model/Upload: OK");
86 final var apexApiResult = new ApexApiResult(Result.FAILED);
87 apexApiResult.addMessage(
88 String.format("uploading Policy '%s' to URL '%s' with userId '%s' failed with status %s",
89 policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
90 ApexEditorMain.getParameters().getUploadUserid(), response.getStatus()));
91 LOGGER.exit(MODEL_UPLOAD_NOT_OK);
94 } catch (Exception e) {
95 final var apexApiResult = new ApexApiResult(Result.FAILED);
97 .addMessage(String.format("uploading Policy '%s' to URL '%s' with userId '%s' failed with error %s",
98 policyModelKey.getId(), ApexEditorMain.getParameters().getUploadUrl(),
99 ApexEditorMain.getParameters().getUploadUserid(), e.getMessage()));
100 LOGGER.exit(MODEL_UPLOAD_NOT_OK);
101 return apexApiResult;