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;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Base64;
26 import java.util.Optional;
27 import javax.ws.rs.core.Response;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.apex.model.modelapi.ApexApiResult;
30 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
31 import org.onap.policy.apex.model.modelapi.ApexModel;
32 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
33 import org.onap.policy.gui.editors.apex.rest.UploadPluginConfigParameters;
34 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ApexConfigProcessor;
35 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.PolicyToscaConverter;
36 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ProcessedTemplate;
37 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.ToscaTemplateProcessor;
38 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.exception.PolicyToscaConverterException;
39 import org.onap.policy.gui.editors.apex.rest.handling.plugin.upload.UploadPluginClient;
40 import org.onap.policy.gui.editors.apex.rest.handling.plugin.upload.UploadPolicyRequestDto;
41 import org.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
45 * Handles the Policy Model upload.
47 public class PolicyUploadHandler {
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyUploadHandler.class);
50 private final PolicyToscaConverter policyToscaConverter;
51 private final ToscaTemplateProcessor toscaTemplateProcessor;
52 private final ApexConfigProcessor apexConfigProcessor;
53 private final UploadPluginClient uploadPluginClient;
54 private final UploadPluginConfigParameters uploadPluginConfigParameters;
57 * Creates the upload handler with its necessary dependencies.
59 * @param uploadPluginClient the UploadPluginClient instance
60 * @param policyToscaConverter the PolicyToscaConverter instance
61 * @param toscaTemplateProcessor the ToscaTemplateProcessor instance
62 * @param apexConfigProcessor the ApexConfigProcessor instance
63 * @param uploadPluginConfigParameters the Config instance
65 public PolicyUploadHandler(final UploadPluginClient uploadPluginClient,
66 final PolicyToscaConverter policyToscaConverter,
67 final ToscaTemplateProcessor toscaTemplateProcessor,
68 final ApexConfigProcessor apexConfigProcessor,
69 final UploadPluginConfigParameters uploadPluginConfigParameters) {
70 this.uploadPluginClient = uploadPluginClient;
71 this.policyToscaConverter = policyToscaConverter;
72 this.toscaTemplateProcessor = toscaTemplateProcessor;
73 this.apexConfigProcessor = apexConfigProcessor;
74 this.uploadPluginConfigParameters = uploadPluginConfigParameters;
78 * Handles the policy model upload converting it to TOSCA with given template files.
80 * @param apexModel the apex model that contains the policy model
81 * @param toscaTemplateInputStream the tosca template input stream
82 * @param apexConfigInputStream the apex config input stream
83 * @return the result of the upload process
85 public ApexApiResult doUpload(final ApexModel apexModel, final InputStream toscaTemplateInputStream,
86 final InputStream apexConfigInputStream, final String userId) {
87 final ProcessedTemplate processedToscaTemplate;
89 processedToscaTemplate = toscaTemplateProcessor.process(toscaTemplateInputStream);
90 } catch (final IOException e) {
91 final ApexApiResult result = new ApexApiResult(Result.FAILED);
92 result.addThrowable(e);
93 final String errorMsg = "Could not process the tosca template file";
94 result.addMessage(errorMsg);
95 LOGGER.error(errorMsg, e);
98 if (!processedToscaTemplate.isValid()) {
99 return buildResponse(processedToscaTemplate);
102 final ProcessedTemplate processedApexConfig;
104 processedApexConfig = apexConfigProcessor.process(apexConfigInputStream);
105 } catch (final IOException e) {
106 final ApexApiResult result = new ApexApiResult(Result.FAILED);
107 result.addThrowable(e);
108 final String errorMsg = "Could not process the apex config file";
109 result.addMessage(errorMsg);
110 LOGGER.error(errorMsg, e);
113 if (!processedApexConfig.isValid()) {
114 return buildResponse(processedApexConfig);
116 return doUpload(apexModel, processedToscaTemplate.getContent(), processedApexConfig.getContent(), userId);
119 private ApexApiResult doUpload(final ApexModel apexModel, final String toscaTemplate, final String apexConfig,
120 final String userId) {
122 if (!isUploadPluginEnabled()) {
123 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
124 apexApiResult.addMessage("Upload feature is disabled");
125 return apexApiResult;
127 final AxPolicyModel policyModel = apexModel.getPolicyModel();
128 final ApexApiResult result = apexModel.listModel();
129 final UploadPolicyRequestDto uploadPolicyRequestDto = new UploadPolicyRequestDto();
130 final AxArtifactKey policyKey = policyModel.getKeyInformation().getKey();
131 final java.util.UUID uuid = policyModel.getKeyInformation().get(policyKey).getUuid();
132 uploadPolicyRequestDto.setUserId(userId);
133 uploadPolicyRequestDto
134 .setFilename(String.format("%s.%s.%s", uuid, policyKey.getName(), policyKey.getVersion()));
135 final String apexPolicy = convert(result.getMessage(), toscaTemplate, apexConfig).orElse(null);
136 if (apexPolicy == null) {
137 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
138 apexApiResult.addMessage(
139 String.format("An error has occurred while uploading the converting the Policy '%s' to YAML.",
140 policyModel.getId()));
141 LOGGER.exit("Model/Upload: NOT OK");
142 return apexApiResult;
144 uploadPolicyRequestDto.setFileData(
145 Base64.getEncoder().encodeToString(apexPolicy.getBytes(StandardCharsets.UTF_8)));
146 final Response response = uploadPluginClient.upload(uploadPolicyRequestDto);
147 if (response.getStatus() == 201) {
148 final ApexApiResult apexApiResult = new ApexApiResult(Result.SUCCESS);
149 apexApiResult.addMessage(String.format("Policy '%s' uploaded successfully", policyModel.getId()));
150 LOGGER.exit("Model/Upload: OK");
151 return apexApiResult;
153 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
154 apexApiResult.addMessage(
155 String.format("An error has occurred while uploading the Policy '%s'. Status was %s",
156 policyModel.getId(), response.getStatus()));
157 LOGGER.exit("Model/Upload: NOT OK");
158 return apexApiResult;
162 private ApexApiResult buildResponse(final ProcessedTemplate processedTemplate) {
163 final ApexApiResult result = new ApexApiResult(Result.SUCCESS);
164 if (!processedTemplate.isValid()) {
165 result.setResult(Result.OTHER_ERROR);
166 processedTemplate.getErrorSet().forEach(result::addMessage);
171 private boolean isUploadPluginEnabled() {
172 return uploadPluginConfigParameters.isEnabled();
175 private Optional<String> convert(final String apexPolicy, final String toscaTemplate, final String apexConfig) {
177 return policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate);
178 } catch (final PolicyToscaConverterException e) {
179 LOGGER.error("Could not convert policy to TOSCA", e);
182 return Optional.empty();