17668310b59a3724e941dcdf56606bbc92a3b2df
[policy/gui.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.gui.editors.apex.rest.handling.plugin.upload;
23
24 import java.nio.charset.StandardCharsets;
25 import java.util.Base64;
26 import javax.ws.rs.client.ClientBuilder;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.MediaType;
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;
36
37 /**
38  * Handles the Policy Model upload.
39  */
40 public class PolicyUploadHandler {
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyUploadHandler.class);
42
43     // Recurring string constants
44     private static final String MODEL_UPLOAD_NOT_OK = "Model/Upload: NOT OK";
45
46     /**
47      * Handles the policy model upload converting it to TOSCA with given template files.
48      *
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      * @param uploadUserId         the userId to use for upload. If blank, the commandline
53      *                             parameter "upload-userid" is used.
54      * @return the result of the upload process
55      */
56     public ApexApiResult doUpload(final String toscaServiceTemplate, final AxArtifactKey policyModelKey,
57         final String policyModelUuid, String uploadUserId) {
58         LOGGER.entry();
59
60         final String uploadUrl = ApexEditorMain.getParameters().getUploadUrl();
61         if (StringUtils.isBlank(uploadUrl)) {
62             final var apexApiResult = new ApexApiResult(Result.FAILED);
63             apexApiResult.addMessage("Model upload is disabled, parameter upload-url is not set on server");
64             LOGGER.exit(MODEL_UPLOAD_NOT_OK);
65             return apexApiResult;
66         }
67
68         if (StringUtils.isBlank(uploadUserId)) {
69             uploadUserId = ApexEditorMain.getParameters().getUploadUserid();
70         }
71
72         final var uploadPolicyRequestDto = new UploadPolicyRequestDto();
73         uploadPolicyRequestDto.setUserId(uploadUserId);
74         uploadPolicyRequestDto
75             .setFileData(Base64.getEncoder().encodeToString(toscaServiceTemplate.getBytes(StandardCharsets.UTF_8)));
76         uploadPolicyRequestDto.setFilename(
77             String.format("%s.%s.%s", policyModelUuid, policyModelKey.getName(), policyModelKey.getVersion()));
78
79         try {
80             final var response = ClientBuilder.newClient().target(uploadUrl)
81                 .request(MediaType.APPLICATION_JSON)
82                 .post(Entity.entity(uploadPolicyRequestDto, MediaType.APPLICATION_JSON));
83
84             if (response.getStatus() == 201) {
85                 final var apexApiResult = new ApexApiResult(Result.SUCCESS);
86                 apexApiResult.addMessage(
87                     String.format("uploading Policy '%s' to URL '%s' with userId '%s' was successful",
88                         policyModelKey.getId(), uploadUrl, uploadUserId));
89                 LOGGER.exit("Model/Upload: OK");
90                 return apexApiResult;
91             } else {
92                 final var apexApiResult = new ApexApiResult(Result.FAILED);
93                 apexApiResult.addMessage(
94                     String.format("uploading Policy '%s' to URL '%s' with userId '%s' failed with status %s",
95                         policyModelKey.getId(), uploadUrl, uploadUserId, response.getStatus()));
96                 LOGGER.exit(MODEL_UPLOAD_NOT_OK);
97                 return apexApiResult;
98             }
99         } catch (Exception e) {
100             final var apexApiResult = new ApexApiResult(Result.FAILED);
101             apexApiResult
102                 .addMessage(String.format("uploading Policy '%s' to URL '%s' with userId '%s' failed with error %s",
103                     policyModelKey.getId(), uploadUrl, uploadUserId, e.getMessage()));
104             LOGGER.exit(MODEL_UPLOAD_NOT_OK);
105             return apexApiResult;
106         }
107     }
108 }