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) {
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());
119 private ApexApiResult doUpload(final ApexModel apexModel, final String toscaTemplate, final String apexConfig) {
121 if (!isUploadPluginEnabled()) {
122 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
123 apexApiResult.addMessage("Upload feature is disabled");
124 return apexApiResult;
126 final AxPolicyModel policyModel = apexModel.getPolicyModel();
127 final ApexApiResult result = apexModel.listModel();
128 final UploadPolicyRequestDto uploadPolicyRequestDto = new UploadPolicyRequestDto();
129 final AxArtifactKey policyKey = policyModel.getKeyInformation().getKey();
130 final java.util.UUID uuid = policyModel.getKeyInformation().get(policyKey).getUuid();
131 uploadPolicyRequestDto
132 .setFilename(String.format("%s.%s.%s", uuid, policyKey.getName(), policyKey.getVersion()));
133 final String apexPolicy = convert(result.getMessage(), toscaTemplate, apexConfig).orElse(null);
134 if (apexPolicy == null) {
135 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
136 apexApiResult.addMessage(
137 String.format("An error has occurred while uploading the converting the Policy '%s' to YAML.",
138 policyModel.getId()));
139 LOGGER.exit("Model/Upload: NOT OK");
140 return apexApiResult;
142 uploadPolicyRequestDto.setFileData(
143 Base64.getEncoder().encodeToString(apexPolicy.getBytes(StandardCharsets.UTF_8)));
144 final Response response = uploadPluginClient.upload(uploadPolicyRequestDto);
145 if (response.getStatus() == 201) {
146 final ApexApiResult apexApiResult = new ApexApiResult(Result.SUCCESS);
147 apexApiResult.addMessage(String.format("Policy '%s' uploaded successfully", policyModel.getId()));
148 LOGGER.exit("Model/Upload: OK");
149 return apexApiResult;
151 final ApexApiResult apexApiResult = new ApexApiResult(Result.FAILED);
152 apexApiResult.addMessage(
153 String.format("An error has occurred while uploading the Policy '%s'. Status was %s",
154 policyModel.getId(), response.getStatus()));
155 LOGGER.exit("Model/Upload: NOT OK");
156 return apexApiResult;
160 private ApexApiResult buildResponse(final ProcessedTemplate processedTemplate) {
161 final ApexApiResult result = new ApexApiResult(Result.SUCCESS);
162 if (!processedTemplate.isValid()) {
163 result.setResult(Result.OTHER_ERROR);
164 processedTemplate.getErrorSet().forEach(result::addMessage);
169 private boolean isUploadPluginEnabled() {
170 return uploadPluginConfigParameters.isEnabled();
173 private Optional<String> convert(final String apexPolicy, final String toscaTemplate, final String apexConfig) {
175 return policyToscaConverter.convert(apexPolicy, apexConfig, toscaTemplate);
176 } catch (final PolicyToscaConverterException e) {
177 LOGGER.error("Could not convert policy to TOSCA", e);
180 return Optional.empty();