ebbe3db312196737ad724163bba85f0a08809749
[policy/gui.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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      * @return the result of the upload process
53      */
54     public ApexApiResult doUpload(final String toscaServiceTemplate, final AxArtifactKey policyModelKey,
55         final String policyModelUuid) {
56         LOGGER.entry();
57
58         if (StringUtils.isBlank(ApexEditorMain.getParameters().getUploadUrl())) {
59             final var 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);
62             return apexApiResult;
63
64         }
65
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()));
72
73         try {
74             final var response = ClientBuilder.newClient().target(ApexEditorMain.getParameters().getUploadUrl())
75                 .request(MediaType.APPLICATION_JSON)
76                 .post(Entity.entity(uploadPolicyRequestDto, MediaType.APPLICATION_JSON));
77
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");
84                 return apexApiResult;
85             } else {
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);
92                 return apexApiResult;
93             }
94         } catch (Exception e) {
95             final var apexApiResult = new ApexApiResult(Result.FAILED);
96             apexApiResult
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;
102         }
103     }
104 }